EVM-based Smart Contract Quickstart
Unity SDK & EVM-based Smart Contracts Quickstart
#Sample Code
#Connecting to a DAppChain
// zekndEvmQuickStartSample.cs
using UnityEngine;
using System;
using System.Threading.Tasks;
using zeknd.Unity3d;
using zeknd.Nethereum.ABI.FunctionEncoding.Attributes;
public class zekndEvmQuickStartSample : MonoBehaviour
{
async Task<EvmContract> GetContract(byte[] privateKey, byte[] publicKey)
{
var writer = RPCClientFactory.Configure()
.WithLogger(Debug.unityLogger)
.WithWebSocket("ws://127.0.0.1:46658/websocket")
.Create();
var reader = RPCClientFactory.Configure()
.WithLogger(Debug.unityLogger)
.WithWebSocket("ws://127.0.0.1:46658/queryws")
.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)
});
// ABI of the Solidity contract
const string abi = "[{\"constant\":false,\"inputs\":[{\"name\":\"_tileState\",\"type\":\"string\"}],\"name\":\"SetTileMapState\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"GetTileMapState\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"name\":\"state\",\"type\":\"string\"}],\"name\":\"OnTileMapStateUpdate\",\"type\":\"event\"}]\r\n";
// Note: With EVM based smart contracts, you can't access them by name.
// Put the address of your deployed contract here.
var contractAddr = Address.FromHexString("0xf420fbbb810698a74120df3723315ee06f472870");
var callerAddr = Address.FromPublicKey(publicKey);
return new EvmContract(client, contractAddr, callerAddr, abi);
}
}#Writing data to a DAppChain
#Reading data from a DAppChain
#Receiving events from a DAppChain
#Putting it all together
Last updated