How do I update my balance on a contract?

I created a function that receives an address as a parameter, I can visualize the balance:


But I want to transfer the balance to an account and update the value, but I can not do:

address.balance = + value;

What do I do?


Balances are at the protocol level, independent of contracts and you can't assign a balance from inside a contract.

There is more than one way to do it, but the most common and safest is to use the .transfer() method that is attached to all variables of type address.

To transfer from this contract to somewhere else:

address somewhereElse = 0x123...

From somewhere else to this contract, someone has to send it. For example, from their wallet to the contract address.

Your contract needs a payable "fallback function" or it will reject the funds.

function () payable {
  // we are here when someone else sends funds.
  // this provides an opportunity to emit an event.
  // address(this).balance will automatically += msg.value

Hope it helps.

  • So I can not handle account balances in the contract? Do I have to transfer the balances to the contract, and then transfer to a specific account? My idea was to make a contract that mimics an investment fund, where there would be 3 accounts, one for the fund, one for the client and one for the trade. At a certain date the test would be carried out, if the trade account balance would have increased or decreased. Then the balance would be sent to the client and to the fund. The only point I could not implement was to transfer the balances to the accounts at the end of the contract. – Jarbas Lucena Nov 23 '18 at 16:44
  • This is a good question that deserves another explanation and example. Yes, it can be done and happens all the time. No, not like that. If you post something like "How can I create a contract to manage customer balances", I will answer later today. – Rob Hitchens Nov 23 '18 at 16:51
  • I think is better if you change your question and title clarifying what you'd like to do so that more people could help – Marco Ottolini Nov 23 '18 at 16:52
  • Thanks for the tips, I just got to know the platform. Let me repeat the question. – Jarbas Lucena Nov 23 '18 at 17:22

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.