๐Œป Transfer Assets in Unity
Transfer Native assets, NFTs, ERC20 tokens in unity on any blockchain - ETH (Ethereum), BNB (Binance Smart Chain), MATIC (Polygon) e.t.c

Transfer Native Assets (ETH/BNB/MATIC etc)

To transfer native assets of the blockchain follow the steps:
  1. 1.
    Construct a TransactionInput object and set
    1. 1.
      Data: "String.Empty"
    2. 2.
      From: "0xe1e8..."
    3. 3.
      To: "0x000..."
    4. 4.
      Value: "new HexBigInteger(5000)"
  2. 2.
    Call the Moralis transfer function as shown below
1
//using directives
2
using MoralisUnity;
3
using MoralisUnity.Web3Api.Models;
4
using Nethereum.Util;
5
using Nethereum.Hex.HexTypes;
6
using MoralisUnity.Platform.Objects;
7
using Nethereum.RPC.Eth.DTOs;
8
โ€‹
9
// function
10
public async void SendRawETH()
11
{
12
// Retrieve from address, the address used to athenticate the user.
13
MoralisUser user = await Moralis.GetUserAsync();
14
int transferAmount = 1;
15
string fromAddress = user.authData["moralisEth"]["id"].ToString();
16
string toAddress = "0x000...";
17
// Create transaction request.
18
TransactionInput txnRequest = new TransactionInput()
19
{
20
Data = String.Empty,
21
//current connected user
22
From = fromAddress,
23
//reciever address
24
To = toAddress,
25
// amount in wei
26
Value = new HexBigInteger(UnitConversion.Convert.ToWei(transferAmount)) // convert to WEI
27
};
28
try
29
{
30
// Execute the transaction.
31
string txnHash = await Moralis.Web3Client.Eth.TransactionManager.SendTransactionAsync(txnRequest);
32
โ€‹
33
Debug.Log(quot;Transfered {transferAmount} ETH from {fromAddress} to {toAddress}. TxnHash: {txnHash}");
34
}
35
catch (Exception exp)
36
{
37
Debug.Log(quot;Transfer of {transferAmount} ETH from {fromAddress} to {toAddress} failed! with error {exp}");
38
}
39
}
Copied!
OR
1
public async void SendRawETH()
2
{
3
string toAddress = "0x000...";
4
int transferAmount = 1;
5
try
6
{
7
string txnHash = await Moralis.SendTransactionAsync(toAddress, new HexBigInteger(UnitConversion.Convert.ToWei(transferAmount)));
8
Debug.Log(quot;Transfered {transferAmount} ETH to {toAddress}. TxnHash: {txnHash}");
9
}
10
catch (Exception exp)
11
{
12
Debug.Log(quot;Transfer of {transferAmount} ETH to {toAddress} failed! with error {exp}");
13
}
14
}
Copied!

Example result:

Returns the hash of the transaction
1
"0x17b920d425938c0796f7fdbc4dec4f8ad37344cb62ef5df8b20d07bacbbaed22"
Copied!
Use UnitConversion.Convert.ToWei to specify the amount in ETH (same goes for BSC and Polygon).
โ€‹UnitConversion.Convert.ToWei gotten from using Nethereum.Util; is a helper function that will convert your ETH amount to wei which is required in order to construct the transaction.

Transfer ERC20 Tokens (Fungible)

There is currently no method for Token transfer, this will be done with ExecuteContractFunction to call smart contract function to transfer Tokens The abi used is inline with the EIP20 standard and all tokens that use that standard should be transferable by changing the contract address of the Token
  • 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
using Nethereum.Util;
6
using System.Numerics;
7
โ€‹
8
//function
9
public async void transferToken()
10
{
11
string EIPTransferTokenABI = "[{\"inputs\":[{\"internalType\":\"address\",\"name\":\"to\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"amount\",\"type\":\"uint256\"}],\"name\":\"transfer\",\"outputs\":[{\"internalType\":\"bool\",\"name\":\"\",\"type\":\"bool\"}],\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]";
12
// change the contract address to transfer different tokens
13
string DAIContractAddress = "0x6B175474E89094C44Da98b954EedeAC495271d0F";
14
string FunctioName = "transfer";
15
string receiverAddress = "0x0EF9...";
16
BigInteger DAIInWei = UnitConversion.Convert.ToWei(5, 18);
17
// params - toAddress, amount
18
object[] inputParams = { receiverAddress, DAIInWei };
19
HexBigInteger value = new HexBigInteger("0x0");
20
HexBigInteger gas = new HexBigInteger("800000");
21
HexBigInteger gasprice = new HexBigInteger("230000");
22
try
23
{
24
string result = await Moralis.ExecuteContractFunction(contractAddress: DAIContractAddress, abi: EIPTransferTokenABI, functionName: FunctioName, args: inputParams, value: value, gas: gas, gasPrice: gasprice);
25
Debug.Log("Txhash :" + result);
26
}
27
catch (Exception error)
28
{
29
Debug.Log("Error :" + error);
30
}
31
}
Copied!

Example result:

Returns the hash of the transaction
1
"0x17b920d425938c0796f7fdbc4dec4f8ad37344cb62ef5df8b20d07bacbbaed22"
Copied!

Transfer ERC721 Tokens (Non-Fungible)

There is currently no method for NFT transfer, this will be done ExecuteContractFunction to call smart contract function to transfer NFTs The abi used is inline with the EIP721 standard and all tokens that use that standard should be transferable by changing the contract address of the NFT This can also be recreated for NFT minting
  • 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 MoralisUnity.Platform.Objects;
5
using Nethereum.Hex.HexTypes;
6
โ€‹
7
โ€‹
8
//function
9
public async void transferNFT721()
10
{
11
string EIPTransferNFTABI = "[{\"inputs\":[{\"internalType\":\"address\",\"name\":\"from\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"to\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"tokenId\",\"type\":\"uint256\"}],\"name\":\"transferFrom\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]";
12
MoralisUser user = await Moralis.GetUserAsync();
13
string fromAddress = user.authData["moralisEth"]["id"].ToString().ToLower();
14
string toAddress = "0xE6502...";
15
// change the contract address to transfer different tokens
16
string BAYCContractAddress = "0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D";
17
string FunctioName = "transferFrom";
18
// params - fromAddress, toAddress, tokenId
19
object[] inputParams = { fromAddress, toAddress, 2 };
20
HexBigInteger value = new HexBigInteger("0x0");
21
HexBigInteger gas = new HexBigInteger("800000");
22
HexBigInteger gasprice = new HexBigInteger("230000");
23
try
24
{
25
string result = await Moralis.ExecuteContractFunction(contractAddress: BAYCContractAddress, abi: EIPTransferNFTABI, functionName: FunctioName, args: inputParams, value: value, gas: gas, gasPrice: gasprice);
26
Debug.Log("Txhash :" + result);
27
}
28
catch (Exception error)
29
{
30
Debug.Log("Error :" + error);
31
}
32
}
Copied!
safeTransferFrom for NFT 1155 (Semi-Fungible) token transfers

Example result:

Returns the hash of the transaction
1
"0x17b920d425938c0796f7fdbc4dec4f8ad37344cb62ef5df8b20d07bacbbaed22"
Copied!