Simplified Subscriptions to System Events via the typescript SDK
Starting from @somnia-chain/[email protected], the typescript SDK introduces two new convenience functions to streamline creating subscriptions for system-generated events: BlockTick and Schedule. These functions reduce boilerplate by handling the underlying SubscriptionData structure and precompile interactions for you, while still allowing customization of gas fees, guarantees, and other parameters.
For a deeper understanding of how these system events work under the hood (including event signatures and behaviors), refer to the System Events documentation.
Block Tick Subscription
The BlockTick event triggers at the start of every block if no specific block number is provided, or at a targeted block if specified.
Using the SDK
Use createOnchainBlockTickSubscription to set up the subscription with minimal code:
import{SDK}from'@somnia-chain/reactivity';// Assuming you have an instance of SomniaReactivity SDKconstreactivity=newSDK(/* your config */);asyncfunctionsetupBlockTick(){try{consttxHash=awaitreactivity.createOnchainBlockTickSubscription({ // Optional: Specify a future block number; omit for every block // blockNumber: BigInt(123456789),handlerContractAddress:'0xYourHandlerContractAddress', // Optional: Override default handler selector (defaults to onEvent) // handlerFunctionSelector: '0xYourSelector',priorityFeePerGas:BigInt(1000000000),// 1 nanoSomimaxFeePerGas:BigInt(20000000000),// 20 nanoSomigasLimit:BigInt(100000),isGuaranteed:true,// Ensure delivery even if delayedisCoalesced:false// Handle each event separately});console.log('Subscription created with tx hash:',txHash);}catch (error) {console.error('Error creating subscription:',error);}}setupBlockTick();
This returns the transaction hash on success or an error if the subscription fails.
Equivalent in Solidity
For comparison, here's the lower-level Solidity equivalent (as in the core docs):
Schedule Event (One-Off Cron Job)
The Schedule event is ideal for one-time future actions. Key notes:
Timestamp must be in the future (at least 12 seconds from n ow).
It's a one-off subscription—automatically deleted after triggering.
This returns the transaction hash on success or an error if the subscription fails.
Equivalent in Solidity
For comparison, here's the lower-level Solidity equivalent (as in the core docs):
These SDK functions default the emitter to SOMNIA_REACTIVITY_PRECOMPILE_ADDRESS and handle event topics automatically, ensuring your handler only responds to genuine system events.
To get started, update your typescript SDK package to @somnia-chain/[email protected] or later and integrate these functions into your dApp.
If you need more advanced customizations (e.g., additional filters like origin or caller), you can still use the full SoliditySubscriptionData type directly with the createSoliditySubscription SDK function (scheduleOnchainCronJob is calling that internally).