I am learning solidity and want to transfer tokens to others in my contract. But something blocked the way. Hope someone to help me. Below is my code:
pragma solidity >= 0.8.0;
contract Test {
bytes4 private constant SELECTOR = bytes4(keccak256(bytes('transfer(address,uint256)')));
constructor() {
}
function safeTransfer(address token, address to, uint value) public {
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(SELECTOR, to, value));
require(success && (data.length == 0 || abi.decode(data, (bool))), 'TRANSFER_FAILED');
}
}
And these are the arguments I pass to safeTransfer
in remix on the goerli testnet.
token
: 0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6, which is WETH
.
And here is the error message.
I do not understand why this fails. I've tried multiple tokens and all of them just failed with the error message TRANSFER_FAILED
I am confident there are enough WETH token on the account I call this function.