Web3ModalUnity icon indicating copy to clipboard operation
Web3ModalUnity copied to clipboard

WriteContractAsync RPC Errors

Open QuipHop opened this issue 2 years ago • 13 comments

Describe the bug When I try to call WriteContractAsync there are different errors appeared.

To Reproduce

Steps to reproduce the behavior:

  1. Initialize Web3Modal as per documentation with custom chainset and log in:

    private WalletConnectUnity.Core.Chain currentChain = new WalletConnectUnity.Core.Chain(
        chainNamespace: "eip155", 
        chainReference: "97",
        name: "Binance Smart Chain Testnet",
        nativeCurrency: new WalletConnectUnity.Core.Currency
        (
            "Binance Coin",
            "tBNB",
            18
        ),
        blockExplorer: new WalletConnectUnity.Core.BlockExplorer
        ("BscScan", "https://testnet.bscscan.com"),
        rpcUrl: "https://data-seed-prebsc-1-s1.bnbchain.org:8545/",
        isTestnet: true,
        imageUrl: "https://cryptologos.cc/logos/binance-coin-bnb-logo.png" // Example image URL for Binance Coin
    );
    
    await Web3Modal.InitializeAsync(new Web3ModalConfig
    {
        includedWalletIds = new[]
        {
            "c57ca95b47569778a828d19178114f4db188b89b763c899ba0be274e97267d96",
        },
        supportedChains = new WalletConnectUnity.Core.Chain[]
        {
            currentChain
        },
    });
    
    Web3Modal.OpenModal(ViewType.QrCode);
    
  2. Call WriteContractAsync on the contract:

    var result = await Web3Modal.Evm.WriteContractAsync(CONSTANTS.ContractAddress, CONSTANTS.ContractABI, "transfer", arguments);
    

    or

    BigInteger gasLimit = new BigInteger(2100000);
    BigInteger value = new BigInteger(0);
    var result = await Web3Modal.Evm.WriteContractAsync(CONSTANTS.ContractAddress, CONSTANTS.ContractABI, "transfer", value, gasLimit, arguments);
    
  3. Receive the following errors respectively:

    WalletConnectSharp.Common.Model.Errors.WalletConnectException: Internal JSON-RPC error.
      at WalletConnectSharp.Sign.Engine.Request[T,TR] (System.String topic, T data, System.String chainId, System.Nullable`1[T] expiry) [0x00344] in <c57a78b4016d4c7dbbd47fe3f527f1d6>:0 
      at WalletConnectUnity.Nethereum.WalletConnectServiceCore.SendTransactionAsyncCore (Nethereum.RPC.Eth.DTOs.TransactionInput transaction) [0x000f3] in .\Library\PackageCache\[email protected]\Runtime\WalletConnectServiceCore.cs:56 
    

    OR

    Nethereum.JsonRpc.Client.RpcClientUnknownException: Error occurred when trying to send rpc requests(s): eth_getTransactionReceipt ---> System.Net.Http.HttpRequestException: 429 (Too Many Requests)
    

Expected behavior Transaction sent successfully.

Screenshots If applicable, add screenshots to help explain your problem.

Environment

  • OS: Windows 10
  • Unity version: 2022.3.25f1
  • Web3Modal Package Version: v0.4.1

Is this happening in Unity Editor or on a player like Android, iOS, Windows? Tested in Unity Player

Wallets used for testing Metamask Android

Additional context If Logging enabled in the settings, there is an additional currency/chaindId error in the console right after the login. Approve contract function works in the same way with same errors. Tried different RPC URLs but same result. Contract has been tested via web + metamask with same chain installed and it's correct. Also I've just figured out that Metamask does not show connected sessions after the app was connected.

QuipHop avatar Aug 01 '24 08:08 QuipHop

Hello,

I was able to connect MetaMask to the Binance Smart Chain Testnet (with the Chain code you provided) and sign messages and typed data, as shown in the screen recording.

https://github.com/user-attachments/assets/1d8a8718-94fd-410e-80f9-598864dc0168

The Internal JSON-RPC error message that you get is coming directly from MetaMask wallet. I suggest you to remove the network from MetaMask app and add it again with another RPC URL. You can read more about the error in MetaMask documentation..

You can also try the SafePal wallet. It allows you to add custom networks, including test chains, and is more stable compared to MetaMask.

If Logging enabled in the settings, there is an additional currency/chaindId error in the console right after the login.

That's most likely because our Blockchain API doesn't support Binance Smart Chain Testnet and cannot load account balance.

skibitsky avatar Aug 02 '24 08:08 skibitsky

Hi @skibitsky Glad to hear these functions work as expected. Could you please confirm that Web3Modal.Evm.WriteContractAsync() works as well? As per Documentation Examples We're facing a huge release blocker due to the inability to write contracts. Your example does not even include write button as I see :( Also there is an outdated file if I install package via OpenUPM. Please let me know if I can share some additional details.

QuipHop avatar Aug 02 '24 09:08 QuipHop

I used 0x84ced597c29517841C91F589b9AE45d3157CfbC4 contract for testing. I didn't confirm the transaction because I don't have fund or tokens on that chain, but I can see session request confirmation UI in MetaMask. Can you see that screen in MetaMask too? Do you get Internal JSON-RPC error after approving transaction?

const string contractAddress = "0x84ced597c29517841C91F589b9AE45d3157CfbC4";
const string recipientAddress = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045";
const string abi = @"[{""constant"":false,""inputs"":[{""name"":""to"",""type"":""address""},{""name"":""value"",""type"":""uint256""}],""name"":""transfer"",""outputs"":[{""name"":"""",""type"":""bool""}],""payable"":false,""stateMutability"":""nonpayable"",""type"":""function""}]";

BigInteger amount = 1;

// Arguments for the transfer method. The order of the arguments must match the order in the method signature.
// Method signature: `function transfer(address _to, uint256 _value) public returns (bool success)`
var arguments = new object[]
{
    recipientAddress,
    amount
};
var result = await Web3Modal.Evm.WriteContractAsync(contractAddress, abi, "transfer", arguments);
Notification.ShowMessage($"Transaction hash: {result}");

skibitsky avatar Aug 02 '24 09:08 skibitsky

Also there is an outdated file if I install package via OpenUPM.

A new OpenUPM release is scheduled for today, but it shouldn't affect RPC calls and contract interactions on platforms other than WebGL.

skibitsky avatar Aug 02 '24 09:08 skibitsky

@skibitsky yes, the problems I have described occur after the Confirm button is clicked. I use 0x28595B5A02512858bA8c65160C599e61C2174B04 contract for tests and can send you some tokens if you need.

QuipHop avatar Aug 02 '24 09:08 QuipHop

@QuipHop could you please send some tokens to 0x424dbCF46146a8e8891133aB6Ca70c63d209F5a7?

skibitsky avatar Aug 02 '24 09:08 skibitsky

@skibitsky It's done.

QuipHop avatar Aug 02 '24 10:08 QuipHop

@QuipHop,

I was able to reproduce the issue and solve it by specifying gas amount without gas value. It works in MetaMask and SafePal.

var txHash = await Web3Modal.Evm.WriteContractAsync(
    contractAddress, 
    abi, "transfer", 
    value: default,
    gas: new BigInteger(53000), 
    arguments
);

I'll add another overload of WriteContractAsync that doesn't require specifying value: default in the next release.

skibitsky avatar Aug 02 '24 11:08 skibitsky

@skibitsky Even with higher Gas still same error Nethereum.JsonRpc.Client.RpcClientUnknownException: Error occurred when trying to send rpc requests(s): eth_getTransactionReceipt ---> System.Net.Http.HttpRequestException: 429 (Too Many Requests) . Could it be because of network/provider issue or ban?

QuipHop avatar Aug 02 '24 12:08 QuipHop

@QuipHop

Could it be because of network/provider issue or ban?

I don't think it's.

Do you set value argument to default? Do you test in the Unity Editor?

skibitsky avatar Aug 02 '24 12:08 skibitsky

@skibitsky, yes, I've used your code and one transaction has been processed, but the rest have not. However, I was unable to obtain txHash with error above. Yes, I'm in Unity Editor currently + Metamask Android/IOS with same BSC testnet.

QuipHop avatar Aug 02 '24 13:08 QuipHop

@QuipHop

May I ask you to clone the package into the Packages folder or clone the repository and install the package locally through the Package Manager? This will give you write access.

Then, in NethereumEvmService.cs replace this method with the following code and try to send the transaction again a few times.

protected override async Task<string> WriteContractAsyncCore(string contractAddress, string contractAbi, string methodName, BigInteger value = default, BigInteger gas = default, params object[] arguments)
{
    var contract = Web3.Eth.GetContract(contractAbi, contractAddress);
    var function = contract.GetFunction(methodName);

    return await function.SendTransactionAsync(
        from: null, // will be automatically filled by interceptor
        gas: new HexBigInteger(gas),
        value: new HexBigInteger(value),
        arguments
    );
}

skibitsky avatar Aug 02 '24 13:08 skibitsky

@skibitsky I have installed everything via git url https://github.com/WalletConnect/Web3ModalUnity.git?path=Packages/com.walletconnect.web3modal and now it works good as expected without code changes ! UPD: gas limit should be 110000 for my case

QuipHop avatar Aug 02 '24 14:08 QuipHop