Fuel in NodeJS in under 10 Minutes

Setup, faucet and transfer using Fuel with NodeJS

Fuel in NodeJS in under 10 Minutes

Fuel is a complete optimistic rollup sidechain currently operating on Ethereum’s Ropsten and Görli testnets.

Today we will go through getting started with Fuel!

Why use Fuel?

  • 💸 Cost: extremely low transaction fees for any ERC-20 tokens or ether (below 3500 gas per tx, to be 2400 in v1.0 VS currently ~21–50,000 gas)
  • Speed: quick zero-conf. transaction times (below 1.4s, avg. 0.8 seconds)
  • 🔀 Meta-transactional: pay fees in any ERC-20 token or ether
  • 🛣️ High throughput: can handle extremely high volumes of token and ether transactions (i.e. in the 10s of thousands per second)
  • Production direction: our architecture is carefully crafted for large-scale fault-tolerant consumer hardware settings for both validation and usage, without any reliance on novel cryptography, trusted setups, or expensive or customized computing hardware
  • 🙋 Roll your own: completely open-source under Apache-2.0

How does Fuel Work?

  • Basics: A single smart contract contains all the consensus rules, deposit and withdrawal systems for the Fuel optimistic rollup Ethereum side-chain
  • Depositing: Users deposit assets (i.e. any ERC-20 or Ether) which can then benefit from rock bottom transaction costs and quick confirmation times with no loss of custody or control at any time
  • Withdrawals: Users can withdraw at any time using a liquidity provider swap or standard withdrawals (similar to other rollup systems)
  • Transfers: When users transfer within Fuel, the data is compressed by our aggregator and dumped onto Ethereum, however you can always compress and post your own transaction yourself
  • Keys: Preferably, a secondary signing key is used to sign off on transfers and withdrawals within the side chain. Funds are deposited to that singing key’s address, which then has control over those funds within Fuel. Third-party signer objects can also be used such as those that interact with MetaMask, however raw elliptic signing of hashes must be available.
  • Wallet: the fuel-core wallet object will manage a user’s spendable inputs in a local key-value store database of your choosing (i.e. index db, local storage, level db etc.), output production and the signing of transactions

Is Fuel Right For You?

Yes, if you’re any project:

  • doing transactions where you are cost or volume sensitive
  • that needs fast confirmation speed (under 1.4 seconds)
  • interested in building a Burner Wallet-like system
  • that needs permissionless atomic swaps between any ERC-20 tokens
  • that will eventually want to deploy your own large-scale rollup system

Getting Started in NodeJS

This tutorial will cover: (1) setting up a persistent Fuel wallet in Node, (2) fauceting some fake DAI to your wallet, and (3) making your first transfer.

Let’s start a new Node project and install Fuel, open your terminal:

mkdir fuel-example && cd fuel-example

npm init

npm install --save fuel-core

nano index.js

First let’s (1) import fuel-core, (2) set up a signing key, (3) set up local persistent storage using a wrapped version of LevelDB, and (4) a Signer object.

Note: we use ethers heavily across Fuel and here we use its standardized SigningKey object for the Fuel rollup wallet. This key can be thought of as your Fuel key, where tokens can be deposited to in Fuel.

Note: in production, private key generation and handling must be done with better entropy and with greater care in storage. For now we will just use the DB.

Setup, faucet and transfer using Fuel with NodeJS
Setup, faucet and transfer using Fuel with NodeJS. GitHub Gist: instantly share code, notes, and snippets.
LevelDB with fuel-core in NodeJS

Now let’s (1) set up a Fuel wallet object with our DB and signer, and (2) faucet some Fake DAI to our address.

Note: you can only faucet Fake DAI every 10 minutes per IP, so we wrap the faucet in an empty try/catch as to not throw past the first 10 minute window.

const wallet = new Wallet({ db, signer });

try { await wallet.faucet(); } catch (e) {}

Now let’s listen for changes in our wallet balance; this will activate our live mempool websocket-based pubsub system.

Note: listen will fire at any change to the wallet balance; this includes faucet amounts, transfers, deposits, and withdrawals.

await wallet.listen(async () => {    
    console.log('Balance update in transit',
   utils.formatEther(await wallet.balance(wallet.tokens.fakeDai)));
});

Now let’s make a transfer transfer of 1.5 fake DAI to our own account to test out a transfer.

await wallet.transfer(utils.parseEther(‘1.5’), wallet.tokens.fakeDai, wallet.address);

console.log('You transfered 1.5 Fake Dai to yourself, congrats!');

})(); // finish async method

Now let’s save and run the code locally in Node.

// cntrl + x then y then enter

node index.js

Now you have successfully set up (1) a local Fuel wallet in Node with a (2) persistent key value store, (3) received some fake DAI from the faucet, and (4) made your first Fuel transfer!

Please follow/DM us on Twitter @FuelLabs_, @IAmNickDodson or give us a Github star or follow!

Our codebase can be found here:
https://github.com/FuelLabs/fuel-core

The entire code to this tutorial can be foun d at this Gist here:
https://gist.github.com/SilentCicero/fb854c440dbc615df6ff419f2c33bd06

P.S. next, we will be demonstrating how Fuel can be used in the browser.

Fin.