From 4a9395ab4f21e1a24408091d5687de21c0aa64ed Mon Sep 17 00:00:00 2001 From: Haiss2 Date: Wed, 7 Aug 2024 11:50:44 +0700 Subject: [PATCH 1/2] Feature/Mark_0x000_as_ETH_to_fill_price --- pkg/pricefiller/price_fillter.go | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/pkg/pricefiller/price_fillter.go b/pkg/pricefiller/price_fillter.go index 71e5ca8..786e39b 100644 --- a/pkg/pricefiller/price_fillter.go +++ b/pkg/pricefiller/price_fillter.go @@ -17,15 +17,17 @@ const ( NetworkETHChanID = 1 NetworkETH = "ETH" updateAllCoinInfoInterval = time.Hour - backfillTradeLogsPriceInterval = 30 * time.Second + backfillTradeLogsPriceInterval = 10 * time.Minute backfillTradeLogsLimit = 60 - addressETH = "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" + addressETH1 = "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" + addressETH2 = "0x0000000000000000000000000000000000000000" coinUSDT = "USDT" invalidSymbolErrString = " code=-1121, msg=Invalid symbol." ) var ( - ErrNoPrice = errors.New(("no price from binance")) + ErrNoPrice = errors.New(("no price from binance")) + ErrWeirdTokenCatalogResp = errors.New("weird token catalog response") ) type CoinInfo struct { @@ -52,10 +54,16 @@ func NewPriceFiller(l *zap.SugaredLogger, binanceClient *binance.Client, ksClient: NewKsClient(), binanceClient: binanceClient, mappedCoinInfo: map[string]CoinInfo{ - addressETH: { + addressETH1: { Coin: "ETH", Network: NetworkETH, - ContractAddress: addressETH, + ContractAddress: addressETH1, + Decimals: 18, + }, + addressETH2: { + Coin: "ETH", + Network: NetworkETH, + ContractAddress: addressETH2, Decimals: 18, }, }, @@ -192,6 +200,9 @@ func (p *PriceFiller) getPriceAndAmountUsd(address, rawAmt string, at int64) (fl if coin.Decimals == 0 { d, err := p.getDecimals(address) if err != nil { + if errors.Is(err, ErrWeirdTokenCatalogResp) { + return 0, 0, nil + } p.l.Errorw("Failed to getDecimals", "err", err, "address", address) return 0, 0, err } @@ -238,8 +249,8 @@ func (p *PriceFiller) getDecimals(address string) (int64, error) { } if len(resp.Data.Tokens) != 1 { - p.l.Errorw("Weird token catalog response", "resp", resp) - return 0, errors.New("weird token catalog response") + p.l.Warnw("Weird token catalog response", "resp", resp) + return 0, ErrWeirdTokenCatalogResp } return resp.Data.Tokens[0].Decimals, nil From 40f51e75b09a621b9fddaf63991842a8b091b610 Mon Sep 17 00:00:00 2001 From: Haiss2 Date: Wed, 7 Aug 2024 14:03:55 +0700 Subject: [PATCH 2/2] Import ks-setting token if not found --- pkg/pricefiller/ks_client.go | 41 ++++++++++++++++++++++++++++++++ pkg/pricefiller/price_fillter.go | 19 +++++++++++++-- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/pkg/pricefiller/ks_client.go b/pkg/pricefiller/ks_client.go index 02dff50..38dba3c 100644 --- a/pkg/pricefiller/ks_client.go +++ b/pkg/pricefiller/ks_client.go @@ -103,3 +103,44 @@ func (c *KsClient) GetTokenCatalog(address string) (TokenCatalogResp, error) { return resp, nil } + +type ImportedToken struct { + ChainID string `json:"chainId"` + Address string `json:"address"` +} + +type ImportTokenParam struct { + Tokens []ImportedToken `json:"tokens"` +} + +type ImportTokenResp struct { + Code int64 `json:"code"` + Message string `json:"message"` + Data struct { + Tokens []struct { + Data TokenCatalog `json:"data"` + } `json:"tokens"` + } `json:"data"` +} + +func (c *KsClient) ImportToken(chainID, address string) (ImportTokenResp, error) { + param := ImportTokenParam{ + Tokens: []ImportedToken{ + { + ChainID: chainID, + Address: address, + }, + }, + } + var resp ImportTokenResp + err := c.DoRequest(context.Background(), http.MethodPost, c.baseURL+"/tokens/import", param, &resp) + if err != nil { + return ImportTokenResp{}, err + } + + if resp.Code != 0 { + return ImportTokenResp{}, fmt.Errorf("invalid response code: %d", resp.Code) + } + + return resp, nil +} diff --git a/pkg/pricefiller/price_fillter.go b/pkg/pricefiller/price_fillter.go index 786e39b..8fa8550 100644 --- a/pkg/pricefiller/price_fillter.go +++ b/pkg/pricefiller/price_fillter.go @@ -15,6 +15,7 @@ import ( const ( NetworkETHChanID = 1 + NetworkETHChanIDString = "1" NetworkETH = "ETH" updateAllCoinInfoInterval = time.Hour backfillTradeLogsPriceInterval = 10 * time.Minute @@ -248,10 +249,24 @@ func (p *PriceFiller) getDecimals(address string) (int64, error) { return 0, err } - if len(resp.Data.Tokens) != 1 { + if len(resp.Data.Tokens) == 1 { + return resp.Data.Tokens[0].Decimals, nil + } + if len(resp.Data.Tokens) > 1 { p.l.Warnw("Weird token catalog response", "resp", resp) return 0, ErrWeirdTokenCatalogResp } - return resp.Data.Tokens[0].Decimals, nil + // try to import token if token is not found. + newResp, err := p.ksClient.ImportToken(NetworkETHChanIDString, address) + if err != nil { + p.l.Errorw("Failed to ImportToken", "err", err) + return 0, err + } + if len(newResp.Data.Tokens) == 1 { + return newResp.Data.Tokens[0].Data.Decimals, nil + } + + p.l.Warnw("Weird ImportToken response", "resp", newResp) + return 0, ErrWeirdTokenCatalogResp }