Have you ever looked at contracts on Arweave and tried to make sense of the syntax?
Today we’re going to do just that!
Introduction
Let’s start off by briefly understanding the protocol that sets the standard for these contract definitions enabling users to write contracts on Arweave.
SmartWeave allows developers to write and deploy smart contracts for decentralised applications (dApps) with the security, reliability and transparency of a blockchain, while eliminating many computational restrictions and high transaction fees associated with platforms like Ethereum.
As it is built on top of Arweave, it is able to leverage the benefits of permanence and decentralisation that Arweave offers.
The Evaluation Mechanism
SmartWeave is particularly unique because it relies on a mechanism known as Lazy Evaluation.
SmartWeave contracts are stateful, that means they store state along with the contract logic that helps modify this state. Thus, every contract must be initiated with two things:
- An initial state
- Contract logic to modify state
The contract also has an ordered list of actions (interactions) associated with it. These actions are all the user request to modify its state and are ordered by order of request.
Evaluation of the contract and its transactions is only needed when a user want to post a new request. Additionally, the burden is shifted from the nodes to the users’ end.
Starting from the initial state, the user evaluates the actions in order, referencing the contract logic, to arrive at the current state of the contract.
In terms of functional programming, the method used for evaluation is based off reduce operations.
They take in two arguments: an accumulator and the current element being processed and apply logic to the accumulator based on the current element to return the updated accumulator value.
Let us use an example to understand this better:
const numbers = [1, 2, 3, 4, 5];
// Reducer function
const sum = (accumulator, currentValue) => accumulator + currentValue;
// Using reduce to sum all values in the array
const totalSum = numbers.reduce(sum, 0); // The 0 is the initial value for the accumulator
// Output of totalSum: 15
We have a reducer function sum
, which accepts an accumulator
and the currentValue
.
The reduce()
function iterates through each element in the numbers
array and updates the accumulator
by adding the currentValue
for each step.
Once all the elements have been processed, the reduce()
function returns the final value of the accumulator
, which is the total sum. Thus, reducing an array of elements down to a single value, based on a provided reducer function.
Similarly, SmartWeave contracts take in the state and current action as follows:
// initial value of sum variable in state is set as 0
initState = { sum: 0 };
// Contract logic for sum operation
export async function handle (state, action) {
state.sum = state.sum + action.input.value;
}
When a user wishes to post a transaction request to a smart contract, they must first process all the previous interactions with the contract reducing it to a single value, the current state of the contract.
Upon receiving the current state, the users can evaluate submit their own action request.
Now that we have an understanding of how the SmartWeave contracts work, let us look at some benefits its features introduce to the Arweave ecosystem and the process of building contracts:
- As the burden of evaluation is shifted from nodes to the users, high transaction fees experienced by other chains as gas fees can be eliminated. There’s no competition for scarce compute resources!
- Contracts can run and return the current state as fast as the reduce operations. Users can define complexity based on need as opposed to the computational constraints faced by many other chains.
- Another benefit of evaluation on users end is the ability to create contracts with arbitrary scalability.
- Users can choose the execution environment to evaluate contracts, giving them the freedom to write contracts in the language of choice using environment that support it.
- Reduce operations can implement various other operations (conditionals, mapping, filtering, etc.) which is useful for selecting the correct logic (contract can have multiple possible operations/ sub functions) from the contract depending on the scenario.
- A functional pattern evolving from the SmartWeave standard is the ability to have instance of contracts each with their own initial state and interactions that can refer to a single parent source contract for logic of modifying their states.
- The initial state of the contract is immutable. Contract interactions are only evaluated and return the current state (when needed). This is useful for preservation of context.
In summary, the SmartWeave standard of building smart contracts on Arweave sets itself apart from its peers with freedom to build contracts of arbitrary complexity, in the language of choice, with a simple architecture and fee-less on-chain execution.
Have you thought about building smart contracts on Arweave? Now’s the time to give it a shot.
Share your builds or reach out to us for help on Twitter :)
Happy Building!