1

I'm creating a simple ICO contract, it sends the amount of the token bought to the buyer as soon as the contract receives ether, and then sends the received ether to a different wallet, I've been testing it on remix all day but the contracts receive the ether but doesn't send out the ether to the wallet neither does the buyer received hers. Here's the buying function.

function buyTokens(address _beneficiary) public payable {

    uint256 weiAmount = msg.value;
    _preValidatePurchase(_beneficiary, weiAmount);
    uint256 tokens = _getTokenAmount(weiAmount);

    weiRaised = weiRaised.add(weiAmount);
    _processPurchase(_beneficiary, tokens);


 
    _forwardFunds();
    _postValidatePurchase(_beneficiary, weiAmount)
  }

  function _getTokenAmount(uint256 _weiAmount)
    internal view returns (uint256)
  {
    return _weiAmount.mul(rate);
  }

  function _processPurchase(
    address _beneficiary,
    uint256 _tokenAmount
  )
    internal
  {
    _deliverTokens(_beneficiary, _tokenAmount);
  }
 
   function _deliverTokens(
    address _beneficiary,
    uint256 _tokenAmount
  )
    internal
  {
    token.transfer(_beneficiary, _tokenAmount);
  }


  function _forwardFunds() internal {
    bool sent =  wallet.transfer(address(wallet),msg.value);
    require(sent, "Transaction failed");
  }

1 Answer 1

3

The way you are sending ether is wrong.

It should be :

 function _forwardFunds() internal {
     wallet.transfer(msg.value);
   
  }

Note here that transfer doesn't return a bool, and it takes one argument which is the amount to send. Also, make sure the address is payable.

About the tokens not being transferred to the buyer, can you provide more details, also check if this ICO contract has a balance or not, because the tokens are deducted from this contract.

8
  • I'm using the IERC20 interface, and the transfer function takes in two arguments, I just tried the single argument and it said it needed two arguments.
    – Santos
    Commented Aug 12, 2023 at 11:55
  • Wait a minute, I am talking about the transfer function in solidity, which is used to send ether. The ERC20 transfer obviously takes 2 arguments. May I know what wallet means in the _forwardFunds function? Commented Aug 12, 2023 at 13:26
  • when a buyer sends in BNB to the contract to purchase the ICO token, the BNB is sent to the wallet(the _forwardfsFunds) and the _processPurchase, sends the token to the sender
    – Santos
    Commented Aug 12, 2023 at 13:36
  • That's what I am talking about, wallet is an EOA, right? And BNB is not an ERC20 token. So you won't use ERC20 transfer but the solidity transfer function, which sends the BNB to the wallet. And it accepts only one argument. There's a difference between both the transfers. Commented Aug 12, 2023 at 14:23
  • Yes, you're right. I made a mistake there and it has been updated but still the contract doesn't send the funds to the wallet neither is the ERC20 sent to the buyer,
    – Santos
    Commented Aug 12, 2023 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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