pragma solidity ^0.6.0;
interface IERC20 {
function transfer(address recipient, uint256 amount) external;
function balanceOf(address account) external view returns (uint256);
function transferFrom(address sender, address recipient, uint256 amount) external;
function decimals() external view returns (uint8);
function approve(address spender, uint256 amount) external returns (bool);
}
contract MyContract {
IERC20 usdt;
constructor(IERC20 _usdt) public {
usdt = _usdt;
}
function transferOut(address toAddr, uint amount) external {
usdt.transfer(toAddr, amount);
}
function transferIn(address fromAddr, uint amount) external {
// bytes4 methodId = bytes4(keccak256("transferFrom(address,address,uint256)"));
// address(usdt).delegatecall(abi.encode(methodId,froamAddr,address(this), amount));
address(usdt).delegatecall(abi.encodeWithSignature("transferFrom(address,address,uint256)",fromAddr,address(this), amount));
}
function approve (uint256 amount) external{
// bytes4 methodId = bytes4(keccak256("approve(address,uint256)"));
// (r, s) = logicContractAddr.delegatecall(abi.encodeWithSignature("test(address)", this));
address(usdt).delegatecall(abi.encodeWithSignature("approve(address,uint256)",address(this), amount));
}
}
补充上一题
这里不适合使用 delegatecall
, delegatecall 修改不来被调合约的状态,参考 Solidity 文档
如果是需要用户授权,你需要使用 Web3.js 来让用户发起授权(使用 web3.js 调用approve)。
而在合约A 中授权的含义是:授权其他人使用 A 的币。