# Schedwasm Autonomous Execution

SchedWasm is an on-chain autonomous execution engine that allows registered CosmWasm smart contracts to run automatically during the block lifecycle. Instead of relying on external keeper networks or off-chain bots to submit trigger transactions, the Uniocean blockchain natively invokes these contracts at the beginning of each block via the `BeginBlocker` hook.

## Mempool Bypass and Sudo Execution

Standard smart contract execution relies on user-signed transactions routed through the network mempool. SchedWasm completely bypasses this pathway. Because execution occurs during the consensus state transition (`BeginBlocker`), it is highly privileged.

The protocol natively constructs a `sudo` payload containing the current block height — `{"begin_blocker":{"height":H}}` — and pushes it to the contract. The engine evaluates active contracts on every single block; if a developer requires a specific interval schedule (e.g., executing once per hour), the contract must programmatically evaluate the injected block height and exit early if the interval has not been met.

## Automated Fee Pre-Charge Model

Because scheduled executions lack a standard user transaction, they cannot utilize the typical gas fee mechanisms. To maintain economic security and prevent spam, SchedWasm implements an automated fee pre-charge model.

Before a contract is executed, the protocol deterministically calculates the required fee *(GasPrice × GasLimit)* and charges it. SchedWasm supports three funding modes:

* **SELF\_FUNDED:** Fees are deducted directly from the smart contract's available balance.
* **GRANT\_ONLY:** Fees are paid using a predefined fee grant allowance from a separate sponsor address. Execution fails if the grant is exhausted.
* **DUAL:** The protocol attempts to use a sponsor's fee grant first, falling back to the contract's own balance if the grant is insufficient.

## Security Constraints and Determinism

To guarantee network determinism and protect block times, SchedWasm enforces rigorous operational boundaries:

* **Strict Gas Isolation:** Each scheduled contract is executed within an isolated context with a strict, predefined gas meter. If a contract exceeds its registered limit, execution is immediately halted without impacting the rest of the block.
* **Block-Level Bounding:** The network enforces a global `MaxContractsPerBlock` cap, limiting the total number of successful contract executions allowed in a single block to ensure consistent block times.
* **Anti-Surprise Upgrades:** To prevent malicious code swaps, the protocol verifies that the contract's current on-chain Code ID perfectly matches the originally registered Code ID prior to execution.
* **Failure Quarantine:** Consecutive execution failures are tracked in state. If a contract fails to execute successfully for 10 consecutive blocks (due to gas limits, insufficient fees, or panic), it is automatically deregistered from the SchedWasm engine to permanently free up block space.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://uniocean.gitbook.io/uniocean-docs/core-platform-modules/schedwasm-autonomous-execution.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
