forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 1
/
banking.go
115 lines (102 loc) · 2.77 KB
/
banking.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package banking
import (
"fmt"
"strings"
"github.com/idoall/gocryptotrader/common"
"github.com/idoall/gocryptotrader/currency"
)
// SetAccounts safely overwrites bank account slice
func SetAccounts(accs ...Account) {
m.Lock()
defer m.Unlock()
accounts = accs
}
// AppendAccounts safely adds to bank account slice
func AppendAccounts(accs ...Account) {
m.Lock()
defer m.Unlock()
accountRange:
for j := range accs {
for i := range accounts {
if accounts[i].AccountNumber == accs[j].AccountNumber {
continue accountRange
}
}
accounts = append(accounts, accs[j])
}
}
// GetBankAccountByID Returns a bank account based on its ID
func GetBankAccountByID(id string) (*Account, error) {
m.Lock()
defer m.Unlock()
for x := range accounts {
if strings.EqualFold(accounts[x].ID, id) {
return &accounts[x], nil
}
}
return nil, fmt.Errorf(ErrBankAccountNotFound, id)
}
// ExchangeSupported Checks if exchange is supported by bank account
func (b *Account) ExchangeSupported(exchange string) bool {
exchList := strings.Split(b.SupportedExchanges, ",")
return common.StringDataCompareInsensitive(exchList, exchange)
}
// Validate validates bank account settings
func (b *Account) Validate() error {
if b.BankName == "" ||
b.BankAddress == "" ||
b.BankPostalCode == "" ||
b.BankPostalCity == "" ||
b.BankCountry == "" ||
b.AccountName == "" ||
b.SupportedCurrencies == "" {
return fmt.Errorf(
"banking details for %s is enabled but variables not set correctly",
b.BankName)
}
if b.SupportedExchanges == "" {
b.SupportedExchanges = "ALL"
}
if strings.Contains(strings.ToUpper(
b.SupportedCurrencies),
currency.AUD.String()) {
if b.BSBNumber == "" {
return fmt.Errorf(
"banking details for %s is enabled but BSB/SWIFT values not set",
b.BankName)
}
} else {
if b.IBAN == "" && b.SWIFTCode == "" {
return fmt.Errorf(
"banking details for %s is enabled but SWIFT/IBAN values not set",
b.BankName)
}
}
return nil
}
// ValidateForWithdrawal confirms bank account meets minimum requirements to submit
// a withdrawal request
func (b *Account) ValidateForWithdrawal(exchange string, cur currency.Code) (err []string) {
if !b.Enabled {
err = append(err, ErrBankAccountDisabled)
}
if !b.ExchangeSupported(exchange) {
err = append(err, "Exchange "+exchange+" not supported by bank account")
}
if b.AccountNumber == "" {
err = append(err, ErrAccountCannotBeEmpty)
}
if !common.StringDataCompareInsensitive(strings.Split(b.SupportedCurrencies, ","), cur.String()) {
err = append(err, ErrCurrencyNotSupportedByAccount)
}
if cur.Upper() == currency.AUD {
if b.BSBNumber == "" {
err = append(err, ErrBSBRequiredForAUD)
}
} else {
if b.IBAN == "" && b.SWIFTCode == "" {
err = append(err, ErrIBANSwiftNotSet)
}
}
return
}