-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
3 changed files
with
137 additions
and
0 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
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 | ||
} |
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,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"` | ||
} | ||
) |
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