Skip to main content

Functions

Functions are a set of Rust-based features enabling developers to extend the native capabilities of Satellites. Functions facilitate the creation and management of serverless behaviors within smart contracts, or Satellites. Triggered by specific events like document and asset operations, they allow developers to embed custom logic directly into the blockchain environment.


How does it work?

Functions in Juno are defined using hooks that automatically handle event triggers related to documents and assets. These hooks include creating, updating, and deleting operations, allowing for a wide range of features within your Satellite.

A naive schema representation of a hook that is triggered when a document is set:

Functions hooks flow

Asynchronous Hook Spawning

When a Function is triggered, it spawns hooks asynchronously, operating independently of the caller's action. This means that the execution of the hooks is initiated without waiting for a synchronous response, ensuring that the flow of update calls to the Satellite remains unhindered. Consequently, callers may receive feedback on their actions before the corresponding hooks have commenced their execution.

Error-Resilient Execution

Hooks are initiated only when there are no errors in the preceding operations. This ensures a robust and dependable execution flow, promoting reliability and consistency in the functioning of Functions.

Optional

In the stock Satellite, custom hooks are not active by default. Developers should opt-in to activate these hooks to enable event-driven execution of custom logic if they wish to.


Available Hooks

HookProviderDescription
on_set_docDatastoreTriggered when a document is created or updated.
on_set_many_docsDatastoreActivated for operations involving multiple documents.
on_delete_docDatastoreInvoked when a document is deleted.
on_delete_many_docsDatastoreUsed when multiple documents are deleted.
on_delete_filtered_docsDatastoreInvoked when documents are deleted according filters.
on_upload_assetStorageTriggered during asset upload.
on_delete_assetStorageActivated when an asset is deleted.
on_delete_many_assetsStorageUsed for deleting multiple assets.
on_delete_filtered_assertsStorageInvoked when assets are deleted according filters.
on_initSatelliteCalled during the initialization of the satellite.
on_post_upgradeSatelliteInvoked after the satellite has been upgraded to a new version.

Assertions

In addition to hooks, developers have the option to expand the native rule set of their Satellites by creating custom assertions. These assertions can be implemented similarly to hooks, with the key difference being that they are synchronous and must return a result indicating the outcome of the assertion.

AssertionProviderDescription
assert_set_docDatastoreEnsures a document can be created or updated.
assert_delete_docDatastoreVerifies that a document can be deleted.
assert_upload_assetStorageConfirms an asset upload can be committed.
assert_delete_assetStorageChecks that an asset can be deleted.