This document describes the core concepts behind the SmartWeave smart contracts protocol and is based on the original smartweave.js contract-guide.
SmartWeave is a data-centered protocol built on top of the Arweave chain that allows to implement smart contracts. The key difference between SmartWeave and other similar solutions is that the contracts can be evaluated on-demand and "lazily".
Most smart contracts treat data as a side-product that slows down processing. Data is digested, pruned, and pushed out to side chains. Nonetheless, the bloated global state is still the biggest challenge of modern blockchains - as it leads to extreme costs of data storage.
SmartWeave approach, on the other hand, has several advantages:
Building SmartWeave on the Arweave allows saving time on the development of the basic infrastructure.
The SmartWeave protocol consists of few key concepts:
In order to deploy a new contract on Arweave the SmartWeave client must:
Transaction field | Value |
---|---|
data |
Contract Source Code specification |
tag['App-Name'] |
SmartWeaveContractSource |
tag['App-Version'] |
the current version of the SDK, e.g. version from package.json |
tag['Content-Type'] |
application/javascript |
Transaction field | Value |
---|---|
data (*) |
Contract initial state - stringified json object - e.g. JSON.stringify(initialState) |
tag['Init-State'] (*) |
Contract initial state - as alternative for setting it in data field |
tag['Init-State-TX'] (*) |
Id of the transaction the holds the initial state |
tag['App-Name'] |
SmartWeaveContract |
tag['App-Version'] |
the current version of the SDK, e.g. version from package.json |
tag['Content-Type'] |
application/javascript |
tag['Contract-Src'] |
id of the transaction created in previous step, eg: ovqOT6dVD7zYZYmIkq52gmSippk2MGs_TjXs3-D4BLU |
(*) Either one of these options must be chosen for storing the initial state.
handle
in it's global scope.handle
function MUST accept exactly two arguments: state
and action
state
argument MUST be the current state of the contract.action
argument MUST contain caller
field (i.e. Arweave wallet address)
and input
field. The input
field MUST contain the function
field and any
additional data required to perform given operation.input
field MUST NOT exceed 2048 bytes.handle
function MUST terminate by either:
{ state: newState }
- this will cause the contract state to be updated{ result: newResult }
ContractError
Contract interactions allow to modify the contract state. Each interaction MUST be deployed as a separate Arweave transaction:
Transaction field | Value |
---|---|
tag['App-Name'] |
SmartWeaveAction |
tag['App-Version'] |
the current version of the SDK, e.g. version from package.json |
tag['Contract'] |
Transaction id of the contract |
tag['Input'] |
JSON.stringified representation of the input |
In its current form, the SmartWeave protocol assumes that smart contract code is a javascript function, defined as:
contractFunction = (state, action) => state;
This function takes a state and an action as input arguments - and produces a new state as a result.
In functional programming terms it acts as a fold
function - the state of the contract is derived from:
In order to evaluate contract state, SmartWeave Protocol client:
sha256(transactionId + blockHash)
.
The full ordering is [ block_height, sha256(transactionId + blockHash) ]
.handler
function - evaluating contract's state
up to the requested block height.