Skip to content

Commit

Permalink
Add binance client (#13)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
madcake and vikmeup authored Dec 14, 2020
1 parent 38bb4b8 commit 6999558
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 0 deletions.
51 changes: 51 additions & 0 deletions blockchain/binance/client.go
Original file line number Diff line number Diff line change
@@ -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
}
82 changes: 82 additions & 0 deletions blockchain/binance/model.go
Original file line number Diff line number Diff line change
@@ -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"`
}
)
4 changes: 4 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 6999558

Please sign in to comment.