Once you've written a contract in Sway and compiled it with forc build, you'll have in your hands two important artifacts: the compiled binary file and the JSON ABI file.
Icon InfoCircle
Note: Read here Icon Link for more on how to work with Sway.
First, the Contract::load_from function is used to load a contract binary with a LoadConfiguration. If you are only interested in a single instance of your contract, use the default configuration: LoadConfiguration::default(). After the contract binary is loaded, you can use the deploy() method to deploy the contract to the blockchain.
// This helper will launch a local node and provide a test wallet linked to itlet wallet =launch_provider_and_get_wallet().await?;// This will load and deploy your contract binary to the chain so that its ID can// be used to initialize the instancelet contract_id =Contract::load_from("../../packages/fuels/tests/contracts/contract_test/out/debug/contract_test.bin",LoadConfiguration::default(),)?.deploy(&wallet, TxPolicies::default()).await?;println!("Contract deployed @ {contract_id}");
Alternatively, you can use LoadConfiguration to configure how the contract is loaded. LoadConfiguration let's you:
Load the same contract binary with Salt to get a new contract_id
Change the contract's storage slots
Update the contract's configurables
Icon InfoCircle
Note: The next section will give more information on how configurables can be used.
Additionally, you can set custom TxParameters when deploying the loaded contract.
After the contract is deployed, you can use the contract's methods like this:
// This will generate your contract's methods onto `MyContract`.// This means an instance of `MyContract` will have access to all// your contract's methods that are running on-chain!abigen!(Contract( name ="MyContract", abi ="packages/fuels/tests/contracts/contract_test/out/debug/contract_test-abi.json"));// This is an instance of your contract which you can use to make calls to your functionslet contract_instance =MyContract::new(contract_id_2, wallet);let response = contract_instance.methods().initialize_counter(42) // Build the ABI call.call() // Perform the network call.await?;assert_eq!(42, response.value);let response = contract_instance.methods().increment_counter(10).call().await?;assert_eq!(52, response.value);