This guide demonstrates how to make contract calls using different wallets and providers by passing either an Account
or a Provider
to the contract on instantiation.
To change the wallet associated with a contract instance, assign a new wallet to the instance's account
property. This allows you to make contract calls with different wallets in a concise manner:
import { Provider, Wallet } from 'fuels';
import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../../../../env';
import { ReturnContextFactory } from '../../../../typegend';
const provider = await Provider.create(LOCAL_NETWORK_URL);
const deployer = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider);
const deployContract = await ReturnContextFactory.deploy(deployer);
const { contract } = await deployContract.waitForResult();
// Update the wallet
const newWallet = Wallet.generate({ provider });
contract.account = newWallet;
Similarly, you can assign a custom provider to a contract instance by modifying its provider property. This enables you to use a provider wrapper of your choice:
const newProvider = await Provider.create(NEW_URL);
deployedContract.provider = newProvider;
Note: When connecting a different wallet to an existing contract instance, the provider used to deploy the contract takes precedence over the newly set provider. If you have two wallets connected to separate providers (each communicating with a different fuel-core instance), the provider assigned to the deploying wallet will be used for contract calls. This behavior is only relevant when multiple providers (i.e., fuel-core instances) are present and can be ignored otherwise.