-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d496da6
commit 7c64d67
Showing
11 changed files
with
898 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
) | ||
} |
Oops, something went wrong.