4

I'm creating a smart contract to sell my tokens, but I'm having the following problem:

TypeError: "send" and "transfer" are only available for objects of type "address payable", not "address".

The problem is in this line, I already added the option "payable" but then the problem changed to:

address payable owner = owner();

New error after added payable:

TypeError: Type address is not implicitly convertible to expected type address payable.

My full smart contract.

// 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);
  }
}
0

2 Answers 2

5

owner() returns an address, not a payable address, you still need to cast it :

address payable owner = payable(owner());
1

You can convert an address to payable address and otherways too.

// 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);
1

Not the answer you're looking for? Browse other questions tagged or ask your own question.