Solidity: Sending ERC20 Tokens

+1 vote

I have a solidity contract that has minted a fixed number of ERC20 tokens (using the ropsten test network). I am in need of a way to send the tokens from a wallet to another wallet (preferrably the using the web3js library, but JSON-RPC would work, I have the private key to the account).

Here is my code thus far

var Web3 = require('web3')
var web3 = new Web3(new 
Web3.providers.HttpProvider('https://ropsten.infura.io/xxxxxxx'));
const abi = [ {} ];
const contract = new web3.eth.Contract(abi).at("0x...")
contract.transferFrom('0x....', '0x.....', 100);

When I execute this snippet, I get issues saying "TypeError: (intermediate value).at is not a function". I am relatively new to web3, so any thoughts/suggestions would be most appreciated.

Sep 26, 2018 in Blockchain by digger
• 26,550 points
175 views

1 answer to this question.

0 votes

You can try this code

transferTokensTo: function(contract, address_from, address, tokens) {
    return new Promise(function(resolve, reject) {
        contract.methods.decimals().call().then(function (result) {
            var decimals = result;
            console.log("Token decimals: " + decimals);
            var amount = tokens * Math.pow(10, decimals);

            console.log('Transfer to:', address);
            console.log('Tokens: ' + tokens + " (" + amount + ")");
            contract.methods.transfer(address, amount).send({
                from: address_from,
                gas: 150000
            }).on('transactionHash', function (hash) {
                console.log('\n[TRANSACTION_HASH]\n\n' + hash);
            }).on('confirmation', function (confirmationNumber, receipt) {
                console.log('\n[CONFIRMATION] ', confirmationNumber);

                resolve(receipt);
            }).on('receipt', function (receipt) {
                console.log('\n[RECEIPT]\n\n', receipt);

                // TODO: process receipt if needed
            }).on('error', function (error) {
                console.log('\n[ERROR]\n\n' + error);

                reject(error);
            }).then(function (done) {
                console.log('\n[DONE]\n\n', done);
            });
        });
    });
}
answered Sep 26, 2018 by slayer
• 29,170 points

Related Questions In Blockchain

0 votes
1 answer
+1 vote
1 answer

Getting issues while Sending signTransaction with web3.js

The chain ID for ropsten is 3. ...READ MORE

answered Mar 28, 2018 in Blockchain by Johnathon
• 9,080 points
495 views
+1 vote
1 answer

What is the difference between if() and require() statement in solidity??

If() and require() have separate functions and ...READ MORE

answered Apr 18, 2018 in Blockchain by Shashank
• 10,350 points
441 views
+1 vote
1 answer

what is use of msg.sender in solidity code?

msg.sender(address) function indicated the sender of the ...READ MORE

answered Apr 24, 2018 in Blockchain by Shashank
• 10,350 points
491 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
169 views
0 votes
1 answer
0 votes
1 answer

Solidity geth: Error encountered during contract execution [Bad instruction]

recipes is a dynamic storage array. You need ...READ MORE

answered Oct 15, 2018 in Blockchain by Omkar
• 67,660 points
101 views
0 votes
1 answer

How to write a program to get details of ERC tokens?

You can use Javascript with the Metamask ...READ MORE

answered Jul 13, 2018 in Blockchain by slayer
• 29,170 points
52 views
0 votes
1 answer

Solidity code to change value of name

Try this for setName: function setName(string _name) public ...READ MORE

answered Jul 29, 2018 in Blockchain by slayer
• 29,170 points
43 views