diff --git a/app/app.go b/app/app.go index f8e0230..377a94c 100644 --- a/app/app.go +++ b/app/app.go @@ -124,8 +124,8 @@ import ( // this line is used by starport scaffolding # stargate/app/moduleImport pmante "github.com/planetmint/planetmint-go/app/ante" - appparams "github.com/planetmint/planetmint-go/app/params" "github.com/planetmint/planetmint-go/docs" + appparams "github.com/planetmint/planetmint-go/lib/params" ) const ( diff --git a/app/encoding.go b/app/encoding.go index edcc0b3..ad3e8e9 100644 --- a/app/encoding.go +++ b/app/encoding.go @@ -6,7 +6,7 @@ import ( "github.com/cosmos/cosmos-sdk/std" "github.com/cosmos/cosmos-sdk/x/auth/tx" - "github.com/planetmint/planetmint-go/app/params" + "github.com/planetmint/planetmint-go/lib/params" ) // makeEncodingConfig creates an EncodingConfig for an amino based test configuration. diff --git a/cmd/planetmint-god/cmd/root.go b/cmd/planetmint-god/cmd/root.go index 056c12c..be003ef 100644 --- a/cmd/planetmint-god/cmd/root.go +++ b/cmd/planetmint-god/cmd/root.go @@ -40,13 +40,18 @@ import ( // this line is used by starport scaffolding # root/moduleImport "github.com/planetmint/planetmint-go/app" - appparams "github.com/planetmint/planetmint-go/app/params" planetmintconfig "github.com/planetmint/planetmint-go/config" + "github.com/planetmint/planetmint-go/lib" + appparams "github.com/planetmint/planetmint-go/lib/params" ) // NewRootCmd creates a new root command for a Cosmos SDK application func NewRootCmd() (*cobra.Command, appparams.EncodingConfig) { encodingConfig := app.MakeEncodingConfig() + // Initialize library + libConfig := lib.GetConfig() + libConfig.SetEncodingConfig(encodingConfig) + libConfig.SetBech32PrefixForAccount("plmnt") initClientCtx := client.Context{}. WithCodec(encodingConfig.Marshaler). WithInterfaceRegistry(encodingConfig.InterfaceRegistry). diff --git a/go.mod b/go.mod index 2b4d221..5e663a6 100644 --- a/go.mod +++ b/go.mod @@ -20,6 +20,7 @@ require ( github.com/gorilla/mux v1.8.0 github.com/grpc-ecosystem/grpc-gateway v1.16.0 github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.2 + github.com/planetmint/planetmint-go/lib v0.0.0-00010101000000-000000000000 github.com/spf13/cast v1.5.0 github.com/spf13/cobra v1.6.1 github.com/spf13/pflag v1.0.5 @@ -180,3 +181,5 @@ require ( ) replace github.com/syndtr/goleveldb => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 + +replace github.com/planetmint/planetmint-go/lib => ./lib diff --git a/lib/README.md b/lib/README.md index 8ae8f1d..fa3cdef 100644 --- a/lib/README.md +++ b/lib/README.md @@ -18,12 +18,16 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + "github.com/planetmint/planetmint-go/app" "github.com/planetmint/planetmint-go/lib" ) func main() { + encodingConfig := app.MakeEncodingConfig() + libConfig := lib.GetConfig() libConfig.SetBech32PrefixForAccount("plmnt") + libConfig.SetEncodingConfig(encodingConfig) libConfig.SetRPCEndpoint("https://testnet-api.rddl.io") addr0 := sdk.MustAccAddressFromBech32("plmnt168z8fyyzap0nw75d4atv9ucr2ye60d57dzlzaf") diff --git a/lib/config.go b/lib/config.go index c9478df..edad3fa 100644 --- a/lib/config.go +++ b/lib/config.go @@ -4,13 +4,15 @@ import ( "sync" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/planetmint/planetmint-go/lib/params" ) // Config defines library top level configuration. type Config struct { - ChainID string `mapstructure:"chain-id" json:"chain-id"` - RootDir string `mapstructure:"root-dir" json:"root-dir"` - RPCEndpoint string `mapstructure:"rpc-endpoint" json:"rpc-endpoint"` + ChainID string `mapstructure:"chain-id" json:"chain-id"` + EncodingConfig params.EncodingConfig `mapstructure:"encoding-config" json:"encoding-config""` + RootDir string `mapstructure:"root-dir" json:"root-dir"` + RPCEndpoint string `mapstructure:"rpc-endpoint" json:"rpc-endpoint"` } // lib wide global singleton @@ -23,9 +25,10 @@ var ( // DefaultConfig returns library default configuration. func DefaultConfig() *Config { return &Config{ - ChainID: "planetmint-testnet-1", - RootDir: "~/.planetmint-go/", - RPCEndpoint: "http://127.0.0.1:1317", + ChainID: "planetmint-testnet-1", + EncodingConfig: params.EncodingConfig{}, + RootDir: "~/.planetmint-go/", + RPCEndpoint: "http://127.0.0.1:1317", } } @@ -44,6 +47,12 @@ func (config *Config) SetBech32PrefixForAccount(bech32Prefix string) *Config { return config } +// SetEncodingConfig sets the encoding config and must not be nil. +func (config *Config) SetEncodingConfig(encodingConfig params.EncodingConfig) *Config { + config.EncodingConfig = encodingConfig + return config +} + // SetChainID sets the chain ID parameter. func (config *Config) SetChainID(chainID string) *Config { config.ChainID = chainID diff --git a/app/params/encoding.go b/lib/params/encoding.go similarity index 100% rename from app/params/encoding.go rename to lib/params/encoding.go diff --git a/lib/tx.go b/lib/tx.go index 17c242f..8c0ce17 100644 --- a/lib/tx.go +++ b/lib/tx.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/hex" "encoding/json" + "errors" "fmt" "io" "log" @@ -20,7 +21,6 @@ import ( sdktx "github.com/cosmos/cosmos-sdk/types/tx" "github.com/cosmos/cosmos-sdk/types/tx/signing" xauthsigning "github.com/cosmos/cosmos-sdk/x/auth/signing" - "github.com/planetmint/planetmint-go/app" ) // KeyPair defines a public/private key pair to e.g. sign a transaction. @@ -117,7 +117,11 @@ func getAccountNumberAndSequence(address sdk.AccAddress) (accountNumber, sequenc // BuildAndSignTx constructs the transaction from address' private key and messages. func BuildAndSignTx(address sdk.AccAddress, msgs ...sdk.Msg) (txBytes []byte, txJSON string, err error) { - encodingConfig := app.MakeEncodingConfig() + encodingConfig := GetConfig().EncodingConfig + if encodingConfig.TxConfig == nil { + err = errors.New("encoding config must not be nil") + return + } txBuilder := encodingConfig.TxConfig.NewTxBuilder() err = txBuilder.SetMsgs(msgs...) diff --git a/util/issue_commands.go b/util/issue_commands.go index 16f8d5b..ebc8bbb 100644 --- a/util/issue_commands.go +++ b/util/issue_commands.go @@ -1,58 +1,63 @@ package util import ( - "os/exec" "strconv" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/planetmint/planetmint-go/config" - "github.com/planetmint/planetmint-go/x/dao/types" + "github.com/planetmint/planetmint-go/lib" + daotypes "github.com/planetmint/planetmint-go/x/dao/types" ) -func InitRDDLReissuanceProcess(ctx sdk.Context, proposerAddress string, txUnsigned string, blockHeight int64) error { +func buildSignBroadcastTx(ctx sdk.Context, sendingValidatorAddress string, msg sdk.Msg) (err error) { + logger := ctx.Logger() + addr := sdk.MustAccAddressFromBech32(sendingValidatorAddress) + txBytes, txJSON, err := lib.BuildAndSignTx(addr, msg) + if err != nil { + return + } + logger.Debug("REISSUE: tx: " + txJSON) + _, err = lib.BroadcastTx(txBytes) + return +} + +func InitRDDLReissuanceProcess(ctx sdk.Context, proposerAddress string, txUnsigned string, blockHeight int64) (err error) { //get_last_PoPBlockHeight() // TODO: to be read form the upcoming PoP-store logger := ctx.Logger() - // Construct the command sendingValidatorAddress := config.GetConfig().ValidatorAddress - logger.Debug("REISSUE: create Proposal") - cmd := exec.Command("planetmint-god", "tx", "dao", "reissue-rddl-proposal", - "--from", sendingValidatorAddress, "-y", - proposerAddress, txUnsigned, strconv.FormatInt(blockHeight, 10)) - - logger.Debug("REISSUE: create Proposal") - return cmd.Start() + logger.Debug("REISSUE: create Reissuance Proposal") + msg := daotypes.NewMsgReissueRDDLProposal(sendingValidatorAddress, proposerAddress, txUnsigned, blockHeight) + err = buildSignBroadcastTx(ctx, sendingValidatorAddress, msg) + return } -func SendRDDLReissuanceResult(ctx sdk.Context, proposerAddress string, txID string, blockHeight int64) error { +func SendRDDLReissuanceResult(ctx sdk.Context, proposerAddress string, txID string, blockHeight int64) (err error) { logger := ctx.Logger() - // Construct the command sendingValidatorAddress := config.GetConfig().ValidatorAddress - logger.Debug("REISSUE: create Result") - cmd := exec.Command("planetmint-god", "tx", "dao", "reissue-rddl-result", - "--from", sendingValidatorAddress, "-y", - proposerAddress, txID, strconv.FormatInt(blockHeight, 10)) - logger.Debug("REISSUE: create Result") - return cmd.Start() + logger.Debug("REISSUE: create Reissuance Result") + msg := daotypes.NewMsgReissueRDDLResult(sendingValidatorAddress, proposerAddress, txID, blockHeight) + err = buildSignBroadcastTx(ctx, sendingValidatorAddress, msg) + return } -func SendRDDLDistributionRequest(ctx sdk.Context, distribution types.DistributionOrder) error { +func SendRDDLDistributionRequest(ctx sdk.Context, distribution daotypes.DistributionOrder) (err error) { logger := ctx.Logger() - // Construct the command sendingValidatorAddress := config.GetConfig().ValidatorAddress - cmd := exec.Command("planetmint-god", "tx", "dao", "distribution-request", - "--from", sendingValidatorAddress, "-y", "'"+distribution.String()+"'") - logger.Debug("REISSUE: create Result") - return cmd.Start() + logger.Debug("REISSUE: create Distribution Request") + msg := daotypes.NewMsgDistributionRequest(sendingValidatorAddress, &distribution) + err = buildSignBroadcastTx(ctx, sendingValidatorAddress, msg) + return } -func SendRDDLDistributionResult(ctx sdk.Context, lastPoP string, daoTxid string, invTxid string, popTxid string) error { +func SendRDDLDistributionResult(ctx sdk.Context, lastPoP string, daoTxID string, invTxID string, popTxID string) (err error) { logger := ctx.Logger() - // Construct the command sendingValidatorAddress := config.GetConfig().ValidatorAddress - logger.Debug("REISSUE: create Result") - cmd := exec.Command("planetmint-god", "tx", "dao", "distribution-result", - "--from", sendingValidatorAddress, "-y", - lastPoP, daoTxid, invTxid, popTxid) - logger.Debug("REISSUE: create Result") - return cmd.Start() + logger.Debug("REISSUE: create Distribution Result") + iLastPoP, err := strconv.ParseInt(lastPoP, 10, 64) + if err != nil { + return + } + msg := daotypes.NewMsgDistributionResult(sendingValidatorAddress, iLastPoP, daoTxID, invTxID, popTxID) + err = buildSignBroadcastTx(ctx, sendingValidatorAddress, msg) + return }