You can make all the transfer functions private
and add a fallback
function which is payable
. In the fallback
function use msg.sig
to determine which function to call. You can calculate the signatures like this.
bytes4 sigOne = bytes4(keccak256('safeTransferFrom(address,address,uint256)'))
bytes4 sigTwo = bytes4(keccak256('safeTransferFrom(address,address,uint256,bytes)'))
then you can implement the fallback function that calls the private transfer functions with arguments decoded. like so.
pragma solidity 0.6.6;
contract Test {
bytes4 sigOne = bytes4(keccak256('safeTransferFrom(address,address,uint256)'));
bytes4 sigTwo = bytes4(keccak256('safeTransferFrom(address,address,uint256,bytes)'));
fallback () external payable {
require(msg.value > 0);
// transfer amount to the address
bytes memory payload = abi.encodePacked(bytes28(0), msg.data);
bytes4 sig = msg.sig;
if (sig == sigOne) {
(
bytes32 _,
address from,
address to,
uint256 value,
) = abi.decode(payload, (bytes32, address, address, uint256));
safeTransferFrom(from, to, value);
} else if (sig == sigTwo) {
(
bytes32 _,
address from,
address to,
uint256 value,
bytes memory data,
) = abi.decode(payload, (bytes32, address, address, uint256, bytes));
safeTransferFrom(from, to, value, data);
} else {
revert("signature did not match");
}
}
}
Hope it helps!