reown-com / reown-dotnet

Toolkits to build onchain UX
https://docs.reown.com/
Apache License 2.0
8 stars 1 forks source link

JSON RPC ERROR: Gas Estimates error; Random #20

Open Umair72Raza opened 19 hours ago

Umair72Raza commented 19 hours ago

Hello, I need help in my dApp where I am using Reown appkit with Unity and polygon amoy testnet to deploy and interact with contracts.

I am new to blockchain and using Reown Appkit to interact with ERC 20 and ERC1155 contracts. I am connecting to my account using the wallet connect feature from the Reown Appkit Modal to connect my mobile wallet to the unity app. In one of my code chunk, I am trying to mint ERC 20 token by giving the Polygon Amoy Testnet currency. The transaction fails randomly. It gets successful like 4 in 10 times. I am unable to observe the test case because the error and success are random. The mobile wallet app crashes and in the logs I see JSON RPC error or gas estimate failed. I am using Reown App-kit and deployed my contracts on the polygon amoy testnet that has chainId eip:80002.

` await AppKit.InitializeAsync(new AppKitConfig { projectId = "your-project-id", metadataUrl = "https://metadata-url.com",

supportedChains = new[]
{
    ChainConstants.Chains.Polygon,

    new Chain(ChainConstants.Namespaces.Evm,
        chainReference: "80002",
        name: "Polygon Amoy",
        nativeCurrency: new Currency("Pol", "POL", 18),
        blockExplorer: new BlockExplorer("Oklink", "https://www.oklink.com/amoy"),
        rpcUrl: "https://rpc-amoy.polygon.technology",
        isTestnet: true,
        imageUrl: $"https://api.web3modal.com/public/getAssetImage/your-image-id",
        viemName: "polygonAmoyTestnet"
    )
}

});

`

here is the function hashes of the contract:

{ "dd62ed3e": "allowance(address,address)", "095ea7b3": "approve(address,uint256)", "70a08231": "balanceOf(address)", "313ce567": "decimals()", "4b94f50e": "getTokenPrice()", "40c10f19": "mint(address,uint256)", "06fdde03": "name()", "8da5cb5b": "owner()", "715018a6": "renounceOwnership()", "6a61e5fc": "setTokenPrice(uint256)", "95d89b41": "symbol()", "7ff9b596": "tokenPrice()", "18160ddd": "totalSupply()", "a9059cbb": "transfer(address,uint256)", "23b872dd": "transferFrom(address,address,uint256)", "f2fde38b": "transferOwnership(address)" }

Here is the code:

` // mint mr token using matics [ContextMenu("MintTokensWithMatic")] private async void MintTokensWithMatic() { // Get the user's account information Account account = await AppKit.GetAccountAsync(); string recipientAddress = account.Address;

    // Set the amount of MR tokens to mint (adjust this to the desired number of tokens)
    BigInteger mintAmount = new BigInteger(1e18); // Mint 1 MR token, scaled to 18 decimals

    // Hardcoded price: 1 MATIC per token
    BigInteger maticPricePerToken = new BigInteger(1e18); // 1 MATIC in wei

    // Calculate the total MATIC needed (1 MATIC per token)
    BigInteger totalMaticToSend = mintAmount * maticPricePerToken / new BigInteger(1e18); // Adjust for scaling

    // ABI encoding for "mint(address to, uint256 amount)"
    var functionSignature = "0x40c10f19";
    string recipientParam = recipientAddress.Substring(2).PadLeft(64, '0'); // Pad address to 32 bytes
    string amountParam = mintAmount.ToString("x").PadLeft(64, '0');         // Pad amount to 32 bytes

    // Concatenate function signature and parameters
    string data = functionSignature + recipientParam + amountParam;
    try
    {
        // Send the transaction to mint tokens, including 1 MATIC in wei
        var transactionHash = await AppKit.Evm.SendTransactionAsync(contractAddressMRToken, totalMaticToSend, data);

        Debug.Log("Mint transaction with 1 MATIC sent, transaction hash: " + transactionHash);
    }
    catch (Exception ex)
    {
        Debug.LogError("Failed to mint tokens: " + ex.Message);
    }
}

`

skibitsky commented 17 hours ago

Hi!

You don't need to build data from the method signature manually. Instead you can call contract methods directly with the contract ABI. Docs with example: https://docs.reown.com/appkit/unity/core/usage#send-erc20-token

Umair72Raza commented 2 hours ago

Now, I am getting this error

ReownNetworkException: Internal JSON-RPC error. Reown.Sign.Engine.Request[T,TR] (System.String topic, T data, System.String chainId, System.Nullable`1[T] expiry) (at ./mailto:library/packagecache/com.reown.sign@1.0.1/runtime/engine.cs:734) Reown.Sign.Nethereum.ReownSignServiceCore.SendTransactionAsyncCore (Nethereum.RPC.Eth.DTOs.TransactionInput transaction) (at ./mailto:library/packagecache/com.reown.sign.nethereum@1.0.1/runtime/reownsignservicecore.cs:55) .....

This is my updated code:

`

[ContextMenu("MintTokensWithMatic")] private async void MintTokensWithMatic() { // Get the user's account information Account account = await AppKit.GetAccountAsync(); string recipientAddress = account.Address;

    // Set the amount of MR tokens to mint (adjust this to the desired number of tokens)
    BigInteger mintAmount = new BigInteger(1e18); // Mint 1 MR token, scaled to 18 decimals

    // Hardcoded price: 1 MATIC per token
    BigInteger maticPricePerToken = new BigInteger(1e18); // 1 MATIC in wei

    // Calculate the total MATIC needed (1 MATIC per token)
    BigInteger totalMaticToSend = mintAmount * maticPricePerToken / new BigInteger(1e18); // Adjust for scaling

    // ABI encoding for "mint(address to, uint256 amount)"
    var functionSignature = "0x40c10f19";
    string recipientParam = recipientAddress.Substring(2).PadLeft(64, '0'); // Pad address to 32 bytes
    string amountParam = mintAmount.ToString("x").PadLeft(64, '0');         // Pad amount to 32 bytes

    // Concatenate function signature and parameters
    string data = functionSignature + recipientParam + amountParam;

    ////////////////////////////////////////////////////////////////////////

    BigInteger amount = new BigInteger(1e18);

    var arguments = new object[]
    {
        recipientAddress,
        totalMaticToSend
    };
    var result = await AppKit.Evm.WriteContractAsync(contractAddressMRToken, PayableMRTokenABI ,"mint",  amount ,arguments);
    Debug.Log("<color=yellow> Result </color>" + result);
    ////////////////////////////////////////////////////////////////////////
        }

`

skibitsky commented 1 hour ago

Could you please share your contract ABI and address so I could take a look myself? 🙏

Umair72Raza commented 1 hour ago

I have attached the files with abi and contract MRTokenABI.txt MRTokenContract.txt