rooch-network / rooch

VApp Container with Move Language for Bitcoin ecosystem
https://rooch.network
Apache License 2.0
158 stars 83 forks source link

Tick solution #766

Open jolestar opened 1 year ago

jolestar commented 1 year ago

For a fully on-chain game, a mechanism is needed to drive the game logic automatically. However, current on-chain contracts can only be triggered by user transactions. To address this, a Tick solution is required to enable the scheduled execution of transactions by the system.

How to define a tick contract

  1. static register based
module mygame::my_module{

  fun init(ctx: &mut StorageContext){
    rooch_framework::tick::register(module: mygame::my_module, interval: 10 seconds)
  }

  fun tick(ctx: &mut StorageContext){

  }

}
  1. one-time callback based
module mygame::my_module{

  fun game_loop(ctx: &mut StorageContext){
     //so something
     rooch_framework::tick::set_timeout(function: mygame::MyModule::game_loop, time: 10 seconds);
  }
}
Zombieliu commented 10 months ago

good idea!

jolestar commented 10 months ago

Another solution is to provide a message-based trigger, a contract can watch an Event, and the tick is also an Event.