Skip to content

Commit

Permalink
Merge pull request #205 from tonkeeper/fix_rates
Browse files Browse the repository at this point in the history
fix rates
  • Loading branch information
zakhar-petukhov authored Oct 3, 2023
2 parents 0cc1e0d + a8d54c1 commit 7c95fac
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 52 deletions.
5 changes: 3 additions & 2 deletions pkg/api/rates_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ func (h *Handler) GetRates(ctx context.Context, params oas.GetRatesParams) (*oas

ratesRes := make(map[string]tokenRates)
for _, token := range tokens {
_, err = tongo.ParseAccountID(token)
if err != nil {
if accountID, err := tongo.ParseAccountID(token); err == nil {
token = accountID.ToHuman(true, false)
} else {
token = strings.ToUpper(token)
}
for _, currency := range currencies {
Expand Down
81 changes: 31 additions & 50 deletions pkg/rates/sources.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"math"
"net/http"
"strconv"
"sync"

"github.com/labstack/gommon/log"
"github.com/tonkeeper/opentonapi/pkg/references"
Expand Down Expand Up @@ -126,7 +125,6 @@ func getStonFiPool(tonPrice float64) map[string]float64 {
return mapOfPool
}

// TODO: update code for get price from dedust
func (m *Mock) getDedustPool() map[string]float64 {
resp, err := http.Get("https://api.dedust.io/v2/pools")
if err != nil {
Expand All @@ -153,61 +151,44 @@ func (m *Mock) getDedustPool() map[string]float64 {
return map[string]float64{}
}

var wg sync.WaitGroup
chanMapOfPool := make(chan map[string]float64)
mapOfPool := make(map[string]float64)
for _, pool := range respBody {
wg.Add(1)

go func(pool Pool) {
defer wg.Done()

if len(pool.Assets) != 2 || len(pool.Reserves) != 2 {
return
}
if len(pool.Assets) != 2 || len(pool.Reserves) != 2 {
continue
}

firstAsset, secondAsset := pool.Assets[0], pool.Assets[1]
if firstAsset.Metadata == nil || firstAsset.Metadata.Symbol != "TON" {
return
}
firstAsset, secondAsset := pool.Assets[0], pool.Assets[1]
if firstAsset.Metadata == nil || firstAsset.Metadata.Symbol != "TON" {
continue
}

var firstReserve, secondReserve float64
if firstReserve, err = strconv.ParseFloat(pool.Reserves[0], 64); err != nil {
return
}
if secondReserve, err = strconv.ParseFloat(pool.Reserves[1], 64); err != nil {
return
}
if firstReserve < float64(100*ton.OneTON) || secondReserve < float64(100*ton.OneTON) {
return
}
firstReserve, err := strconv.ParseFloat(pool.Reserves[0], 64)
if err != nil {
continue
}
secondReserve, err := strconv.ParseFloat(pool.Reserves[1], 64)
if err != nil {
continue
}
if firstReserve < float64(100*ton.OneTON) || secondReserve < float64(100*ton.OneTON) {
continue
}

secondReserveDecimals := float64(9)
if secondAsset.Metadata == nil || secondAsset.Metadata.Decimals != 0 {
accountID, _ := tongo.ParseAccountID(secondAsset.Address)
meta, err := m.Storage.GetJettonMasterMetadata(context.Background(), accountID)
if err == nil && meta.Decimals != "" {
decimals, err := strconv.Atoi(meta.Decimals)
if err == nil {
secondReserveDecimals = float64(decimals)
}
secondReserveDecimals := float64(9)
if secondAsset.Metadata == nil || secondAsset.Metadata.Decimals != 0 {
accountID, _ := tongo.ParseAccountID(secondAsset.Address)
meta, err := m.Storage.GetJettonMasterMetadata(context.Background(), accountID)
if err == nil && meta.Decimals != "" {
decimals, err := strconv.Atoi(meta.Decimals)
if err == nil {
secondReserveDecimals = float64(decimals)
}
}

price := 1 / ((secondReserve / math.Pow(10, secondReserveDecimals)) / (firstReserve / math.Pow(10, 9)))
chanMapOfPool <- map[string]float64{secondAsset.Address: price}
}(pool)
}

go func() {
wg.Wait()
close(chanMapOfPool)
}()

mapOfPool := make(map[string]float64)
for pools := range chanMapOfPool {
for address, price := range pools {
mapOfPool[address] = price
}

// TODO: change algorithm math price for other type pool (volatile/stable)
price := 1 / ((secondReserve / math.Pow(10, secondReserveDecimals)) / (firstReserve / math.Pow(10, 9)))
mapOfPool[secondAsset.Address] = price
}

return mapOfPool
Expand Down

0 comments on commit 7c95fac

Please sign in to comment.