1

I'm creating a contract that holds users' funds and sends them back in a week.
I've created a retrieve() function (message) which does that.
Is there a way in ink! to postpone function execution for a period of time? (1 week)
Is there a way in ink! to call function on each block? (if the first option does not work)
How can I trigger that function except for explicitely calling it?

1 Answer 1

2

Contract logic runs on nodes in the network and each set of contract instructions are part of a single block. You cannot execute smart contract logic indefinitely like a cron job. Otherwise nodes could very quickly be overwhelmed by the number of continuous jobs running and the network would grind to a halt.

In your example, you will need to trigger the retrieve function after a specified amount of time from outside the contract. When you lock up funds, store the block_timestamp at which they were locked.

    pub struct LockedFunds {
        locked_at: Timestamp // use self.env().block_timestamp() here
        value: Balance
        account: AccountId
    }

Then in the retrieve function do something like

        #[ink(message)]
        pub fn retrieve(
            &mut self,
            locked_id: Hash
        ) -> Result<(), Error> {
            let caller = self.env().caller();
            let mut locked = self.get_locked_funds(locked_id)?;
            if caller == locked.account {
                if self.env().transfer(caller, locked.value).is_err() {
                    return Err(Error::ContractTransferFailed);
                }
                locked.value = 0
                self.update_locked_funds(locked);
                
            }
            Ok(())
        }
3
  • Wouldn't it be possible to use the ink! chain extension to call the scheduler pallet? Commented Jun 8, 2022 at 15:20
  • 1
    Yes, but the principal is still the same. You have something external to ink! that is doing the call to the retrieve function. It's not native to ink!. Plus, you would need to incentivise a network of nodes running your customised substrate node containing the aforementioned chain extension.
    – forgetso
    Commented Jun 8, 2022 at 15:28
  • Got it, thanks! Commented Jun 8, 2022 at 15:34

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.