Skip to content

Commit

Permalink
feat(network): add network add-account command (#2955)
Browse files Browse the repository at this point in the history
* refactor service

* command

* add command

* improve error

* changelog

* Update ignite/services/network/join.go

Co-authored-by: Jerónimo Albi <[email protected]>

* remove unused

* changelog

* import

* Update ignite/cmd/network_request_add_account.go

Co-authored-by: Jerónimo Albi <[email protected]>

* Update changelog.md

Co-authored-by: Alex Johnson <[email protected]>

* Update ignite/cmd/network_request_add_account.go

Co-authored-by: Thomas Bruyelle <[email protected]>

* revert command

Co-authored-by: Jerónimo Albi <[email protected]>
Co-authored-by: Alex Johnson <[email protected]>
Co-authored-by: Thomas Bruyelle <[email protected]>
  • Loading branch information
4 people authored Oct 20, 2022
1 parent 5545dcd commit 25361b3
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 59 deletions.
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@

# Changelog

## Unreleased

### Features

- [#2955](https://github.com/ignite/cli/pull/2955/) Add `ignite network request add-account` command.

## [`v0.25.0`](https://github.com/ignite/cli/releases/tag/v0.25.0)

### Features
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
90 changes: 90 additions & 0 deletions ignite/cmd/network_request_add_account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
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]",
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
}

c, err := nb.Chain(networkchain.SourceLaunch(chainLaunch))
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,
)
}
68 changes: 64 additions & 4 deletions ignite/services/network/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
launchtypes "github.com/tendermint/spn/x/launch/types"

"github.com/ignite/cli/ignite/pkg/cliui/icons"
"github.com/ignite/cli/ignite/pkg/cosmosutil"
"github.com/ignite/cli/ignite/pkg/events"
"github.com/ignite/cli/ignite/pkg/xurl"
Expand Down Expand Up @@ -85,16 +86,75 @@ 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())

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.Icon(icons.Bullet),
events.ProgressFinished(),
)
} else {
n.ev.Send(
fmt.Sprintf("Request %d to add account to the network has been submitted!", requestRes.RequestID),
events.Icon(icons.Bullet),
events.ProgressFinished(),
)
}
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
}

0 comments on commit 25361b3

Please sign in to comment.