Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(network): add network add-account command #2955

Merged
merged 17 commits into from
Oct 20, 2022
Merged
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Add `ignite network validator` command set.
- Move cosmoscmd in chain's templates.
- Add generated TS client test support to integration tests.
- Add `ignite network request add-account` command.

### Changes

Expand Down
1 change: 1 addition & 0 deletions ignite/cmd/network_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func NewNetworkRequest() *cobra.Command {
NewNetworkRequestApprove(),
NewNetworkRequestReject(),
NewNetworkRequestVerify(),
NewNetworkRequestAddAccount(),
)

return c
Expand Down
96 changes: 96 additions & 0 deletions ignite/cmd/network_request_add_account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package ignitecmd

import (
"errors"
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/spf13/cobra"

"github.com/ignite/cli/ignite/pkg/cliui"
"github.com/ignite/cli/ignite/services/network"
"github.com/ignite/cli/ignite/services/network/networkchain"
)

// NewNetworkRequestAddAccount creates a new command to send add account request
func NewNetworkRequestAddAccount() *cobra.Command {
c := &cobra.Command{
Use: "add-account [launch-id] [address] [coins]",
lumtis marked this conversation as resolved.
Show resolved Hide resolved
Short: "Send request to add account",
RunE: networkRequestAddAccountHandler,
Args: cobra.RangeArgs(2, 3),
}

flagSetClearCache(c)
c.Flags().AddFlagSet(flagNetworkFrom())
c.Flags().AddFlagSet(flagSetHome())
c.Flags().AddFlagSet(flagSetKeyringBackend())
c.Flags().AddFlagSet(flagSetKeyringDir())
return c
}

func networkRequestAddAccountHandler(cmd *cobra.Command, args []string) error {
session := cliui.New(cliui.StartSpinner())
defer session.End()

nb, err := newNetworkBuilder(cmd, CollectEvents(session.EventBus()))
if err != nil {
return err
}

// parse launch ID
launchID, err := network.ParseID(args[0])
if err != nil {
return err
}

address := args[1]

n, err := nb.Network()
if err != nil {
return err
}

chainLaunch, err := n.ChainLaunch(cmd.Context(), launchID)
if err != nil {
return err
}

var networkOptions []networkchain.Option

if flagGetCheckDependencies(cmd) {
networkOptions = append(networkOptions, networkchain.CheckDependencies())
}
lumtis marked this conversation as resolved.
Show resolved Hide resolved

c, err := nb.Chain(networkchain.SourceLaunch(chainLaunch), networkOptions...)
if err != nil {
return err
}

var balance sdk.Coins
if c.IsAccountBalanceFixed() {
balance = c.AccountBalance()
if len(args) == 3 {
return fmt.Errorf(
"balance can't be provided, balance has been set by coordinator to %s",
balance.String(),
)
}
} else {
if len(args) < 3 {
return errors.New("account balance expected")
}
balanceStr := args[2]
balance, err = sdk.ParseCoinsNormalized(balanceStr)
if err != nil {
return err
}
}

return n.SendAccountRequest(
cmd.Context(),
launchID,
address,
balance,
)
}
62 changes: 58 additions & 4 deletions ignite/services/network/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,70 @@ func (n Network) Join(
}

if !o.accountAmount.IsZero() {
if err := n.sendAccountRequest(ctx, launchID, accountAddress, o.accountAmount); err != nil {
if err := n.SendAccountRequest(ctx, launchID, accountAddress, o.accountAmount); err != nil {
return err
}
}

return n.sendValidatorRequest(ctx, launchID, peer, accountAddress, gentx, gentxInfo)
return n.SendValidatorRequest(ctx, launchID, peer, accountAddress, gentx, gentxInfo)
}

// sendValidatorRequest creates the RequestAddValidator message into the SPN
func (n Network) sendValidatorRequest(
func (n Network) SendAccountRequestForCoordinator(ctx context.Context, launchID uint64, amount sdk.Coins) error {
addr, err := n.account.Address(networktypes.SPN)
if err != nil {
return err
}

return n.SendAccountRequest(ctx, launchID, addr, amount)
}

// SendAccountRequest creates an add AddAccount request message.
func (n Network) SendAccountRequest(
ctx context.Context,
launchID uint64,
address string,
amount sdk.Coins,
) error {
addr, err := n.account.Address(networktypes.SPN)
if err != nil {
return err
}

msg := launchtypes.NewMsgSendRequest(
addr,
launchID,
launchtypes.NewGenesisAccount(
launchID,
address,
amount,
),
)

n.ev.Send("Broadcasting account transactions", events.ProgressStarted())
lumtis marked this conversation as resolved.
Show resolved Hide resolved

res, err := n.cosmos.BroadcastTx(ctx, n.account, msg)
if err != nil {
return err
}

var requestRes launchtypes.MsgSendRequestResponse
if err := res.Decode(&requestRes); err != nil {
return err
}

if requestRes.AutoApproved {
n.ev.Send("Account added to the network by the coordinator!", events.ProgressFinished())
} else {
n.ev.Send(
fmt.Sprintf("Request %d to add account to the network has been submitted!", requestRes.RequestID),
events.ProgressFinished(),
)
}
lumtis marked this conversation as resolved.
Show resolved Hide resolved
return nil
}

// SendValidatorRequest creates the RequestAddValidator message into the SPN
func (n Network) SendValidatorRequest(
ctx context.Context,
launchID uint64,
peer launchtypes.Peer,
Expand Down
55 changes: 0 additions & 55 deletions ignite/services/network/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package network

import (
"context"
"fmt"
"os"
"path/filepath"

Expand Down Expand Up @@ -262,57 +261,3 @@ func (n Network) Publish(ctx context.Context, c Chain, options ...PublishOption)

return launchID, campaignID, nil
}

func (n Network) SendAccountRequestForCoordinator(ctx context.Context, launchID uint64, amount sdk.Coins) error {
addr, err := n.account.Address(networktypes.SPN)
if err != nil {
return err
}

return n.sendAccountRequest(ctx, launchID, addr, amount)
}

// SendAccountRequest creates an add AddAccount request message.
func (n Network) sendAccountRequest(
ctx context.Context,
launchID uint64,
address string,
amount sdk.Coins,
) error {
addr, err := n.account.Address(networktypes.SPN)
if err != nil {
return err
}

msg := launchtypes.NewMsgSendRequest(
addr,
launchID,
launchtypes.NewGenesisAccount(
launchID,
address,
amount,
),
)

n.ev.Send("Broadcasting account transactions", events.ProgressStarted())

res, err := n.cosmos.BroadcastTx(ctx, n.account, msg)
if err != nil {
return err
}

var requestRes launchtypes.MsgSendRequestResponse
if err := res.Decode(&requestRes); err != nil {
return err
}

if requestRes.AutoApproved {
n.ev.Send("Account added to the network by the coordinator!", events.ProgressFinished())
} else {
n.ev.Send(
fmt.Sprintf("Request %d to add account to the network has been submitted!", requestRes.RequestID),
events.ProgressFinished(),
)
}
return nil
}