Caduceus

⌘K
Quickstart
Blockchain Basics
What is a Blockchain?
Types of Blockchain
Consensus Mechanism
What is a Smart Contract?
The Ethereum Virtual Machine
Solidity
Caduceus Basics
What is Caduceus?
Consensus
Parallel Execution
Storage
Edge Rendering
Smart Contracts
Nodes
Tokenomics
GameFi
Introduction to GameFi
Introduction to Unity
ChainSafe SDK overview
Using ChainSafe with Unity
Users
Wallets
Exchanges
D'Apps
🦉Oracles
Redstone
Developers
To Api's
Caduceus RPC Endpoints
Getting Testnet Tokens
SDK's
Templates
Tutorials
EVM Tools
Explorers
XR Dev Kit
Node Operators
Node Architecture
Network Specs
Running a V1 Node
Staking
Feedback
Docs powered by archbee 

Using ChainSafe with Unity

19min

Chainsafe SDK enables Unity developers to connect their games to the Caduceus EVM compatible blockchain. Unity is a very popular game development platform, and makes for intuitive collaboration with developers working with the Caduceus Metaverse blockchain ecosystem.

Chainsafe SDK contains prefab components powered by the Nethereum .NET library.

Industry background

Unity 3D currently enjoys about 50 percent market share of the independent game development market, followed by Unreal at about 15%. Larger game studios usually use custom gaming engines built in C++.

While no-code platforms exist, they are not sufficiently integrated with EVM compatible blockchains and this actually represents a higher entry to barrier for blockchain platforms. Hence, we should look at coding platforms as a priority.

Caduceus utilises Chainsafe SDK to connect with the widest possible gaming developer audience, introducing them to GameFi with the easiest possible onboarding.

Getting Started

C Sharp Scripts in Unity

Unity houses C Sharp scripts in a component known as Game Object. Game Object contains the game's control windows and other graphical objects.

Viewing this object lists all relevant C Sharp scripts in a right hand panel pane within the Unity IDE.

ChainSafe SDK

Let's take a look at the various components that make up the ChainSafe SDK.



Installation of ChainSafe SDK



ChainSafe Web3 Unity SDK can be installed using the Unity install package file. (Preferred option) https://github.com/ChainSafe/web3.unity/releases

Download web3.unitypackage



Document image




Pull package into Unity project's Assets space. It will extract automatically.

See Loom video for more information:







Install web3.unitypackage







Setup Procedure

https://docs.gaming.chainsafe.io/installation#viewing-the-project-in-the-browser

Pointer_stringify function must be updated to a later stringify function UTF8ToString inside:

https://github.com/ChainSafe/web3.unity/blob/499a356c8c4c8975e73206098a2f85dfe722e9f4/Web3Unity/Scripts/Library/WebGL/web3gl.jslib

Chainsafe with prefab components powered by the Nethereum .NET library is the gaming stack we use to connect to the Caduceus RPC.



Architecture

The ChainSafe SDK is comprised of independent compoinents that can work on their own, or together. These are the

  • the Web3Wallet Library
  • the EVM Library
  • the Web3GL Library
  • Prefabs that enable drag and drop of the libraries functionality into Unity games.

The SDK's assets are structured in the following manner:

Document image






Let's examine these components and libraries one by one.

Prefabs

Prefabs are a special type of component that allows fully configured GameObjects to be saved in the Project for reuse. These assets can then be shared between scenes, or even other projects without having to be configured again.

This forms the gaming tech stack we use to connect our games to the Caduceus RPC.

Prefabs have embedded C Sharp scripts, so one should carefully browse over to the right hand side of the screen and double click on the script to open it in VS Code.

Web3Wallet Library

The Web3Wallet Library is used for Wallet based or Signed Transactions. This would account for the majority of connection use cases that involve Wallets.

It calls separate endpoints from the EVM:

The private static string url = "https://metamask.app.link/dapp/chainsafe.github.io/game-web3wallet/"; private static string url = "https://chainsafe.github.io/game-web3wallet/";



Document image


Example - Message signing

We can sign a message using the SignMessage prefab. It redirects to our Metamask wallet within our Chrome Browser.

See Loom video









Connecting to Caduceus

Recommended methods to connect to Caduceus by including the Chain ID in code

To connect your ChainSafe using game to Caduceus, all you need to do is to specify the chainID within the prefab scripts. This is 256256 for Caduceus Main net, and 512512 for Galaxy Test Net.

Transfer ERC 721



Shell
|
# install truffle
# prefab script code

// https://chainlist.org/

string chainId = "256256"; // Mainnet

// contract to interact with 

string contract = "0xde458cd3deaa28ce67beefe3f45368c875b3ffd6";

// value in wei

string value = "0";

// abi in json format

string abi = "[{ \"inputs\": [ { \"internalType\": \"address\", \"name\": \"from\", \"type\": \"address\" }, { \"internalType\": \"address\", \"name\": \"to\", \"type\": \"address\" }, { \"internalType\": \"uint256\", \"name\": \"tokenId\", \"type\": \"uint256\" } ], \"name\": \"safeTransferFrom\", \"outputs\": [], \"stateMutability\": \"nonpayable\", \"type\": \"function\" }]"; 

// smart contract method to call

string method = "safeTransferFrom";

// account to send erc721 to

string toAccount = PlayerPrefs.GetString("Account");

// token id to send

string tokenId = "1";

// array of arguments for contract

string[] obj = { PlayerPrefs.GetString("Account"), toAccount, tokenId };

string args = JsonConvert.SerializeObject(obj);

// create data to interact with smart contract

string data = await EVM.CreateContractData(abi, method, args);

// gas limit OPTIONAL

string gasLimit = "";

// gas price OPTIONAL

string gasPrice = "";

// send transaction

string response = await Web3Wallet.SendTransaction(chainId, contract, value, data, gasLimit, gasPrice);

print(response);


Transfer ERC20

Shell
|
// https://chainlist.org/

# prefab script code

string chainId = "256256"; // Mainnet

// contract to interact with 

string contract = "0xc778417e063141139fce010982780140aa0cd5ab";

// value in wei

string value = "0";

// abi in json format

string abi = "";

// smart contract method to call

string method = "transfer";

// account to send erc20 to

string toAccount = "0xdD4c825203f97984e7867F11eeCc813A036089D1";

// amount of erc20 tokens to send

string amount = "1000000000000000";

// array of arguments for contract

string[] obj = {toAccount, amount};

string args = JsonConvert.SerializeObject(obj);

// create data to interact with smart contract

string data = await EVM.CreateContractData(abi, method, args);

// gas limit OPTIONAL

string gasLimit = "";

// gas price OPTIONAL

string gasPrice = "";

// send transaction

string response = await Web3Wallet.SendTransaction(chainId, contract, value, data, gasLimit, gasPrice);

print(response);





EVM Class

This method is pending Chainsafe SDK approval. Please use the other methods such as Web3Wallet for immediate deployment.

The EVM class is located at web3.unity/Web3Unity/Scripts/Library/EVM.cs

This class connects to a permissioned API host endpoint at "https://api.gaming.chainsafe.io/evm"

Blockchain and network strings resolve to actual data at the above endpoint. You cannot call using the chain ID directly.

EVM Calls EVM calls are utilized by upper level libraries such as ERC 20 and ERC 721. EVM calls utilize Unity's own WebRequest.POST methods

Typical fields utilized by EVM include:

string _chain, string _network, string _contract, string _abi, string _method, string _args, string _multicall

When used as an object, the class's methods are used as follows:

await EVM.CreateMint(chain, network, account, to, cid721, type721);

chain, network, and account must be provided as strings and are pre-set at API endpoint.



Web3GL-2020x

The Web3GL-2020x library allows for Web3GL based web applications to utilize Metamask. This enables users to use their Metamask wallet to connect to Web3GL games.

This library contains a network.js file that configures chainids for connection.

A currently active chain can be defined in network.js under the window.web3ChainID object as follows:

Document image




Updated 03 Mar 2023
Did this page help you?
Yes
No
PREVIOUS
ChainSafe SDK overview
NEXT
Users
Docs powered by archbee 
TABLE OF CONTENTS
Industry background
Getting Started
C Sharp Scripts in Unity
ChainSafe SDK
Architecture
Prefabs
Web3Wallet Library
Example - Message signing
Connecting to Caduceus
Transfer ERC 721
Transfer ERC20
EVM Class
Web3GL-2020x