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 71e5ca8..8fa8550 100644 --- a/pkg/pricefiller/price_fillter.go +++ b/pkg/pricefiller/price_fillter.go @@ -15,17 +15,20 @@ import ( const ( NetworkETHChanID = 1 + NetworkETHChanIDString = "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 +55,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 +201,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 } @@ -237,10 +249,24 @@ func (p *PriceFiller) getDecimals(address string) (int64, error) { return 0, err } - if len(resp.Data.Tokens) != 1 { - p.l.Errorw("Weird token catalog response", "resp", resp) - return 0, errors.New("weird token catalog response") + 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 + } + + // 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 } - return resp.Data.Tokens[0].Decimals, nil + p.l.Warnw("Weird ImportToken response", "resp", newResp) + return 0, ErrWeirdTokenCatalogResp }