3

I was able to create raw transactions for eth following this article https://medium.com/blockchain-musings/how-to-create-raw-transactions-in-ethereum-part-1-1df91abdba7c

However, how can I create raw transactions when send ERC721 tokens? More specifically, where do I add the token id field when creating raw transactions?

1 Answer 1

4

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'
};
9
  • I tried contract.transfer(to, tokenId ).encodeABI(), however it gives me this error Error: invalid address. After a little bit of search it seems to be because the default address is not set. However, I want to sign this transaction offline, so I don't want to set any value for web.eth.defaultaccount. How can I do it?
    – kosta
    Commented Nov 14, 2018 at 13:05
  • In your original post you said you managed to build the raw transaction according to that tutorial. Did you manage to also sign and broadcast that transaction? It should work the same way for this transaction type too Commented Nov 14, 2018 at 14:03
  • ok, I got it to work like this contract.transfer.getData(to,tokenId). Thanks.
    – kosta
    Commented Nov 14, 2018 at 14:09
  • can you please add the full example on how that getData() works? I couldn't find anything in the web3js docs regarding to that method Commented Nov 14, 2018 at 14:25
  • Basically, I referred from here github.com/ethereum/web3.js/issues/1151#issuecomment-341321119
    – kosta
    Commented Nov 14, 2018 at 21:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.