Skip to content

Deploying Contracts

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

1. Obtaining Contract Artifacts

After writing a contract in Sway and compiling it with forc build (read more 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.

2. 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.

ts
const PRIVATE_KEY = "..."

const provider = await Provider.create(FUEL_NETWORK_URL);

const wallet = Wallet.fromPrivateKey(PRIVATE_KEY, provider);
See code in context

3. Loading Contract Artifacts

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

ts
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'));
See code in context

4. Deploying the Contract

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

ts
const factory = new ContractFactory(byteCode, abi, wallet);

const { minGasPrice: gasPrice } = wallet.provider.getGasConfig();

const contract = await factory.deployContract({ gasPrice });
See code in context

5. 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.

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

expect(value).toBe(15);
See code in context

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