Using the Wallet SDK

npm version

The wallet is how users and developers can interact with Fuel.

Install

The Fuel module is currently available as a UMD through CDN or NPM.

node.js

npm install --save @fuel-js/wallet

browser (UMD)

<script src="https://unpkg.com/@fuel-js/wallet@latest/umd/fuel.umd.min.js" type="text/javascript">
</script>

browser (ESM)

<script type="module">
  import fuel from "https://unpkg.com/@fuel-js/wallet@latest/umd/fuel.umd.min.js";
</script>

Creating a Wallet

The Fuel wallet can be used with or without a standard web3 or ethers provider. The provider will be used for account signing or any Ethereum related actions, such as deposits and withdrawals.

If an override privateKey is not specified, the first account given by the provider will be used.

If an Ethereum network is not specified (e.g. rinkeby), it will be requested from the provider.

import fuel from '@fueljs/wallet';

// no provider, privateKey is generated
const wallet = fuel.wallet(null);

// web3 provider, first account used, network gathered from provider
const wallet = fuel.wallet(window.web3.currentProvider);

// provider with override privateKey and network specified
const wallet = fuel.wallet(window.web3.currentProvider, {
  privateKey: fuel.utils.hexlify(fuel.utils.randomBytes(32)),
  network: 'rinkeby',
});

Fauceting

All Fuel test networks have a token faucet that will dispense fake tokens to requesting accounts (within several seconds to minutes).

The faucet will dispense 1000 faucetTokens.

The faucet test token identifier will always be available in fuel.contants.faucetToken.

import fuel from '@fueljs/wallet';

// new wallet
const wallet = fuel.wallet(null, {
  network: 'rinkeby',
});

// faucet fake tokens
await wallet.faucet();

// check your balance
console.log('my balance: ', await wallet.balance(fuel.constants.faucetToken));

Depositing

You can deposit ERC-20 tokens or Ether into Fuel by using the deposit method. Simply specify the token and amount to deposit. Since Fuel uses Ethereum key pairs, this process can be done directly with your provided MetaMask account, for example.

The deposit method will make two Ethereum transactions, depositing your funds into Fuel and making them accessible for Fuel activities (e.g. transfers and swaps).

import fuel from '@fuel-js/wallet';

const wallet = new fuel.Wallet(window.web3.currentProvider);

(async () => {

  // rinkeby dai address
  const daiAddress = '0xc3dbf84Abb494ce5199D5d4D815b10EC29529ff8';

  // deposit funds
  await wallet.deposit(daiAddress, fuel.utils.parseEther('1.0'));

})();

Transferring

Now that you have funds in Fuel, either by deposit or faucet you can begin to transfer them around.

Let's try transferring 1 Fuel token to our own wallet address and check the submission by its transaction ID.

import fuel from '@fuel-js/wallet';

const wallet = new fuel.Wallet(window.web3.currentProvider);

(async () => {

  await wallet.faucet();

  // transfer one faucet token to our own wallet
  const { transactionId } = await wallet.transfer(
    fuel.constants.faucetToken,
    wallet.address,
    fuel.utils.parseEther('1.0'));

  console.log('view my transaction: https://rinkeby.fuel.sh/tx/' + transactionId);

})();

Withdrawing

Currently, the Fuel wallet only supports standard withdrawals, which means you will send an initial withdraw transaction on Fuel first, than release it after the finality delay of 1 week has expired. In the future, the wallet will support fast withdrawals via atomic swaps, where total withdraw time can be reduced safely to as little as 5-10 minutes.

In order to release the tokens on Ethereum, you must have some Ether for the transaction.

import fuel from '@fuel-js/wallet';

const wallet = new fuel.Wallet(window.web3.currentProvider);

(async () => {

  await wallet.faucet();

  // withdraw one faucet token
  await wallet.withdraw(fuel.constants.faucetToken, utils.parseEther('1.0'));

  // wait one week...

  // release any withdrawals onto Ethereum
  await wallet.release();
})();