From 69995584ddf5b2275f78f3b942a280b3685c4dee Mon Sep 17 00:00:00 2001 From: Maksim Rasputin <133312+madcake@users.noreply.github.com> Date: Tue, 15 Dec 2020 02:57:42 +0700 Subject: [PATCH] Add binance client (#13) * Add binance client * Add binance client * Improve client and clean code * Improve client and clean code * Add sugar Clean code * Add contract address * Update client.go * Update client.go * Update client.go Co-authored-by: Viktor Radchenko <1641795+vikmeup@users.noreply.github.com> --- blockchain/binance/client.go | 51 ++++++++++++++++++++++ blockchain/binance/model.go | 82 ++++++++++++++++++++++++++++++++++++ client/client.go | 4 ++ 3 files changed, 137 insertions(+) create mode 100644 blockchain/binance/client.go create mode 100644 blockchain/binance/model.go diff --git a/blockchain/binance/client.go b/blockchain/binance/client.go new file mode 100644 index 0000000..5cc327e --- /dev/null +++ b/blockchain/binance/client.go @@ -0,0 +1,51 @@ +package binance + +import ( + "fmt" + "net/url" + "strconv" + "time" + + "github.com/trustwallet/golibs/client" +) + +type Client struct { + req client.Request +} + +func InitClient(url, apiKey string) Client { + request := client.InitJSONClient(url) + request.AddHeader("apikey", apiKey) + return Client{ + req: request, + } +} + +func (c Client) FetchNodeInfo() (result NodeInfoResponse, err error) { + err = c.req.Get(&result, "api/v1/node-info", nil) + return result, err +} + +func (c Client) FetchTransactionsInBlock(blockNumber int64) (result TransactionsInBlockResponse, err error) { + err = c.req.Get(&result, fmt.Sprintf("api/v2/transactions-in-block/%d", blockNumber), nil) + return result, err +} + +func (c Client) FetchTransactionsByAddressAndTokenID(address, tokenID string, limit int) ([]Tx, error) { + startTime := strconv.Itoa(int(time.Now().AddDate(0, -3, 0).Unix() * 1000)) + params := url.Values{"address": {address}, "txAsset": {tokenID}, "startTime": {startTime}, "limit": {strconv.Itoa(limit)}} + var result TransactionsInBlockResponse + err := c.req.Get(&result, "api/v1/transactions", params) + return result.Tx, err +} + +func (c Client) FetchAccountMeta(address string) (result AccountMeta, err error) { + err = c.req.Get(&result, fmt.Sprintf("api/v1/account/%s", address), nil) + return result, err +} + +func (c Client) FetchTokens(limit int) (result Tokens, err error) { + query := url.Values{"limit": {strconv.Itoa(limit)}} + err = c.req.Get(&result, "api/v1/tokens", query) + return result, err +} diff --git a/blockchain/binance/model.go b/blockchain/binance/model.go new file mode 100644 index 0000000..46f9eca --- /dev/null +++ b/blockchain/binance/model.go @@ -0,0 +1,82 @@ +package binance + +import "time" + +type ( + NodeInfoResponse struct { + SyncInfo struct { + LatestBlockHeight int `json:"latest_block_height"` + } `json:"sync_info"` + } + + TransactionsInBlockResponse struct { + BlockHeight int `json:"blockHeight"` + Tx []Tx `json:"tx"` + } + + TxType string + + Tx struct { + TxHash string `json:"txHash"` + BlockHeight int `json:"blockHeight"` + TxType TxType `json:"txType"` + TimeStamp time.Time `json:"timeStamp"` + FromAddr interface{} `json:"fromAddr"` + ToAddr interface{} `json:"toAddr"` + Value string `json:"value"` + TxAsset string `json:"txAsset"` + TxFee string `json:"txFee"` + OrderID string `json:"orderId,omitempty"` + Code int `json:"code"` + Data string `json:"data"` + Memo string `json:"memo"` + Source int `json:"source"` + SubTransactions []SubTransactions `json:"subTransactions,omitempty"` + Sequence int `json:"sequence"` + } + + TransactionData struct { + OrderData struct { + Symbol string `json:"symbol"` + OrderType string `json:"orderType"` + Side string `json:"side"` + Price string `json:"price"` + Quantity string `json:"quantity"` + TimeInForce string `json:"timeInForce"` + OrderID string `json:"orderId"` + } `json:"orderData"` + } + + SubTransactions struct { + TxHash string `json:"txHash"` + BlockHeight int `json:"blockHeight"` + TxType string `json:"txType"` + FromAddr string `json:"fromAddr"` + ToAddr string `json:"toAddr"` + TxAsset string `json:"txAsset"` + TxFee string `json:"txFee"` + Value string `json:"value"` + } + + AccountMeta struct { + Balances []TokenBalance `json:"balances"` + } + + TokenBalance struct { + Free string `json:"free"` + Frozen string `json:"frozen"` + Locked string `json:"locked"` + Symbol string `json:"symbol"` + } + + Tokens []Token + + Token struct { + ContractAddress string `json:"contract_address"` + Name string `json:"name"` + OriginalSymbol string `json:"original_symbol"` + Owner string `json:"owner"` + Symbol string `json:"symbol"` + TotalSupply string `json:"total_supply"` + } +) diff --git a/client/client.go b/client/client.go index 2debcf6..186b2ef 100644 --- a/client/client.go +++ b/client/client.go @@ -54,6 +54,10 @@ var DefaultErrorHandler = func(res *http.Response, uri string) error { return nil } +func (r *Request) AddHeader(key string, value string) { + r.Headers[key] = value +} + func (r *Request) GetWithContext(result interface{}, path string, query url.Values, ctx context.Context) error { var queryStr = "" if query != nil {