Solidity

從合約到地址的無效隱式轉換

  • November 23, 2020
pragma solidity 0.5.10;


contract ERC20 {
   function totalSupply() public view returns (uint supply);
   function balanceOf(address _owner) public view returns (uint balance);
   function transfer(address _to, uint _value) public returns (bool success);
   function transferFrom(address _from, address _to, uint _value) public returns (bool success);
   function approve(address _spender, uint _value) public returns (bool success);
   function allowance(address _owner, address _spender) public view returns (uint remaining);
   function decimals() public view returns(uint digits);
   event Approval(address indexed _owner, address indexed _spender, uint _value);
}

contract TestContract{


   address payable public owner;

   

   constructor() public {
       owner = msg.sender;
       
   }

  
   function depositToken(address _addr) external
   {
       ERC20 tokenContract = ERC20(0x722dd3F80BAC40c951b51BdD28Dd19d435762180);
       
       require(tokenContract.allowance(msg.sender,this)>0);
       
       uint tokenAmount = tokenContract.allowance(msg.sender, this);
       require(tokenContract.transferFrom(msg.sender, this, tokenAmount));
       
   }
}

當我編譯這個契約返回

TypeError: Invalid type for argument in function call. Invalid implicit conversion from contract TestContract to address requested. require(tokenContract.allowance(msg.sender,this)>0); ^--^

和:

function allowance(address _owner, address _spender)

你應該改變這個:

tokenContract.allowance(msg.sender, this)

對此:

tokenContract.allowance(msg.sender, address(this))

函式的第二個輸入參數的類型是address.

但是,您正在傳遞一個 type 的值TestContract

在鏈下介面(例如web3.js)方面,這兩種類型是相同的。

但就鏈上介面(Solidity 編譯器)而言,它們是不同的。

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