Skip to content

Commit 998c210

Browse files
committed
new logic for adding accounts
1 parent 16b2f77 commit 998c210

File tree

4 files changed

+107
-27
lines changed

4 files changed

+107
-27
lines changed

ignite/cmd/network_chain_join.go

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ import (
1919
)
2020

2121
const (
22-
flagGentx = "gentx"
23-
flagAmount = "amount"
22+
flagGentx = "gentx"
23+
flagAmount = "amount"
24+
flagNoAccount = "no-account"
2425
)
2526

2627
// NewNetworkChainJoin creates a new chain join command to join
@@ -35,6 +36,7 @@ func NewNetworkChainJoin() *cobra.Command {
3536

3637
c.Flags().String(flagGentx, "", "Path to a gentx json file")
3738
c.Flags().String(flagAmount, "", "Amount of coins for account request")
39+
c.Flags().Bool(flagNoAccount, false, "Prevent sending a request for a genesis account")
3840
c.Flags().AddFlagSet(flagNetworkFrom())
3941
c.Flags().AddFlagSet(flagSetHome())
4042
c.Flags().AddFlagSet(flagSetKeyringBackend())
@@ -53,6 +55,7 @@ func networkChainJoinHandler(cmd *cobra.Command, args []string) error {
5355
joinOptions []network.JoinOption
5456
gentxPath, _ = cmd.Flags().GetString(flagGentx)
5557
amount, _ = cmd.Flags().GetString(flagAmount)
58+
noAccount, _ = cmd.Flags().GetBool(flagNoAccount)
5659
)
5760

5861
nb, err := newNetworkBuilder(cmd, CollectEvents(session.EventBus()))
@@ -116,25 +119,32 @@ func networkChainJoinHandler(cmd *cobra.Command, args []string) error {
116119
}
117120
}
118121

119-
if amount != "" {
120-
// parse the amount.
121-
amountCoins, err := sdk.ParseCoinsNormalized(amount)
122-
if err != nil {
123-
return errors.Wrap(err, "error parsing amount")
124-
}
125-
joinOptions = append(joinOptions, network.WithAccountRequest(amountCoins))
126-
} else {
127-
if !getYes(cmd) {
128-
question := fmt.Sprintf(
129-
"You haven't set the --%s flag and therefore an account request won't be submitted. Do you confirm",
130-
flagAmount,
131-
)
132-
if err := session.AskConfirm(question); err != nil {
133-
return session.PrintSaidNo()
122+
// genesis account request
123+
if !noAccount {
124+
if c.IsAccountBalanceFixed() {
125+
// fixed account balance
126+
joinOptions = append(joinOptions, network.WithAccountRequest(c.AccountBalance()))
127+
} else if amount != "" {
128+
// account balance set by user
129+
amountCoins, err := sdk.ParseCoinsNormalized(amount)
130+
if err != nil {
131+
return errors.Wrap(err, "error parsing amount")
132+
}
133+
joinOptions = append(joinOptions, network.WithAccountRequest(amountCoins))
134+
} else {
135+
// fixed balance and no amount entered by the user, we ask if they want to skip account request
136+
if !getYes(cmd) {
137+
question := fmt.Sprintf(
138+
"You haven't set the --%s flag and therefore an account request won't be submitted. Do you confirm",
139+
flagAmount,
140+
)
141+
if err := session.AskConfirm(question); err != nil {
142+
return session.PrintSaidNo()
143+
}
134144
}
135-
}
136145

137-
_ = session.Printf("%s %s\n", icons.Info, "Account request won't be submitted")
146+
_ = session.Printf("%s %s\n", icons.Info, "Account request won't be submitted")
147+
}
138148
}
139149

140150
// create the message to add the validator.

ignite/services/network/networkchain/networkchain.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os/exec"
88
"time"
99

10+
sdk "github.com/cosmos/cosmos-sdk/types"
1011
"github.com/go-git/go-git/v5"
1112
"github.com/go-git/go-git/v5/plumbing"
1213

@@ -36,6 +37,8 @@ type Chain struct {
3637
genesisHash string
3738
launchTime time.Time
3839

40+
accountBalance sdk.Coins
41+
3942
keyringBackend chaincmd.KeyringBackend
4043

4144
isInitialized bool
@@ -96,6 +99,7 @@ func SourceLaunch(launch networktypes.ChainLaunch) SourceOption {
9699
c.genesisHash = launch.GenesisHash
97100
c.home = ChainHome(launch.ID)
98101
c.launchTime = launch.LaunchTime
102+
c.accountBalance = launch.AccountBalance
99103
}
100104
}
101105

@@ -241,6 +245,14 @@ func (c Chain) SourceHash() string {
241245
return c.hash
242246
}
243247

248+
func (c Chain) IsAccountBalanceFixed() bool {
249+
return !c.accountBalance.IsZero()
250+
}
251+
252+
func (c Chain) AccountBalance() sdk.Coins {
253+
return c.accountBalance
254+
}
255+
244256
func (c Chain) IsHomeDirExist() (ok bool, err error) {
245257
home, err := c.chain.Home()
246258
if err != nil {

ignite/services/network/networktypes/chainlaunch.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package networktypes
22

33
import (
4+
sdk "github.com/cosmos/cosmos-sdk/types"
45
"time"
56

67
launchtypes "github.com/tendermint/spn/x/launch/types"
@@ -23,6 +24,7 @@ type (
2324
LaunchTriggered bool `json:"LaunchTriggered"`
2425
Network NetworkType `json:"Network"`
2526
Reward string `json:"Reward,omitempty"`
27+
AccountBalance sdk.Coins `json:"AccountBalance"`
2628
}
2729
)
2830

@@ -57,6 +59,7 @@ func ToChainLaunch(chain launchtypes.Chain) ChainLaunch {
5759
CampaignID: chain.CampaignID,
5860
LaunchTriggered: chain.LaunchTriggered,
5961
Network: network,
62+
AccountBalance: chain.AccountBalance,
6063
}
6164

6265
// check if custom genesis URL is provided.

ignite/services/network/publish_test.go

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ func TestPublish(t *testing.T) {
7272
GenesisHash: "",
7373
HasCampaign: false,
7474
CampaignID: 0,
75-
AccountBalance: sdk.NewCoins(),
7675
},
7776
).
7877
Return(testutil.NewResponse(&launchtypes.MsgCreateChainResponse{
@@ -91,6 +90,69 @@ func TestPublish(t *testing.T) {
9190
suite.AssertAllMocks(t)
9291
})
9392

93+
t.Run("publish chain with custom account balance", func(t *testing.T) {
94+
var (
95+
account = testutil.NewTestAccount(t, testutil.TestAccountName)
96+
suite, network = newSuite(account)
97+
)
98+
99+
accountBalance, err := sdk.ParseCoinsNormalized("1000foo,500bar")
100+
require.NoError(t, err)
101+
102+
addr, err := account.Address(networktypes.SPN)
103+
require.NoError(t, err)
104+
105+
suite.ProfileQueryMock.
106+
On(
107+
"CoordinatorByAddress",
108+
context.Background(),
109+
&profiletypes.QueryGetCoordinatorByAddressRequest{
110+
Address: addr,
111+
},
112+
).
113+
Return(&profiletypes.QueryGetCoordinatorByAddressResponse{
114+
CoordinatorByAddress: profiletypes.CoordinatorByAddress{
115+
Address: addr,
116+
CoordinatorID: 1,
117+
},
118+
}, nil).
119+
Once()
120+
suite.CosmosClientMock.
121+
On(
122+
"BroadcastTx",
123+
account,
124+
&launchtypes.MsgCreateChain{
125+
Coordinator: addr,
126+
GenesisChainID: testutil.ChainID,
127+
SourceURL: testutil.ChainSourceURL,
128+
SourceHash: testutil.ChainSourceHash,
129+
GenesisURL: "",
130+
GenesisHash: "",
131+
HasCampaign: false,
132+
CampaignID: 0,
133+
AccountBalance: accountBalance,
134+
},
135+
).
136+
Return(testutil.NewResponse(&launchtypes.MsgCreateChainResponse{
137+
LaunchID: testutil.LaunchID,
138+
}), nil).
139+
Once()
140+
suite.ChainMock.On("SourceHash").Return(testutil.ChainSourceHash).Once()
141+
suite.ChainMock.On("SourceURL").Return(testutil.ChainSourceURL).Once()
142+
suite.ChainMock.On("ChainID").Return(testutil.ChainID, nil).Once()
143+
suite.ChainMock.On("CacheBinary", testutil.LaunchID).Return(nil).Once()
144+
145+
launchID, campaignID, publishError := network.Publish(
146+
context.Background(),
147+
suite.ChainMock,
148+
WithAccountBalance(accountBalance),
149+
)
150+
require.NoError(t, publishError)
151+
require.Equal(t, testutil.LaunchID, launchID)
152+
require.Equal(t, uint64(0), campaignID)
153+
suite.AssertAllMocks(t)
154+
})
155+
94156
t.Run("publish chain with pre created campaign", func(t *testing.T) {
95157
var (
96158
account = testutil.NewTestAccount(t, testutil.TestAccountName)
@@ -138,7 +200,6 @@ func TestPublish(t *testing.T) {
138200
GenesisHash: "",
139201
HasCampaign: true,
140202
CampaignID: testutil.CampaignID,
141-
AccountBalance: sdk.NewCoins(),
142203
},
143204
).
144205
Return(testutil.NewResponse(&launchtypes.MsgCreateChainResponse{
@@ -226,7 +287,6 @@ func TestPublish(t *testing.T) {
226287
GenesisHash: "",
227288
HasCampaign: true,
228289
CampaignID: testutil.CampaignID,
229-
AccountBalance: sdk.NewCoins(),
230290
},
231291
).
232292
Return(testutil.NewResponse(&launchtypes.MsgCreateChainResponse{
@@ -291,7 +351,6 @@ func TestPublish(t *testing.T) {
291351
GenesisHash: customGenesisHash,
292352
HasCampaign: false,
293353
CampaignID: 0,
294-
AccountBalance: sdk.NewCoins(),
295354
},
296355
).
297356
Return(testutil.NewResponse(&launchtypes.MsgCreateChainResponse{
@@ -346,7 +405,6 @@ func TestPublish(t *testing.T) {
346405
GenesisHash: "",
347406
HasCampaign: false,
348407
CampaignID: 0,
349-
AccountBalance: sdk.NewCoins(),
350408
},
351409
).
352410
Return(testutil.NewResponse(&launchtypes.MsgCreateChainResponse{
@@ -543,7 +601,6 @@ func TestPublish(t *testing.T) {
543601
GenesisHash: "",
544602
HasCampaign: false,
545603
CampaignID: 0,
546-
AccountBalance: sdk.NewCoins(),
547604
},
548605
).
549606
Return(testutil.NewResponse(&launchtypes.MsgCreateChainResponse{
@@ -690,7 +747,6 @@ func TestPublish(t *testing.T) {
690747
GenesisHash: "",
691748
HasCampaign: false,
692749
CampaignID: 0,
693-
AccountBalance: sdk.NewCoins(),
694750
},
695751
).
696752
Return(testutil.NewResponse(&launchtypes.MsgCreateChainResponse{
@@ -745,7 +801,6 @@ func TestPublish(t *testing.T) {
745801
GenesisHash: "",
746802
HasCampaign: false,
747803
CampaignID: 0,
748-
AccountBalance: sdk.NewCoins(),
749804
},
750805
).
751806
Return(testutil.NewResponse(&launchtypes.MsgCreateChainResponse{

0 commit comments

Comments
 (0)