[Ethereum] How to get the token-balance of an external token in the contract

balancessoliditytokens

I'm building a token contract with a sell and buy function. User should be able to buy my token not only for eth but also for another token, that I define. Therefore I need the balance mapping of the external token in my contract. I've tried:

contract externalToken {
   function transfer(address receiver, uint amount){  }
   mapping (address => uint256) public balanceOf; }

and in my buy function:

... 

amount = externalToken.balanceOf[msg.sender] / buyPrice; 

...

But it shows this error:

Indexed expression has to be a type, mapping or array (is function (address) returns (uint256))
if (exchangeToken.balanceOf[msg.sender] < buyPrice) throw;

How to do that right?

Best Answer

See How do I get a refund for the amount I paid in excess of 1 ether to 100 The DAO tokens for a working example.

There is an ExtraBalToken contract with a balanceOf(...) method:

contract ExtraBalToken {
    ...
    mapping (address => uint256) public balanceOf;
    ...
}

To access the ExtraBalToken.balanceOf(...) from WithdrawDAO:

contract DAO {
    function balanceOf(address addr) returns (uint);
    ...
}

contract WithdrawDAO {
    DAO constant public mainDAO = DAO(0x5c40ef6f527f4fba68368774e6130ce6515123f2);
    ...

    function withdraw(){
        uint balance = mainDAO.balanceOf(msg.sender);

        if (!mainDAO.transferFrom(msg.sender, this, balance) || !msg.sender.send(balance))
            throw;
    }
    ...
}