Skip to main content

Autonomous smart contracts (ASC)

Autonomous smart contracts (ASC) are a set of features that allow smart contracts to be executed asynchronously. It allow a smart contract to be scheduled in the future in a decentralized way. These features enable developers to build more complex and powerful decentralized applications on the Massa blockchain.

Two different types of ASC are available in Massa:

The main difference between them is the guarantee of execution. Deferred calls are guaranteed while async messages are not. In both cases, a smart contract function will be called and executed if conditions are met. An ASC call follow the same execution parameters as a regular smart contract call (including coins, maxGas, fee and function arguments).

Async Call overview

The asyncCall function enables scheduling an operation for execution at a future time. The operation is processed as soon as possible after the specified start slot but no later than the end slot.

Execution Behavior:

  • Operations are executed deterministically on all nodes.
  • Execution follows a priority system based on the fee attached to the message.
  • If an async operation has a low fee/maxGas ratio, it may be delayed if higher-fee async operation take priority.
  • If all slots in the specified period are congested with higher-fee async operations, it may never execute.

Optional Execution Filter:

You can add a filter condition that requires a state update before the operation executes. Instead of executing "as soon as possible within the range", the operation will execute only after a specific state change occurs.

Filter Parameters:

  • filterAddress: The contract or account whose state change will trigger execution.
  • filterKey: A specific datastore entry within filterAddress that must change. filterAddress must be provided.

Execution Conditions:

  • If only filterAddress is provided:
    • The message executes within the specified range only if there is a state change (e.g., balance update) in filterAddress.
  • If filterKey is also provided:
    • The message executes within the range only if the specific filterKey in filterAddress's datastore has changed.
asyncCall(
at: Address,
functionName: string,
validityStartSlot: Slot,
validityEndSlot: Slot,
maxGas: u64,
fee: u64,
functionParams: StaticArray<u8> = [],
coins: u64 = 0,
filterAddress: Address = new Address(),
filterKey: StaticArray<u8> = [],
): void

Parameters:

  • at: Address of the contract to call.
  • functionName: Function to call in the contract.
  • validityStartSlot: Validity start slot.
  • validityEndSlot: Validity end slot.
  • maxGas: Maximum gas to spend for the execution.
  • fee: Fee to be paid for message execution.
  • functionParams: Serialized function call parameters.
  • coins: Coins to allocate for the execution. If called function is on the same contract, this field has no effect as sender and recipient are the same.
  • filterAddress: Address trigger filter.
  • filterKey: Datastore key filter from the trigger address.

Usage example

Syntra leverage the async call feature to allow sending tips or setup vesting plans for users. Check Syntra smart contract for a real world example of async call usage.

Deferred calls (Buildnet Preview)

Unlike async calls, deferred calls are guaranteed to be executed at a given slot. The scheduele mecanism works as a booking market based on computation needs and load of available slots. In order to book for an execution in the future, a user must pay a fee to the network. The booking quote is calculated based on the amount of computation needed, the size of function call parameters in bytes and the load of the network.

Request a quote

The deferredCallQuote function is used to get the booking fee for a deferred call. The quote provides an estimate of the fee required to schedule the deferred call for the given slot.

deferredCallQuote(
targetSlot: Slot,
maxGas: u64,
paramsSize: u64 = 0,
): u64

Parameters:

  • targetSlot: Slot to book for the execution.
  • maxGas: The maximum amount of gas that the deferred call can use.
  • paramsSize: Serialized function call parameters size in bytes.

Returns:

  • The booking quote in nanoMAS for the deferred call.

Register a deferred call

The deferredCallRegister function is used to book a deferred call.

deferredCallRegister(
targetAddress: string,
targetFunction: string,
targetSlot: Slot,
maxGas: u64,
params: StaticArray<u8> = [],
coins: u64 = 0,
): string

Parameters:

  • targetAddress: Address of the contract to call.
  • targetFunction: Function to call in the contract.
  • targetSlot: Slot to book for the execution.
  • maxGas: The maximum amount of gas that the deferred call can use.
  • params: Serialized function call parameters.
  • coins: Coins to allocate for the execution. If called function is on the same contract, this field has no effect as sender and recipient are the same.

Returns:

  • The created deferred call ID.

Cancel a deferred call

The deferredCallCancel function is used to cancel a deferred call. A deferred call can only be canceled by its creator. To avoid booking market manipulation, the booking fee is not refunded and reserved gas for execution is not freed when canceling.

deferredCallCancel(id: string): void

Parameters:

  • id: The deferred call ID to cancel.

Check deferred call status

The deferredCallExists function is used to check if a deferred call is schedueled.

deferredCallExists(id: string): bool

Parameters:

  • id: The deferred call ID to check.

Returns:

  • True if the deferred call is schedueled. False if already executed, canceled or inexistant.

Find a slot to book

The findCheapestSlot function is a helper that compares the booking quotes in a given slot range and returns the cheapest one.

findCheapestSlot(
startPeriod: u64,
endPeriod: u64,
maxGas: u64,
paramsSize: u64,
): Slot

Parameters:

  • startPeriod: First slot to check.
  • endPeriod: Last slot to check.
  • maxGas: The maximum amount of gas that the deferred call can use.
  • paramsSize: Serialized function call parameters size in bytes.

Returns:

  • The slot with the cheapest quote in the given range.

Usage example

Deferred call manager is a smart contract example that allow to schedule tasks in the future and monitor execuions. Check Deferred call manager contract here.