Skip to content

Commit

Permalink
Merge pull request #61 from KyberNetwork/Feature/Mark_0x000_as_ETH_to…
Browse files Browse the repository at this point in the history
…_fill_price

Feature/Mark_0x000_as_ETH_to_fill_price
  • Loading branch information
Haiss2 authored Aug 7, 2024
2 parents 509dd73 + 40f51e7 commit 2cd8915
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 9 deletions.
41 changes: 41 additions & 0 deletions pkg/pricefiller/ks_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
44 changes: 35 additions & 9 deletions pkg/pricefiller/price_fillter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<APIError> 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 {
Expand All @@ -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,
},
},
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

0 comments on commit 2cd8915

Please sign in to comment.