EVM network plugin

The EVM Network Plugin acts as a bridge between TCE and Web3, allowing your game to interact with Web3 by managing assets through an EVM bridge contract.

Any of the plugin operations will only happens when a majority of the TCE session (> 2/3) has sent exactly the same transaction.

As a game developer, you have control over when assets are transferred to the contract. You can lock an asset during the TCE session and release it to participants based on the game's logic. To lock an asset, the game needs to perform an approve operation on the desired asset on the network, granting permission to the public key holder that will be set later.

Setup

In order to setup this plugin you will need first to follow the steps mentioned here, once that is done , set up a network_config.toml file at the top level of your game engine. For example, in a Unity game, this file should be at the same level as the Assets folder.

network_config.toml
# The public key of the participant
public_key = ""
# The private key of the participant
private_key = ""
# The contract address of the EVM bridge contract.
contract_address = ""
# The url of the JSONRPC api for the desired network
rpc_url = ""
# The chain id of the desired network
chain_id= 1

Load plugin bindings on your desired platform

Now that the plugin is setup to load on TCE, you will need to load the EvmPlugin into your platform, this library contains the necessary bindings to facilitate plugin transactions. How to do that will depend on your game engine.

Take unity for example, you would load plugins simply by adding them to any folder under the Assets folder, as described here.

Sending plugin transactions

Once the plugin is configured, you can use the EvmPlugin bindings to send transactions. The supported transactions are TransferAsset or ReleaseAsset.

Common structures:

 public enum AssetType
  {
        Nft = 1,
        Token = 0,
        SemiNft = 2
  }
  
  public class AssetInfo
    {
        public AssetInfo(
              long assetId, 
              string assetAddress, 
              int assetAmount, 
              AssetType assetType
        ) {
              // ...
        }
    }

Transfer asset :

Before trying to transfer an asset the game will need to get an approval of the AssetType that it is trying to transfer to the contract account, otherwise the transfer transaction will fail.

var pluginApi = new EvmPlugin.Api(tce_platform);

var asset = new EvmPlugin.AssetInfo(1, "asset_address", 1, 0);

pluginApi.SendTransferAsset(asset, "owner_address");

Release asset :

var pluginApi = new EvmPlugin.Api(tce_platform);

var asset1 = new EvmPlugin.AssetInfo(1,"asset_address_1", 1, 0);
var asset2 = new EvmPlugin.AssetInfo(1,"asset_address_2", 1, 0);

var releaseAsset = new EvmPlugin.ReleaseAsset();

// You can add multiple assets to be released, the owner information must be saved.
releaseAsset
    .AddAsset("from", asset1, "to")
    .AddAsset("from", asset2, "to");

pluginApi.SendReleaseAsset(release_asset);

Getting the plugin events

You will be able to catch the events in the same way as showed here but with the specifics:

 if (tcePlatform.GetEvent() is Event tceEvent)
{
    var coolPluginEvents = EvmPlugi.Api.GetPluginEvents(tceEvent);

    foreach (var pluginEvent in pluginEvents)
    {
    switch (PluginEvent)
        {
            case PluginEventSuccess success:
                // The command that was sent successfully
                var SuccessCommand = success.Command;
                switch (SuccessCommand)
                {
                    case TransferAsset transferAsset:
                        // The command original fields
                        var assetTransferred = transferAsset._asset;
                        break;
                    case ReleaseAsset releaseAsset:
                        // The command fields
                        var assets = releaseAsset._assets;
                        break;
                }
                // the evm network tx hash.
                var NetworkTxHash = success.NetworkTxHash;
                break;

            case PluginEventError error:
                // The command that was not sent successfully
                var ErrorCommand = error.Command;
                // The error field
                var errorString = error.Error;
                break;

            default:
                throw new InvalidOperationException("Unknown plugin event type.");
        }
    }
}

Last updated