If you've correctly installed the Fuel wallet extension, the wallet SDK will be injected automatically on the window
object on property FuelWeb3
. To access it, you can use window.FuelWeb3
window.FuelWeb3.connect();
You can try this code directly on the developer console.
First of all, you need to connect and authorize your application; this will authorize your application to execute other actions. You can do this by accessing FuelWeb3.connect()
.
const isConnected = await window.FuelWeb3.connect();
console.log("Connection response", isConnected);
The connect()
method returns a promise; if you prefer to do it in an async way, you can use FuelWeb3.on('connection', () => void)
to
listen for changes in the connection.
Once the connection is authorized, you can list the user accounts using window.FuelWeb3.accounts()
.
const accounts = await window.FuelWeb3.accounts();
console.log(accounts);
Having access to the user address and the connection authorized, you can now request the user for signatures using FuelWeb3.signMessage
.
const account = "fuel1<address>"; // example account
const signedMessage = await window.FuelWeb3.signMessage(account, message);
name | signature |
---|---|
connect | async connect(): Promise |
disconnect | async disconnect(): Promise |
accounts | async accounts(): Promise<Array> |
signMessage | async signMessage(address: string, message: string): Promise |
sendTransaction | async sendTransaction(transaction: TransactionRequestLike): Promise |
getWallet | getWallet(address: string | AbstractAddress): FuelWeb3Wallet |
getProvider | getProvider(): FuelWeb3Provider |
connect
- Request permission to start a connection between the project and the wallet
const isConnected = await window.FuelWeb3.connect();
console.log("connection status", isConnected);
disconnect
- Disconnect your project
await window.FuelWeb3.disconnect();
accounts
- List accounts in the wallet
const accounts = await window.FuelWeb3.accounts();
console.log(accounts);
signMessage
- Request a message signature for one specific account
const account = "fuel1<address>";
const signedMessage = await window.FuelWeb3.signMessage(account, message);
sendTransaction
- Send a transaction, this will request the user selected account to review,
sign, and send the transaction.
import { ScriptTransactionRequest, Address, bn, NativeAssetId } from "fuels";
const txRequest = new ScriptTransactionRequest({
gasLimit: 50_000,
gasPrice: 1,
});
const toAddress = Address.fromString("fuel1<to account address>");
const fromAddress = Address.fromString("fuel1<from account address>");
const amount = bn.parseUnits("0.1");
txRequest.addCoinOutput(toAddress, amount);
const provider = window.FuelWeb3.getProvider();
const resources = await provider.getResourcesToSpend(fromAddress, [
[amount, NativeAssetId],
]);
txRequest.addResources(resources);
const transactionId = await window.FuelWeb3.sendTransaction(txRequest);
const response = new TransactionResponse(transactionId, provider);
// wait for transaction to be completed
await response.wait();
// query the balance of the destination wallet
const balance = await fuelWeb3.getWallet(toAddress).getBalance(NativeAssetId);
console.log("to address balance", balance);
getWallet
- Return a FuelWeb3Wallet this class extends the fuels-ts SDK, WalletLocked,
enabling to execute any of the methods available, but using a FuelWeb3Provider on the connection point,
to request signed actions.
import { Address, NativeAssetId, bn } from "fuels";
const wallet = window.FuelWeb3.getWallet("fuel1<from account address>");
const toAddress = Address.fromString("fuel1<to account address>");
const amount = bn.parseUnits("0.1");
const response = await wallet.transfer(toAddress, amount, NativeAssetId, {
gasPrice: 1,
});
// wait for transaction to be completed
await response.wait();
// query the balance of the destination wallet
const balance = await window.FuelWeb3.getWallet(toAddress).getBalance(
NativeAssetId
);
console.log("to address balance", balance);
getProvider
- Return a FuelWeb3Provider this class extends fuels-ts SDK Provider, enabling to execute any of the methods available, but using FuelWeb3SDK on signature points, to request user permissions.
const provider = window.FuelWeb3.getProvider();
const nodeInfo = await provider.getNodeInfo();
console.log(nodeInfo.nodeVersion);
import { Address, NativeAssetId, bn } from "fuels";
const walletLocked = Wallet.fromAddress(
"fuel1<from account address>",
window.FuelWeb3.getProvider()
);
const toAddress = Address.fromString("fuel1<to account address>");
const response = await walletLocked.transfer(
toAddress,
bn.parseUnits("0.1"),
NativeAssetId,
{ gasPrice: 1 }
);
Events are triggered when the state of the respective scope is updated.
accounts
- Listen to changes to the list of authorized accounts. Params Array<string>
window.FuelWeb3.on("accounts", (data) => {
console.log("accounts", data);
});
connection
- Listen to changes in the connection status. Params boolean
window.FuelWeb3.on("connection", (isConnected) => {
console.log("isConnected", isConnected);
});