Skip to content

Commit

Permalink
Add test and do some minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Haiss2 committed Jul 5, 2024
1 parent 39fe69c commit d1d7ba4
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 4 deletions.
3 changes: 1 addition & 2 deletions pkg/pricefiller/ks_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -98,6 +98,7 @@ func (p *PriceFiller) updateAllCoinInfo() error {
}
}

p.l.Infow("New mapped coin info", "data", newMappedCoinInfo)
p.mappedCoinInfo = newMappedCoinInfo
return nil
}
Expand Down Expand Up @@ -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) {
Expand Down
63 changes: 63 additions & 0 deletions pkg/pricefiller/price_fillter_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
12 changes: 12 additions & 0 deletions pkg/pricefiller/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d1d7ba4

Please sign in to comment.