Skip to content

Commit

Permalink
change get price by calling mtm
Browse files Browse the repository at this point in the history
  • Loading branch information
iostream1308 committed Nov 28, 2024
1 parent b441c43 commit 31131dc
Show file tree
Hide file tree
Showing 13 changed files with 514 additions and 103 deletions.
Binary file added v2/.DS_Store
Binary file not shown.
15 changes: 10 additions & 5 deletions v2/cmd/price_filler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (
"log"
"os"

"github.com/KyberNetwork/go-binance/v2"
libapp "github.com/KyberNetwork/tradelogs/v2/pkg/app"
"github.com/KyberNetwork/tradelogs/v2/pkg/price_filler"
"github.com/KyberNetwork/tradelogs/v2/pkg/mtm"
pricefiller "github.com/KyberNetwork/tradelogs/v2/pkg/price_filler"
dashboardStorage "github.com/KyberNetwork/tradelogs/v2/pkg/storage/dashboard"
bebopStorage "github.com/KyberNetwork/tradelogs/v2/pkg/storage/tradelogs/bebop"
hashflowv3Storage "github.com/KyberNetwork/tradelogs/v2/pkg/storage/tradelogs/hashflow_v3"
kyberswapStorage "github.com/KyberNetwork/tradelogs/v2/pkg/storage/tradelogs/kyberswap"
Expand All @@ -21,11 +22,15 @@ import (
zxrfqv3Storage "github.com/KyberNetwork/tradelogs/v2/pkg/storage/tradelogs/zxrfqv3"
"github.com/KyberNetwork/tradinglib/pkg/dbutil"
"github.com/jmoiron/sqlx"
"github.com/joho/godotenv"
"github.com/urfave/cli"
"go.uber.org/zap"
)

func main() {
if err := godotenv.Load("tradelogs.env"); err != nil {
log.Println("load env error", err)
}
app := libapp.NewApp()
app.Name = "trade logs crawler service"
app.Action = run
Expand Down Expand Up @@ -68,9 +73,9 @@ func run(c *cli.Context) error {
zxrfqv3Storage.New(l, db),
pancakeswapStorage.New(l, db),
}

binanceClient := binance.NewClient(c.String(libapp.BinanceAPIKeyFlag.Name), c.String(libapp.BinanceSecretKeyFlag.Name))
priceFiller, err := pricefiller.NewPriceFiller(l, binanceClient, s)
mtmClient := mtm.NewMtmClient(c.String(libapp.MarkToMarketURLFlag.Name))
dashboardStorage := dashboardStorage.New(l, db)
priceFiller, err := pricefiller.NewPriceFiller(l, s, mtmClient, dashboardStorage)
if err != nil {
l.Errorw("Error while init price filler")
return err
Expand Down
63 changes: 59 additions & 4 deletions v2/internal/server/tradelogs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"net/http"
"time"

dashboardStorage "github.com/KyberNetwork/tradelogs/v2/pkg/storage/dashboard"
dashboardTypes "github.com/KyberNetwork/tradelogs/v2/pkg/storage/dashboard/types"
storageTypes "github.com/KyberNetwork/tradelogs/v2/pkg/storage/tradelogs/types"
"github.com/gin-contrib/pprof"
"github.com/gin-gonic/gin"
Expand All @@ -16,10 +18,11 @@ var (
)

type TradeLogs struct {
r *gin.Engine
bindAddr string
l *zap.SugaredLogger
storage []storageTypes.Storage
r *gin.Engine
bindAddr string
l *zap.SugaredLogger
storage []storageTypes.Storage
dashStorage *dashboardStorage.Storage
}

func NewTradeLogs(l *zap.SugaredLogger, s []storageTypes.Storage, bindAddr string) *TradeLogs {
Expand Down Expand Up @@ -51,6 +54,9 @@ func (s *TradeLogs) Run() error {
func (s *TradeLogs) register() {
pprof.Register(s.r, "/debug")
s.r.GET("/tradelogs", s.getTradeLogs)
s.r.GET("/tokens", s.getTokens)
// s.r.GET("/contracts", s.getContracts)
// s.r.POST("/add_contracts", s.addContracts)
}

func (s *TradeLogs) getTradeLogs(c *gin.Context) {
Expand Down Expand Up @@ -91,3 +97,52 @@ func (s *TradeLogs) getTradeLogs(c *gin.Context) {
"data": data,
})
}

func (s *TradeLogs) getTokens(c *gin.Context) {
var queries dashboardTypes.TokenQuery
if err := c.ShouldBindJSON(&queries); err != nil {
responseErr(c, http.StatusBadRequest, err)
return
}

data, err := s.dashStorage.GetTokens(queries)
if err != nil {
responseErr(c, http.StatusBadRequest, err)
return
}
c.JSON(http.StatusOK, gin.H{
"success": true,
"data": data,
})
}

// func (s *TradeLogs) getContracts(c *gin.Context) {
// data, err := s.dashStorage.GetContracts()
// if err != nil {
// responseErr(c, http.StatusBadRequest, err)
// return
// }
// c.JSON(http.StatusOK, gin.H{
// "success": true,
// "data": data,
// })
// }

// func (s *TradeLogs) addContracts(c *gin.Context) {
// var queries []dashboardTypes.Contract

// if err := c.ShouldBindJSON(&queries); err != nil {
// responseErr(c, http.StatusBadRequest, err)
// return
// }

// if err := s.dashStorage.InsertContract(queries); err != nil {
// responseErr(c, http.StatusInternalServerError, err)
// return
// }

// c.JSON(http.StatusOK, gin.H{
// "success": true,
// "data": queries,
// })
// }
5 changes: 5 additions & 0 deletions v2/pkg/app/price_filler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ var BinanceSecretKeyFlag = cli.StringFlag{
Name: "binance-secret-key",
EnvVar: "BINANCE_SECRET_KEY",
}
var MarkToMarketURLFlag = cli.StringFlag{
Name: "mark-to-market-url",
EnvVar: "MARK_TO_MARKET_URL",
}

func PriceFillerFlags() []cli.Flag {
return []cli.Flag{
BinanceAPIKeyFlag,
BinanceSecretKeyFlag,
MarkToMarketURLFlag,
}
}
116 changes: 116 additions & 0 deletions v2/pkg/mtm/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package mtm

import (
"context"
"encoding/json"
"fmt"
"net/http"

tokenStorage "github.com/KyberNetwork/tradelogs/v2/pkg/storage/dashboard/types"
)

type MtmClient struct {
host string
}

func NewMtmClient(host string) *MtmClient {
return &MtmClient{
host: host,
}
}

func (m *MtmClient) GetListTokens(ctx context.Context) ([]tokenStorage.Token, error) {
const path = "/tokens"
req, err := http.NewRequestWithContext(ctx, http.MethodGet, m.host+path, nil)
if err != nil {
return nil, fmt.Errorf("unable to create request: %w", err)
}
req.Header.Set("Accept", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("request failed: %w", err)
}
defer resp.Body.Close()
var tokens TokenResponse

if err := json.NewDecoder(resp.Body).Decode(&tokens); err != nil {
return nil, fmt.Errorf("failed to decode JSON: %w", err)
}

return tokens.Data, nil
}

func (m *MtmClient) GetHistoricalRate(
ctx context.Context,
base, quote, chainId string,
timestamp int64,
) (float64, error) {
const path = "/v3/historical"
queryParam := fmt.Sprintf("?base=%s&quote=%s&time=%d&chainid=%s",
base,
quote,
timestamp,
chainId,
)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, m.host+path+queryParam, nil)
if err != nil {
return 0, fmt.Errorf("unable to create request: %w", err)
}
req.Header.Set("Accept", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return 0, fmt.Errorf("request failed: %w", err)
}
defer resp.Body.Close()

var rate RateResponse

if err := json.NewDecoder(resp.Body).Decode(&rate); err != nil {
return 0, fmt.Errorf("failed to decode JSON: %w", err)
}

return rate.Data.Price, nil
}

func (m *MtmClient) GetCurrentRate(ctx context.Context, base, quote, chainId string) (float64, error) {
const path = "/v3/rate"
queryParam := fmt.Sprintf("?base=%s&quote=%s&chainid=%s",
base,
quote,
chainId,
)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, m.host+path+queryParam, nil)
if err != nil {
return 0, fmt.Errorf("unable to create request: %w", err)
}
req.Header.Set("Accept", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return 0, fmt.Errorf("request failed: %w", err)
}
defer resp.Body.Close()

var rate RateResponse

if err := json.NewDecoder(resp.Body).Decode(&rate); err != nil {
return 0, fmt.Errorf("failed to decode JSON: %w", err)
}

return rate.Data.Price, nil
}

type RateResponse struct {
Success bool `json:"success"`
Data struct {
Price float64 `json:"price"`
TimeUnix int64 `json:"timeUnix"`
} `json:"data"`
}

type TokenResponse struct {
Success bool `json:"success"`
Data []tokenStorage.Token `json:"data"`
}
28 changes: 28 additions & 0 deletions v2/pkg/mtm/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package mtm

import (
"context"
"fmt"
"testing"

"github.com/test-go/testify/require"
)

func TestNewMtmClient(t *testing.T) {
// need mtm url
t.Skip()
MTM_URL := ""
client := NewMtmClient(MTM_URL)

rate, err := client.GetCurrentRate(context.Background(), "0x9be89d2a4cd102d8fecc6bf9da793be995c22541", "0x9702230a8ea53601f5cd2dc00fdbc13d4df4a8c7", "1")
require.NoError(t, err)
fmt.Println("rate", rate)

rate, err = client.GetHistoricalRate(context.Background(), "0x9be89d2a4cd102d8fecc6bf9da793be995c22541", "0x9702230a8ea53601f5cd2dc00fdbc13d4df4a8c7", "1", 1732608687)
require.NoError(t, err)
fmt.Println("historical rate", rate)

tokens, err := client.GetListTokens(context.Background())
require.NoError(t, err)
fmt.Println("tokens", tokens)
}
13 changes: 11 additions & 2 deletions v2/pkg/price_filler/ks_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ type TokenCatalogResp struct {
}

type TokenCatalog struct {
Decimals int64 `json:"decimals"`
Decimals int64 `json:"decimals"`
Symbol string `json:"symbol"`
ChainId int16 `json:"chainId"`
}

func (c *KsClient) GetTokenCatalog(address string) (TokenCatalogResp, error) {
Expand Down Expand Up @@ -113,12 +115,19 @@ type ImportTokenParam struct {
Tokens []ImportedToken `json:"tokens"`
}

type TokenCatalogImportResp struct {
Decimals int64 `json:"decimals"`
ChainId string `json:"chainId"`
Address string `json:"address"`
Symbol string `json:"symbol"`
}

type ImportTokenResp struct {
Code int64 `json:"code"`
Message string `json:"message"`
Data struct {
Tokens []struct {
Data TokenCatalog `json:"data"`
Data TokenCatalogImportResp `json:"data"`
} `json:"tokens"`
} `json:"data"`
}
Expand Down
Loading

0 comments on commit 31131dc

Please sign in to comment.