Build High-Performance Dapps using QuikNode

Build High-Performance Dapps using QuikNode

What is QuikNode?

With the evolution of Web3, developers are rapidly building decentralized applications on the Ethereum blockchain. Building DApps on Ethereum is cool, though it comes with some infrastructure setup drawbacks. You need to set up an Ethereum node to interact with the blockchain and this setup requires time, infra and skills. QuikNode solves this problem by providing Ethereum Node-as-a-Service.

As QuikNode’s website states:

“The fastest and easiest way to run your own Ethereum node.”

Why should you use QuikNode?

Running your own private Ethereum node is cumbersome. There are three problems with that:

  • Infra — Blockchains are distributed Ledgers and a full node means the entire copy of the ledger; this may not be possible on some home computers anymore as Ethereum’s chain size is around 670 GB now, even fast sync (w/ pruning) is now around 120 GB.
  • Time — Initial blockchain sync takes time, and syncing the ETH MainNet can take days (or weeks), depending on the internet speed (CPU, disk i/o, among other factors).
  • Skills — Sysadmin and Linux skills are required to work the command line, plus one must have some cybersecurity experience to secure the node from malicious activity.

What does QuikNode provide?

QuikNode solves the above problems by providing a full ETH node within a few clicks. Instead of using shared public nodes, QuikNode provides users with their own, personal node. QuikNode is optimized for performance, speed, and flexibility.

Let's see how QuikNode achieves this:

Dedicated Node- Using a dedicated node helps you increase performance for your blockchain queries, as it’s only taking queries for your DApp. You’re also able to use API calls that are not available with existing public/shared node services, like “txpool” and “pendingTransactions”. New transactions can also be submitted in large batches, for things like airdrops.

Multiple Zones- QuikNode supports 8 different Zones around the world. That helps in optimizing networks call time which boosts speed and performance for your DApp.

Multiple Testnet support — QuikNode support almost all famous Ethereum TestNets. This yields immense flexibility, allowing developers to test their application on their preferred test network.

Archive Nodes — QuikNode provides Parity archive nodes too. An archive node keeps a full copy of the blockchain ledger (as opposed to a full node, which performs pruning for disk-space optimization). This is a very important feature for businesses who benefit from blockchain analysis and research. You can learn more about QuikNode archive nodes here.

Also, QuikNode offers support for both Parity and Geth clients. Isn’t it cool???

Signing up with QuikNode

Let’s sign up for the QuikNode, Don’t forget to store your password as you will need it to access your node and also QuikNode Wallet.

Once you have completed registration, you can configure your node. You can choose Network, Zone, Client, and Synchronization mode. You will see a link for your Node something like this. Bookmark it and don’t share it with anyone.

https://quiknode.io/node/86d9e35e-8cdb-47ad-80a4-84f9e9537afa/A-cU-9vjaepXeR6qPC8eOg==

Once you Logged in you can see a dashboard, something like this.

Connecting with QuikNode?

I have created a Kovan Testnet and my zone is Bangalore (India). Remember, your node has to be in READY status, otherwise, it will not work. You can rebuild your node with a single click from the UI, or contact QuikNode team via Slack if you face any problems.

Connection URI

QuikNode supports both HTTPProvider (https://) and Websockets (we will see them later). You can find these links under “Dev Tools” tab.

Now Let’s deep dive in some code.

Sending ethereum transaction using web3js and QuikNode

Let's test QuikNode and see how it works. To start, let’s broadcast a raw transaction with QuikNode using web3js.

Let’s create a Nodejs project and install Web3js. Create a project directory and run below commands.

mkdir quiknode
cd quiknode
npm init
npm install web3

Setup Web3 using QuikNode

Let’s create an Index.js file and setup web3. As you can see we are using QuikNode HttpProvider link. We will connect to our node using this link. Now all the commands will be going through this node and will use Kovan Network.

const Web3 = require('web3')
const httpProvider = "https://mistakenly-smart-jay.quiknode.io/86d9e35e-8cdb-47ad-80a4-84f9e9537afa/C0_tKUunhUc0rM_i1HMxHA==/"
var web3 = new Web3(new Web3.providers.HttpProvider(httpProvider));

To test the network add these line and check the network id, Kovan network Id is 42.

web3.eth.net.getId(function(err, data){
console.log(data);
})

Creating an account

Let’s create a new Ethereum account on Kovan network.

var addressData =web3.eth.accounts.create(web3.utils.randomHex(32));
console.log(addressData);

This will give us a private key and address, using which we will create a raw transaction.

You can check the balance using below

web3.eth.getBalance('0x75E18d32f2DbEEfaF4055aD709BDe98eCB57C379', (err, wei) => {
balance = web3.utils.fromWei(wei, 'ether')
console.log(balance);
});

Signing an Ethereum transaction

Let’s get some test Ethers using Kovan network faucet and sign a transaction. Either you can generate a new address as a recipient or just use any address from Kovan block explorer.

web3.eth.accounts.signTransaction({
    from: address, // our address 
    to: address2, // any other kovan network address you want to send 
    value: '2000000000000000',
    gas: '8000000'
}, privateKey, function(err, data) {
    console.log(data);
});

Sending transaction

Now let’s broadcast this signed transaction using QuikNode.

web3.eth.accounts.signTransaction({
    from: address,
    to: address2,
    value: '2000000000000000',
    gas: '8000000'
}, privateKey, function(err, data) {
    web3.eth.sendSignedTransaction(data.rawTransaction, function(err, receipt) {
        console.log("receipt", receipt);
        web3.eth.getTransaction(receipt, function(err, data) {
            console.log("transaction", data);
        })
    });
});

We have successfully sent a transaction using QuikNode on Kovan network, you can check this transaction on Kovan block explorer.

Using WebSockets with QuikNode

QuikNode supports WebSockets (wss://) too. You can find a web socket connection link under Dev tools. Either you can use it with HTTPs Auth or Token Auth. Token auth link will look like something below.

- Token auth: wss://mistakenly-smart-jay.quiknode.io/86d9e35e-8cdb-47ad-80a4-84f9e9537afa/C0_tKUunhUc0rM_i1HMxHA==/

WebSockets allow both the server and the client to push messages at any time without any relation to a previous request as opposed to HTTP where the client creates a connection with the server every time. With WebSockets, the connection gets created once, and server & client push messages using the connection. Websockets are best for an event-based system. Websockets are supported by almost every browser.

Subscribing to pending Transaction events

Let’s subscribe to a pending transaction on the Ethereum blockchain. You can see we are passing QuikNode WebSocket URI while initializing web3.

const Web3 = require('web3')
const webSocket = "wss://mistakenly-smart-jay.quiknode.io/86d9e35e-8cdb-47ad-80a4-84f9e9537afa/C0_tKUunhUc0rM_i1HMxHA==/"
var web3 = new Web3(webSocket);
var subscription = web3.eth.subscribe('pendingTransactions', function(error, result) {
        if (!error)
            console.log(result);
    })
    .on("data", function(transaction) {
        console.log(transaction);
    });

QuikNode with Truffle

Let’s see how we’ll use QuikNode with Truffle and deploy smart contracts. So for that lets download Truffle Petshop. We will simply unbox it and deploy it using QuikNode.

truffle unbox pet-shop 

We will use truffle-HD-wallet to deploy our pet-shop smart contracts. So you need to install it too.

npm install truffle-hdwallet-provider

Now let’s see the configuration of our Truffle’s config. We will simply add HttpProvider URI to use QuikNode as mentioned below.

var HDWalletProvider = require("truffle-hdwallet-provider");
var mnemonic = "YOUR_MEMONICS"; // use a funded wallet
module.exports = {
    networks: {
        development: {
            host: "127.0.0.1",
            port: 7545,
            network_id: "*" // Match any network id
        },
        kovan: {
            provider: function() {
                return new HDWalletProvider(mnemonic, "https://mistakenly-smart-jay.quiknode.io/86d9e35e-8cdb-47ad-80a4-84f9e9537afa/C0_tKUunhUc0rM_i1HMxHA==/")
            },
            network_id: 42
        }
    }
};

QuikNode Metrics & Analytics

So now let's talk about QuikNode’s metrics & analytics feature, which gives you visibility into what's happening with your DApp.

  • Stats / Node Peers / Node Logs — QuikNode provides different types of stats, by which you can measure your DApp’s Usage. You can check the number of requests, Load on your node and WebSocket messages other things. QuikNode also provides peers contacted your node and the logs.


QuikNode Wallet

QuikNode also integrated a web wallet with your node. It is your own, private installation of MyEtherWallet, using their open-source code. This wallet automatically connects with your node and you can access this wallet with the same username & password that you used at signup. Other wallet websites and/or their node infrastructure can be offline, but you’ll still be able to broadcast transactions using your QuikNode!

Conclusion

QuikNode is an awesome and useful addition to the Ethereum ecosystem. It’s fast and completely dedicated to your business. DApps requiring fast data response and reliable service will end up running their own node for better performance. Now they don’t need to set up nor manage the node -- just spin up a QuikNode.

QuikNode’s team is awesome and are always available for support and comment on their Slack channel!

Note: Mentioned QuikNode links will not work, my QuikNode subscription is expired.

This article is originally published on QuikNode's medium publication.


要查看或添加评论,请登录

Gaurav Agrawal的更多文章

社区洞察

其他会员也浏览了