NodeJS & Browser Quick Start
Overview
The zeknd-js library contains everything you need to build web apps and NodeJS services that interact with your smart contracts running on zeknd.
You can install zeknd-js by running the following command:
yarn add zeknd-js
# or if you prefer...
npm install zeknd-js#Sample Code
In this tutorial, we're reproducing a few snippets from the Truffle DAppChain Example repository. Make sure to check out the full source code once you finish this tutorial.
#Initializing the Application
To interact with the smart contract deployed to zeknd, we define a class called Contract. Let's look at what's inside.
First, we create a client that will help our application "talk" to zeknd:
_createClient() {
this.privateKey = CryptoUtils.generatePrivateKey()
this.publicKey = CryptoUtils.publicKeyFromPrivateKey(this.privateKey)
let writeUrl = 'ws://127.0.0.1:46658/websocket'
let readUrl = 'ws://127.0.0.1:46658/queryws'
let networkId = 'default'
if (process.env.NETWORK == 'extdev') {
writeUrl = 'ws://extdev-plasma-us1.dappchains.com:80/websocket'
readUrl = 'ws://extdev-plasma-us1.dappchains.com:80/queryws'
networkId = 'extdev-plasma-us1'
}
this.client = new Client(networkId, writeUrl, readUrl)
this.client.on('error', msg => {
console.error('Error on connect to client', msg)
console.warn('Please verify if zeknd command is running')
})
}Next, we want to retrieve the user's address:
And instantiate zeknProvider:
At this point, we are ready to create an instance of the smart contract and start listening to events:
#Writing data to zeknd
Here's how you can write data to zeknd and mutate the state of your smart contract:
#Reading data from zeknd
Reading data from zeknd is as simple as:
Last updated