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:
Construct a TransactionInput object and set
Data: "String.Empty"
From: "0xe1e8..."
To: "0x000..."
Value: "new HexBigInteger(5000)"
Call the Moralis transfer function as shown below
//using directivesusingMoralisUnity;usingMoralisUnity.Web3Api.Models;usingNethereum.Util;usingNethereum.Hex.HexTypes;usingMoralisUnity.Platform.Objects;usingNethereum.RPC.Eth.DTOs;// functionpublicasyncvoidSendRawETH() { // Retrieve from address, the address used to athenticate the user.MoralisUser user =awaitMoralis.GetUserAsync();int transferAmount =1;string fromAddress =user.authData["moralisEth"]["id"].ToString();string toAddress ="0x000..."; // Create transaction request.TransactionInput txnRequest =newTransactionInput() { Data =String.Empty, //current connected user From = fromAddress, //reciever address To = toAddress, // amount in wei Value =newHexBigInteger(UnitConversion.Convert.ToWei(transferAmount)) // convert to WEI };try { // Execute the transaction.string txnHash =awaitMoralis.Web3Client.Eth.TransactionManager.SendTransactionAsync(txnRequest);Debug.Log($"Transfered {transferAmount} ETH from {fromAddress} to {toAddress}. TxnHash: {txnHash}"); }catch (Exception exp) {Debug.Log($"Transfer of {transferAmount} ETH from {fromAddress} to {toAddress} failed! with error {exp}"); } }
OR
publicasyncvoidSendRawETH() {string toAddress ="0x000...";int transferAmount =1;try { string txnHash = await Moralis.SendTransactionAsync(toAddress, new HexBigInteger(UnitConversion.Convert.ToWei(transferAmount)));
Debug.Log($"Transfered {transferAmount} ETH to {toAddress}. TxnHash: {txnHash}"); }catch (Exception exp) {Debug.Log($"Transfer of {transferAmount} ETH to {toAddress} failed! with error {exp}"); } }
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
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
//using directivesusingMoralisUnity;usingMoralisUnity.Web3Api.Models;usingMoralisUnity.Platform.Objects;usingNethereum.Hex.HexTypes;//functionpublicasyncvoidtransferNFT721() { 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\"}]";
MoralisUser user =awaitMoralis.GetUserAsync();string fromAddress =user.authData["moralisEth"]["id"].ToString().ToLower();string toAddress ="0xE6502..."; // change the contract address to transfer different tokensstring BAYCContractAddress ="0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D";string FunctioName ="transferFrom"; // params - fromAddress, toAddress, tokenIdobject[] inputParams = { fromAddress, toAddress,2 };HexBigInteger value =newHexBigInteger("0x0");HexBigInteger gas =newHexBigInteger("800000");HexBigInteger gasprice =newHexBigInteger("230000");try { string result = await Moralis.ExecuteContractFunction(contractAddress: BAYCContractAddress, abi: EIPTransferNFTABI, functionName: FunctioName, args: inputParams, value: value, gas: gas, gasPrice: gasprice);
Debug.Log("Txhash :"+ result); }catch (Exception error) {Debug.Log("Error :"+ error); } }
safeTransferFrom for NFT 1155 (Semi-Fungible) token transfers
publicasyncvoidtransferNFT1155() { string EIPTransferNFTABI = "[{\"inputs\":[{\"internalType\":\"address\",\"name\":\"from\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"to\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"id\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"amount\",\"type\":\"uint256\"},{\"internalType\":\"bytes\",\"name\":\"data\",\"type\":\"bytes\"}],\"name\":\"safeTransferFrom\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]";
MoralisUser user =awaitMoralis.GetUserAsync();string fromAddress =user.authData["moralisEth"]["id"].ToString().ToLower();string toAddress ="0xE6502..."; // change the contract address to transfer different tokensstring PWContractAddress ="0x5E627f2DEB8Cd6728c5386195E16a220b7bD43ac";string FunctioName ="transferFrom"; // params - fromAddress, toAddress, tokenId, token amountobject[] inputParams = { fromAddress, toAddress,2,100 };HexBigInteger value =newHexBigInteger("0x0");HexBigInteger gas =newHexBigInteger("800000");HexBigInteger gasprice =newHexBigInteger("230000");try { string result = await Moralis.ExecuteContractFunction(contractAddress: PWContractAddress, abi: EIPTransferNFTABI, functionName: FunctioName, args: inputParams, value: value, gas: gas, gasPrice: gasprice);
Debug.Log("Txhash :"+ result); }catch (Exception error) {Debug.Log("Error :"+ error); } }