Search code examples
c#windowsunity-game-engineethereummetamask

Metamask Unity Plugin shows different Ethereum amount for user confirmation on Metamask mobile wallet than that requested by Unity


I am integrating the MetaMask Unity Plugin given here: https://assetstore.unity.com/packages/decentralization/infrastructure/metamask-246786

and followed the docs provided here: https://c0f4f41c-2f55-4863-921b-sdk-docs.github.io/guide/metamask-sdk-unity.html

to allow users to connect their MetaMask mobile wallet app with my Windows Unity app, and for authorizing transactions.

I was testing transactions on the Sepolia testnet (rpc: rpc.sepolia.org, chainID: 11155111)

I edited this snippet provided in their docs and the demo_class provided in their Unity package:

var wallet = MetaMaskUnity.Instance.Wallet;
var transactionParams = new MetaMaskTranscation
{
    To = "0xd00..................................a",
    From = MetaMaskUnity.Instance.Wallet.SelectedAddress,
    Value = "0x0" //------------------------> note
};

var request = new MetaMaskEthereumRequest
{
    Method = "eth_sendTransaction",
    Parameters = new MetaMaskTranscation[] { transactionParams }
};
await wallet.Request(request);

I modified the receiving address and the value to be sent. The wallet is connected and transactions are working, except for the incorrect amount shown and transferred to the receiver. I understand that the value requested from the plugin is in wei, whereas that shown in the mobile app will show ether amounts greater than 0.00001 eth. For the smaller amounts, I needed to confirm from the block explorer.

Here is an example transaction I did (DO NOT SCAN THIS): https://i.sstatic.net/NYMES.png

For the following txRequests the Mobile Wallet shows corresponding amounts in the popup request.

Sent Request           Received Request     Gas Fee       Total
1000000000000          0.00028            + 0.000032    = 0.000313
10000000000000         0.00045            + 0.000032    = 0.004536
100000000000000        0.07206            + 0.000032    = 0.007209

I do not understand this conversion, since as I understood once, 1 wei = 10^-18


Solution

  • It was intended that strings starting with "0x" would be parsed as hex, while other strings be parsed as decimals. However at present the method tries to parse both kinds of strings in hexadecimal format. So if we send '1' or '0x1' it will treat both as 1 Wei, 'A' as '10', and '16' as 22, and so on..

    Therefore we can pass the hexadecimal format of our values instead. However this will be fixed in a future update:

    https://community.metamask.io/t/metamask-unity-plugin-shows-different-ethereum-amount-for-user-confirmation-on-metamask-mobile-wallet-than-that-requested-by-unity/24757