0

So I have the following functions

buyTokens

function buyTokens(address beneficiary) public payable {
    require(beneficiary != address(0));
    require(validPurchase());

    uint256 weiAmount = msg.value;

    uint256 tokens = weiAmount.mul(getRate());

    weiRaised = weiRaised.add(weiAmount);

    token.issue(address(vault), tokens);
    vault.deposit.value(msg.value)(beneficiary, tokens);

    emit TokenPurchase(msg.sender, beneficiary, weiAmount, tokens);

    forwardFunds();
}

forwardFunds

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

deposit (in another smart contract)

function deposit(address supporter, uint256 tokensAmount) onlyOwner public payable{

    depositedETH[supporter] = depositedETH[supporter].add(msg.value);
    depositedToken[supporter] = depositedToken[supporter].add(tokensAmount);

    emit Deposit(supporter, msg.value, tokensAmount);
}

So buyTokens won't work unless I comment out EITHER the deposit call or the forwardFunds call. After doing some reading I was thinking that it had something to do with gas?

Furthermore, if I call the deposit function by passing the value as a param rather than receiving through the msg object in the other function (not using .value() methodology) it works fine.

vault.deposit(beneficiary, tokens, msg.value)

Is there an issue with using a .value() call and also forwarding to an internal function that uses .transfer()?

Thanks!

  • Hi! You said > "buyTokens won't work", but in which way? Kindly share which message lets you know there is a problem. Like a call with arguments and what you see then. – Utgarda Sep 13 '18 at 15:45
1

This is forwarding the ether sent to the vault contract

vault.deposit.value(msg.value)(beneficiary, tokens);

And this is forwarding the ether sent to the wallet contract

wallet.transfer(msg.value);

Obviously you cannot forward the same ether to two different contracts.

  • I think you pointed out the issue of it trying to send an amount twice. So, out of curiosity, if the contract containing the buyTokens function had a bunch of ether itself stored in it, would it be able to execute? Ex: if someone fires the buyTokens function, it fires the deposit function and sends a value (using the ether stored within the contract), AND THEN does THE transfer function, sending the same amount (from itself) to the wallet. Provided the contract had enough ether, would it work? This is just as a thought experiment. Obviously I won't want to do that – tylerism Sep 13 '18 at 18:01
  • Yes, if the contract has enough funds the second transfer will succeed. – Ismael Sep 13 '18 at 19:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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