Plugin-based Smart Contract Quickstart
Plugin-based Smart Contract Quickstart
zeknd supports EVM (Ethereum Virtual Machine) and plugin-based smart contracts. As an example, Plugin-based smart contracts can be created with go-zeknd.
In this quick tutorial, we will demonstrate how to use the Unity SDK to communicate with plugin-based smart contracts.
#Sample Code
You can find all the code on this page and a ready-to-go Unity scene in the zeknd Unity SDK under Assets/
zekndSDK/Samples/QuickStart
.
#Connecting to a DAppChain
The Contract
class provides a convenient way to interact with a smart contract running on a zeknd DAppChain. Let's write a method that creates a Contract
instance to interact with the sample BluePrint smart contract provided in 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 Contract
class takes care of most of this when you use the Contract.CallAsync()
method.
The BluePrint smart contract has a public SetMsg
method that can be called to store an association between a key and a value. Note that this method doesn't return anything. Let's add a method to the zekndQuickStartSample
class that calls BluePrint.SetMsg()
:
Smart contract methods that mutate the state may return a value. The BluePrint smart contract has a public SetMsgEcho
method that will store a key/value and return the key/value it stored. Let's add another method to the zekndQuickStartSample
class that calls BluePrint.SetMsgEcho
.
#Reading data from a DAppChain
To read the state of a smart contract, you need to call one of its public read-only methods. Calling a read-only method doesn't modify the smart contract state. You can call a read-only method on a smart contract by using the Contract.StaticCallAsync()
method.
The BluePrint smart contract has a public GetMsg
method that can be called to look up an association between a key and a value. Let's add a method to the zekndQuickStartSample
class that calls BluePrint.GetMsg
:
#Putting it all together
Add the following method to the zekndQuickStartSample
class:
Now that we have all the code in place let's test it out:
Create an empty
GameObject
in a Unity scene and attach the zekndQuickStartSample
script to it.Deploy the BluePrint smart contract on a local zeknd DAppChain node.
Hit
Play
in the Unity Editor.
Last updated