Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add autonomous smart contract documentation #346

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions docs/build/autonomous-smart-contract/intro.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
sidebar_position: 1
---

# Autonomous Smart Contracts: Introduction

Autonomous Smart Contracts (ASCs) represent a revolutionary concept in blockchain technology, enabling smart contracts to operate independently without the need of external triggers. This capability is a cornerstone feature of the Massa blockchain, setting it apart from traditional blockchain platforms.

## Key Features

1. **Self-Execution**: ASCs can schedule their own future executions.
2. **Inter-Contract Communication**: ASCs can trigger functions in other contracts.
3. **Conditional Execution**: Executions can be based on specific blockchain conditions.

## Looking Ahead

In the following sections, we'll explore how to implement Autonomous Smart Contracts using Massa's `sendMessage` function and discuss the upcoming feature of guaranteed execution for even more reliable autonomous operations.

Stay tuned to learn how you can leverage these powerful capabilities in your Massa blockchain projects!
104 changes: 104 additions & 0 deletions docs/build/autonomous-smart-contract/send-message.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
---
id: send-message
sidebar_label: Send Message
---

# Autonomous Smart Contracts: Send Message

The `sendMessage` function is one of the core mechanism enabling Autonomous Smart Contracts (ASCs) in the Massa blockchain. This powerful feature allows contracts to schedule future executions, creating truly autonomous systems.

## Understanding `sendMessage`

### Function Signature

```typescript
function sendMessage(
at: Address,
functionName: string,
validityStartPeriod: u64,
validityStartThread: u8,
validityEndPeriod: u64,
validityEndThread: u8,
maxGas: u64,
rawFee: u64,
coins: u64,
functionParams: StaticArray<u8>,
filterAddress: Address = new Address(),
filterKey: StaticArray<u8> = new StaticArray<u8>(0)
): void;
```

### Key Parameters

- `at`: The target contract address
- `functionName`: The function to be called
- `validityStartPeriod` & `validityEndPeriod`: The time window for execution
- `maxGas`: Maximum gas allowed for execution
- `rawFee`: Fee for prioritizing the message
- `filterAddress` & `filterKey`: Optional conditions for execution

## Usage Patterns

### Self-Calling Functions

Create recursive processes:

```typescript
export function recursiveFunction(): void {
if (shouldContinue()) {
sendMessage(
Context.callee(), // Current contract's address
"recursiveFunction", // Function to call
Context.currentPeriod(), // Start validity period
Context.currentThread(), // Example start thread
Context.currentPeriod() + 10, // Meaning 10 periods of validity
Context.currentThread(), // Example end thread
4_000_000_000, // Example maxGas
100000, // Example rawFee
0, // Example coins
[] // Example functionParams
);
}
}
```

### Inter-Contract Communication

Schedule calls to other contracts:

```typescript
export function scheduleExternalCall(): void {
sendMessage(
new Address("AS..."), // Other contract's address
"externalFunction",
Context.currentPeriod(),
Context.currentThread(),
Context.currentPeriod() + 10,
Context.currentThread(),
4_000_000_000, // maxGas
100000, // rawFee
0, // coins
new Args().add("my_param").serialize()
);
}
```

## Important Considerations

### Execution Not Guaranteed

The execution of scheduled messages depends on network conditions and the provided fee. Higher network traffic and lower fees may result in failed executions.

### Best Practices

1. Provide adequate fees, especially for critical operations
2. Use appropriate validity periods to balance execution likelihood and timeliness
3. Carefully manage contract state and handle potential race conditions

Ben-Rey marked this conversation as resolved.
Show resolved Hide resolved
:::note
Find more information about autonomous smart contracts in the [Massa documentation](https://docs.massa.net/docs/learn/architecture/basic-concepts#autonomous-smart-contract-execution).
:::

## Conclusion

The `sendMessage` function is a powerful tool for creating autonomous systems on Massa. While it offers great flexibility, developers must carefully consider execution dynamics and implement appropriate safeguards to ensure reliable operation of their Autonomous Smart Contracts.
32 changes: 32 additions & 0 deletions docs/build/autonomous-smart-contract/with-guaranteed-execution.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
id: with-guaranteed-execution
sidebar_label: With Guaranteed Execution
---

# Autonomous Smart Contracts: Guaranteed Execution

## Introduction

Massa is developing an exciting new feature for Autonomous Smart Contracts (ASCs) called Guaranteed Execution. This feature aims to enhance the reliability and predictability of smart contract operations on the Massa blockchain.

## Concept Overview

Guaranteed Execution introduces a mechanism for smart contracts to reserve future execution slots, ensuring that critical operations occur at specified times, regardless of network conditions.

### Key Aspects

1. **Future Slot Reservation**: Smart contracts can book specific future time slots for execution.
2. **Execution Assurance**: Once reserved, the execution is guaranteed to occur at the specified time.
3. **Resource Management**: The system allocates computational resources for these guaranteed executions.
4. **Advance Planning**: Allows for scheduling executions further into the future with certainty.

## Advantages Over Current System

Guaranteed Execution addresses limitations in the current ASC system:

- **Predictability**: Eliminates uncertainty about whether a transaction will be processed in high-congestion periods.
- **Timeliness**: Ensures critical operations occur exactly when

## Conclusion

Guaranteed Execution is a significant step forward in the evolution of Autonomous Smart Contracts. This feature will provide developers with a powerful tool for building robust, reliable, and predictable smart contract systems. Stay tuned for more updates on this exciting development!
7 changes: 0 additions & 7 deletions docs/build/smart-contract/_category_.json

This file was deleted.

23 changes: 22 additions & 1 deletion sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,28 @@ const sidebars = {
},
{
type: "category",
label: "Web3 v2",
label: "Autonomous Smart Contract",
items: [
{
type: "doc",
id: "build/autonomous-smart-contract/intro",
label: "Introduction",
},
{
type: "doc",
id: "build/autonomous-smart-contract/send-message",
label: "Send Message",
},
{
type: "doc",
id: "build/autonomous-smart-contract/with-guaranteed-execution",
label: "With Guaranteed Execution",
},
],
},
{
type: "category",
label: "Massa-Web3",
items: [
{
type: "doc",
Expand Down
Loading