Coinbase transfer code

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.

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);
    }
},

Hello @mkiefer , Welcome to the forum! Thank you for taking an interest in Coinbase Cloud services. 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. Please let me know what the correct code is or what I am missing to get an amount of a currency (eg. LTC, BTC) to transfer from a user to another user. I am assuming the request, receive, send, and transfer need to all be in the code for this to work. Thank you.

Hello. Please let me know any changes to the code and what I am missing. Thank you.