A token transfer operation is actually a smart contract method call, which is a bit different than simple ETH sending:
- you'll have to fill the
data: portion of the tx with the info related to the smart contract method call (more below)
- the
from: address is the same as in a normal ETH send operation
- the
to: address is the address of the smart contract, not the address of the recipient
- the
value: field is 0
How to fill the data field. The example is done using web3js v1.0+.
You'll need to have:
- your smart contract's ABI. Let's say you store that in variable
ctrABI
- an instance of Web3. Let's say that's stored in variable
web3
the address where your ERC721 smart contract is deployed - ctrAddress
const ctrInstance = new web3.eth.Contract(JSON.parse(ctrABI), ctrAddress)
const dataField = await ctrInstance.methods.transfer(addrTo, tokenId ).encodeABI();
And then proceed to build your transaction:
const txParams = {
nonce: '0x6', // Replace by nonce for your account on geth node
gasPrice: '0x09184e72a000',
gasLimit: '0x30000', //modify accordingly
to: ctrAddress,
data: dataField,
value: '0x00'
};
web.eth.defaultaccount. How can I do it? – kosta Nov 14 '18 at 13:05contract.transfer.getData(to,tokenId). Thanks. – kosta Nov 14 '18 at 14:09getData()works? I couldn't find anything in theweb3jsdocs regarding to that method – Tudor Constantin Nov 14 '18 at 14:25web3v0.2x.x. My example is for the newerweb3v1.0+. If my answer helped you, please upvote & accept it, so others can also recognize it as helpful info. – Tudor Constantin Nov 15 '18 at 10:05ERC-20 Token Transfer Erroreven though I am trying to send ERC721 tokens. I even created a new question for it https://is.gd/ZbMAUx. – kosta Nov 15 '18 at 22:281.0.0-beta.36. If you could just mention the web.js version in your answer, I will accept it. – kosta Nov 17 '18 at 23:15