Skip to content

Commit

Permalink
add pools to fctl
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-nicolas committed Dec 21, 2023
1 parent d496da6 commit 7c64d67
Show file tree
Hide file tree
Showing 11 changed files with 898 additions and 4 deletions.
54 changes: 51 additions & 3 deletions components/fctl/cmd/payments/accounts/balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ type ListBalancesStore struct {

type ListBalancesController struct {
store *ListBalancesStore

cursorFlag string
pageSizeFlag string
}

var _ fctl.Controller[*ListBalancesStore] = (*ListBalancesController)(nil)
Expand All @@ -30,6 +33,9 @@ func NewListBalanceStore() *ListBalancesStore {
func NewListBalancesController() *ListBalancesController {
return &ListBalancesController{
store: NewListBalanceStore(),

cursorFlag: "cursor",
pageSizeFlag: "page-size",
}
}

Expand Down Expand Up @@ -58,9 +64,21 @@ func (c *ListBalancesController) Run(cmd *cobra.Command, args []string) (fctl.Re
return nil, err
}

var cursor *string
if c := fctl.GetString(cmd, c.cursorFlag); c != "" {
cursor = &c
}

var pageSize *int64
if ps := fctl.GetInt(cmd, c.pageSizeFlag); ps > 0 {
pageSize = fctl.Ptr(int64(ps))
}

response, err := client.Payments.GetAccountBalances(
cmd.Context(),
operations.GetAccountBalancesRequest{
Cursor: cursor,
PageSize: pageSize,
AccountID: args[0],
},
)
Expand Down Expand Up @@ -89,17 +107,47 @@ func (c *ListBalancesController) Render(cmd *cobra.Command, args []string) error
})
tableData = fctl.Prepend(tableData, []string{"ID", "Asset", "Balance",
"CreatedAt", "LastUpdatedAt"})
return pterm.DefaultTable.
if err := pterm.DefaultTable.
WithHasHeader().
WithWriter(cmd.OutOrStdout()).
WithData(tableData).
Render()
Render(); err != nil {
return err
}

tableData = pterm.TableData{}
tableData = append(tableData, []string{pterm.LightCyan("HasMore"), fmt.Sprintf("%v", c.store.Cursor.HasMore)})
tableData = append(tableData, []string{pterm.LightCyan("PageSize"), fmt.Sprintf("%d", c.store.Cursor.PageSize)})
tableData = append(tableData, []string{pterm.LightCyan("Next"), func() string {
if c.store.Cursor.Next == nil {
return ""
}
return *c.store.Cursor.Next
}()})
tableData = append(tableData, []string{pterm.LightCyan("Previous"), func() string {
if c.store.Cursor.Previous == nil {
return ""
}
return *c.store.Cursor.Previous
}()})

if err := pterm.DefaultTable.
WithWriter(cmd.OutOrStdout()).
WithData(tableData).
Render(); err != nil {
return err
}

return nil
}

func NewListBalanceCommand() *cobra.Command {
c := NewListBalancesController()
return fctl.NewCommand("balances <accountID>",
fctl.WithArgs(cobra.ExactArgs(1)),
fctl.WithShortDescription("List accounts balances"),
fctl.WithController[*ListBalancesStore](NewListBalancesController()),
fctl.WithStringFlag(c.cursorFlag, "", "Cursor"),
fctl.WithIntFlag(c.pageSizeFlag, 0, "PageSize"),
fctl.WithController[*ListBalancesStore](c),
)
}
2 changes: 1 addition & 1 deletion components/fctl/cmd/payments/bankaccounts/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (c *CreateController) Run(cmd *cobra.Command, args []string) (fctl.Renderab
}

func (c *CreateController) Render(cmd *cobra.Command, args []string) error {
pterm.Success.WithWriter(cmd.OutOrStdout()).Printfln("Bank Accounts created with ID: %s", c.store.BankAccountID)
pterm.Success.WithWriter(cmd.OutOrStdout()).Printfln("Bank Account created with ID: %s", c.store.BankAccountID)

return nil
}
108 changes: 108 additions & 0 deletions components/fctl/cmd/payments/pools/add_accounts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package pools

import (
"fmt"

"github.com/formancehq/fctl/cmd/payments/versions"
fctl "github.com/formancehq/fctl/pkg"
"github.com/formancehq/formance-sdk-go/pkg/models/operations"
"github.com/formancehq/formance-sdk-go/pkg/models/shared"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)

type AddAccountStore struct {
PoolID string `json:"poolID"`
AccountID string `json:"accountID"`
Success bool `json:"success"`
}
type AddAccountController struct {
PaymentsVersion versions.Version

store *AddAccountStore
}

func (c *AddAccountController) SetVersion(version versions.Version) {
c.PaymentsVersion = version
}

var _ fctl.Controller[*AddAccountStore] = (*AddAccountController)(nil)

func NewAddAccountStore() *AddAccountStore {
return &AddAccountStore{}
}

func NewAddAccountController() *AddAccountController {
return &AddAccountController{
store: NewAddAccountStore(),
}
}

func NewAddAccountCommand() *cobra.Command {
c := NewAddAccountController()
return fctl.NewCommand("add-account <poolID> <accountID>",
fctl.WithShortDescription("Add account to pool"),
fctl.WithArgs(cobra.ExactArgs(2)),
fctl.WithAliases("add", "a"),
fctl.WithController[*AddAccountStore](c),
)
}

func (c *AddAccountController) GetStore() *AddAccountStore {
return c.store
}

func (c *AddAccountController) Run(cmd *cobra.Command, args []string) (fctl.Renderable, error) {
if err := versions.GetPaymentsVersion(cmd, args, c); err != nil {
return nil, err
}

if c.PaymentsVersion < versions.V1 {
return nil, fmt.Errorf("pools are only supported in >= v1.0.0")
}

cfg, err := fctl.GetConfig(cmd)
if err != nil {
return nil, err
}

organizationID, err := fctl.ResolveOrganizationID(cmd, cfg)
if err != nil {
return nil, err
}

stack, err := fctl.ResolveStack(cmd, cfg, organizationID)
if err != nil {
return nil, err
}

ledgerClient, err := fctl.NewStackClient(cmd, cfg, stack)
if err != nil {
return nil, err
}

response, err := ledgerClient.Payments.AddAccountToPool(cmd.Context(), operations.AddAccountToPoolRequest{
PoolID: args[0],
AddAccountToPoolRequest: shared.AddAccountToPoolRequest{
AccountID: args[1],
},
})
if err != nil {
return nil, err
}

if response.StatusCode >= 300 {
return nil, fmt.Errorf("unexpected status code: %d", response.StatusCode)
}

c.store.PoolID = args[0]
c.store.AccountID = args[1]

return c, nil
}

func (c *AddAccountController) Render(cmd *cobra.Command, args []string) error {
pterm.Success.WithWriter(cmd.OutOrStdout()).Printfln("Successfully added '%s' to '%s'", c.store.AccountID, c.store.PoolID)

return nil
}
108 changes: 108 additions & 0 deletions components/fctl/cmd/payments/pools/balances.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package pools

import (
"fmt"
"time"

fctl "github.com/formancehq/fctl/pkg"
"github.com/formancehq/formance-sdk-go/pkg/models/operations"
"github.com/formancehq/formance-sdk-go/pkg/models/shared"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)

type BalancesStore struct {
Balances *shared.PoolBalances `json:"balances"`
}

type BalancesController struct {
store *BalancesStore
}

var _ fctl.Controller[*BalancesStore] = (*BalancesController)(nil)

func NewBalancesStore() *BalancesStore {
return &BalancesStore{
Balances: &shared.PoolBalances{},
}
}

func NewBalancesController() *BalancesController {
return &BalancesController{
store: NewBalancesStore(),
}
}

func (c *BalancesController) GetStore() *BalancesStore {
return c.store
}

func (c *BalancesController) Run(cmd *cobra.Command, args []string) (fctl.Renderable, error) {
cfg, err := fctl.GetConfig(cmd)
if err != nil {
return nil, err
}

organizationID, err := fctl.ResolveOrganizationID(cmd, cfg)
if err != nil {
return nil, err
}

stack, err := fctl.ResolveStack(cmd, cfg, organizationID)
if err != nil {
return nil, err
}

client, err := fctl.NewStackClient(cmd, cfg, stack)
if err != nil {
return nil, err
}

at, err := time.Parse(time.RFC3339, args[1])
if err != nil {
return nil, err
}

response, err := client.Payments.GetPoolBalances(
cmd.Context(),
operations.GetPoolBalancesRequest{
At: at,
PoolID: args[0],
},
)
if err != nil {
return nil, err
}

if response.StatusCode >= 300 {
return nil, fmt.Errorf("unexpected status code: %d", response.StatusCode)
}

c.store.Balances = &response.PoolBalancesResponse.Data

return c, nil
}

func (c *BalancesController) Render(cmd *cobra.Command, args []string) error {
tableData := fctl.Map(c.store.Balances.Balances, func(balance shared.PoolBalance) []string {
return []string{
balance.Asset,
balance.Amount.String(),
}
})
tableData = fctl.Prepend(tableData, []string{"Asset", "Amount"})
return pterm.DefaultTable.
WithHasHeader().
WithWriter(cmd.OutOrStdout()).
WithData(tableData).
Render()
}

func NewBalancesCommand() *cobra.Command {
c := NewBalancesController()
return fctl.NewCommand("balances <poolID> <at>",
fctl.WithArgs(cobra.ExactArgs(2)),
fctl.WithShortDescription("List pool balances"),
fctl.WithController[*BalancesStore](c),
)
}
Loading

0 comments on commit 7c64d67

Please sign in to comment.