I'd like to create a contract that behaves a certain way based on how much total tez has been sent to the contract.

How should I track and check the total amount ever sent to a contract?


  • User A is the first to send 1 tez to the contract, the contract does something with the information that 1 tez has been sent (1 tez now and 0 tez previously).
  • User B sends 1 tez to the contract, the contract does something with the information that 2 tez has been sent to the contract (1 tez now and 1 tez previously).

1 Answer 1


In SmartPy you can do this: code

The IDE has an output panel that you can use to check all the calls and updated storage, hope it helps :)

import smartpy as sp

class MyContract(sp.Contract):
    def __init__(self, **kargs):

    def send_tez(self):
        # Do some stuff
        self.data.previousAmount = self.data.latestAmount;
        self.data.latestAmount = sp.amount
    def dont_allow_amount(self):
        sp.verify(sp.amount == sp.tez(0), message = "Entrypoint doesn't allow amount.")

@sp.add_test(name = "Amount recording")
def test():
    scenario = sp.test_scenario()
    scenario.h1("Amount recording")
    c1 = MyContract(latestAmount = sp.tez(0), previousAmount = sp.tez(0))
    scenario += c1
    scenario += c1.send_tez().run(amount = sp.tez(1))
    scenario += c1.send_tez().run(amount = sp.tez(2))
    scenario += c1.send_tez().run(amount = sp.tez(3))
    # amount > 0 is rejected in the other entrypoint
    scenario += c1.dont_allow_amount().run(amount = sp.tez(1), valid = False)
    # amount = 0 is fine
    scenario += c1.dont_allow_amount().run(amount = sp.tez(0))
  • Thank you, this is exactly what I wanted to learn.
    – Geoff
    Commented Jun 27, 2021 at 18:39

Your Answer

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

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