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?

kosta
  • 283
  • 3
  • 12

1 Answers1

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'
};
Tudor Constantin
  • 2,625
  • 1
  • 9
  • 15
  • 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 Nov 14 '18 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 – Tudor Constantin Nov 14 '18 at 14:03
  • ok, I got it to work like this contract.transfer.getData(to,tokenId). Thanks. – kosta Nov 14 '18 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 – Tudor Constantin Nov 14 '18 at 14:25
  • Basically, I referred from here https://github.com/ethereum/web3.js/issues/1151#issuecomment-341321119 – kosta Nov 14 '18 at 21:29
  • oh, you're using web3 v0.2x.x. My example is for the newer web3 v1.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:05
  • Actually, I haven't been able to make a successful transaction yet. I get this error ERC-20 Token Transfer Error even 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:28
  • I got the transaction to work using the steps you described by upgrading web3.js to 1.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
  • yes, that's a good observation. I updated my answer to state that explicitly. – Tudor Constantin Nov 18 '18 at 07:23