@fluent.xyz/assembly-script-sdk
v0.0.1
Published
An SDK for developing smart contracts on the Fluent blockchain, written in AssemblyScript
Readme
AssemblyScript SDK for Fluent Blockchain
The AssemblyScript SDK is a toolkit for building smart contracts in AssemblyScript on the Fluent blockchain.
Getting Started
To develop a smart contract using Assembly Script for the Fluent blockchain, follow these steps:
Step 1: Install Assembly Script SDK
First, ensure you have Node.js and npm installed on your machine. Then, install the @fluent.xyz/assembly-script-sdk package:
npm install @fluent.xyz/assembly-script-sdkStep 2: Create Your Assembly Script Application
Create a new TypeScript file with the .ts extension for your smart contract. For instance, you can create a file named assembly/contract.ts.
Step 3: Implement the Smart Contract
In your newly created .ts file, import the required methods and define the main function. Here's a basic template:
import { readStorage, readInput, writeOutput, writeStorage } from "@fluent.xyz/assembly-script-sdk/assembly";
export function main(): void {
// Your smart contract implementation here
}Refer to Assembly Script documentation for more details on the language syntax and features.
Step 4: Build the Smart Contract into WASM Bytecode
Once your implementation is complete, compile the smart contract to WebAssembly (WASM) bytecode using the following command:
npx compile assembly/contract.ts -o lib.wasmThis command essentially passes the specified arguments to the AssemblyScript compiler. You can include additional options like --optimize to produce an optimized WASM binary. For more detailed options and configurations, visit the AssemblyScript Compiler documentation.
Step 5: Deploy to Fluent Blockchain
With the bytecode ready (lib.wasm), you can proceed to deploy it to the Fluent blockchain.
Additional Resources
You can find more comprehensive examples in the examples/ folder of the SDK package or refer to specific documentation provided by Fluent.
Documentation
This section describes all available methods in the AssemblyScript SDK, a toolkit for building smart contracts in AssemblyScript on the Fluent blockchain.
function exit(code: i32): voidTerminates the execution with the specified exit code.function inputSize(): u32Returns the size of the input data.function readInput(): Uint8ArrayReads the input data and returns it as aUint8Array.function readOutput(): Uint8ArrayReads the output data and returns it as aUint8Array.function writeOutput(buffer: Uint8Array): voidWrites the providedUint8Arrayto the output.function exec(codeHash: Uint8Array, input: Uint8Array, gasLimit: u64, state: u32): ExecResultExecutes a smart contract with the given parameters:codeHash: The hash of the code to execute.input: Input data as aUint8Array.gasLimit: Maximum gas allowed for the execution.state: State context identifier.
Returns an
ExecResultobject that contains:gasUsed: The amount of gas consumed during execution.exitCode: The exit code of the execution.
function keccak256(data: Uint8Array): Uint8ArrayComputes the Keccak-256 hash of the givenUint8Array.function writeStorage(slot: Uint8Array, value: Uint8Array): voidWrites a value to the specified storage slot.function readStorage(slot: Uint8Array): Uint8ArrayReads the value stored at the specified storage slot.function abort(messagePtr: usize, fileNamePtr: usize, line: u32, column: u32): voidAborts execution and provides debugging details:messagePtr: Pointer to the error message.fileNamePtr: Pointer to the file name.line: Line number.column: Column number.
Instead of calling this function directly, you can use the TypeScript syntax:
throw new Error("this is an error!");function seed(): f64Supposed to return a random value, but currently, it only returns the block number. Do not use this value to produce secure random numbers.function getContext(): ContextRetrieves the current execution context as a Context object, which contains essential details about the transaction, block, and contract. This includes information such as the block number, caller address, and other relevant metadata.
Missing Features
- No named value storage or Solidity-like mappings.
- Contracts have a single
mainfunction that must manually parse transaction inputs. - Ethereum primitives (
Address,U256, etc.) are not supported; useUint8Arrayinstead. - Event emission is not yet implemented.
Known Issues
- SDK import is mandatory for all contracts to resolve abort, trace, and seed functions.
- Contracts without initialized memory (memory $0 0 in .wat) may not execute correctly
- Randomness generated using the
seedfunction is insecure and predictable, as it relies on the block number.
How to Run an Example
- Navigate to the example directory:
cd examples/<example_name>- Install dependencies and build the smart contract:
make- Deploy the generated
lib.wasmfile to the Fluent blockchain using your preferred deployment tool or method.
Testing
To run tests, use the following commands:
For testing against a local fluent node:
npx hardhat test --localTo skip tests requiring Fluent features:
npx hardhat test
