Solidity

如何修復用solidity編寫的智能合約?

  • November 20, 2021

TypeError:函式呼叫中的參數類型無效。請求的從地址到應付地址的隱式轉換無效。自毀(管理員);

pragma solidity >=0.4.21 <0.6.0;
import "./DappToken.sol";
contract DappTokenSale{
   address admin;
   DappToken public tokenContract;
   uint256 public tokenPrice;
   uint256 public tokensSold;

   event Sell(address _buyer, uint256 _amount);

   constructor(DappToken _tokenContract, uint256 _tokenPrice) public {
       //Assign an admin
       admin = msg.sender;
       //token Contract
       tokenContract = _tokenContract;
       //Token Price
       tokenPrice = _tokenPrice;
   }
   // multiply function
   function multiply(uint x, uint y) internal pure returns (uint z) {
       require(y == 0 || (z = x * y) / y == x);
   }
   // Buy Token
   function buyTokens(uint256 _numberOfTokens) public payable{
       // Reqiured that value is equal to tokens
       require(msg.value == multiply(_numberOfTokens, tokenPrice));
       // Required that the contract has enough token
       require(tokenContract.balanceOf(address(this)) >= _numberOfTokens);    
       // Required that a trnasfer is successfuly
       require(tokenContract.transfer(msg.sender,_numberOfTokens));
       // keep track of number of token sold
       tokensSold += _numberOfTokens;

       // Trigger sell event
       emit Sell(msg.sender, _numberOfTokens);
   }

   // Ending Token DappTokenSale
   function endSale() public {
       // Require admin
       require(msg.sender == admin);
       // Transfer remaining dapp token to admin
       require(tokenContract.transfer(admin, tokenContract.balanceOf(address(this))));
       // UPDATE: Let's not destroy the contract here
       // Just transfer the balance to the admin
       // admin.transfer(address payable(this).balance);
       // selfdestruct(address payable admin);
       // selfdestruct(address payable admin);
       //address payable adminAddress = address(uint160(admin));
       selfdestruct(admin);
   }
}

將第 4 行更改為:

address payable admin;

希望能幫助到你。

=> 在第一行添加這段程式碼 // SPDX-License-Identifier: MIT

=> 更改地址管理員;處理應付管理員;

=> 在每個事務事件之前添加發射,例如發射 Sell(msg.sender, _numberOfTokens);

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