generated from KyberNetwork/go-project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/Add_usd_price_and_amount_to_tradelogs
- Loading branch information
Showing
12 changed files
with
454 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
cmd/tradelogs/migrations/00008_add_price_usd_column.up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
CREATE TYPE tradelog_states AS ENUM ('new', 'processed'); | ||
|
||
ALTER TABLE tradelogs | ||
ADD COLUMN maker_token_price FLOAT, | ||
ADD COLUMN taker_token_price FLOAT, | ||
ADD COLUMN maker_usd_amount FLOAT, | ||
ADD COLUMN taker_usd_amount FLOAT, | ||
ADD COLUMN state tradelog_states NOT NULL DEFAULT 'new'; | ||
|
||
CREATE INDEX tradelogs_state_idx ON tradelogs (state); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package pricefiller | ||
|
||
import ( | ||
"github.com/urfave/cli" | ||
) | ||
|
||
var BinanceAPIKeyFlag = cli.StringFlag{ | ||
Name: "binance-api-key", | ||
EnvVar: "BINANCE_API_KEY", | ||
} | ||
|
||
var BinanceSecretKeyFlag = cli.StringFlag{ | ||
Name: "binance-secret-key", | ||
EnvVar: "BINANCE_SECRET_KEY", | ||
} | ||
|
||
func PriceFillerFlags() []cli.Flag { | ||
return []cli.Flag{ | ||
BinanceAPIKeyFlag, | ||
BinanceSecretKeyFlag, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package pricefiller | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
const ksSettingUrl = "https://ks-setting.kyberswap.com/api/v1" | ||
|
||
type KsClient struct { | ||
client *http.Client | ||
baseURL string | ||
} | ||
|
||
func NewKsClient() *KsClient { | ||
return &KsClient{ | ||
client: &http.Client{}, | ||
baseURL: ksSettingUrl, | ||
} | ||
} | ||
|
||
func (c *KsClient) DoRequest(ctx context.Context, method, path string, jsonData interface{}, out interface{}) error { | ||
req, err := createRequest(ctx, method, path, jsonData) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
resp, err := c.client.Do(req) | ||
if err != nil { | ||
return err | ||
} | ||
defer resp.Body.Close() | ||
|
||
bb, err := readResponse(resp.Body, out) | ||
if err != nil { | ||
return fmt.Errorf("readResponse error: %w, data: %s", err, string(bb)) | ||
} | ||
if resp.StatusCode != http.StatusOK { | ||
return fmt.Errorf("server return %d - %v", resp.StatusCode, string(bb)) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func createRequest(ctx context.Context, method, url string, jsonData interface{}) (*http.Request, error) { | ||
var buf io.Reader | ||
if jsonData != nil { | ||
body, err := json.Marshal(jsonData) | ||
if err != nil { | ||
return nil, err | ||
} | ||
buf = bytes.NewBuffer(body) | ||
} | ||
req, err := http.NewRequestWithContext(ctx, method, url, buf) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if jsonData != nil { | ||
req.Header.Set("Content-Type", "application/json") | ||
} | ||
return req, nil | ||
} | ||
|
||
func readResponse(data io.Reader, dataField interface{}) ([]byte, error) { | ||
if dataField == nil { | ||
return nil, fmt.Errorf("nil data") | ||
} | ||
bb, err := io.ReadAll(data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return bb, json.Unmarshal(bb, dataField) | ||
} | ||
|
||
type TokenCatalogResp struct { | ||
Code int64 `json:"code"` | ||
Message string `json:"message"` | ||
Data struct { | ||
Tokens []TokenCatalog `json:"tokens"` | ||
} | ||
} | ||
|
||
type TokenCatalog struct { | ||
Decimals int64 `json:"decimals"` | ||
} | ||
|
||
func (c *KsClient) GetTokenCatalog(address string) (TokenCatalogResp, error) { | ||
var resp TokenCatalogResp | ||
err := c.DoRequest(context.Background(), http.MethodGet, | ||
fmt.Sprintf("%s/tokens?chainIds=%d&query=%s", c.baseURL, NetworkETHChanID, address), | ||
nil, &resp) | ||
if err != nil { | ||
return TokenCatalogResp{}, err | ||
} | ||
|
||
if resp.Code != 0 { | ||
return TokenCatalogResp{}, errors.New("unsuccess to get funding info") | ||
} | ||
|
||
return resp, nil | ||
} |
Oops, something went wrong.