I'm using safeTransferFrom
to transfer a token. I'm using the fourth parameter bytes
to pass a value and decode it from onERC721Received
. The problem is that the decode value is different from what is passed.
Code to reproduce
How I'm passing the value:
const sendingContract = artifacts.require("SendingContract");
const admin = accounts[4];
const tokenOwner = accounts[0]
const tokenRecipient = accounts[1]
const contract = await sendingContract.deployed({ from: admin });
// Uses an existing tokenId
try {
const price = web3.utils.toWei("1", "ether");
const priceConverted = web3.eth.abi.encodeParameter('bytes', price);
await contract.methods['safeTransferFrom(address,address,uint256,bytes)'](tokenOwner, tokenRecipient, tokenId, priceConverted, { from: tokenOwner })
} catch (error) {
console.log(error)
}
How I'm receiving the value:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract ReceivingContract is IERC721Receiver {
uint price;
function onERC721Received(address, address, uint256 _tokenId, bytes calldata _data) public virtual override returns (bytes4) {
(uint _price) = abi.decode(_data, (uint));
price = _price;
return this.onERC721Received.selector;
}
}
Check the received value:
const price = await receivingContract.price.call();
console.log(price); // BN { negative: 0, words: [ 32, <1 empty item> ], length: 1, red: null }
console.log(price.toString()) // 32
I'm not sure how the stringified received value is 32 and how to receive the value in a Wei form.
Environment
Truffle v5.3.14 (core: 5.3.14)
Solidity - ^0.8.0 (solc-js)
Node v14.16.0
Web3.js v1.4.0