Skip to content

Commit 935a28b

Browse files
in working
1 parent 661e733 commit 935a28b

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

pkg/rates/market.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ func calculatePoolPrice(firstAsset, secondAsset Asset, pools map[ton.AccountID]f
565565
return ton.AccountID{}, 0
566566
}
567567
var calculatedAccount ton.AccountID // The account for which we will find the price
568-
var decimals int
568+
var firstAssetDecimals, secondAssetDecimals int
569569
if okFirst { // Knowing the first asset's price, we determine the second asset's price
570570
firstAsset.Reserve *= priceFirst // Converting reserve prices to TON
571571
if firstAsset.Reserve < minReserve {
@@ -575,7 +575,7 @@ func calculatePoolPrice(firstAsset, secondAsset Asset, pools map[ton.AccountID]f
575575
return ton.AccountID{}, 0
576576
}
577577
calculatedAccount = secondAsset.Account
578-
decimals = secondAsset.Decimals
578+
firstAssetDecimals, secondAssetDecimals = firstAsset.Decimals, secondAsset.Decimals
579579
}
580580
if okSecond { // Knowing the second asset's price, we determine the first asset's price
581581
secondAsset.Reserve *= priceSecond // Converting reserve prices to TON
@@ -586,19 +586,19 @@ func calculatePoolPrice(firstAsset, secondAsset Asset, pools map[ton.AccountID]f
586586
return ton.AccountID{}, 0
587587
}
588588
calculatedAccount = firstAsset.Account
589-
decimals = firstAsset.Decimals
590589
firstAsset, secondAsset = secondAsset, firstAsset
590+
firstAssetDecimals, secondAssetDecimals = firstAsset.Decimals, secondAsset.Decimals
591591
}
592-
if decimals == 0 {
592+
if firstAssetDecimals == 0 || secondAssetDecimals == 0 {
593593
return ton.AccountID{}, 0
594594
}
595595
var price float64
596596
if isStable {
597-
x := secondAsset.Reserve / math.Pow(10, float64(decimals))
598-
y := firstAsset.Reserve / math.Pow(10, 9)
597+
x := secondAsset.Reserve / math.Pow(10, float64(secondAssetDecimals))
598+
y := firstAsset.Reserve / math.Pow(10, float64(firstAssetDecimals))
599599
price = (3*x*x*y + y*y*y) / (x*x*x + 3*y*y*x)
600600
} else {
601-
price = (firstAsset.Reserve / secondAsset.Reserve) * math.Pow(10, float64(decimals)-9)
601+
price = (firstAsset.Reserve / secondAsset.Reserve) * math.Pow(10, float64(secondAssetDecimals)-float64(firstAssetDecimals))
602602
}
603603
return calculatedAccount, price
604604
}

pkg/rates/rates_test.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ func TestCalculateJettonPriceFromStonFiPool(t *testing.T) {
2424
csv: `
2525
asset_0_account_id,asset_1_account_id,asset_0_reserve,asset_1_reserve,asset_0_metadata,asset_1_metadata,asset_0_holders,asset_1_holders
2626
0:8cdc1d7640ad5ee326527fc1ad0514f468b30dc84b0173f0e155f451b4e11f7c,0:65aac9b5e380eae928db3c8e238d9bc0d61a9320fdc2bc7a2f6c87d6fedf9208,356773586306,572083446808,"{""decimals"":""9"",""name"":""Proxy TON"",""symbol"":""pTON""}","{""name"":""Scaleton"",""symbol"":""SCALE""}",52,17245
27-
0:b113a994b5024a16719f69139328eb759596c38a25f59028b146fecdc3621dfe,0:8cdc1d7640ad5ee326527fc1ad0514f468b30dc84b0173f0e155f451b4e11f7c,54581198678395,9745288354931876,"{""decimals"":""6"",""name"":""Tether USD"",""symbol"":""USD₮""}","{""decimals"":""9"",""name"":""Proxy TON"",""symbol"":""pTON""}",1038000,52`,
27+
0:b113a994b5024a16719f69139328eb759596c38a25f59028b146fecdc3621dfe,0:8cdc1d7640ad5ee326527fc1ad0514f468b30dc84b0173f0e155f451b4e11f7c,54581198678395,9745288354931876,"{""decimals"":""6"",""name"":""Tether USD"",""symbol"":""USD₮""}","{""decimals"":""9"",""name"":""Proxy TON"",""symbol"":""pTON""}",1038000,52
28+
0:b113a994b5024a16719f69139328eb759596c38a25f59028b146fecdc3621dfe,0:afc49cb8786f21c87045b19ede78fc6b46c51048513f8e9a6d44060199c1bf0c,996119000168,921942515487299500,"{""decimals"":""6"",""name"":""Tether USD"",""symbol"":""USD₮""}","{""decimals"":""9"",""name"":""Dogs"",""symbol"":""DOGS""}",1050066,881834`,
2829
expected: map[ton.AccountID]float64{
2930
ton.MustParseAccountID("0:8cdc1d7640ad5ee326527fc1ad0514f468b30dc84b0173f0e155f451b4e11f7c"): 1, // Default pTon price
3031
ton.MustParseAccountID("0:65aac9b5e380eae928db3c8e238d9bc0d61a9320fdc2bc7a2f6c87d6fedf9208"): 0.6236390657633181,
3132
ton.MustParseAccountID("0:b113a994b5024a16719f69139328eb759596c38a25f59028b146fecdc3621dfe"): 0.17854661661707652,
33+
ton.MustParseAccountID("0:afc49cb8786f21c87045b19ede78fc6b46c51048513f8e9a6d44060199c1bf0c"): 0.00019291189444059387,
3234
},
3335
},
3436
// To display more accurate prices, the default minimum number of holders is set to 200
@@ -84,12 +86,13 @@ asset_0_account_id,asset_1_account_id,asset_0_native,asset_1_native,asset_0_rese
8486
NULL,0:022d70f08add35b2d8aa2bd16f622268d7996e5737c3e7353cbb00d2aba257c5,true,false,100171974809,1787220634679,NULL,"{""decimals"":""8"",""name"":""Spintria"",""symbol"":""SP""}",false,0,3084
8587
NULL,0:48cef1de34697508200b8026bf882f8e88aff894586cfd304ab513633fa7e2d3,true,false,458457277157,4171862045823,NULL,"{""decimals"":""9"",""name"":""AI Coin"",""symbol"":""AIC""}",false,0,1239
8688
0:48cef1de34697508200b8026bf882f8e88aff894586cfd304ab513633fa7e2d3,0:b113a994b5024a16719f69139328eb759596c38a25f59028b146fecdc3621dfe,false,false,22004762576054,13287924673,"{""decimals"":""9"",""name"":""AI Coin"",""symbol"":""AIC""}","{""decimals"":""6"",""name"":""Tether USD"",""symbol"":""USD₮""}",false,1239,1039987
87-
`,
89+
NULL,0:cf76af318c0872b58a9f1925fc29c156211782b9fb01f56760d292e56123bf87,true,false,5406255533839,3293533372962,NULL,"{""decimals"":""9"",""name"":""Hipo Staked TON"",""symbol"":""hTON""}",true,0,2181`,
8890
expected: map[ton.AccountID]float64{
8991
ton.MustParseAccountID("0:0000000000000000000000000000000000000000000000000000000000000000"): 1, // Default TON price
9092
ton.MustParseAccountID("0:022d70f08add35b2d8aa2bd16f622268d7996e5737c3e7353cbb00d2aba257c5"): 0.005604902543383612,
9193
ton.MustParseAccountID("0:48cef1de34697508200b8026bf882f8e88aff894586cfd304ab513633fa7e2d3"): 0.1098927222715866,
9294
ton.MustParseAccountID("0:b113a994b5024a16719f69139328eb759596c38a25f59028b146fecdc3621dfe"): 0.18198201163316488,
95+
ton.MustParseAccountID("0:cf76af318c0872b58a9f1925fc29c156211782b9fb01f56760d292e56123bf87"): 1.0290600202253966, // Stable pool
9396
},
9497
},
9598
}

0 commit comments

Comments
 (0)