This guide walks you through deploying a contract using the SDK, covering loading contract artifacts, initializing a contract factory, and deploying the contract.
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.
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 = new Provider(FUEL_NETWORK_URL);
const wallet = Wallet.fromPrivateKey(PRIVATE_KEY, provider);
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/debug/${contractName}.bin`);
const byteCode = readFileSync(byteCodePath);
const abiJsonPath = join(projectsPath, `${contractName}/out/debug/${contractName}-abi.json`);
const abi = JSON.parse(readFileSync(abiJsonPath, 'utf8'));
Initialize a ContractFactory
with the bytecode, ABI, and wallet. Deploy the contract and use its methods.
const factory = new ContractFactory(byteCode, abi, wallet);
const contract = await factory.deployContract();
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