3

I'd like to publish a contract that allows calls up until a certain time and date.

For example, say I want to allow anyone to call it for 1 year after publishing the contract to the blockchain, then after that time I want that method to no longer work when called.

How might this be implemented, ideally using a Smartpy example?

1 Answer 1

4

You can do this by simply adding a time check.

import smartpy as sp

class TimedCall(sp.Contract):
    def __init__(self):
        self.init(
            time_limit = sp.timestamp_from_utc_now().add_days(365),
            val = sp.nat(5)
        )
    
    @sp.entry_point
    def timed_method(self, param):
        sp.set_type(param, sp.TNat)
        
        sp.verify(sp.now < self.data.time_limit, "TIME LIMIT CROSSED")
        
        self.data.val = param

@sp.add_test(name = "main")
def test():
    scenario = sp.test_scenario()
    
    c = TimedCall()
    
    scenario += c
    
    current_time = sp.timestamp_from_utc_now()
    
    # PASSED call
    scenario += c.timed_method(6).run(now = current_time.add_days(50))
    
    # FAILED call
    scenario += c.timed_method(7).run(now = current_time.add_days(366), valid = False)

IDE link: https://smartpy.io/ide?cid=QmVrHP4qpWbDwXd2MsYPEsCnZGhz7naVSHPKwG4NdJGHSU&k=05862a678a886954120c

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.