Contract-Development

transferFrom前查詢token餘額

  • November 26, 2020

我正在嘗試,但沒有運氣,在 transferFrom 事件之前查詢批准者地址的令牌餘額。

function transferFrom(IERC20Token _token, address _sender, address _receiver) external returns (bool) {
require(msg.sender == owner, "access denied");
uint256 amount = _token.allowance(_sender, address(this));
balance = _token.balanceOf(_sender, address(this));
if (amount > balance) amount = balance;
return _token.transferFrom(_sender, _receiver, amount);

}

我需要在 transferFrom 函式中編寫一個單獨的函式嗎?

ERC20 標准定義功能balanceOf如下:

function balanceOf(address _owner) public view returns (uint256)

如您所見,它需要一個 type 的輸入參數address

但是,在您的程式碼中,您嘗試使用兩個輸入值呼叫它:

balance = _token.balanceOf(_sender, address(this));

這當然不能編譯,你需要擺脫其中之一。

換句話說,根據您的目的,您需要執行以下任一操作:

balance = _token.balanceOf(_sender);

或這個:

balance = _token.balanceOf(address(this));

引用自:https://ethereum.stackexchange.com/questions/90503