Solidity
“發送”和“轉移”僅適用於“應付地址”類型的對象,而不適用於“地址”
我正在創建一個智能合約來出售我的代幣,但我遇到了以下問題:
TypeError: "send" and "transfer" are only available for objects of type "address payable", not "address".
問題出在這一行,我已經添加了“payable”選項,但隨後問題變為:
address payable owner = owner();
添加應付賬款後的新錯誤:
TypeError: Type address is not implicitly convertible to expected type address payable.
我的完整智能合約。
// SPDX-License-Identifier: MIT pragma solidity ^0.8.2; import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; import "@openzeppelin/contracts/utils/math/SafeMath.sol"; import "@openzeppelin/contracts/access/Ownable.sol"; contract SALESTOKEN is Ownable { using SafeMath for uint256; IERC20 token; uint256 private RATE = 3000; // Number of tokens per Ether uint256 private CAP = 5350; // Cap in Ether uint256 private START = 1519862400; // Mar 26, 2018 @ 12:00 EST uint256 private DAYS = 45; // 45 Day uint256 public initialTokens; // Initial number of tokens available bool public initialized = false; uint256 public raisedAmount = 0; /** * BoughtTokens * @dev Log tokens bought onto the blockchain */ event BoughtTokens(address indexed to, uint256 value); /** * buyTokens * @dev function that sells available tokens **/ function buyTokens() public payable whenSaleIsActive { uint256 weiAmount = msg.value; // Calculate tokens to sell uint256 tokens = weiAmount.mul(RATE); address owner = owner(); emit BoughtTokens(msg.sender, tokens); // log event onto the blockchain raisedAmount = raisedAmount.add(msg.value); // Increment raised amount token.transfer(msg.sender, tokens); // Send tokens to buyer owner.transfer(msg.value);// Send money to owner } /** * whenSaleIsActive * @dev ensures that the contract is still active **/ modifier whenSaleIsActive() { // Check if sale is active assert(isActive()); _; } function isActive() public view returns (bool) { return ( initialized == true && block.timestamp >= START && // Must be after the START date block.timestamp <= START.add(DAYS * 1 days) && // Must be before the end date goalReached() == false // Goal must not already be reached ); } /** * goalReached * @dev Function to determin is goal has been reached **/ function goalReached() public view returns (bool) { return (raisedAmount >= CAP * 1 ether); } function setRate(uint _rate) public onlyOwner{ RATE = _rate; } function setCap(uint _cap) public onlyOwner{ CAP = _cap; } function setStart(uint _start) public onlyOwner{ START = _start; } function setDays(uint _days) public onlyOwner{ DAYS = _days; } function setAddrERC20(address _tokenAddr) public onlyOwner{ require(_tokenAddr != address(0)); token = IERC20(_tokenAddr); } }
owner() 返回一個地址,而不是一個應付地址,你仍然需要轉換它:
address payable owner = payable(owner());
您也可以將地址轉換為應付地址等。
// convert from address to payable address address payable payable_addr = payable(addr); // or address payable payable_addr = address(uint160(addr)); // convert from payable address to address address addr = address(payable_addr);