Skip to content

Commit

Permalink
fix decimals to calculate jetton prices
Browse files Browse the repository at this point in the history
  • Loading branch information
zakhar-petukhov committed Sep 2, 2024
1 parent 661e733 commit 60577ac
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
14 changes: 7 additions & 7 deletions pkg/rates/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ func calculatePoolPrice(firstAsset, secondAsset Asset, pools map[ton.AccountID]f
return ton.AccountID{}, 0
}
var calculatedAccount ton.AccountID // The account for which we will find the price
var decimals int
var firstAssetDecimals, secondAssetDecimals int
if okFirst { // Knowing the first asset's price, we determine the second asset's price
firstAsset.Reserve *= priceFirst // Converting reserve prices to TON
if firstAsset.Reserve < minReserve {
Expand All @@ -575,7 +575,7 @@ func calculatePoolPrice(firstAsset, secondAsset Asset, pools map[ton.AccountID]f
return ton.AccountID{}, 0
}
calculatedAccount = secondAsset.Account
decimals = secondAsset.Decimals
firstAssetDecimals, secondAssetDecimals = firstAsset.Decimals, secondAsset.Decimals
}
if okSecond { // Knowing the second asset's price, we determine the first asset's price
secondAsset.Reserve *= priceSecond // Converting reserve prices to TON
Expand All @@ -586,19 +586,19 @@ func calculatePoolPrice(firstAsset, secondAsset Asset, pools map[ton.AccountID]f
return ton.AccountID{}, 0
}
calculatedAccount = firstAsset.Account
decimals = firstAsset.Decimals
firstAsset, secondAsset = secondAsset, firstAsset
firstAssetDecimals, secondAssetDecimals = firstAsset.Decimals, secondAsset.Decimals
}
if decimals == 0 {
if firstAssetDecimals == 0 || secondAssetDecimals == 0 {
return ton.AccountID{}, 0
}
var price float64
if isStable {
x := secondAsset.Reserve / math.Pow(10, float64(decimals))
y := firstAsset.Reserve / math.Pow(10, 9)
x := secondAsset.Reserve / math.Pow(10, float64(secondAssetDecimals))
y := firstAsset.Reserve / math.Pow(10, float64(firstAssetDecimals))
price = (3*x*x*y + y*y*y) / (x*x*x + 3*y*y*x)
} else {
price = (firstAsset.Reserve / secondAsset.Reserve) * math.Pow(10, float64(decimals)-9)
price = (firstAsset.Reserve / secondAsset.Reserve) * math.Pow(10, float64(secondAssetDecimals)-float64(firstAssetDecimals))
}
return calculatedAccount, price
}
Expand Down
7 changes: 5 additions & 2 deletions pkg/rates/rates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ func TestCalculateJettonPriceFromStonFiPool(t *testing.T) {
csv: `
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
0:8cdc1d7640ad5ee326527fc1ad0514f468b30dc84b0173f0e155f451b4e11f7c,0:65aac9b5e380eae928db3c8e238d9bc0d61a9320fdc2bc7a2f6c87d6fedf9208,356773586306,572083446808,"{""decimals"":""9"",""name"":""Proxy TON"",""symbol"":""pTON""}","{""name"":""Scaleton"",""symbol"":""SCALE""}",52,17245
0:b113a994b5024a16719f69139328eb759596c38a25f59028b146fecdc3621dfe,0:8cdc1d7640ad5ee326527fc1ad0514f468b30dc84b0173f0e155f451b4e11f7c,54581198678395,9745288354931876,"{""decimals"":""6"",""name"":""Tether USD"",""symbol"":""USD₮""}","{""decimals"":""9"",""name"":""Proxy TON"",""symbol"":""pTON""}",1038000,52`,
0:b113a994b5024a16719f69139328eb759596c38a25f59028b146fecdc3621dfe,0:8cdc1d7640ad5ee326527fc1ad0514f468b30dc84b0173f0e155f451b4e11f7c,54581198678395,9745288354931876,"{""decimals"":""6"",""name"":""Tether USD"",""symbol"":""USD₮""}","{""decimals"":""9"",""name"":""Proxy TON"",""symbol"":""pTON""}",1038000,52
0:b113a994b5024a16719f69139328eb759596c38a25f59028b146fecdc3621dfe,0:afc49cb8786f21c87045b19ede78fc6b46c51048513f8e9a6d44060199c1bf0c,996119000168,921942515487299500,"{""decimals"":""6"",""name"":""Tether USD"",""symbol"":""USD₮""}","{""decimals"":""9"",""name"":""Dogs"",""symbol"":""DOGS""}",1050066,881834`,
expected: map[ton.AccountID]float64{
ton.MustParseAccountID("0:8cdc1d7640ad5ee326527fc1ad0514f468b30dc84b0173f0e155f451b4e11f7c"): 1, // Default pTon price
ton.MustParseAccountID("0:65aac9b5e380eae928db3c8e238d9bc0d61a9320fdc2bc7a2f6c87d6fedf9208"): 0.6236390657633181,
ton.MustParseAccountID("0:b113a994b5024a16719f69139328eb759596c38a25f59028b146fecdc3621dfe"): 0.17854661661707652,
ton.MustParseAccountID("0:afc49cb8786f21c87045b19ede78fc6b46c51048513f8e9a6d44060199c1bf0c"): 0.00019291189444059387,
},
},
// To display more accurate prices, the default minimum number of holders is set to 200
Expand Down Expand Up @@ -84,12 +86,13 @@ asset_0_account_id,asset_1_account_id,asset_0_native,asset_1_native,asset_0_rese
NULL,0:022d70f08add35b2d8aa2bd16f622268d7996e5737c3e7353cbb00d2aba257c5,true,false,100171974809,1787220634679,NULL,"{""decimals"":""8"",""name"":""Spintria"",""symbol"":""SP""}",false,0,3084
NULL,0:48cef1de34697508200b8026bf882f8e88aff894586cfd304ab513633fa7e2d3,true,false,458457277157,4171862045823,NULL,"{""decimals"":""9"",""name"":""AI Coin"",""symbol"":""AIC""}",false,0,1239
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
`,
NULL,0:cf76af318c0872b58a9f1925fc29c156211782b9fb01f56760d292e56123bf87,true,false,5406255533839,3293533372962,NULL,"{""decimals"":""9"",""name"":""Hipo Staked TON"",""symbol"":""hTON""}",true,0,2181`,
expected: map[ton.AccountID]float64{
ton.MustParseAccountID("0:0000000000000000000000000000000000000000000000000000000000000000"): 1, // Default TON price
ton.MustParseAccountID("0:022d70f08add35b2d8aa2bd16f622268d7996e5737c3e7353cbb00d2aba257c5"): 0.005604902543383612,
ton.MustParseAccountID("0:48cef1de34697508200b8026bf882f8e88aff894586cfd304ab513633fa7e2d3"): 0.1098927222715866,
ton.MustParseAccountID("0:b113a994b5024a16719f69139328eb759596c38a25f59028b146fecdc3621dfe"): 0.18198201163316488,
ton.MustParseAccountID("0:cf76af318c0872b58a9f1925fc29c156211782b9fb01f56760d292e56123bf87"): 1.0290600202253966, // Stable pool
},
},
}
Expand Down

0 comments on commit 60577ac

Please sign in to comment.