Intro to web3-fusion-extend: First Step to Developing an Application Connected to the Fusion Blockchain.

Disclaimer: My JavaScript knowledge is quite limited but I am hopeful that might prove to be an advantage to this little quickstart guide since its focus will mostly be on what is needed to get started for the first time. It is all based on my recent first attempt at building something connected to the Fusion blockchain using the tools I found.

Web3-fusion-extend is a JavaScript library that extends the popular Web3.js built for Ethereum with all functionality needed to utilize the unique features of the Fusion blockchain. The beauty of this is that if you have already worked with Ethereum and Web3.js then working with Fusion will be very familiar to you. At the same time there is no reason to be worried if this is your first blockchain application because working with these libraries are very straight forward. You use this library with the public gateways supplied by Fusion or by connecting to your own node.

Note: At the time of writing the testnet images are missing and there is no faucet to get testnet FSN. This might not be a problem for your first application since you probably dont want to start right away with sending transactions but something to keep in mind. Either you use the public gateways supplied by fusion (both test and mainnet) or you run your own node but remember that using a mainnet node would require you to pay gas for signing transactions.

To get started we first need a development environment. For this guide we will be using Node.js with NPM to install the packages. These tools are platform independent so the steps after installing Node.js and NPM will be the same wether you use Windows, *NIX or anything in between. To get the right version of Web3.js working on my setup (Windows 10) I had to install Node.js 11.15.0 which why I recommend starting there.

When Node.js is installed we head over to the console of your system (powershell, cmd, bash etc.) to install the packages we need to get started. First we will update NPM which is included in Node.js but updated more often so even if you are trying to use the latest version of Node.js it is a good idea.

npm install [email protected] -g

Next we need the Web3.js library which we are going to extend later with web3-fusion-extend. The problem here is that web3-fusion-extend uses a deprecated method of Web3.js to extend the library called extend(). This method was removed in [email protected]1.0.0-beta.38 and thus the latest version of Web3.js we can use is [email protected]1.0.0-beta.37. If you are using windows you probably need windows-build-tools installed to be able to install different versions of libraries so we start with that.

// WINDOWS ONLY
npm install --global --production windows-build-tools

Install [email protected], from here on out if you want your dependencies listed in your project’s package.json you just add -save as a command after every npm install.

npm install [email protected]

Next we install web3-fusion-extend.

npm install web3-fusion-extend

That is all of the packages you are going to need to build your first application. To start writing your code, fire up your favourite IDE or text-editor and create a project and or a file called hellofusion.js. We begin our code by requiring Web3.js and web3-fusion-extend to be able to use them.

var Web3 = require("web3");
var web3FusionExtend = require('web3-fusion-extend');

Next we need to create a provider that will be used to connect Web3 and web3FusionExtend to the public gateways or your local node. This functionality is built-in to the web3.js library. We are going to use what is called a websocket to connect to the public testnet gateway but there are a multitude of different providers to choose from depending on how you want to connect to the node. See some examples in web3 documentation here: https://web3js.readthedocs.io/en/v2.0.0-alpha/web3.html#id14

provider = new Web3.providers.WebsocketProvider('wss://testnetpublicgateway1.fusionnetwork.io:10001');

The address above is aimed at the public testnet gateway. If you are running your own node this is where you aim it at localhost or the ip of your node.

Next we input our provider in the Web3 object as a parameter while creating a new object and then we extend that object with web3FusionExtend in order to have the full functionality in our web3 object.

web3 = web3FusionExtend.extend(web3);

Now we can start using the object to load an manipulate data from the node. Your web3 object will contain all the methods listed in the fusionapi documentation and the official Web3.js documentation. Let’s combine all the code fragments from above and write our first program using the getBalance method of web3.eth that returns the balance of a specific address in wei or in this case the smallets part of FSN.

//Require packages
var Web3 = require("web3");
var web3FusionExtend = require('web3-fusion-extend');
//Create provider and connect
provider = new Web3.providers.WebsocketProvider('wss://testnetpublicgateway1.fusionnetwork.io:10001');
web3 = new Web3(provider);
//Extend web3 fusion web3-fusion-extend
web3 = web3FusionExtend.extend(web3);
//main code
let walletD="0xa9C66B3DbD01bb7d24928402cbD7CFb7Dd445dc3";
web3.eth.getBalance(walletD)
.then( balance => {
console.log("Hello Fusion");
console.log("Wallet D's Balance is: ", balance);
})
.catch(err => {
console.log(err);
});

And there you have it. You have created your first little application that reads data off the Fusion blockchain.
To run it just enter: “node /path/to/hellofusion.js” in your terminal.

Feel free to come by the FUSION Developers Community and tell us what you’re going to be building next or ask for help if you run into any issues.
https://t.me/FsnDevCommunity

Oh, just one last little thing. When you do make your millions as a blockchain developer, please remember where you came from and feel free to drop me a FSN or two. Or maybe even some tokens of your own ico running on Fusion! 🙂
Donations: 0xFaFCd500C32D28d0614052E1742d150e070bDa7d [FSN/ETH]