Coinbase code transfer:

Hello. I am trying to transfer an amount of a currency (LTC, ETH, BTC, etc.) from one user to another. I have tried the request, receive, send, and transfer docs on the coinbase api, but the javascript code is not doing anything. Please let me know what I to code in JavaScript so that it works. Thanks.

const coinbase = require(‘coinbase’);
logErrorSchema = require(‘…/models/logErrors’);

module.exports = {
getClient: async (req, res) => {
try {

        const Client = require('coinbase').Client;
        const client = new Client({ 'accessToken': accessToken, 'refreshToken': refreshToken });

        res.send(client);
    } catch (error) {
        res.status(500).send(error);
    }
},
getAccount: async (req, res) => {
    try {
        //wallet:accounts:read,wallet:addresses:read,wallet:buys:read
        const data = req.body;
        // console.log(data);
        const client = new coinbase.Client({
                                            'accessToken': data.accessToken, 
                                            'refreshToken': data.refreshToken,
                                             strictSSL: false
                                            });

        let address = null;                               
        let amount = null;
        let currency = null; 


        client.getAccount('primary', function (err, account) {
            // console.log('account', account);
            // console.log('account-err', err);
          
            account.createAddress(null, function (err, addr) {
                //console.log(err);
                address = addr;
                // console.log('err - ', err);
                // console.log('add - ', address);

                if (account && parseFloat(account.balance > 0)) {
                    account.sendMoney({
                        'to': address,
                        'amount': amount,
                        'currency': currency
                    }, function (err, tx) {

                      //  console.log('ERR', err);
                       // console.log('TX', tx);
                        // return res.send({ data: 'ok' });
                    });
                }
            });

            
            
        });
      // return res.send({ data: 'no data'});
    } catch (error) {
        console.log(error.response);
        const logErrorModel = {
            controller: 'coinbaseController',
            error: error,
            model: req.body,
        };
        const logE = new logErrorSchema(logErrorModel);
        logE.save();
        return res.status(500).send(error);
    }
},
setFunds: async (req, res) => {
    try {
        const Client = require('coinbase').Client;

        const client = new Client({
            'apiKey': '',
            'apiSecret': '',
            'version': 'YYYY-MM-DD',
            strictSSL: false
        });

        let address = null; //'3KTwyFhct2o9rfD6KRmXatA4ZGTNkTkayT';                               
        let amount = null;
        let currency = null; 

        client.getAccount('primary', function (err, account) {
            account.createAddress(function (err, addr) {
                console.log(err);
                address = addr;
            });
        });

        console.log(address, 'address');

        client.getAccount('primary', function (err, account) {
            account.sendMoney({
                'to': address,
                'amount': amount,
                'currency': currency
            }, function (err, tx) {

                console.log('ERR', tx);
                console.log('TX', tx);
            });
        });

    } catch (error) {
        console.log(error.response);
        const logErrorModel = {
            controller: 'coinbaseController',
            error: error,
            model: req.body,
        };
        const logE = new logErrorSchema(logErrorModel);
        logE.save();
        res.status(500).send(error);
    }
},
sending: (req, res) => {
    try {
        //wallet:accounts:read,wallet:addresses:read,wallet:buys:read
        const data = req.body;
        // console.log(data);
        // Token will be important for minting NFTs uniquely
        const client = new coinbase.Client({
            'accessToken': data.accessToken,
            'refreshToken': data.refreshToken,
            strictSSL: false
        });

        client.getAccount('primary', function (err, account) {

            var args = {
                'to': address,
                'amount': '0.00001',
                'currency': 'LTC',
                'description': ' A sample transaction for you'
            };
            account.sendMoney(args, function (err, txn) {
                console.log('my txn id is: ' + err);
                return res.send({ data: txn });
            });
        });
        // return res.send({ data: 'no data'});
    } catch (error) {
        console.log(error.response);
        const logErrorModel = {
            controller: 'coinbaseController',
            error: error,
            model: req.body,
        };
        const logE = new logErrorSchema(logErrorModel);
        logE.save();
        return res.status(500).send(error);
    }
},
requesting: async (req, res) => {
    try {
        // following coinbase API docs on requesting an amount of currency via na email address for a transaction request
        const Client = require('coinbase').Client;

        const client = new Client({
            'apiKey': '',
            'apiSecret': '',
            'version': 'YYYY-MM-DD',
            strictSSL: false
        });

        let address = null; //'3KTwyFhct2o9rfD6KRmXatA4ZGTNkTkayT';                             
        let amount = null;
        let currency = null; 

        client.getAccount('primary', function (err, account) {
            account.createAddress(function (err, addr) {
                console.log(err);
                address = addr;
            });
        });

        console.log(address, 'address');

        client.getAccount('primary', function (err, account) {
            account.requestMoney({
                'to': 'mkiefer@cryptoramaglobal.com',
                'amount': amount,
                'currency': currency
            }, function (err, tx) {

                console.log('ERR', tx);
                console.log('TX', tx);
            });
        });

    } catch (error) {
        console.log(error.response);
        const logErrorModel = {
            controller: 'coinbaseController',
            error: error,
            model: req.body,
        };
        const logE = new logErrorSchema(logErrorModel);
        logE.save();
        res.status(500).send(error);
    }
},
        
transferring: async (req, res) => {
    try {
        // following the coinbase API docs for transferring a currency amount from wallet to wallet
        const Client = require('coinbase').Client;

        const client = new Client({
            'apiKey': '',
            'apiSecret': '',
            'version': 'YYYY-MM-DD',
            strictSSL: false
        });

        let address = null; //'3KTwyFhct2o9rfD6KRmXatA4ZGTNkTkayT';                              
        let amount = null;
        let currency = null; 

        client.getAccount('primary', function (err, account) {
            account.createAddress(function (err, addr) {
                console.log(err);
                address = addr;
            });
        });

        console.log(address, 'address');

        client.getAccount('primary', function (err, account) {
            account.transferMoney({
                'to': address,
                'amount': amount,
                'currency': currency
            }, function (err, tx) {

                console.log('ERR', tx);
                console.log('TX', tx);
            });
        });

    } catch (error) {
        console.log(error.response);
        const logErrorModel = {
            controller: 'coinbaseController',
            error: error,
            model: req.body,
        };
        const logE = new logErrorSchema(logErrorModel);
        logE.save();
        res.status(500).send(error);
    }
},

Hi @mkiefer! Thank you for your continued support in trying out Coinbase APIs. For the details regarding your concern, we will check on this for you with our team. We will get back to you once we have more information. Keep in touch!

3 Likes

Hello. It has been over a week. Please send me updated code and the correct answer.

@mkiefer - apologies for the delay here. we are still working to get you an update and a correct answer.

Hello. Please let me know what is going on. I have not heard anything back regarding the code changes. Thank you.

@mkiefer - right now, the best support I can offer is Welcome - this looks like a p2p request and our team current is trying to hit end of quarter deadlines so getting someone to review your code and provide better JS is taking a bit longer than we had hoped.

Hello. Please update me on the best code improvements and changes. Thank you.

Hello. I urgently need the code solution. Please email the correct code.

@mkiefer - thank you for your continued patience here. It seems you are using the coinbase npm package that is deprecated. We tried using the package and get an error UNABLE_TO_GET_ISSUER_CERT_LOCALLY
var Client = require(‘coinbase’).Client;

var client = new Client(
{‘apiKey’: ‘EDITED’,
‘apiSecret’: ‘EDITED’}
);

client.getCurrentUser(function(err, user) {
if (err) {
return console.log(err);
}
});
Then I tried to construct my own queries to the API using plain JS and they work.
var crypto = require(‘crypto’);
var request = require(‘request’);

var apiKey = ‘EDITED’;
var apiSecret = ‘EDITED’;

var timestamp = Math.floor(Date.now() / 1000);
var req = { method: ‘GET’, path: ‘/v2/user’, body: ‘’};
var message = timestamp + req.method + req.path + req.body;
var signature = crypto.createHmac(“sha256”, apiSecret).update(message).digest(“hex”);

var options = {
baseUrl: ‘https://api.coinbase.com/’,
url: req.path,
method: req.method,
headers: {
‘CB-ACCESS-SIGN’: signature,
‘CB-ACCESS-TIMESTAMP’: timestamp,
‘CB-ACCESS-KEY’: apiKey,
‘CB-VERSION’: ‘2021-11-12’
}
};

request(options,function(err, response){
if (err) {
return console.log(err);
}
console.log(response.body);
});

Here is the up to date package - coinbase - npm

1 Like