Icon HelpCircleForumIcon Link

⌘K

Icon HelpCircleForumIcon Link
Deploying Contracts

Icon LinkDeploying Contracts

This guide walks you through deploying a contract using the SDK, covering loading contract artifacts, initializing a contract factory, and deploying the contract.

Icon Link1. Obtaining Contract Artifacts

After writing a contract in Sway and compiling it with forc build (read more Icon Link on how to work with Sway), you will obtain two important artifacts: the compiled binary file and the JSON ABI file. These files are required for deploying a contract using the SDK.

Icon Link2. Setting up the SDK Environment

Before deploying a contract, set up the necessary environment by importing the required SDK components and initializing a wallet and a provider.

const PRIVATE_KEY = "..."
 
const provider = await Provider.create(FUEL_NETWORK_URL);
 
const wallet = Wallet.fromPrivateKey(PRIVATE_KEY, provider);

Icon Link3. Loading Contract Artifacts

Load the contract bytecode and JSON ABI, generated from the Sway source, into the SDK.

const contractsDir = join(__dirname, '../path/to/contracts/dir')
const contractName = "contract-name"
 
const byteCodePath = join(projectsPath, `${contractName}/out/release/${contractName}.bin`);
const byteCode = readFileSync(byteCodePath);
 
const abiJsonPath = join(projectsPath, `${contractName}/out/release/${contractName}-abi.json`);
const abi = JSON.parse(readFileSync(abiJsonPath, 'utf8'));

Icon Link4. Deploying the Contract

Initialize a ContractFactory with the bytecode, ABI, and wallet. Deploy the contract and use its methods.

const factory = new ContractFactory(byteCode, abi, wallet);
 
const { minGasPrice: gasPrice } = wallet.provider.getGasConfig();
 
const contract = await factory.deployContract({ gasPrice });

Icon Link5. Executing a Contract Call

Now that the contract is deployed, you can interact with it. In the following steps, you'll learn how to execute contract calls.

const { value } = await contract.functions.echo_u8(15).simulate();
 
expect(value).toBe(15);

For a more comprehensive TypeScript-backed Fuel usage, learn how to generate types from ABI