Ethers.js: How to send custom token

Created on 20 Feb 2018  路  5Comments  路  Source: ethers-io/ethers.js

Hi all, one newbie question, I can send ETH token with ethers created wallet using wallet.send() or provider.sendTransaction(), the transaction looks like this:
var transaction = {
nonce: 10000,
gasLimit: 21000,
gasPrice: utils.bigNumberify("20000000000"),
to: address,
value: utils.parseEther("1.0"),
data: "0x",
chainId: providers.Provider.chainId.ropsten
};
This is for ETH by default, right? how do I specify a custom ETH-based token in the transaction object, and then send to another address of that custom token?
Thanks
Idoor

discussion

Most helpful comment

Heya! Sorry this took so long to reply. We've been at ETHDenver busy hacking away. But now we're back. :)

So, tokens are just normal Ethereum smart contract. If the token you are using is ERC-20 or ERC-721 you can use something similar to the following:

// Connect to the contract
var contractAddress = "0x012345...";
var contractAbiFragment = [
   {
      "name" : "transfer",
      "type" : "function",
      "inputs" : [
         {
            "name" : "_to",
            "type" : "address"
         },
         {
            "type" : "uint256",
            "name" : "_tokens"
         }
      ],
      "constant" : false,
      "outputs" : [],
      "payable" : false
   }
];
var contract = new ethers.Contract(contractAddress, contractAbiFragment, wallet);

// How many tokens?
var numberOfDecimals = 18;
var numberOfTokens = ethers.utils.parseUnits('1.0', numberOfDecimals);

// Send tokens
contract.transfer(targetAddress, numberOfTokens).then(function(tx) {
    console.log(tx);
});

Let me know of you run into any problems. :)

All 5 comments

Heya! Sorry this took so long to reply. We've been at ETHDenver busy hacking away. But now we're back. :)

So, tokens are just normal Ethereum smart contract. If the token you are using is ERC-20 or ERC-721 you can use something similar to the following:

// Connect to the contract
var contractAddress = "0x012345...";
var contractAbiFragment = [
   {
      "name" : "transfer",
      "type" : "function",
      "inputs" : [
         {
            "name" : "_to",
            "type" : "address"
         },
         {
            "type" : "uint256",
            "name" : "_tokens"
         }
      ],
      "constant" : false,
      "outputs" : [],
      "payable" : false
   }
];
var contract = new ethers.Contract(contractAddress, contractAbiFragment, wallet);

// How many tokens?
var numberOfDecimals = 18;
var numberOfTokens = ethers.utils.parseUnits('1.0', numberOfDecimals);

// Send tokens
contract.transfer(targetAddress, numberOfTokens).then(function(tx) {
    console.log(tx);
});

Let me know of you run into any problems. :)

I'm closing this for now, but please feel free to re-open if you have any further questions or issues.

Thanks!

Hi, I have tried your solution and it worked but only if I use privateKey of the wallet which I used to create custom tokens but what if I want to send from B wallet to C wallet and when I try that it says insufficient funds and both wallets have funds of same tokens which I sent from Metamask.

(node:93326) UnhandledPromiseRejectionWarning: Error: insufficient funds (version=4.0.27)

Failed transaction

var contract = new ethers.Contract(contractAddress, abi, wallet);

var numberOfTokens = ethers.utils.parseUnits('10.0',numberOfDecimals);
var options = { gasLimit: 1500000, gasPrice: ethers.utils.parseUnits('1.0', 'gwei') };

contract.transferFrom(fromAddress, targetAddress,numberOfTokens,options).then(function (tx) { console.log(tx); });

thanks

To use "transferFrom", the sending address must have an allowance. If you use the "Read Contract" tab on that contract and scroll down to the allowance pane, you will see that the sending address in your transaction (which is an EOA, generally not what transferFrom is used for) does not have any allowance to send on behalf of 0xacb0608933acb84cb04ca34545b54c7c3b6ab47b.

You must set your allowance higher first, from the owning entity (which may be an EOA or a Contract) first. Or you can use transfer if you actually just meant to send from the EOA's balance.

Make sense?

(btw; EOA means Externally Owned Account, if you are unfamiliar with the term ;))

Thank you for your reply.
How to set an allowance?
Can you please guide me as I am new to this?

Was this page helpful?
0 / 5 - 0 ratings