Can we have a function in a smart contract to be executed automatically after some time?

for example, transfer some tokens after every 5 seconds.

  • 2
    Call the action as a deferred transaction recursively. – Rajat Chaudhary Nov 6 at 6:44
  • explain in detail sir please I don't know anything about the deferred transaction? – yash vadhvani Nov 6 at 6:53

A deferred transaction can be created and submit that will be executed at the specified future time. If you want the action to execute again, then the action can simply schedule a new deferred transaction to invoke itself.

// Create a deferred transaction
transaction out{};
out.actions.emplace_back(
        permission_level{_self, "active"_n},
        _self,
        "myaction"_n,
        std::make_tuple(actionParam)
);
out.delay_sec = refundInterval;
out.send(deferredTxID, _self);

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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