I'm implementing an ERC-721 contract that supports the function:
safeTransferFrom(address from, address to, uint256 tokenId, bytes data)
The last parameter of this function is an arbitrary byte array. I imagine most implementations of this function ignore this parameter, but I am interested in using it. Specifically, I want to pass two additional uint256
fields in this parameter and decode them within the contract. How can I achieve this? For example:
function safeTransferFrom(address from, address to, uint256 tokenId, bytes calldata data) public {
(uint256 startDate, uint256 endDate) = decode(data);
// etc...
}
In the above example, I'm asking how to implement the decode
function.
Best Answer
You can use abi.decode() function of solidity to extract your uint256 values.
Reference: abi.decoder in solidity
First make the bytes data using in the script, or using web3:
Then extract those uint256 in solidity:
And this should do the trick!