To interact with a deployed contract using the SDK without redeploying it, you only need the contract ID and its JSON ABI. This allows you to bypass the deployment setup.
The contractId
property from the Contract
class is of type AbstractAddress
, an abstract class that is exclusively extended by the Address
class.
The Address
class wraps all methods from the AbstractAddress
class and adds a single property: bech32Address
. This property is a string encoded in Bech32
format, recognizable by the human-readable prefix fuel
followed by the separator 1
.
When you log the contractId
property of an instantiated Contract using console.log
, the output appears as follows:
Address {
bech32Address: 'fuel1e5tdjlzufcvwut5dvs5yglweepmrevpnvuvt2djj6pyl3mygkwaq8m7f20'
}
If you have already an instantiated and deployed contract in hands you can create another contract instance simply by using the contractId
property and the contract JSON ABI:
const deployedContract = new Contract(contractId, abi, wallet);
const { value } = await deployedContract.functions
.echo_u8(10)
.txParams({ gasLimit: 10_000 })
.simulate();
expect(value).toEqual(10);
The previous example assumes that you have a Contract
instance at hand. However, some Fuel tools and Sway use the b256
type format, a hex-encoded string-like type, for contract IDs.
You might have this format instead, for example, if you have deployed your contract with forc deploy
.
The process of instantiating a Contract
remains the same when using a contract ID of type b256
:
const b256 = '0x50007a55ccc29075bc0e9c0ea0524add4a7ed4f91afbe1fdcc661caabfe4a82f';
const deployedContract = new Contract(b256, abi, wallet);
const { value } = await deployedContract.functions
.echo_u8(50)
.txParams({ gasLimit: 10_000 })
.simulate();
expect(value).toEqual(50);