0

I have two separate contracts, let's say Token and BuyFrom.

pragma solidity >=0.4.22 <0.7.0;

interface InterfaceToken {
    function invest(address contributor) external payable;
}

contract Token {
    string public name = "Token";
    string public symbol = "TKN";
    uint public decimals = 0;
    uint tokenPrice = 1000000000000000;

    uint public supply;
    address public founder;
    address payable public deposit;

    mapping(address => uint) public balances;

    constructor(address payable _deposit, address _founder) public{
        supply = 300000;
        deposit = _deposit;
        founder = _founder;
        balances[founder] = supply;
    }

    function invest(address contributor) public payable {
        uint tokens = msg.value / tokenPrice;

        balances[contributor] += tokens;
        balances[founder] -= tokens;

        deposit.transfer(msg.value);
    }
}

I want to buy tokens from BuyFrom using an interface. But it doesn't work.

pragma solidity >=0.4.22 <0.7.0;

import 'Token.sol';

contract BuyFrom {
    address payable private token;

    constructor(address payable _token) public {
        token = _token;
    }

    function buyToken(address contributor) external payable {
       InterfaceToken(token).invest(contributor);
    }
}

What I'm doing wrong?

0
1

Since InterfaceToken(token).invest is an external call (i.e., to a function in another contract), you need to pass along the value that your function has received.

In other words, change this:

InterfaceToken(token).invest(contributor);

To this:

InterfaceToken(token).invest.value(msg.value)(contributor);

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.