Skip to content

Commit

Permalink
Import ks-setting token if not found
Browse files Browse the repository at this point in the history
  • Loading branch information
Haiss2 committed Aug 7, 2024
1 parent 4a9395a commit 40f51e7
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 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
}
19 changes: 17 additions & 2 deletions pkg/pricefiller/price_fillter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

const (
NetworkETHChanID = 1
NetworkETHChanIDString = "1"
NetworkETH = "ETH"
updateAllCoinInfoInterval = time.Hour
backfillTradeLogsPriceInterval = 10 * time.Minute
Expand Down Expand Up @@ -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
}

0 comments on commit 40f51e7

Please sign in to comment.