MoralisWeb3/unity-web3-game-kit

Paybale function showing JSON-RPC error on Metamask

DebabratDas1 opened this issue · 3 comments

The contract has been deployed on Cronos Testnet. And testing through Unity editor by connecting and signing on cronos testnet using Metamask mobile. (Also did same for Mumbai testnet)

When calling CallContractFunction() through unity, confirm pop up is shown on metamask mobile wallet but after clicking on confirm showing log on unity editor and error on metamask as "JSON-RPC error.: eth_SendTransaction when calling buyTokens Payable function". But the contract function work well while testing through remix ide.

The contract script is Assets/Contract Scripts/ BuySell.sol. This is the contract function :

function buyTokens() public payable returns (uint256 tokenAmount) {
    require(msg.value > 0, "Send ETH to buy some tokens");

    uint256 amountToBuy = msg.value * tokensPerEth;

    // check if the Vendor Contract has enough amount of tokens for the transaction
    uint256 vendorBalance = balanceOf(deployerAddress);
    require(vendorBalance >= amountToBuy, "Vendor contract has not enough tokens in its balance");

    // Transfer token to the msg.sender
    _transfer(deployerAddress, msg.sender, amountToBuy);
    //require(sent, "Failed to transfer token to user");

    // emit the event
    emit BuyTokens(msg.sender, msg.value, amountToBuy);

    return amountToBuy;
  }

This is the Unity Function (Assets/Scripts/ContractTest.cs)

private async UniTask<string> CallContractFunction()
    {
        Debug.Log(contractABI);
        MoralisUnity.Platform.Objects.MoralisUser user = await Moralis.GetUserAsync();
        Debug.Log(user);
        string fromAddress = user.authData["moralisEth"]["id"].ToString();
        Debug.Log(fromAddress + "  FromAddress");
        object[] parameters = {
        //fromAddress
        //999999999999999999
        };
        HexBigInteger value = new HexBigInteger(100);
        HexBigInteger gas = new HexBigInteger(0);
        HexBigInteger gasPrice = new HexBigInteger(0);

        string resp = await Moralis.ExecuteContractFunction(contractAddress, contractABI,
            "buyTokens", parameters,
            value, gas, gasPrice);
        Debug.Log(contractABI.Length);
        Debug.Log("resp" + resp);
        //RunContractFunction()
        return resp;

    }

@DebabratDas1 I see you tested in Remix - very good - in the Unity call make sure that you are meeting all permissions requirements in the contract method being called.

The RPC error is returned by the wallet due to:

  1. Not enough gas
  2. Incorrect parameter types
  3. Execute permissions on the target contract / function for the wallet address
  4. Asserts / conditions in the function that are not met by the wallet address / contract conditions / parameter values

Thank you very much. Now the problem is that it's working fine on Unity Editor but not on webGL build.

I am getting these types of error 1. Uncaught (in promise) Error: JsonRpcEngine: Response has no error or result for request 2.MetaMask - RPC Error: JsonRpcEngine: Response has no error or result for request: while calling ExecuteContractFunction on webgl. But working fine on editor. What need to be fixed.

Here is a screenshot of errors : Errors Screenshot

stale commented

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.