Submitted by bin2chen
In gate.cairo, when the user calls deposit(), it calculates the corresponding shares through convert_to_yang_helper().
The code is as follows:
The calculation formula is: (asset_amt.into() * total_yang) / get_total_assets_helper(asset).into().
The actual calculation of converting Wad to pure numbers is: (asset_amt * total_yang / 1e18) * 1e18 / total_assets.
The above formula (asset_amt * total_yang / 1e18) will lose precision, especially when the assets decimals are less than 18.
Assume btc as an example, decimals = 8 after add_yang(btc) INITIAL_DEPOSIT_AMT = 1000 so:
total_assets = 1000
total_yang = 1000e10 = 1e13
If the user deposits 0.0009e8 BTC, according to the formula = (asset_amt * total_yang / 1e18):
= 0.0009e8 * 1e13 /1e18 = 0.9e5 * 1e13 /1e18 = 0
With BTCs price at 40,000 USD, 0.0009e8 = 36 USD. The user will lose 36 USD.
We should cancel dividing by 1e18 and then multiplying by 1e18, and calculate directly: shares = asset_amt.into() * total_yang.into() / total_assets.into().
shares = 0.0009e8 * 1e13 / 1000 = 0.0009e18 = 900000000000000
Note: In order to successfully deposit should be > 0.0009e8 such as 0.0019e8, which is simplified and convenient to explain.
Due to the premature division by 1e18, precision is lost, and the user loses a portion of their funds.
Add to test_abbot.cairo:
Decimal
tserg (Opus) confirmed
0xsomeone (judge) commented:
