Skip to content

Commit dc3782b

Browse files
add a price for the Beetroot jetton
1 parent 5df4c64 commit dc3782b

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

pkg/rates/sources.go

+43
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ func (m *Mock) GetCurrentRates() (map[string]float64, error) {
1818
const (
1919
tonstakers string = "tonstakers"
2020
bemo string = "bemo"
21+
beetroot string = "beetroot"
2122
slpTokens string = "slp_tokens"
2223
)
2324

@@ -58,6 +59,11 @@ func (m *Mock) GetCurrentRates() (map[string]float64, error) {
5859
pools[account] = price
5960
}
6061
}
62+
if beetRootPrice, err := retry(beetroot, medianTonPriceToUsd, pools, m.getBeetrootPrice); err == nil {
63+
for account, price := range beetRootPrice {
64+
pools[account] = price
65+
}
66+
}
6167
pools = m.updatePools(pools)
6268
if slpTokensPrice, err := retry(slpTokens, medianTonPriceToUsd, pools, m.getSlpTokensPrice); err == nil {
6369
for account, price := range slpTokensPrice {
@@ -221,3 +227,40 @@ func (m *Mock) getSlpTokensPrice(tonPrice float64, pools map[ton.AccountID]float
221227

222228
return accountsPrice, nil
223229
}
230+
231+
func (m *Mock) getBeetrootPrice(tonPrice float64, pools map[ton.AccountID]float64) (map[tongo.AccountID]float64, error) {
232+
if tonPrice == 0 {
233+
return map[ton.AccountID]float64{}, fmt.Errorf("unknown TON price")
234+
}
235+
var beetRootContract = ton.MustParseAccountID("EQDC8MY5tY5rPM6KFFxz58fMUES6qSsFxi_Pbaig1QuO3F7y")
236+
var beetRootAccount = ton.MustParseAccountID("EQAFGhmx199oH6kmL78PGBHyAx4d5CiJdfXwSjDK5F5IFyfC")
237+
url := fmt.Sprintf("https://tonapi.io/v2/blockchain/accounts/%v/methods/get_price_data", beetRootContract)
238+
respBody, err := sendRequest(url, m.TonApiToken)
239+
if err != nil {
240+
return map[ton.AccountID]float64{}, err
241+
}
242+
defer respBody.Close()
243+
type data struct {
244+
Success bool `json:"success"`
245+
Stack []struct {
246+
Num string `json:"num"`
247+
} `json:"stack"`
248+
}
249+
var result data
250+
if err = json.NewDecoder(respBody).Decode(&result); err != nil {
251+
return map[ton.AccountID]float64{}, fmt.Errorf("[getBeetrootPrice] failed to decode response: %v", err)
252+
}
253+
if !result.Success {
254+
return map[ton.AccountID]float64{}, fmt.Errorf("not success")
255+
}
256+
if len(result.Stack) == 0 {
257+
return map[ton.AccountID]float64{}, fmt.Errorf("empty stack")
258+
}
259+
num, err := strconv.ParseInt(result.Stack[0].Num, 0, 64)
260+
if err != nil {
261+
return map[tongo.AccountID]float64{}, err
262+
}
263+
usdPrice := float64(num) / 100
264+
price := usdPrice / tonPrice
265+
return map[ton.AccountID]float64{beetRootAccount: price}, nil
266+
}

0 commit comments

Comments
 (0)