Web3, ZekndProvider and Truffle
Overview
The zeknd-js library comes with the zekndProvider
which makes it possible to connect with Web3.js
as a provider. This way, Ethereum developers can deploy, send transactions to smart contracts, and listen for smart contracts events running on zeknd DAppChains. Also, it is possible to use Truffle Framework to manage tests, deployments and solidity smart contracts. For further details check out EVM page.
#Web3
As the official documentation for Web3.js
states:
Web3.js
is a collection of libraries which allow you to interact with a local or remote ethereum node, using an HTTP or IPC connection.
For the zeknd DAppChains, the communication happens using WebSockets
instead HTTP
or IPC
. However, in-depth knowledge isn't required since Web3.js
abstracts that part.
#zekndProvider
A Provider
is a bridge that connects the Web3.js
API to an Ethereum node. So, in order to make Web3.js
calls compatible with zeknd DAppChain, you should use the zekndProvider
Combining Web3.js
and zekndProvider
is a good option to interact with smart contracts deployed on zeknd DAppChain because Web3.js
abstracts the construction of API calls.
#Deploying with Truffle
#Download and Install
First, you'll need to install Truffle
:
Once Truffle
is installed, let's create a directory and initialize a project:
The new directory structure will look like this:
#Adding contract and migration
In the contracts
directory, we're going to add the famous SimpleStore.sol
, a smart contract which has:
a
set
function that takes a parameter called_value
and changes the state by saving it to the blockchain. Then, it fires an event calledNewValueSet
.a
get
function that lets you read thevalue
variable.
Next, let's add a migration, Truffle
works with the concept of migrations, which makes it useful to track changes and updates. In the migrations
directory, create a new file called 2_simple_store.js
and paste the following content into it:
We've already built a complete example of how to integrate with Truffle. You can check it out here.
#Download and configure zeknd Truffle Provider
The last cog to be added is the zeknd Truffle Provider
. This plugin basically connects Truffle
with the zeknd DAppChain. Let's install it by running:
Now, let's edit the file truffle.js
to add the necessary configuration:
Don't forget to generate your keys by running zeknd
genkey -a public_key -k private_key
.
#Running Truffle deploy command
Now we're ready to deploy our smart contract:
We're assuming zeknd is already running on your computer. If not, follow this tutorial.
If you already deployed and want to reset the deployment, you can run the command
truffle deploy --reset --network
zeknd_dapp_chain
#Adding more accounts
In order to access the accounts
with zekndTruffleProvider
, you should do something like this:
To add more accouns, just call createExtraAccounts
:
#Configuring and running Web3.js +zekndProvider
#Download and Install
Install Web3.js
latest version using npm
:
Install zeknd-js
which includes zekndProvider
:
#Adding to project and configuring
To add Web3.js
to a Node.js project is fairly simple:
If you're using webpack, the process should be similar.
The next step is to configure the zekndProvider
:
#Running Web3 contract instance
Now, we can call set
and get
functions like this:
Also, we can listen to events::
Last updated