diff --git a/pkg/pricefiller/ks_client.go b/pkg/pricefiller/ks_client.go index 01efcfc..02dff50 100644 --- a/pkg/pricefiller/ks_client.go +++ b/pkg/pricefiller/ks_client.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "encoding/json" - "errors" "fmt" "io" "net/http" @@ -99,7 +98,7 @@ func (c *KsClient) GetTokenCatalog(address string) (TokenCatalogResp, error) { } if resp.Code != 0 { - return TokenCatalogResp{}, errors.New("unsuccess to get funding info") + return TokenCatalogResp{}, fmt.Errorf("invalid response code: %d", resp.Code) } return resp, nil diff --git a/pkg/pricefiller/price_getter.go b/pkg/pricefiller/price_fillter.go similarity index 97% rename from pkg/pricefiller/price_getter.go rename to pkg/pricefiller/price_fillter.go index fc347da..58b791e 100644 --- a/pkg/pricefiller/price_getter.go +++ b/pkg/pricefiller/price_fillter.go @@ -57,7 +57,7 @@ func NewPriceFiller(binanceClient *binance.Client, s *storage.Storage) (*PriceFi } func (p *PriceFiller) getPrice(token string, timestamp int64) (float64, error) { - candles, err := p.binanceClient.NewKlinesService().Symbol(token + "USDT"). + candles, err := p.binanceClient.NewKlinesService().Symbol(withAlias(token) + "USDT"). Interval("1s").StartTime(timestamp).EndTime(timestamp).Do(context.Background()) if err != nil { return 0, err @@ -98,6 +98,7 @@ func (p *PriceFiller) updateAllCoinInfo() error { } } + p.l.Infow("New mapped coin info", "data", newMappedCoinInfo) p.mappedCoinInfo = newMappedCoinInfo return nil } @@ -194,7 +195,7 @@ func (p *PriceFiller) FullFillTradeLogs(tradeLogs []storage.TradeLog) []storage. } resp[idx] = filledTradeLogs } - return nil + return resp } func (p *PriceFiller) getDecimals(address string) (int64, error) { diff --git a/pkg/pricefiller/price_fillter_test.go b/pkg/pricefiller/price_fillter_test.go new file mode 100644 index 0000000..9f782c2 --- /dev/null +++ b/pkg/pricefiller/price_fillter_test.go @@ -0,0 +1,63 @@ +package pricefiller + +import ( + "testing" + + "github.com/KyberNetwork/go-binance/v2" + "github.com/KyberNetwork/tradelogs/pkg/storage" + "github.com/test-go/testify/require" +) + +// go test -v -timeout 30s -run ^TestFillPrice$ github.com/KyberNetwork/tradelogs/pkg/pricefiller +func TestFillPrice(t *testing.T) { + t.Skip("Need to add Binance credentials") + bClient := binance.NewClient("", "") + filler, err := NewPriceFiller(bClient, nil) + if err != nil { + require.NoError(t, err) + } + + newLogs := filler.FullFillTradeLogs([]storage.TradeLog{ + { + Taker: "0x807cf9a772d5a3f9cefbc1192e939d62f0d9bd38", + MakerToken: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", + TakerToken: "0x320623b8e4ff03373931769a31fc52a4e78b5d70", + MakerTokenAmount: "503568522079108960", + TakerTokenAmount: "336970435721651800000000", + ContractAddress: "0x6131b5fae19ea4f9d964eac0408e4408b66337b5", + BlockNumber: 20230391, + TxHash: "0x0aad2e38b90390d6d060a5886ddd20d312c6beb3a305b2360bd6d25593ddf058", + LogIndex: 57, + Timestamp: 1720062815000, + EventHash: "0xd6d4f5681c246c9f42c203e287975af1601f8df8035a9251f79aab5c8f09e2f8", + }, + { + Taker: "0x807cf9a772d5a3f9cefbc1192e939d62f0d9bd38", + MakerToken: "0x7fc66500c84a76ad7e9c93437bfc5ac33e2ddae9", + TakerToken: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", + MakerTokenAmount: "179003122862979007021", + TakerTokenAmount: "4688506567482331000", + ContractAddress: "0x6131b5fae19ea4f9d964eac0408e4408b66337b5", + BlockNumber: 20230305, + TxHash: "0x21b8f97e43ff6debbe2ab323f079f0a936c14c16bdb6693587b48a5d66dcc37c", + LogIndex: 136, + Timestamp: 1720061783000, + EventHash: "0xd6d4f5681c246c9f42c203e287975af1601f8df8035a9251f79aab5c8f09e2f8", + }, + { + Taker: "0x807cf9a772d5a3f9cefbc1192e939d62f0d9bd38", + MakerToken: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", + TakerToken: "0x514910771af9ca656af840dff83e8264ecf986ca", + MakerTokenAmount: "783414511682884466", + TakerTokenAmount: "190359937738916760000", + ContractAddress: "0x6131b5fae19ea4f9d964eac0408e4408b66337b5", + BlockNumber: 20230291, + TxHash: "0x2ff00bcfa69c85fdbd0c7fc9b193717009751a32cf661c31815d9745deb68552", + LogIndex: 136, + Timestamp: 1720061615000, + EventHash: "0xd6d4f5681c246c9f42c203e287975af1601f8df8035a9251f79aab5c8f09e2f8", + }, + }) + + t.Log(newLogs) +} diff --git a/pkg/pricefiller/utils.go b/pkg/pricefiller/utils.go index 74829b0..a2311c4 100644 --- a/pkg/pricefiller/utils.go +++ b/pkg/pricefiller/utils.go @@ -6,6 +6,18 @@ import ( "github.com/KyberNetwork/tradelogs/pkg/convert" ) +var aliasCoinMap = map[string]string{ + "WETH": "ETH", + "STETH": "ETH", +} + +func withAlias(coin string) string { + if s, ok := aliasCoinMap[coin]; ok { + return s + } + return coin +} + // calculateAmountUsd returns raw / (10**decimals) * price func calculateAmountUsd(raw string, decimals int64, price float64) float64 { rawAmt, ok := new(big.Int).SetString(raw, 10)