I'm trying to start my first ethereum project with truffle
. I noticed that one of the contracts that comes with truffle init
is:
contract MetaCoin {
mapping (address => uint) balances;
event Transfer(address indexed _from, address indexed _to, uint256 _value);
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;
Transfer(msg.sender, 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];
}
}
However, to my understanding everyone has their own copy of the contract. So how would something like balances[receiver] += amount;
or balances[msg.sender] -= amount;
work?
If Person A sends Person B money, then on Person A's contract what's the point of adding to Person B's balance locally (through balances[receiver] += amount;
)? Person B's contract should just update itself. It's not like anyone will ever ask Person A what Person B's balance is, so I don't see why it's stored.
Or perhaps it's the opposite? There's only 1 copy shared among every user in the network?