CryptoAPI is a interface in Golang that aims to abstract the connection with cryptocurrency exchanges. The API supports both spot and derivative markets.
First you need to install the CryptoAPI in your project, run go get github.com/grinply/cryptoapi
to add the dependency.
Three main interfaces are provided with abstractions to access exchanges in a unified way. OrderConnector allows users to execute orders and access private information (such as asset balances):
package trade
type OrderConnector interface {
FindOrderByID(tradingPair CurrencyPair, orderID string) (Order, error)
OpenOrders(tradingPair CurrencyPair) ([]Order, error)
NewOpenOrder(orderToOpen Order) (string, error)
CancelOrder(tradingPair CurrencyPair, orderID string) error
CancelOpenOrders(tradingPair CurrencyPair) error
}
The PriceConnector provides access to price data without the need for authentication:
type PriceConnector interface {
LatestPrice(tradingPair CurrencyPair) (string, error)
Candles(tradingPair CurrencyPair, qty int, interval CandleInterval)
([]CandleStick, error)
PriceFeed(tradingPair CurrencyPair) <-chan CandleStick
}
The InfoConnector provides access to general exchange information about what is avaliable
type InfoConnector interface {
TradingPairs() ([]CurrencyPair, error)
TradingRules(tradingPair CurrencyPair) (Rule, error)
CoinsBalance() ([]Asset, error)
}
To make use of both connectors you just need to provide the required information to a function, a implementation for the requested exchange will be provided for your use:
package main
import (
"fmt"
"github.com/grinply/cryptoapi"
)
func main() {
//replace the keys with your own values.
var apiKey = "my_api_key"
var secretKey = "my_secret_key"
var exchange = "binance"
connector, err := cryptoapi.OrderConnector(exchange, apiKey, secretKey, false)
if err != nil {
fmt.Printf("Connector failed with the provided credentials.%v\n", err.Error())
return
}
//Print the amount of each asset present in the exchange wallet
if assetsBalance, err := connector.WalletBalances(); err == nil {
for _, asset := range assetsBalance {
fmt.Printf("%s - available: %s | locked: %s\n",
asset.Name, asset.FreeQty, asset.LockedQty)
}
}
}