WASM implementation of Forc (Fuel Orchestrator) for creating, building, testing, and deploying Sway projects directly in the browser. Inspired by WASM-Cairo and aptos-move-js.
npm install --save forc-wasm
import * as forc from 'forc-wasm';
// the contract source code
const code = `
contract;
abi MyContract {
fn test_function() -> bool;
}
impl MyContract for Contract {
fn test_function() -> bool {
true
}
}`;
// build the contract
var params = new forc.BuildParams(code, 'testnet');
var result = forc.build(params);
The result of forc.build
is an object with the following fields, if compile
failed, result.error
will be set.
{
"abi": "{}",
"bytecode": "0x1af0...0c74",
"storageSlots": "[]",
"forcVersion": "forc 0.60.0",
"error": "",
}
var result = await forc.deploy({
// the bytecode of the contract
bytecode: '0x1af0...0c74',
// the address to deploy the contract to
address: 'fuel18caanqmumttfnm8qp0eq7u9yluydxtqmzuaqtzdjlsww5t2jmg9skutn8n',
// the toolchain to use
toolchain: 'testnet',
});
Before deploying the contract, ensure you have a Fuel wallet installed in your browser and acquire test funds using the faucet. After deployment, you will receive details including the Contract ID and the block where the transaction was signed. For further information, refer to the 'Setting up a local wallet' section.
The WASM in this package is generated by Rust living in the src
directory. You
need Rust tooling to work on it. We use
wasm-bindgen and
wasm-pack. To build forc-wasm
you will need to install the dependencies:
- Git https://git-scm.com/downloads
- Rust https://www.rust-lang.org/tools/install
- wasm-pack https://rustwasm.github.io/wasm-pack/installer/
- Just https://github.com/casey/just
See our setup for complete details, but after the typical git clone
it's as
simple as running just
in the package directory to lists all the available
commands.
Licensed under the Apache License, Version 2.0, see the LICENSE file for more information.