Skip to content

Latest commit

 

History

History
221 lines (167 loc) · 7.05 KB

WALLET_SDK.md

File metadata and controls

221 lines (167 loc) · 7.05 KB

Fuel Wallet SDK

How to use

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.

Quickstart

Request connection

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.

List user accounts

Once the connection is authorized, you can list the user accounts using window.FuelWeb3.accounts().

const accounts = await window.FuelWeb3.accounts();
console.log(accounts);

Signing a message

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);

SDK API

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

Methods

Connect

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 - Disconnect your project

await window.FuelWeb3.disconnect();

List Accounts

accounts - List accounts in the wallet

const accounts = await window.FuelWeb3.accounts();
console.log(accounts);

Request signature message

signMessage - Request a message signature for one specific account

const account = "fuel1<address>";
const signedMessage = await window.FuelWeb3.signMessage(account, message);

Send transaction

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);

Get Wallet

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);

Get Provider

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.

Using provider to query node info
const provider = window.FuelWeb3.getProvider();
const nodeInfo = await provider.getNodeInfo();
console.log(nodeInfo.nodeVersion);
Using provider on a fuels-ts Wallet
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

Events are triggered when the state of the respective scope is updated.

Accounts

accounts - Listen to changes to the list of authorized accounts. Params Array<string>

window.FuelWeb3.on("accounts", (data) => {
  console.log("accounts", data);
});

Connection

connection - Listen to changes in the connection status. Params boolean

window.FuelWeb3.on("connection", (isConnected) => {
  console.log("isConnected", isConnected);
});