Blockchain Interactions Unity
Smart contracts interactions in Unity with the Moralis Unity SDK.

smart contracts

what are smart contracts ?
  • Simple definition : Smart contracts are simply programs stored on a blockchain that run when predetermined conditions are met. Learn more about smart contractsโ€‹

Execute smart contract function

ExecuteContractFunction calls smart contract functions

options

  • contractAddress : The address of the deployed smart contract
  • abi : The abi of the contract or just of that specific function. You can convert the ABI to a string here to pass it as a string in code or it can be passed through the inspector
  • functionName: The name of the smart contract function to be called
  • args: The arguments passed into the function
  • value : msg.value of a contract function
  • gas: Transaction fee for the transaction
  • gasPrice: This is the amount in wei the sender is willing to pay for the transaction
1
//using directives
2
using MoralisUnity;
3
using MoralisUnity.Web3Api.Models;
4
using Nethereum.Hex.HexTypes;
5
โ€‹
6
โ€‹
7
//function
8
public async void executeContractFunction()
9
{
10
string ABI = "[{\"inputs\":[{\"internalType\":\"int256\",\"name\":\"p\",\"type\":\"int256\"}],\"name\":\"test\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]";
11
string ContractAddress = "0xD3622d5eDA04B0A393EA10513239A1fD50A61B65";
12
string FunctioName = "test";
13
object[] inputParams = { 3 };
14
HexBigInteger value = new HexBigInteger("0x0");
15
HexBigInteger gas = new HexBigInteger("800000");
16
HexBigInteger gasprice = new HexBigInteger("230000");
17
try
18
{
19
string result = await Moralis.ExecuteContractFunction(contractAddress: ContractAddress, abi: ABI, functionName: FunctioName, args: inputParams, value: value, gas: gas, gasPrice: gasprice);
20
Debug.Log("Txhash :" + result);
21
}
22
catch (Exception error)
23
{
24
Debug.Log("Error :" + error);
25
}
26
}
Copied!

Example result:

Returns the hash of the transaction
1
"0x17b920d425938c0796f7fdbc4dec4f8ad37344cb62ef5df8b20d07bacbbaed22"
Copied!
ExecuteContractFunction is currently the only accepted way to call smart contract functions, there are other methods like SendEvmTransactionAsync, SendTransactionAndWaitForReceiptAsync but they only work for platform and are deprecated