Files
gocryptotrader/portfolio/portfolio.go
Andrew b26ec86d43 Withdraw additional functionality (validation/submission/tracking) (#409)
* reworked request struct and exchange response started work on validation system

* removed import cycle until work around

* Added intial withdraw support via CLI added

* Added Crypto command to gctcli

* moved var declartion to single line

* Test updates for binance and anx

* All exchange tests have been updated test coverage added to validate

* First pass at adding withdrawl select from database

* started adding basic lru cache system

* Added basic LRU cache including Add Get Remove Contains ContainsOrAdd Clear

* wording changes on comments

* removed exported var's in strut as they are not required

* Added README

* README updates

* corrected ID on commands

* rm line :D

* merged in origin/cache

* linter fixes (gofmt)

* Added basic cache lookup to events

* swapped to mutex over rwmutex updated comments

* unexported getNewest & getOldest

* unexported getNewest & getOldest

* Updated comments and cited references in source

* updated comments

* WIP

* Migrated exchange WithdrawFiat wrapper to new struct response

* Migrated exchange WithdrawFiat wrapper to new struct response

* started work on bank management

* Added exchange level banking details back with migration to banking package

* Removed broken tests for now

* Added validation to bank accounts

* removed duplicate bank details from withdraw struct

* Test coverage increased

* gofmt

* merged upstream/master with clean up

* First pass at adding command line linking to gctcli

* added validation for crypto address, added gctcli support to retreive previous withdrawal requests

* general cleanup

* general cleanup

* reordered imports

* Increased test coverage moved to database sublogger

* Pass incorrect currency no longer return error from c.CheckBankAccountConfig

* remove TestMain() for now as other tests in this package will need to be reworked

* Happy little race car

* Reverted to upstream tests

* Added test covarege for validation method, corrected response on cli protobuf

* query clean up and removal of duplicated code

* cleaned up queries into singlem ethod increased test coverage

* Migrated international fund withdraw to new exchange response and added cache size override

* Migrated international fund withdraw to new exchange response and added cache size override

* Extended gctcli commands

* lowered default cache to 25

* small code clean up

* added get by date method

* add returned error

* cli commands cleaing return error on nil results to fix out of bounds

* merged write & read helpers into one for test coverage and increased engine/withdraw test coverage

* gofmt

* Added test coverage for valid ID

* removed unused param

* converted to use timestamp package from protobuf

* gofmt

* use built in RFC3339 timestamp

* remove setting of CreatedAt & UpdatedAt and allow ORm to take care of it

* also use ptype on byid

* code flow improvements

* remove inverse conditional check and linters run

* removed test data

* removed comment

* removed comment

* also write failures to database for auditing

* converted to use default time for start & end

* Default to time.Now() minus 30 days

* Default to time.Now() minus 30 days

* small code clean up

* fixed missing semicolon on migrations, code clean up

* updated sqlite migrations

* Added additonal check for exchange level bank account if global is not found

* case sensativity fix for currency names

* use correct compare

* test coverage fixed

* removed space

* return pointer to banking.Account

* return pointer to banking.Account

* added else check back to validate()

* Added empty string as default to migration over NULL due to retrivial of data
2020-02-26 15:45:13 +11:00

481 lines
13 KiB
Go

package portfolio
import (
"errors"
"fmt"
"strings"
"time"
"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/log"
)
const (
cryptoIDAPIURL = "https://chainz.cryptoid.info"
ethplorerAPIURL = "https://api.ethplorer.io"
ethplorerAddressInfo = "getAddressInfo"
// PortfolioAddressExchange is a label for an exchange address
PortfolioAddressExchange = "Exchange"
// PortfolioAddressPersonal is a label for a personal/offline address
PortfolioAddressPersonal = "Personal"
)
// Portfolio is variable store holding an array of portfolioAddress
var Portfolio Base
// Verbose allows for debug output when sending an http request
var Verbose bool
// GetEthereumBalance single or multiple address information as
// EtherchainBalanceResponse
func GetEthereumBalance(address string) (EthplorerResponse, error) {
valid, _ := common.IsValidCryptoAddress(address, "eth")
if !valid {
return EthplorerResponse{}, errors.New("not an Ethereum address")
}
urlPath := fmt.Sprintf(
"%s/%s/%s?apiKey=freekey", ethplorerAPIURL, ethplorerAddressInfo, address,
)
result := EthplorerResponse{}
return result, common.SendHTTPGetRequest(urlPath, true, Verbose, &result)
}
// GetCryptoIDAddress queries CryptoID for an address balance for a
// specified cryptocurrency
func GetCryptoIDAddress(address string, coinType currency.Code) (float64, error) {
ok, err := common.IsValidCryptoAddress(address, coinType.String())
if !ok || err != nil {
return 0, errors.New("invalid address")
}
var result interface{}
url := fmt.Sprintf("%s/%s/api.dws?q=getbalance&a=%s",
cryptoIDAPIURL,
coinType.Lower(),
address)
err = common.SendHTTPGetRequest(url, true, Verbose, &result)
if err != nil {
return 0, err
}
return result.(float64), nil
}
// GetAddressBalance acceses the portfolio base and returns the balance by passed
// in address, coin type and description
func (p *Base) GetAddressBalance(address, description string, coinType currency.Code) (float64, bool) {
for x := range p.Addresses {
if p.Addresses[x].Address == address &&
p.Addresses[x].Description == description &&
p.Addresses[x].CoinType == coinType {
return p.Addresses[x].Balance, true
}
}
return 0, false
}
// ExchangeExists checks to see if an exchange exists in the portfolio base
func (p *Base) ExchangeExists(exchangeName string) bool {
for x := range p.Addresses {
if p.Addresses[x].Address == exchangeName {
return true
}
}
return false
}
// AddressExists checks to see if there is an address associated with the
// portfolio base
func (p *Base) AddressExists(address string) bool {
for x := range p.Addresses {
if p.Addresses[x].Address == address {
return true
}
}
return false
}
// ExchangeAddressExists checks to see if there is an exchange address
// associated with the portfolio base
func (p *Base) ExchangeAddressExists(exchangeName string, coinType currency.Code) bool {
for x := range p.Addresses {
if p.Addresses[x].Address == exchangeName && p.Addresses[x].CoinType == coinType {
return true
}
}
return false
}
// AddExchangeAddress adds an exchange address to the portfolio base
func (p *Base) AddExchangeAddress(exchangeName string, coinType currency.Code, balance float64) {
if p.ExchangeAddressExists(exchangeName, coinType) {
p.UpdateExchangeAddressBalance(exchangeName, coinType, balance)
} else {
p.Addresses = append(
p.Addresses, Address{Address: exchangeName, CoinType: coinType,
Balance: balance, Description: PortfolioAddressExchange},
)
}
}
// UpdateAddressBalance updates the portfolio base balance
func (p *Base) UpdateAddressBalance(address string, amount float64) {
for x := range p.Addresses {
if p.Addresses[x].Address == address {
p.Addresses[x].Balance = amount
}
}
}
// RemoveExchangeAddress removes an exchange address from the portfolio.
func (p *Base) RemoveExchangeAddress(exchangeName string, coinType currency.Code) {
for x := range p.Addresses {
if p.Addresses[x].Address == exchangeName && p.Addresses[x].CoinType == coinType {
p.Addresses = append(p.Addresses[:x], p.Addresses[x+1:]...)
return
}
}
}
// UpdateExchangeAddressBalance updates the portfolio balance when checked
// against correct exchangeName and coinType.
func (p *Base) UpdateExchangeAddressBalance(exchangeName string, coinType currency.Code, balance float64) {
for x := range p.Addresses {
if p.Addresses[x].Address == exchangeName && p.Addresses[x].CoinType == coinType {
p.Addresses[x].Balance = balance
}
}
}
// AddAddress adds an address to the portfolio base
func (p *Base) AddAddress(address, description string, coinType currency.Code, balance float64) error {
if address == "" {
return errors.New("address is empty")
}
if coinType.String() == "" {
return errors.New("coin type is empty")
}
if description == PortfolioAddressExchange {
p.AddExchangeAddress(address, coinType, balance)
}
if !p.AddressExists(address) {
p.Addresses = append(
p.Addresses, Address{Address: address, CoinType: coinType,
Balance: balance, Description: description},
)
} else {
if balance <= 0 {
p.RemoveAddress(address, description, coinType)
} else {
p.UpdateAddressBalance(address, balance)
}
}
return nil
}
// RemoveAddress removes an address when checked against the correct address and
// coinType
func (p *Base) RemoveAddress(address, description string, coinType currency.Code) error {
if address == "" {
return errors.New("address is empty")
}
if coinType.String() == "" {
return errors.New("coin type is empty")
}
for x := range p.Addresses {
if p.Addresses[x].Address == address &&
p.Addresses[x].CoinType == coinType &&
p.Addresses[x].Description == description {
p.Addresses = append(p.Addresses[:x], p.Addresses[x+1:]...)
return nil
}
}
return errors.New("portfolio item does not exist")
}
// UpdatePortfolio adds to the portfolio addresses by coin type
func (p *Base) UpdatePortfolio(addresses []string, coinType currency.Code) error {
if strings.Contains(strings.Join(addresses, ","), PortfolioAddressExchange) ||
strings.Contains(strings.Join(addresses, ","), PortfolioAddressPersonal) {
return nil
}
if coinType == currency.ETH {
for x := range addresses {
result, err := GetEthereumBalance(addresses[x])
if err != nil {
return err
}
if result.Error.Message != "" {
return errors.New(result.Error.Message)
}
p.AddAddress(addresses[x],
PortfolioAddressPersonal,
coinType,
result.ETH.Balance)
}
}
for x := range addresses {
result, err := GetCryptoIDAddress(addresses[x], coinType)
if err != nil {
return err
}
p.AddAddress(addresses[x],
PortfolioAddressPersonal,
coinType,
result)
}
return nil
}
// GetPortfolioByExchange returns currency portfolio amount by exchange
func (p *Base) GetPortfolioByExchange(exchangeName string) map[currency.Code]float64 {
result := make(map[currency.Code]float64)
for x := range p.Addresses {
if strings.Contains(p.Addresses[x].Address, exchangeName) {
result[p.Addresses[x].CoinType] = p.Addresses[x].Balance
}
}
return result
}
// GetExchangePortfolio returns current portfolio base information
func (p *Base) GetExchangePortfolio() map[currency.Code]float64 {
result := make(map[currency.Code]float64)
for _, x := range p.Addresses {
if x.Description != PortfolioAddressExchange {
continue
}
balance, ok := result[x.CoinType]
if !ok {
result[x.CoinType] = x.Balance
} else {
result[x.CoinType] = x.Balance + balance
}
}
return result
}
// GetPersonalPortfolio returns current portfolio base information
func (p *Base) GetPersonalPortfolio() map[currency.Code]float64 {
result := make(map[currency.Code]float64)
for _, x := range p.Addresses {
if x.Description == PortfolioAddressExchange {
continue
}
balance, ok := result[x.CoinType]
if !ok {
result[x.CoinType] = x.Balance
} else {
result[x.CoinType] = x.Balance + balance
}
}
return result
}
// getPercentage returns the percentage of the target coin amount against the
// total coin amount.
func getPercentage(input map[currency.Code]float64, target currency.Code, totals map[currency.Code]float64) float64 {
subtotal := input[target]
total := totals[target]
percentage := (subtotal / total) * 100 / 1
return percentage
}
// getPercentageSpecific returns the percentage a specific value of a target coin amount
// against the total coin amount.
func getPercentageSpecific(input float64, target currency.Code, totals map[currency.Code]float64) float64 {
total := totals[target]
percentage := (input / total) * 100 / 1
return percentage
}
// GetPortfolioSummary returns the complete portfolio summary, showing
// coin totals, offline and online summaries with their relative percentages.
func (p *Base) GetPortfolioSummary() Summary {
personalHoldings := p.GetPersonalPortfolio()
exchangeHoldings := p.GetExchangePortfolio()
totalCoins := make(map[currency.Code]float64)
for x, y := range personalHoldings {
totalCoins[x] = y
}
for x, y := range exchangeHoldings {
balance, ok := totalCoins[x]
if !ok {
totalCoins[x] = y
} else {
totalCoins[x] = y + balance
}
}
var portfolioOutput Summary
for x, y := range totalCoins {
coins := Coin{Coin: x, Balance: y}
portfolioOutput.Totals = append(portfolioOutput.Totals, coins)
}
for x, y := range personalHoldings {
coins := Coin{
Coin: x,
Balance: y,
Percentage: getPercentage(personalHoldings, x, totalCoins),
}
portfolioOutput.Offline = append(portfolioOutput.Offline, coins)
}
for x, y := range exchangeHoldings {
coins := Coin{
Coin: x,
Balance: y,
Percentage: getPercentage(exchangeHoldings, x, totalCoins),
}
portfolioOutput.Online = append(portfolioOutput.Online, coins)
}
var portfolioExchanges []string
for _, x := range p.Addresses {
if x.Description == PortfolioAddressExchange {
if !common.StringDataCompare(portfolioExchanges, x.Address) {
portfolioExchanges = append(portfolioExchanges, x.Address)
}
}
}
exchangeSummary := make(map[string]map[currency.Code]OnlineCoinSummary)
for x := range portfolioExchanges {
exchgName := portfolioExchanges[x]
result := p.GetPortfolioByExchange(exchgName)
coinSummary := make(map[currency.Code]OnlineCoinSummary)
for y, z := range result {
coinSum := OnlineCoinSummary{
Balance: z,
Percentage: getPercentageSpecific(z, y, totalCoins),
}
coinSummary[y] = coinSum
}
exchangeSummary[exchgName] = coinSummary
}
portfolioOutput.OnlineSummary = exchangeSummary
offlineSummary := make(map[currency.Code][]OfflineCoinSummary)
for _, x := range p.Addresses {
if x.Description != PortfolioAddressExchange {
coinSummary := OfflineCoinSummary{
Address: x.Address,
Balance: x.Balance,
Percentage: getPercentageSpecific(x.Balance, x.CoinType,
totalCoins),
}
result, ok := offlineSummary[x.CoinType]
if !ok {
offlineSummary[x.CoinType] = append(offlineSummary[x.CoinType],
coinSummary)
} else {
result = append(result, coinSummary)
offlineSummary[x.CoinType] = result
}
}
}
portfolioOutput.OfflineSummary = offlineSummary
return portfolioOutput
}
// GetPortfolioGroupedCoin returns portfolio base information grouped by coin
func (p *Base) GetPortfolioGroupedCoin() map[currency.Code][]string {
result := make(map[currency.Code][]string)
for _, x := range p.Addresses {
if strings.Contains(x.Description, PortfolioAddressExchange) {
continue
}
result[x.CoinType] = append(result[x.CoinType], x.Address)
}
return result
}
// Seed appends a portfolio base object with another base portfolio
// addresses
func (p *Base) Seed(port Base) {
p.Addresses = port.Addresses
}
// StartPortfolioWatcher observes the portfolio object
func StartPortfolioWatcher() {
addrCount := len(Portfolio.Addresses)
log.Debugf(log.PortfolioMgr,
"PortfolioWatcher started: Have %d entries in portfolio.\n", addrCount,
)
for {
data := Portfolio.GetPortfolioGroupedCoin()
for key, value := range data {
err := Portfolio.UpdatePortfolio(value, key)
if err != nil {
log.Errorf(log.PortfolioMgr,
"PortfolioWatcher error %s for currency %s\n",
err,
key)
continue
}
log.Debugf(log.PortfolioMgr,
"PortfolioWatcher: Successfully updated address balance for %s address(es) %s\n",
key,
value)
}
time.Sleep(time.Minute * 10)
}
}
// GetPortfolio returns a pointer to the portfolio base
func GetPortfolio() *Base {
return &Portfolio
}
// IsExchangeSupported checks if exchange is supported by portfolio address
func IsExchangeSupported(exchange, address string) (ret bool) {
for x := range Portfolio.Addresses {
if Portfolio.Addresses[x].Address != address {
continue
}
exchangeList := strings.Split(Portfolio.Addresses[x].SupportedExchanges, ",")
return common.StringDataContainsInsensitive(exchangeList, exchange)
}
return
}
// IsColdStorage checks if address is a cold storage wallet
func IsColdStorage(address string) (ret bool) {
for x := range Portfolio.Addresses {
if Portfolio.Addresses[x].Address != address {
continue
}
return Portfolio.Addresses[x].ColdStorage
}
return
}
// IsWhiteListed checks if address is whitelisted for withdraw transfers
func IsWhiteListed(address string) (ret bool) {
for x := range Portfolio.Addresses {
if Portfolio.Addresses[x].Address != address {
continue
}
return Portfolio.Addresses[x].WhiteListed
}
return
}