2

I'm testing the following contract using Truffle framework and deploying on TestRPC network.

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

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

  function sendCoin(address receiver, uint amount) returns(bool sufficient) {
    if (balances[msg.sender] < amount) return false;
    balances[msg.sender] -= amount;
    balances[receiver] += amount;
    return true;
  }

  function getBalanceInEth(address addr) returns(uint){
    return ConvertLib.convert(getBalance(addr),2);
  }

  function getBalance(address addr) returns(uint) {
    return balances[addr];
  }
}

I load it up in the browser and it seems to work. What I wonder is if it is possible to change the coinbase dynamically.

For example, after I've send some MetaCoins from user1 to user2 then I want to change the coinbase to user2 and reload the app to show me the MetaCoins balance of user2.

How can I achieve that?

3
  • Can you define "coinbase"? It has a specific definition in a mining context that I think you may be misuing
    – A F
    Apr 20, 2016 at 22:15
  • getBalance and getBalanceInEth might be declared constant. Then you can call them for free, fast and with any address from your Dapp.
    – jbaylina
    Apr 21, 2016 at 3:12
  • 1
    @jbaylina you don't have to declare it constant, you can use getBalance.call() to achieve the same result. It would be better style to make it constant, though Apr 21, 2016 at 13:32

1 Answer 1

3

With coinbase I guess you mean default address your web-application uses? Testrpc initialized by default 10 accounts. If you want to switch account in the javascript frontend, you can change the default account using the web3 api: https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethdefaultaccount

Alternatively, if you have an separate authentication mechanism in our web-application (for example a social login), you could associatie an ethereum address with a user. How and if depends on how your dapp/application is set up.

Your Answer

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

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