2

I am working with the basic truffle framework. The MetaCoin.sol file which is installed automatically has the following

contract MetaCoin {
    mapping (address => uint) balances;

    function MetaCoin() {
        balances[tx.origin] = 10000;        
    }...

What this accomplishes is adding 10000 MetaCoin into account 0. What I want to do is access account 1, 2, 3, perhaps to add MetaCoin or do something else. But I don't know how to do this. I can do it by explicitly including the address (e.g. balances[0x90afa53bce...]) but this is bad because truffle addresses are not static between sessions.

I know that truffle tests starts out with all accounts in a list, but I want to access them within the contract first, and then test.

4

In the truffle console you can access them with web3.eth.getBalance(web3.eth.accounts[0]), just replace the zero.

In a Javascript file you can easily access them as showed in the basic metacoin test:

var MetaCoin = artifacts.require("./MetaCoin.sol");

contract('MetaCoin', function(accounts) {
  it("should put 10000 MetaCoin in the first account", function() {
    return MetaCoin.deployed().then(function(instance) {
      return instance.getBalance.call(accounts[0]);
    }).then(function(balance) {
      assert.equal(balance.valueOf(), 10000, "10000 wasn't in the first account");
    });
  });
});

In principle the contract doesn't know on which network it's deployed, therefore I'm not sure that you can do it from within the solidity contract.

| improve this answer | |

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.