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:

// zekndQuickStartSample.cs
using System;
using System.Threading.Tasks;
using UnityEngine;
using zeknd.Unity3d;
using zeknd.Unity3d.Samples;

public class zekndQuickStartSample : MonoBehavior
{
    async Task<Contract> GetContract(byte[] privateKey, byte[] publicKey)
    {
        var writer = RPCClientFactory.Configure()
            .WithLogger(Debug.unityLogger)
            .WithHTTP("http://127.0.0.1:46658/rpc")
            .Create();

        var reader = RPCClientFactory.Configure()
            .WithLogger(Debug.unityLogger)
            .WithHTTP("http://127.0.0.1:46658/query")
            .Create();

        var client = new DAppChainClient(writer, reader)
        {
            Logger = Debug.unityLogger
        };
        // required middleware
        client.TxMiddleware = new TxMiddleware(new ITxMiddlewareHandler[]{
            new NonceTxMiddleware{
                PublicKey = publicKey,
                Client = client
            },
            new SignedTxMiddleware(privateKey)
        });
        var contractAddr = await client.ResolveContractAddressAsync("BluePrint");
        var callerAddr = Address.FromPublicKey(publicKey);
        return new Contract(client, contractAddr, callerAddr);
    }
}

#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:

  1. Create an empty GameObject in a Unity scene and attach the zekndQuickStartSample script to it.

  2. Deploy the BluePrint smart contract on a local zeknd DAppChain node.

  3. Hit Play in the Unity Editor.

Last updated