Skip to content

Commit 9abc469

Browse files
committed
add default values, further refactor account-types vs account-ids
1 parent 9c51eca commit 9abc469

27 files changed

+504
-193
lines changed

client/account.go

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,17 @@
11
package client
22

33
import (
4-
"path/filepath"
5-
"strings"
6-
74
oc "github.com/cordialsys/offchain"
85
)
96

10-
// - accounts/funding
11-
// - accounts/trading
12-
// - accounts/x/subacounts/y
13-
14-
// - core/funding
15-
// - core/trading
16-
// - core/margin
17-
18-
type AccountName string
19-
type AccountType string
20-
21-
// var CoreFunding AccountName = "core/funding"
22-
// var CoreTrading AccountName = "core/trading"
23-
// var CoreMargin AccountName = "core/margin"
24-
25-
func (name AccountName) Id() string {
26-
if strings.HasPrefix(string(name), "core/") {
27-
return ""
28-
}
29-
return strings.TrimPrefix(string(name), "accounts/")
30-
}
31-
32-
func NewAccountName(id string) AccountName {
33-
return AccountName(filepath.Join("accounts", id))
34-
}
7+
type AccountId string
358

369
type AccountTransferArgs struct {
37-
from AccountName
38-
to AccountName
10+
from AccountId
11+
to AccountId
3912

40-
fromType AccountType
41-
toType AccountType
13+
fromType oc.AccountType
14+
toType oc.AccountType
4215

4316
symbol oc.SymbolId
4417
amount oc.Amount
@@ -55,21 +28,21 @@ func NewAccountTransferArgs(symbol oc.SymbolId, amount oc.Amount) AccountTransfe
5528
}
5629
}
5730

58-
func (args *AccountTransferArgs) SetFrom(from AccountName, fromType AccountType) {
31+
func (args *AccountTransferArgs) SetFrom(from AccountId, fromType oc.AccountType) {
5932
args.from = from
6033
args.fromType = fromType
6134
}
6235

63-
func (args *AccountTransferArgs) SetTo(to AccountName, toType AccountType) {
36+
func (args *AccountTransferArgs) SetTo(to AccountId, toType oc.AccountType) {
6437
args.to = to
6538
args.toType = toType
6639
}
6740

68-
func (args *AccountTransferArgs) GetFrom() (AccountName, AccountType) {
41+
func (args *AccountTransferArgs) GetFrom() (AccountId, oc.AccountType) {
6942
return args.from, args.fromType
7043
}
7144

72-
func (args *AccountTransferArgs) GetTo() (AccountName, AccountType) {
45+
func (args *AccountTransferArgs) GetTo() (AccountId, oc.AccountType) {
7346
return args.to, args.toType
7447
}
7548

client/address.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import (
55
)
66

77
type GetDepositAddressArgs struct {
8-
symbol oc.SymbolId
9-
network oc.NetworkId
10-
accountMaybe AccountName
8+
symbol oc.SymbolId
9+
network oc.NetworkId
10+
subaccount AccountId
1111
}
1212

1313
func NewGetDepositAddressArgs(coin oc.SymbolId, network oc.NetworkId, options ...GetDepositAddressOption) GetDepositAddressArgs {
@@ -30,19 +30,18 @@ func (args *GetDepositAddressArgs) GetNetwork() oc.NetworkId {
3030
return args.network
3131
}
3232

33-
func (args *GetDepositAddressArgs) GetAccount() (AccountName, bool) {
34-
return args.accountMaybe, args.accountMaybe != ""
35-
}
36-
37-
func (args *GetDepositAddressArgs) GetAccountId() (string, bool) {
38-
id := args.accountMaybe.Id()
39-
return id, id != ""
33+
func (args *GetDepositAddressArgs) GetSubaccount() (AccountId, bool) {
34+
return args.subaccount, args.subaccount != ""
4035
}
4136

4237
type GetDepositAddressOption func(args *GetDepositAddressArgs)
4338

44-
func WithAccount(account AccountName) GetDepositAddressOption {
39+
func WithSubaccount(subaccount AccountId) GetDepositAddressOption {
4540
return func(args *GetDepositAddressArgs) {
46-
args.accountMaybe = account
41+
args.subaccount = subaccount
4742
}
4843
}
44+
45+
func (args *GetDepositAddressArgs) SetSubaccount(subaccount AccountId) {
46+
args.subaccount = subaccount
47+
}

client/balance.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
package client
22

3+
import oc "github.com/cordialsys/offchain"
4+
35
type GetBalanceArgs struct {
4-
accountType AccountType
6+
accountType oc.AccountType
57
}
68

7-
func NewGetBalanceArgs(accountType AccountType) GetBalanceArgs {
9+
func NewGetBalanceArgs(accountType oc.AccountType) GetBalanceArgs {
810
return GetBalanceArgs{
911
accountType: accountType,
1012
}
1113
}
1214

13-
func (args *GetBalanceArgs) GetAccountType() AccountType {
15+
func (args *GetBalanceArgs) GetAccountType() oc.AccountType {
1416
return args.accountType
1517
}
18+
19+
func (args *GetBalanceArgs) SetAccountType(accountType oc.AccountType) {
20+
args.accountType = accountType
21+
}

cmd/oc/config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package main
33
import (
44
"fmt"
55

6-
"github.com/cordialsys/offchain/loader"
6+
oc "github.com/cordialsys/offchain"
77
"github.com/spf13/cobra"
88
"gopkg.in/yaml.v3"
99
)
@@ -15,7 +15,7 @@ func NewConfigCmd() *cobra.Command {
1515
Use: "config",
1616
Short: "Print out the current config",
1717
RunE: func(cmd *cobra.Command, args []string) error {
18-
config, err := loader.LoadConfig(configPath)
18+
config, err := oc.LoadConfig(configPath)
1919
if err != nil {
2020
return err
2121
}
@@ -32,7 +32,7 @@ func NewConfigCmd() *cobra.Command {
3232
"config",
3333
"c",
3434
"",
35-
fmt.Sprintf("path to the config file (may set %s)", loader.ENV_OFFCHAIN_CONFIG),
35+
fmt.Sprintf("path to the config file (may set %s)", oc.ENV_OFFCHAIN_CONFIG),
3636
)
3737
return cmd
3838
}

cmd/oc/exchange/account_transfer.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func NewAccountTransferCmd() *cobra.Command {
2323
Short: "Transfer funds between accounts on exchange",
2424
RunE: func(cmd *cobra.Command, args []string) error {
2525
exchangeConfig, secrets := unwrapAccountConfig(cmd.Context())
26-
cli, err := loader.NewClient(exchangeConfig.ExchangeId, &exchangeConfig.ExchangeClientConfig, secrets)
26+
cli, err := loader.NewClient(exchangeConfig, secrets)
2727
if err != nil {
2828
return err
2929
}
@@ -38,14 +38,21 @@ func NewAccountTransferCmd() *cobra.Command {
3838
return fmt.Errorf("--symbol is required")
3939
}
4040

41+
fromType, ok, message := exchangeConfig.ResolveAccountType(fromType)
42+
if !ok {
43+
return fmt.Errorf("%s", message)
44+
}
45+
toType, ok, message := exchangeConfig.ResolveAccountType(toType)
46+
if !ok {
47+
return fmt.Errorf("%s", message)
48+
}
4149
transferArgs := client.NewAccountTransferArgs(
42-
// client.AccountName(from),
43-
// client.AccountName(to),
4450
oc.SymbolId(symbol),
4551
amount,
4652
)
47-
transferArgs.SetFrom(client.AccountName(from), client.AccountType(fromType))
48-
transferArgs.SetTo(client.AccountName(to), client.AccountType(toType))
53+
54+
transferArgs.SetFrom(client.AccountId(from), fromType.Type)
55+
transferArgs.SetTo(client.AccountId(to), toType.Type)
4956

5057
resp, err := cli.CreateAccountTransfer(transferArgs)
5158
if err != nil {

cmd/oc/exchange/cmd.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
oc "github.com/cordialsys/offchain"
1010
"github.com/cordialsys/offchain/cmd"
11-
"github.com/cordialsys/offchain/loader"
1211
"github.com/spf13/cobra"
1312
)
1413

@@ -51,7 +50,7 @@ func NewExchangeCmd() *cobra.Command {
5150
SilenceUsage: true,
5251
PersistentPreRunE: func(preCmd *cobra.Command, args []string) error {
5352
cmd.SetVerbosityFromCmd(preCmd)
54-
config, err := loader.LoadConfig(configPath)
53+
config, err := oc.LoadConfig(configPath)
5554
if err != nil {
5655
return err
5756
}
@@ -103,7 +102,7 @@ func NewExchangeCmd() *cobra.Command {
103102
cmd.AddCommand(NewGetDepositAddressCmd())
104103
cmd.AddCommand(NewListWithdrawalHistoryCmd())
105104
cmd.AddCommand(NewListSubaccountsCmd())
106-
105+
cmd.AddCommand(NewListAccountTypesCmd())
107106
cmd.PersistentFlags().StringVarP(
108107
&exchange,
109108
"exchange",
@@ -125,7 +124,7 @@ func NewExchangeCmd() *cobra.Command {
125124
"config",
126125
"c",
127126
"",
128-
fmt.Sprintf("path to the config file (may set %s)", loader.ENV_OFFCHAIN_CONFIG),
127+
fmt.Sprintf("path to the config file (may set %s)", oc.ENV_OFFCHAIN_CONFIG),
129128
)
130129

131130
return cmd

cmd/oc/exchange/get_assets.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func NewGetAssetsCmd() *cobra.Command {
1212
Short: "list the asset symbols and networks of the exchange",
1313
RunE: func(cmd *cobra.Command, args []string) error {
1414
exchangeConfig, secrets := unwrapAccountConfig(cmd.Context())
15-
cli, err := loader.NewClient(exchangeConfig.ExchangeId, &exchangeConfig.ExchangeClientConfig, secrets)
15+
cli, err := loader.NewClient(exchangeConfig, secrets)
1616
if err != nil {
1717
return err
1818
}

cmd/oc/exchange/get_balances.go

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,46 @@
11
package exchange
22

33
import (
4+
"fmt"
5+
6+
oc "github.com/cordialsys/offchain"
47
"github.com/cordialsys/offchain/client"
58
"github.com/cordialsys/offchain/loader"
9+
"github.com/sirupsen/logrus"
610
"github.com/spf13/cobra"
711
)
812

913
func NewListBalancesCmd() *cobra.Command {
10-
var account string
14+
var accountType string
1115
cmd := &cobra.Command{
1216
SilenceUsage: true,
1317
Use: "balances",
1418
Short: "List your balances on the exchange",
1519
RunE: func(cmd *cobra.Command, args []string) error {
1620
exchangeConfig, secrets := unwrapAccountConfig(cmd.Context())
17-
cli, err := loader.NewClient(exchangeConfig.ExchangeId, &exchangeConfig.ExchangeClientConfig, secrets)
21+
cli, err := loader.NewClient(exchangeConfig, secrets)
1822
if err != nil {
1923
return err
2024
}
21-
accountType := client.AccountType("") //client.CoreFunding
22-
if account != "" {
23-
accountType = client.AccountType(account)
25+
balanceArgs := client.NewGetBalanceArgs("")
26+
if accountType != "" {
27+
at, ok, message := exchangeConfig.ResolveAccountType(accountType)
28+
if !ok {
29+
return fmt.Errorf("%s", message)
30+
}
31+
balanceArgs.SetAccountType(at.Type)
32+
} else {
33+
// try taking the first account type
34+
defaults, ok := oc.GetDefaultConfig(exchangeConfig.ExchangeId)
35+
if ok {
36+
if len(defaults.AccountTypes) > 0 {
37+
logrus.WithFields(logrus.Fields{
38+
"type": defaults.AccountTypes[0].Type,
39+
}).Infof("using default account type")
40+
balanceArgs.SetAccountType(defaults.AccountTypes[0].Type)
41+
}
42+
}
2443
}
25-
balanceArgs := client.NewGetBalanceArgs(accountType)
2644

2745
assets, err := cli.ListBalances(balanceArgs)
2846
if err != nil {
@@ -33,10 +51,10 @@ func NewListBalancesCmd() *cobra.Command {
3351
},
3452
}
3553
cmd.Flags().StringVar(
36-
&account,
37-
"account",
54+
&accountType,
55+
"type",
3856
"",
39-
"the account to get balances for",
57+
"the account type to get balances for",
4058
)
4159
return cmd
4260
}

cmd/oc/exchange/get_deposit_address.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ import (
1212
func NewGetDepositAddressCmd() *cobra.Command {
1313
var symbol string
1414
var network string
15-
var account string
15+
var subaccount string
1616
cmd := &cobra.Command{
1717
SilenceUsage: true,
1818
Use: "deposit",
1919
Short: "Get a deposit address for a symbol and network",
2020
RunE: func(cmd *cobra.Command, args []string) error {
2121
exchangeConfig, secrets := unwrapAccountConfig(cmd.Context())
22-
cli, err := loader.NewClient(exchangeConfig.ExchangeId, &exchangeConfig.ExchangeClientConfig, secrets)
22+
cli, err := loader.NewClient(exchangeConfig, secrets)
2323
if err != nil {
2424
return err
2525
}
@@ -31,8 +31,8 @@ func NewGetDepositAddressCmd() *cobra.Command {
3131
}
3232

3333
options := []client.GetDepositAddressOption{}
34-
if account != "" {
35-
options = append(options, client.WithAccount(client.AccountName(account)))
34+
if subaccount != "" {
35+
options = append(options, client.WithSubaccount(client.AccountId(subaccount)))
3636
}
3737

3838
resp, err := cli.GetDepositAddress(client.NewGetDepositAddressArgs(
@@ -50,6 +50,6 @@ func NewGetDepositAddressCmd() *cobra.Command {
5050
}
5151
cmd.Flags().StringVar(&symbol, "symbol", "", "The symbol to withdraw")
5252
cmd.Flags().StringVar(&network, "network", "", "The network to transact on")
53-
cmd.Flags().StringVar(&account, "account", "", "The account to get the deposit address for (optional)")
53+
cmd.Flags().StringVar(&subaccount, "for", "", "The subaccount to get the deposit address for, when using the main account to query (optional)")
5454
return cmd
5555
}

0 commit comments

Comments
 (0)