gocoin

package module
v0.2.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 21, 2022 License: GPL-3.0 Imports: 13 Imported by: 0

README

Go-Coin

A Go library which supports fetching your personal account information using the Coinbase API. The application supports the API key Authentication method.

Prerequisites

  1. Enable API Key access for your Coinbase account. Coinbase has over 160 Wallet types, it is recommended to assign access conservatively. The library functions returns the most comprehensive view when the following permissions are enabled:
  • wallet:accounts:read
  • wallet:deposits:read
  • wallet:trades:read
  • wallet:buys:read
  • wallet:checkouts:read
  • wallet:orders:read
  • wallet:transactions:read
  • wallet:withdrawals:read
  • wallet:sells:read

Installation

Get the latest version of go-coin library:

go get github.com/cdugga/go-coin

Functions

Wrapper functions include:

Function Parameters Description Return
GetAccounts Returns Coinbase accounts including Bitcoin, Bitcoin Cash, Litecoin, and Ethereum wallets, fiat currency, and vaults. []GenericAccount
GetAccountActivity []GenericAccount Return buy and sell orders []AccountActivity
GetPaginatedBuyOrders GenericAccount Returns buy orders associated with account ID's returned from GetAccounts AccountActivity
GetPaginatedSellOrders GenericAccount Returns sell orders associated with account ID's returned from GetAccounts AccountActivity
GetTransactions GenericAccount Returns transaction types including send, request,transfer,buy,sell,fiat_deposit,fiat_withdrawal,exchange_deposit,exchange_withdrawal,vault_withdrawal,advanced_trade_fill AccountActivity

Usage

Create Client

Create a new instance of BrokerService by passing in your Coinbase Api Key and Secret. Credentials should be kept secure at all times. godotenv or similar can be used to load credentials from a environment file.

Note: Do not store credentials in source control.

package main
import (
	gocoin "github.com/cdugga/go-coin"
	"github.com/joho/godotenv"
	"os"
)

func main(){
	godotenv.Load()
	
	apiKey := os.Getenv("API_KEY")
    apiSecret :=  os.Getenv("API_SECRET")
    
    svc := gocoin.NewBrokerService(apiKey, apiSecret)
}
 
Fetch Accounts

Returns your personal accounts. The list of returned accounts may vary depending on permissions assigned to API Key. Accounts represent Coinbase Wallets, Vault, and Fiat. Data fields returned include name, ID, currency, amount, and type.

package main

import (
	gocoin "github.com/cdugga/go-coin"
	"go.uber.org/zap"
	"os"
)

var logger *zap.Logger

func main(){
    apiKey := os.Getenv("API_KEY")
    apiSecret :=  os.Getenv("API_SECRET")
    
    svc := gocoin.NewBrokerService(apiKey, apiSecret)
    accounts, err := svc.Client.GetAccounts()
    if err != nil {
        logger.Error("Something went wrong ", zap.Error(err))
    }
	// do something with account IDs
}

Documentation

Index

Constants

View Source
const (
	GET_ACCOUNTS_PATH = "/v2/accounts"
	BASE_URL          = "https://api.coinbase.com"
)
View Source
const (
	BUY  int = 0
	SELL int = 1
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Account

type Account interface {
	// contains filtered or unexported methods
}

type AccountActivity

type AccountActivity struct {
	//Transactions []GenericTransaction
	Wallet       GenericAccount
	ActiveWallet bool
	BuyOrders    []GenericBuy
	Type         int
	SellOrders   []GenericSell
}

type AccountService

type AccountService struct {
}

type Accounts

type Accounts struct {
	Pagination Pagination `json:"pagination"`
	Data       []Data     `json:"data"`
}

type Balance

type Balance struct {
	Amount   string `json:"amount"`
	Currency string `json:"currency"`
}

type Broker

type Broker interface {
	GetAccounts() ([]GenericAccount, error)
	GetAccountActivity(ctx context.Context, acc []GenericAccount) (t []AccountActivity)
	GetTransactions(nextUrl string)
}

type BrokerImpl

type BrokerImpl struct {
	AccountService Account
}

func (*BrokerImpl) GetAccountActivity

func (b *BrokerImpl) GetAccountActivity(ctx context.Context, acc []GenericAccount) (t []AccountActivity)

func (*BrokerImpl) GetAccounts

func (b *BrokerImpl) GetAccounts() (uAcc []GenericAccount, err error)

func (*BrokerImpl) GetTransactions

func (b *BrokerImpl) GetTransactions(nextUrl string)

TODO

type BrokerService

type BrokerService struct {
	Client Broker
}

func NewBrokerService

func NewBrokerService(apiKey, apiSecret string) *BrokerService

type Buys

type Buys struct {
	Pagination struct {
		EndingBefore  interface{} `json:"ending_before"`
		StartingAfter interface{} `json:"starting_after"`
		Limit         int         `json:"limit"`
		Order         string      `json:"order"`
		PreviousURI   interface{} `json:"previous_uri"`
		NextURI       interface{} `json:"next_uri"`
	} `json:"pagination"`
	Data []struct {
		ID            string `json:"id"`
		Status        string `json:"status"`
		PaymentMethod struct {
			ID           string `json:"id"`
			Resource     string `json:"resource"`
			ResourcePath string `json:"resource_path"`
		} `json:"payment_method"`
		Transaction struct {
			ID           string `json:"id"`
			Resource     string `json:"resource"`
			ResourcePath string `json:"resource_path"`
		} `json:"transaction"`
		Amount struct {
			Amount   string `json:"amount"`
			Currency string `json:"currency"`
		} `json:"amount"`
		Total struct {
			Amount   string `json:"amount"`
			Currency string `json:"currency"`
		} `json:"total"`
		Subtotal struct {
			Amount   string `json:"amount"`
			Currency string `json:"currency"`
		} `json:"subtotal"`
		CreatedAt    string `json:"created_at"`
		UpdatedAt    string `json:"updated_at"`
		Resource     string `json:"resource"`
		ResourcePath string `json:"resource_path"`
		Committed    bool   `json:"committed"`
		Instant      bool   `json:"instant"`
		Fee          struct {
			Amount   string `json:"amount"`
			Currency string `json:"currency"`
		} `json:"fee"`
		PayoutAt string `json:"payout_at"`
	} `json:"data"`
}

type Client

type Client struct {
	Signer Signer
	Sender Dispatcher
}

func NewClient

func NewClient() *Client

func (*Client) NewRequest

func (c *Client) NewRequest(ctx context.Context, m Message, apikey, apiSecret string) ([]byte, error)

type Data

type Data struct {
	ID           string    `json:"id"`
	Name         string    `json:"name"`
	Primary      bool      `json:"primary"`
	Type         string    `json:"type"`
	Currency     string    `json:"currency"`
	Balance      Balance   `json:"balance"`
	CreatedAt    time.Time `json:"created_at"`
	UpdatedAt    time.Time `json:"updated_at"`
	Resource     string    `json:"resource"`
	ResourcePath string    `json:"resource_path"`
	Ready        bool      `json:"ready,omitempty"`
}

type Dispatcher

type Dispatcher interface {
	Sign(m Message, apiSecret string) string
	GetEpochTime() int64
}

type Environment

type Environment struct {
	APIKey    string
	APISecret string
}

type GenericAccount

type GenericAccount struct {
	Name     string
	ID       string
	Currency string
	Amount   string
	Type     string
	Primary  bool
}

type GenericBuy

type GenericBuy struct {
	ID            string
	Status        string
	BuyQuantity   string
	BoughtWith    string
	BuyCurrency   string
	BuyTotal      string
	Fee           float64
	FeeCurrency   string
	Resource      string
	Committed     string
	CreatedAt     string
	TransactionID string
}

type GenericSell

type GenericSell struct {
	ID            string
	Status        string
	SellQuantity  string
	SoldWith      string
	SellCurrency  string
	SellTotal     string
	Fee           float64
	FeeCurrency   string
	Resource      string
	Committed     string
	CreatedAt     string
	TransactionID string
}

type Message

type Message struct {
	Method string
	Path   string
	Body   string
	Secret string
	URL    string
	Epoch  int64
}

type Pagination

type Pagination struct {
	EndingBefore  interface{} `json:"ending_before"`
	StartingAfter interface{} `json:"starting_after"`
	Limit         int         `json:"limit"`
	Order         string      `json:"order"`
	PreviousURI   interface{} `json:"previous_uri"`
	NextURI       interface{} `json:"next_uri"`
}

type Req

type Req string

func (Req) GetEpochTime

func (r Req) GetEpochTime() int64

func (Req) Sign

func (r Req) Sign(m Message, apiSecret string) string

type Sells

type Sells struct {
	Pagination struct {
		EndingBefore  interface{} `json:"ending_before"`
		StartingAfter interface{} `json:"starting_after"`
		Limit         int         `json:"limit"`
		Order         string      `json:"order"`
		PreviousURI   interface{} `json:"previous_uri"`
		NextURI       interface{} `json:"next_uri"`
	} `json:"pagination"`
	Data []struct {
		ID            string `json:"id"`
		Status        string `json:"status"`
		PaymentMethod struct {
			ID           string `json:"id"`
			Resource     string `json:"resource"`
			ResourcePath string `json:"resource_path"`
		} `json:"payment_method"`
		Transaction struct {
			ID           string `json:"id"`
			Resource     string `json:"resource"`
			ResourcePath string `json:"resource_path"`
		} `json:"transaction"`
		Amount struct {
			Amount   string `json:"amount"`
			Currency string `json:"currency"`
		} `json:"amount"`
		Total struct {
			Amount   string `json:"amount"`
			Currency string `json:"currency"`
		} `json:"total"`
		Subtotal struct {
			Amount   string `json:"amount"`
			Currency string `json:"currency"`
		} `json:"subtotal"`
		CreatedAt    string `json:"created_at"`
		UpdatedAt    string `json:"updated_at"`
		Resource     string `json:"resource"`
		ResourcePath string `json:"resource_path"`
		Committed    bool   `json:"committed"`
		Instant      bool   `json:"instant"`
		Fee          struct {
			Amount   string `json:"amount"`
			Currency string `json:"currency"`
		} `json:"fee"`
		PayoutAt string `json:"payout_at"`
	} `json:"data"`
}

type Sig

type Sig string

func (Sig) GenerateHMAC

func (s Sig) GenerateHMAC(data string, secret string) string

Hash-based message authentication code

func (Sig) GenerateSignature

func (s Sig) GenerateSignature(m Message) string

type Signer

type Signer interface {
	GenerateHMAC(data string, secret string) string
	GenerateSignature(message Message) string
}

type Task

type Task struct {
	Err     error
	ExecFn  func(ctx context.Context, acc GenericAccount) (AccountActivity, error)
	Account GenericAccount
}

func NewTask

func NewTask(f func(ctx context.Context, acc GenericAccount) (AccountActivity, error), account GenericAccount) *Task

func (*Task) Run

type WorkerPool

type WorkerPool struct {
	Done chan struct{}
	// contains filtered or unexported fields
}

func NewWorkerPool

func NewWorkerPool(concurrency int) WorkerPool

func (WorkerPool) GenerateFrom

func (wp WorkerPool) GenerateFrom(tasks []*Task)

func (WorkerPool) ResultSet

func (wp WorkerPool) ResultSet() <-chan AccountActivity

func (*WorkerPool) Run

func (wp *WorkerPool) Run(ctx context.Context)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL