Skip to content

Commit 071ca99

Browse files
fix to find the largest reserves for calculating the jetton price
1 parent a977834 commit 071ca99

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

pkg/rates/market.go

+25-17
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ func (m *Mock) getPools() map[ton.AccountID]float64 {
353353
},
354354
}
355355
pools := make(map[ton.AccountID]float64)
356-
for attempt := 0; attempt < 2; attempt++ {
356+
for attempt := 0; attempt < 3; attempt++ {
357357
for _, market := range markets {
358358
respBody, err := sendRequest(market.URL, "")
359359
if err != nil {
@@ -652,30 +652,38 @@ func convertedDeDustPoolResponse(pools map[ton.AccountID]float64, respBody io.Re
652652
Assets: []Asset{firstAsset, secondAsset},
653653
}, nil
654654
}
655+
normalizeReserve := func(firstReserve, secondReserve float64, firstDecimals, secondDecimals int) (float64, float64) {
656+
decimalDiff := firstDecimals - secondDecimals
657+
if decimalDiff > 0 {
658+
secondReserve *= math.Pow10(decimalDiff)
659+
} else if decimalDiff < 0 {
660+
firstReserve *= math.Pow10(-decimalDiff)
661+
}
662+
return firstReserve, secondReserve
663+
}
655664
actualAssets := make(map[ton.AccountID]DeDustAssets)
656665
// Update the assets with the largest reserves
657666
updateActualAssets := func(mainAsset Asset, deDustAssets DeDustAssets) {
658667
firstAsset, secondAsset := deDustAssets.Assets[0], deDustAssets.Assets[1]
659-
assets, ok := actualAssets[mainAsset.Account]
668+
existingAssets, ok := actualAssets[mainAsset.Account]
660669
if !ok {
661670
actualAssets[mainAsset.Account] = DeDustAssets{Assets: []Asset{firstAsset, secondAsset}, IsStable: deDustAssets.IsStable}
662671
return
663672
}
664-
for idx, asset := range assets.Assets {
665-
assetReserveNormalised := asset.Reserve
666-
mainAssetReserveNormalised := mainAsset.Reserve
667-
// Adjust the reserves of assets considering their decimal places
668-
if asset.Decimals != mainAsset.Decimals {
669-
assetReserveNormalised = asset.Reserve / math.Pow10(asset.Decimals)
670-
mainAssetReserveNormalised = mainAsset.Reserve / math.Pow10(mainAsset.Decimals)
671-
}
672-
if asset.Account == mainAsset.Account && assetReserveNormalised < mainAssetReserveNormalised {
673-
if idx == 0 {
674-
actualAssets[mainAsset.Account] = DeDustAssets{Assets: []Asset{firstAsset, secondAsset}, IsStable: deDustAssets.IsStable}
675-
} else {
676-
actualAssets[mainAsset.Account] = DeDustAssets{Assets: []Asset{secondAsset, firstAsset}, IsStable: deDustAssets.IsStable}
677-
}
678-
}
673+
// Adjust the reserves of assets considering their decimal places
674+
firstAssetReserve, secondAssetReserve := normalizeReserve(
675+
firstAsset.Reserve, secondAsset.Reserve,
676+
firstAsset.Decimals, secondAsset.Decimals,
677+
)
678+
existingFirstReserve, existingSecondReserve := normalizeReserve(
679+
existingAssets.Assets[0].Reserve, existingAssets.Assets[1].Reserve,
680+
existingAssets.Assets[0].Decimals, existingAssets.Assets[1].Decimals,
681+
)
682+
// Calculate the total liquidity of the pool
683+
currentLiquidity := firstAssetReserve + secondAssetReserve
684+
existingLiquidity := existingFirstReserve + existingSecondReserve
685+
if currentLiquidity > existingLiquidity {
686+
actualAssets[mainAsset.Account] = DeDustAssets{Assets: []Asset{firstAsset, secondAsset}, IsStable: deDustAssets.IsStable}
679687
}
680688
}
681689
actualLpAssets := make(map[ton.AccountID]LpAsset)

0 commit comments

Comments
 (0)