SendTransactionAsync Function Doesn't work

Hello, I was trying to test and learn this tutorial.

When I try to send transaction, I get this error.

1

Here is the OtherPlayerPanel. It is very similar to the script in this tutorial.
How can I find the reason why the transaction process did not happen? Where do you think I might be going wrong? It’s a PC project btw. I use moralisweb3sdk_v1_2_1 package and PUN2. Thanks for support or any help.

using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

//MORALIS
using MoralisUnity;
using MoralisUnity.Platform.Objects;
using MoralisUnity.Web3Api.Models;
using Nethereum.Util;
#if UNITY_WEBGL
using System.Numerics;
using Moralis.WebGL.Hex.HexTypes;
using Moralis.WebGL.Models;
#else
using Nethereum.Hex.HexTypes;
using Nethereum.RPC.Eth.DTOs;
#endif

    public class OtherPlayerPanel : MonoBehaviour
    {
        [Header("Info Panel")] 
        public GameObject infoPanel;
        public TextMeshProUGUI toUsernameLabel;
        public TextMeshProUGUI toAddressLabel;

        [Header("Transaction Panel")]
        public GameObject transactionPanel;
        public Button executeButton;
        public TMP_InputField inputField;

        private string _toAddress;
        private string _toUsername;

        private void OnEnable()
        {
            infoPanel.SetActive(true);
            transactionPanel.SetActive(false);
        }

        private void OnDisable()
        {
            inputField.text = String.Empty;
        }

        public void SetInformation(string clickedUsername, string clickedWalletAddress)
        {
            _toUsername = clickedUsername;
            _toAddress = clickedWalletAddress;
            
            toUsernameLabel.text = "Username:<br>" + clickedUsername;
            toAddressLabel.text = "Wallet Address:<br>" + clickedWalletAddress;

            Debug.Log("Information set!!!!");
        }

        public void HandleTransferButtonClick()
        {
            /*
            #if UNITY_WEBGL
                ExecuteTransferWebGL();
            #else
                ExecuteTransfer();
            #endif
            */
            ExecuteTransfer();
        }

        public void HandleOnValueChanged()
        {
            if (inputField.text == String.Empty)
            {
                executeButton.interactable = false;
            }
            else
            {
                executeButton.interactable = true;
            }
        }

#if UNITY_WEBGL
        private async void ExecuteTransferWebGL()
        {
            var nativeAmount = float.Parse(inputField.text);
            var weiAmount = UnitConversion.Convert.ToWei(nativeAmount);

            // Retrieve from address, the address used to authenticate the user.
            var user = await MoralisInterface.GetUserAsync();
            string fromAddress = user.authData["moralisEth"]["id"].ToString();
 
            // Create transaction request.
            TransactionInput txnRequest = new TransactionInput()
            {
                Data = String.Empty,
                From = fromAddress,
                To = _toAddress,
                Value = new HexBigInteger(weiAmount)
            };      

            try
            {
                // Execute the transaction.
                string txnHash = await MoralisInterface.SendTransactionAsync(txnRequest.To, txnRequest.Value);
                
                Debug.Log($"Transfered {weiAmount} WEI from {fromAddress} to {toAddressLabel}.  TxnHash: {txnHash}");
            }
            catch (Exception exp)
            {
                Debug.Log($"Transfer of {weiAmount} WEI from {fromAddress} to {toAddressLabel} failed!");
            }
        }
#else
        private async void ExecuteTransfer()
        {
            var nativeAmount = float.Parse(inputField.text);
            var weiAmount = UnitConversion.Convert.ToWei(nativeAmount); // convert to WEI
            
            // Retrieve from address, the address used to authenticate the user.
            MoralisUser user = await Moralis.GetUserAsync();
            string fromAddress = user.authData["moralisEth"]["id"].ToString();
 
            // Create transaction request.
            TransactionInput txnRequest = new TransactionInput()
            {
                Data = String.Empty,
                From = fromAddress,
                To = _toAddress,
                Value = new HexBigInteger(weiAmount)   
            };

            try
            {
                // Execute the transaction.
                //string txnHash = await MoralisInterface.Web3Client.Eth.TransactionManager.SendTransactionAsync(txnRequest);
                string txnHash = await Moralis.Web3Client.Eth.TransactionManager.SendTransactionAsync(txnRequest);

                Debug.Log($"Transfered {weiAmount} WEI from {fromAddress} to {_toAddress}.  TxnHash: {txnHash}");
            }
            catch (Exception exp)
            {
                Debug.Log($"Transfer of {weiAmount} WEI from {fromAddress} to {_toAddress} failed!");
            }
        }
#endif
    }

@dpradell Hi David, I would be grateful if you can help.

Hi @cagdas, welcome to the Unity Web3 community :slight_smile:

This project was created before the 1.2.0 SDK update, which came with some breaking changes. I’d need to upload the whole project to be able to help you. Atm it is not in my priorities but I take note of this. I’ll let you know if I proceed. In the meantime, let me know if have any advance with that.

Cheers!

1 Like

Hey! I am also trying to incorporate the feature of sending crypto to other players in-game. But I’m running into the same issue. Could you steer me in the right direction?

Hi!

There’s going to be a video about this from my colleague Sam very soon. Stay tuned!

1 Like

This feature is now available when we pay for the product.

I actually managed to figure it out, but there doesn’t seem to be a way to return the token name, is there? I’m alerting the receiver “you received x crypto” Is there a way to say exactly which token they received?

1 Like

You could use the API to get the name of the token contract.

2 Likes

So would the only way be to use the web api? Can’t do it with the Moralis Unity SDK directly?

You can use the API with the Unity SDK - check the Unity tab.

Alternatively, you could call the name function on the token contract.