Building DAppChain Clients
Overview
The go-zeknd
library contains everything you need to build Go apps & services that interact with zeknd
DAppChains, and to build the smart contracts that live on those DAppChains.
To get started, install go-zeknd
by running the following command:
In this section, you'll be introduced to the go-zeknd
API that you will use to write Go code that interacts with a zeknd
DAppChain. Writing smart contracts in Go will be covered in a later section.
In the go-zeknd
package you will find a number of examples. examples/cli
contains a CLI app that can be used to interact with the examples/plugins/helloworld
smart contract. We'll start by building and test driving the CLI app, then we'll introduce you to the go-zeknd
API that was used to build it.
Let's start by generating the ./example-cli
executable:
#Example CLI app
The helloworld smart contract has a public SetMsg
method that can be called to store an association between a key and a value:
The smart contract also has a public read-only GetMsg
method that can be called to look up an association between a key and a value:
You should see the following response:
And that concludes our demonstration of the functionality of the example CLI app. Now it's time to take a look at the parts of the go-zeknd
API that were used to implement it.
#Connecting to a DAppChain
The client.Contract
type provides a convenient way to interact with a smart contract running on a zeknd
DAppChain. Let's write a function that creates a client.Contract
instance to interact with the sample helloworld smart contract from the zeknd
SDK:
#Writing data to a DAppChain
To mutate the state of a smart contract you need to call one of its public methods. To do so, a signed transaction must be sent to and validated by the DAppChain. Fortunately, the client.Contract
type takes care of most of this when you use the Contract.Call()
method.
The helloworld smart contract has a public SetMsg
method that can be called to store an association between a key and a value. Let's write a function that calls this method:
#Reading data from a DAppChain
To read the state of a smart contract you need to call one of its public read-only methods, you can do so by using the Contract.StaticCall()
method.
The helloworld smart contract has a public GetMsg
method that can be called to look up an association between a key and a value. Let's write a function that calls this method:
#Putting it all together
Now that we have all the pieces in place make sure that you have the DAppChain running. Then, run the following code:
You should see Value: hello!
printed to the console.
Last updated