Skip to content

Commit a368933

Browse files
authored
Merge pull request #36 from kjessec/feat/launch
feat: use opinit/launch
2 parents 8dc0031 + d4f6ce7 commit a368933

File tree

7 files changed

+313
-37
lines changed

7 files changed

+313
-37
lines changed

app/app.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net/http"
77
"os"
88
"path/filepath"
9+
"strings"
910

1011
"github.com/gorilla/mux"
1112
"github.com/rakyll/statik/fs"
@@ -972,9 +973,22 @@ func NewMinitiaApp(
972973
}
973974
err = msgservice.ValidateProtoAnnotations(protoFiles)
974975
if err != nil {
975-
// Once we switch to using protoreflect-based antehandlers, we might
976-
// want to panic here instead of logging a warning.
977-
fmt.Fprintln(os.Stderr, err.Error())
976+
errMsg := ""
977+
978+
// ignore injective proto annotations comes from github.com/cosoms/relayer
979+
for _, s := range strings.Split(err.Error(), "\n") {
980+
if strings.Contains(s, "injective") {
981+
continue
982+
}
983+
984+
errMsg += s + "\n"
985+
}
986+
987+
if errMsg != "" {
988+
// Once we switch to using protoreflect-based antehandlers, we might
989+
// want to panic here instead of logging a warning.
990+
fmt.Fprintln(os.Stderr, errMsg)
991+
}
978992
}
979993

980994
// must be before Loading version

cmd/minitiad/launch.go

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"os"
6+
7+
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
8+
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
9+
sdk "github.com/cosmos/cosmos-sdk/types"
10+
"github.com/cosmos/cosmos-sdk/types/module"
11+
"github.com/initia-labs/OPinit/contrib/launchtools"
12+
"github.com/initia-labs/OPinit/contrib/launchtools/steps"
13+
"github.com/initia-labs/initia/app/params"
14+
minitiaapp "github.com/initia-labs/miniwasm/app"
15+
"github.com/pkg/errors"
16+
"github.com/spf13/cobra"
17+
)
18+
19+
// DefaultLaunchStepFactories is a list of default launch step factories.
20+
var DefaultLaunchStepFactories = []launchtools.LauncherStepFuncFactory[launchtools.Input]{
21+
steps.InitializeConfig,
22+
steps.InitializeRPCHelpers,
23+
24+
// Initialize genesis
25+
steps.InitializeGenesis,
26+
27+
// Add system keys to the keyring
28+
steps.InitializeKeyring,
29+
30+
// Run the app
31+
steps.RunApp,
32+
33+
// MINIWASM: Store/Instantiate cw721 and ics721 contracts
34+
StoreAndInstantiateNFTContracts,
35+
36+
// Establish IBC channels for fungible and NFT transfer
37+
// MINIWASM: Use wasm contract addresses for srcPort, dstPort, channelVersion
38+
steps.EstablishIBCChannelsWithNFTTransfer(func() (string, string, string) {
39+
return "wasm." + wasmkeeper.BuildContractAddressClassic(2, 1).String(),
40+
"nft-transfer",
41+
"ics721-1"
42+
}),
43+
44+
// Enable the oracle
45+
steps.EnableOracle,
46+
47+
// Create OP Bridge, using open channel states
48+
steps.InitializeOpBridge,
49+
50+
// Cleanup
51+
steps.StopApp,
52+
}
53+
54+
func LaunchCommand(ac *appCreator, enc params.EncodingConfig, mbm module.BasicManager) *cobra.Command {
55+
return launchtools.LaunchCmd(
56+
ac,
57+
func(denom string) map[string]json.RawMessage {
58+
return minitiaapp.NewDefaultGenesisState(enc.Codec, mbm, denom)
59+
},
60+
DefaultLaunchStepFactories,
61+
)
62+
}
63+
64+
// StoreAndInstantiateNFTContracts stores and instantiates cw721 and ics721 contracts
65+
func StoreAndInstantiateNFTContracts(input launchtools.Input) launchtools.LauncherStepFunc {
66+
return func(ctx launchtools.Launcher) error {
67+
ctx.Logger().Info("Storing and instantiating cw721 and ics721 contracts")
68+
69+
cw721, err := os.ReadFile("contrib/wasm/cw721_base.wasm")
70+
if err != nil {
71+
return errors.Wrapf(err, "failed to read cw721_base.wasm")
72+
}
73+
74+
ics721, err := os.ReadFile("contrib/wasm/ics721_base.wasm")
75+
if err != nil {
76+
return errors.Wrapf(err, "failed to read ics721_base.wasm")
77+
}
78+
79+
msgs := []sdk.Msg{
80+
&wasmtypes.MsgStoreCode{
81+
Sender: input.SystemKeys.Validator.Address,
82+
WASMByteCode: cw721,
83+
InstantiatePermission: nil,
84+
},
85+
&wasmtypes.MsgStoreCode{
86+
Sender: input.SystemKeys.Validator.Address,
87+
WASMByteCode: ics721,
88+
InstantiatePermission: nil,
89+
},
90+
&wasmtypes.MsgInstantiateContract{
91+
Sender: input.SystemKeys.Validator.Address,
92+
Admin: input.SystemKeys.Validator.Address,
93+
CodeID: 2,
94+
Label: "ics721",
95+
Msg: []byte(`{"cw721_base_code_id":1}`),
96+
Funds: nil,
97+
},
98+
}
99+
100+
for i, msg := range msgs {
101+
ctx.Logger().Info(
102+
"Broadcasting tx to store and instantiate cw721 and ics721 contracts",
103+
"step", i+1,
104+
)
105+
106+
res, err := ctx.GetRPCHelperL2().BroadcastTxAndWait(
107+
input.SystemKeys.Validator.Address,
108+
input.SystemKeys.Validator.Mnemonic,
109+
10000000,
110+
sdk.NewCoins(sdk.NewInt64Coin(input.L2Config.Denom, 1500000)),
111+
msg,
112+
)
113+
114+
if err != nil {
115+
return errors.Wrapf(err, "failed to store and instantiate nft contracts")
116+
}
117+
118+
ctx.Logger().Info(
119+
"Successfully stored and instantiated cw721 and ics721 contracts",
120+
"tx_hash", res.Hash,
121+
)
122+
}
123+
124+
return nil
125+
}
126+
}

cmd/minitiad/root.go

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package main
22

33
import (
4+
"context"
45
"errors"
56
"io"
67
"os"
78
"path"
89

910
tmcli "github.com/cometbft/cometbft/libs/cli"
11+
"golang.org/x/sync/errgroup"
1012

1113
"github.com/prometheus/client_golang/prometheus"
1214

@@ -63,7 +65,9 @@ func NewRootCmd() (*cobra.Command, params.EncodingConfig) {
6365
sdkConfig.SetBech32PrefixForValidator(validatorAddressPrefix, validatorPubKeyPrefix)
6466
sdkConfig.SetBech32PrefixForConsensusNode(consNodeAddressPrefix, consNodePubKeyPrefix)
6567
sdkConfig.SetAddressVerifier(wasmtypes.VerifyAddressLen())
66-
sdkConfig.Seal()
68+
69+
// seal moved to post setup
70+
// sdkConfig.Seal()
6771

6872
encodingConfig := minitiaapp.MakeEncodingConfig()
6973
basicManager := minitiaapp.BasicManager()
@@ -145,18 +149,23 @@ func NewRootCmd() (*cobra.Command, params.EncodingConfig) {
145149
}
146150

147151
func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig, basicManager module.BasicManager) {
148-
a := &appCreator{nil, encodingConfig}
149-
// you can get app from a.app in post setup handler
152+
a := &appCreator{}
150153

151154
rootCmd.AddCommand(
152155
InitCmd(basicManager, minitiaapp.DefaultNodeHome),
153156
debug.Cmd(),
154157
confixcmd.ConfigCommand(),
155-
pruning.Cmd(a.newApp, minitiaapp.DefaultNodeHome),
156-
snapshot.Cmd(a.newApp),
158+
pruning.Cmd(a.AppCreator(), minitiaapp.DefaultNodeHome),
159+
snapshot.Cmd(a.AppCreator()),
157160
)
158161

159-
server.AddCommands(rootCmd, minitiaapp.DefaultNodeHome, a.newApp, a.appExport, addModuleInitFlags)
162+
server.AddCommandsWithStartCmdOptions(rootCmd, minitiaapp.DefaultNodeHome, a.AppCreator(), a.appExport, server.StartCmdOptions{
163+
AddFlags: addModuleInitFlags,
164+
PostSetup: func(svrCtx *server.Context, clientCtx client.Context, ctx context.Context, g *errgroup.Group) error {
165+
sdk.GetConfig().Seal()
166+
return nil
167+
},
168+
})
160169
wasmcli.ExtendUnsafeResetAllCmd(rootCmd)
161170

162171
// add keybase, auxiliary RPC, query, and tx child commands
@@ -167,6 +176,9 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig, b
167176
txCommand(),
168177
keys.Commands(),
169178
)
179+
180+
// add launch commands
181+
rootCmd.AddCommand(LaunchCommand(a, encodingConfig, basicManager))
170182
}
171183

172184
func addModuleInitFlags(startCmd *cobra.Command) {
@@ -242,30 +254,34 @@ func txCommand() *cobra.Command {
242254
}
243255

244256
type appCreator struct {
245-
app servertypes.Application
246-
encodingConfig params.EncodingConfig
257+
app servertypes.Application
247258
}
248259

249260
// newApp is an AppCreator
250-
func (a appCreator) newApp(logger log.Logger, db dbm.DB, traceStore io.Writer, appOpts servertypes.AppOptions) servertypes.Application {
251-
baseappOptions := server.DefaultBaseappOptions(appOpts)
261+
func (a *appCreator) AppCreator() servertypes.AppCreator {
262+
return func(logger log.Logger, db dbm.DB, traceStore io.Writer, appOpts servertypes.AppOptions) servertypes.Application {
263+
baseappOptions := server.DefaultBaseappOptions(appOpts)
252264

253-
var wasmOpts []wasmkeeper.Option
254-
if cast.ToBool(appOpts.Get("telemetry.enabled")) {
255-
wasmOpts = append(wasmOpts, wasmkeeper.WithVMCacheMetrics(prometheus.DefaultRegisterer))
256-
}
265+
var wasmOpts []wasmkeeper.Option
266+
if cast.ToBool(appOpts.Get("telemetry.enabled")) {
267+
wasmOpts = append(wasmOpts, wasmkeeper.WithVMCacheMetrics(prometheus.DefaultRegisterer))
268+
}
257269

258-
app := minitiaapp.NewMinitiaApp(
259-
logger, db, traceStore, true,
260-
wasmOpts,
261-
appOpts,
262-
baseappOptions...,
263-
)
270+
app := minitiaapp.NewMinitiaApp(
271+
logger, db, traceStore, true,
272+
wasmOpts,
273+
appOpts,
274+
baseappOptions...,
275+
)
276+
277+
a.app = app
264278

265-
// store app in creator
266-
a.app = app
279+
return app
280+
}
281+
}
267282

268-
return app
283+
func (a *appCreator) App() servertypes.Application {
284+
return a.app
269285
}
270286

271287
func (a appCreator) appExport(

contrib/wasm/cw721_base.wasm

356 KB
Binary file not shown.

contrib/wasm/ics721_base.wasm

715 KB
Binary file not shown.

0 commit comments

Comments
 (0)