Understanding of Metamask: deep immersion in the integration of the plug -in unit
As a developer with Unity and Metamask, you are likely to know the mobile portfolio of the popular decointized finance (Defi) platform. However, when the Plug is integrated -in Metamask unit, the common demand arises: the user shows different quantities of Ethereum compared to the plug -in requested.
The problem:
In our previous example, the Plug -in Metamask unit has been integrated with the package of closed resources and followed the documentation. The purpose of the Built -in module is to show users the correct amount of portfolio (ETH), as required by the platform user interface. In some cases, however, this value does not correspond to the set as a set in the project settings.
The solution:
To solve the problem, immerse yourself on why the Plug -in Metamask unit appears as what you have given. The reason for this is how the Built -in form works with the Ethereum Blockchain and the portfolio account balance.
When users interact with their Ethereum account, they do not interact directly with the balance of the project portfolio. Instead, an external API call returns a specific amount of Eth to the user to transfer or get out of his own balance. Things are interesting here: the plugin uses this external API to recover the right ETH.
Plug code -in unit:
Let’s take a closer look at the Plug code -in Metamask unit to better understand the code. We focus on the UnityPlugin class and on its “Oncoin -changed” events:
`Chard
Use of unityenegine;
Using Metamask.usity;
Public class UnityPlugin: Monobehavour
{{{
// ... Other variables and methods ...
ONCOIN -CANGED NOT VALID PRIVATE (currency currency, ulong value)
{{{
// Update your portfolio balance with the user interface with the correct Eth
Walletbalanceui.intence.setwalletalance (Money.name, value);
}
}
As you can see, the "Oncoin -Changed" event manager questions the current coin (like ether) and its appropriate value. Then update the user interface
Wallabalanceui ‘to view the correct amount of Eth.
Because different quantities appear:
The reason for this difference is the way in which the Metamask unit plugin interacts with the Ethereum blockchain. When the user adds or withdraws the funds from his account, Metamask will send an API request to recover the data on the balance. The plug -in then updates the user’s element element using the API response.
In our case, when we update the “scale of the wallet” copy with the right amount of ETH, we actually update the user portfolio balance on the blockchain Ethereum. However, the plugin does not automatically send a request to update the portfolio balance to the project page.
Repair:
To solve the problem, it is necessary to make sure that the user’s budget is used to use the -in ETH plug on Metamask Unity as requested by the platform. You can do it:
1
2
Here is a detail of the updated example code showing how to update the user’s element element with the right amount of Eth:
“ Chard
Use of unityenegine;
Using Metamask.usity;
Public class wallbalanceui: monobehavour
{{{
ONCOIN -CANGED NOT VALID PRIVATE (currency currency, ulong value)
{{{
// Update your portfolio balance with the user interface with the correct Eth
Walletbalandisplay.intence.setwalletbalance (Money.name, value);
}
}
// Using examples in the C
code of the project:
Private Wallbalandisplay Wallbalandisplay;
Void Start ()
{{{
Walletbalandilsplay = Getcomonent
Walletbalandisplay.
Leave a Reply