All Projects → redstone-finance → redstone-smartcontracts

redstone-finance / redstone-smartcontracts

Licence: MIT License
An implementation of the Arweave SmartWeave smart contracts protocol.

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects
go
31211 projects - #10 most used programming language
rust
11053 projects

Projects that are alternatives of or similar to redstone-smartcontracts

as-sha256
AssemblyScript implementation of SHA256
Stars: ✭ 15 (-64.29%)
Mutual labels:  wasm, assemblyscript
Octopus
Security Analysis tool for WebAssembly module (wasm) and Blockchain Smart Contracts (BTC/ETH/NEO/EOS)
Stars: ✭ 261 (+521.43%)
Mutual labels:  smart-contracts, wasm
Lucet
Lucet, the Sandboxing WebAssembly Compiler.
Stars: ✭ 4,006 (+9438.1%)
Mutual labels:  wasm, assemblyscript
Lunatic
Lunatic is an Erlang-inspired runtime for WebAssembly
Stars: ✭ 2,074 (+4838.1%)
Mutual labels:  wasm, assemblyscript
alpha-interface
✨ Token Exchange App for Arweave Profit Sharing Tokens
Stars: ✭ 34 (-19.05%)
Mutual labels:  smart-contracts, arweave
sworn
Sworn compiles Clarity smart contracts into SmartWeave contracts.
Stars: ✭ 50 (+19.05%)
Mutual labels:  arweave, smartweave
Assemblyscript
A TypeScript-like language for WebAssembly.
Stars: ✭ 13,152 (+31214.29%)
Mutual labels:  wasm, assemblyscript
kyve
KYVE - A protocol for verified data-streams
Stars: ✭ 51 (+21.43%)
Mutual labels:  arweave, smartweave
pallet-contracts-waterfall
Collection of simple Substrate smart contract examples written in Rust, AssemblyScript, Solang and the smart contract language ink! to test substrates pallet-contracts module
Stars: ✭ 28 (-33.33%)
Mutual labels:  smart-contracts, assemblyscript
Ink
Parity's ink! to write smart contracts
Stars: ✭ 407 (+869.05%)
Mutual labels:  smart-contracts, wasm
Wasm3
🚀 The fastest WebAssembly interpreter, and the most universal runtime
Stars: ✭ 4,375 (+10316.67%)
Mutual labels:  smart-contracts, wasm
jupiter
Wasm smart contract networks powered by Substrate FRAME Contracts pallet in Polkadot ecosystem.
Stars: ✭ 49 (+16.67%)
Mutual labels:  smart-contracts, wasm
rabin-wasm
Rabin fingerprinting implemented in WASM
Stars: ✭ 26 (-38.1%)
Mutual labels:  wasm, assemblyscript
eosio-rust
EOSIO SDK for Rust – APIs for building smart contracts on EOSIO blockchains in Rust
Stars: ✭ 93 (+121.43%)
Mutual labels:  smart-contracts, wasm
rgeometry
Computational Geometry library written in Rust
Stars: ✭ 77 (+83.33%)
Mutual labels:  wasm
rust-electron-demo
rust electron demo using yew
Stars: ✭ 15 (-64.29%)
Mutual labels:  wasm
blockhead
Crypto portfolio tracker, DeFi dashboard, NFT viewer and data explorer for the Ethereum/EVM-based blockchain ecosystem and the web 3.0-powered metaverse https://gitcoin.co/grants/2966/blockhead
Stars: ✭ 41 (-2.38%)
Mutual labels:  smart-contracts
vmrp
mrp emulator, virtual machine, mrp模拟器
Stars: ✭ 126 (+200%)
Mutual labels:  wasm
wasm2kt
Web Assembly to Kotlin and Java converter. Allows to compile a C or C++ program/library, and generate a Kotlin or Java program/library.
Stars: ✭ 20 (-52.38%)
Mutual labels:  wasm
cosmicverge
A systematic, sandbox MMO still in the concept phase. Will be built with Rust atop BonsaiDb and Gooey
Stars: ✭ 18 (-57.14%)
Mutual labels:  wasm

RedStone SmartContracts SDK

RedStone SmartContracts SDK is the new, written from scratch, implementation of the SmartWeave Protocol.

It works in both web and Node.js environment (requires Node.js 16.5+).

It has been built with performance (e.g. caching at multiple layers, Arweave calls optimization) and modularity (e.g. ability to use different types of caches, imported from external libraries) in mind.

We're already using the new SDK on production, both in our webapp and nodes. However, if you'd like to use it in production as well, please contact us on discord to ensure a smooth transition and get help with testing.

The base motivation behind rewriting the original SDK (and roadmap proposal) has been described here.
To further improve contract state evaluation time, one can additionally use AWS CloudFront based Arweave cache described here.

Architecture

RedStone SmartContracts SDK consists of main 3 layers:

  1. The Core Protocol layer is the implementation of the original SmartWeave protocol and is responsible for communication with the SmartWeave smart contracts deployed on Arweave. It consists of 5 modules:
    1. Interactions Loader - this module is responsible for loading from Arweave all the interaction transactions registered for given contract.
    2. Interactions Sorter - responsible for sorting the interactions according to the protocol specification. This is crucial operation for the deterministic contract state evaluation.
    3. Definition Loader - this module loads all the data related to the given SmartWeave contract - its source code, initial state, etc.
    4. Executor Factory - this module is responsible for creating "handles" to the SmartWeave contract. These handles are then used by the SDK to call SmartWeave contract methods.
    5. State Evaluator - this module is responsible for evaluating SmartWeave contract state up to the requested block height.
  2. The Caching layer - is build on top of the Core Protocol layer and allows caching results of each of the Core Protocol modules separately. The main interfaces of this layer are the:
    1. SwCache - simple key-value cache, useful for modules like Definition Loader
    2. BlockHeightSwCache - a block height aware cache, crucial for modules like Interactions Loader and State Evaluator. These interfaces - used in conjunction with cache-aware versions of the core modules (like CacheableContractInteractionsLoader or CacheableStateEvaluator) allow to greatly improve performance and SmartWeave contract's state evaluation time - especially for contracts that heavily interact with other contracts.
  3. The Extensions layer - includes everything that can be built on top of the core SDK - including Command Line Interface, Debugging tools, different logging implementations, so called "dry-runs" (i.e. actions that allow to quickly verify the result of given contract interaction - without writing anything on Arweave).

This modular architecture has several advantages:

  1. Each module can be separately tested and developed.
  2. The SmartWeave client can be customized depending on user needs (e.g. different type of caches for web and node environment)
  3. It makes it easier to add new features on top of the core protocol - without the risk of breaking the functionality of the core layer.

State evaluation diagram

readState

In order to perform contract state evaluation (at given block height), SDK performs certain operations. The diagram above and description assume the most basic “mem-cached” SDK client.

  1. Users who are interacting with the contract, call the “readState” method.
  2. Interactions Loader and Contract Definition Loader modules are then called in parallel - to load all the data required for state evaluation. Both Interactions Loader and Contract Definition Loader first check its corresponding cache whether data is already loaded - and load from Arweave only the missing part.
  3. With interactions and contract definition loaded - Executor Factory creates a handle to the SmartWeave contract main function (or loads it from its own cache)
  4. With all the interactions and a contract handle - the State Evaluator evaluates the state from the lastly cached value - and returns the result to User.

Development

PRs are welcome! :-) Also, feel free to submit issues - with both bugs and feature proposals. In case of creating a PR - please use semantic commit messages.

Installation and import

SDK requires node.js version 16.5+.

Using npm

npm install redstone-smartweave

Using yarn

yarn add redstone-smartweave

You can import the full API or individual modules.

import * as SmartWeaveSdk from 'redstone-smartweave'
import { SmartWeave, Contract, ... } from 'redstone-smartweave'

The SDK is available in both the ESM and CJS format - to make it possible for web bundlers (like webpack) to effectively perform tree-shaking.

Using web bundles

Bundle files are possible to use in web environment only. Use minified version for production. It is possible to use latest or specified version.

<!-- Latest -->
<script src="https://unpkg.com/redstone-smartweave/bundles/web.bundle.js"></script>

<!-- Latest, minified-->
<script src="https://unpkg.com/redstone-smartweave/bundles/web.bundle.min.js"></script>

<!-- Specific version -->
<script src="https://unpkg.com/[email protected]/bundles/web.bundle.js"></script>

<!-- Specific version, minified -->
<script src="https://unpkg.com/[email protected]/bundles/web.bundle.min.js"></script>

All exports are stored under rsdk global variable.

<script>
  const smartweave = rsdk.SmartWeaveWebFactory.memCachedBased(arweave);
</script>

Using the RedStone Gateway

In order to use the Redstone Gateway for loading the contract interactions, configure the smartweave instance in the following way:

const smartweave = SmartWeaveNodeFactory.memCachedBased(arweave)
    .useRedStoneGateway()
    .build();

The gateway is currently available under https://gateway.redstone.finance url.
Full API reference is available here.

Optionally - you can pass the second argument to the useRedStoneGateway method that will determine which transactions will be loaded:

  1. no parameter - default mode, compatible with how the Arweave Gateway GQL endpoint works - returns all the interactions. There is a risk of returning corrupted transactions.
  2. {confirmed: true} - returns only confirmed transactions - the most safe mode, eg:
const smartweave = SmartWeaveNodeFactory.memCachedBased(arweave)
    .useRedStoneGateway( {confirmed: true} )
    .build();
  1. {notCorrupted: true} - returns both confirmed and not yet verified interactions (i.e. the latest ones). Not as safe as previous mode, but good if you want combine high level of safety with the most recent data.
const smartweave = SmartWeaveNodeFactory.memCachedBased(arweave)
    .useRedStoneGateway( {notCorrupted: true} )
    .build();

More examples can be found here.

WASM

WASM provides proper sandboxing ensuring execution environment isolation which guarantees security to the contracts execution. As for now - Assemblyscript, Rust and Go languages are supported. WASM contracts templates containing example PST contract implementation within tools for compiling contracts to WASM, testing, deploying (locally, on testnet and mainnet) and writing interactions are available in a dedicated repository.

Using SDKs' methods works exactly the same as in case of a regular JS contract.

Additionally, it is possible to set gas limit for interaction execution in order to e.g. protect a contract against infinite loops. Defaults to Number.MAX_SAFE_INTEGER (2^53 - 1).

contract = smartweave.contract(contractTxId).setEvaluationOptions({
      gasLimit: 14000000
    });

VM2

It is possible to provide an isolated execution environment also in the JavaScript implementation thanks to VM2 - a sandbox that can run untrusted code with whitelisted Node's built-in modules. It works only in a NodeJS environment and it enhances security at a (slight) cost of performance, so it should be used it for contracts one cannot trust.

In order to use VM2, set useVM2 evaluation option to true (defaults to false).

contract = smartweave.contract(contractTxId).setEvaluationOptions({
      useVM2: true
    });

Performance - best practices

In order to get the best performance on production environment (or while performing benchmarks ;-)), please follow these simple rules:

  1. Do NOT use the TsLoggerFactory - it is good for development, as it formats the logs nicely, but might slow down the state evaluation by a factor of 2 or 3 (depending on the logging level).
  2. Use fatal or error log level, e.g.:
// configure the logging first
LoggerFactory.INST.logLevel("fatal");
// or
LoggerFactory.INST.logLevel("error");

// then create an instance of smartweave sdk
const smartweave = SmartWeaveWebFactory.memCached(arweave);

Logging on info or debug level is good for development, but turning it on globally might slow down the evaluation by a factor of 2.
Keep in mind that you can fine tune the log level of each module separately. For example you can switch the fatal globally, but debug for the ArweaveGatewayInteractionsLoader (in order to verify the load times from Arweave GQL endpoint). The names of the modules are derived from the names of TypeScript classes, e.g.:

// configure the logging first
LoggerFactory.INST.logLevel("fatal");
LoggerFactory.INST.logLevel("debug", "ArweaveGatewayInteractionsLoader");

// then create an instance of smartweave sdk
const smartweave = SmartWeaveWebFactory.memCached(arweave);

Examples

Usage examples can be found in a dedicated repository. Please follow instructions in its README.md (and detail-ish comments in the examples files) to learn more. There is also a separate repository with a web application example.

We've also created a tutorial that introduces to the process of writing your own SmartWeave contract from scratch and describes how to interact with it using RedStone SmartContracts SDK.

Migration Guide

If you're already using Arweave smartweave.js SDK and would like to smoothly migrate to RedStone SmartContracts SDK - check out the migration guide.

Documentation

TSDocs can be found here.

Missing features

Some features from the original Arweave's smartweave.js are not yet implemented. They will be either added soon to the core SDK, or as a separate libraries, built on top of the SDK:

  • CLI (though not sure if that is a necessary - even if, it should be probably a separate lib built on top of the base SDK).
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].