Skip to content

Commit

Permalink
Add autonomous smart contract documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben-Rey committed Sep 2, 2024
1 parent 9f10977 commit 32d7ab6
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 8 deletions.
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 constant 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!
100 changes: 100 additions & 0 deletions docs/build/autonomous-smart-contract/send-message.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
---
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

## 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

0 comments on commit 32d7ab6

Please sign in to comment.