Skip to content

Commit

Permalink
feat(cosmos): Support arbitrary core eval builder arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
gibson042 committed Dec 23, 2024
1 parent 837776e commit aa6b8e8
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions golang/cosmos/app/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gaia
import (
"encoding/json"
"fmt"
"reflect"
"strings"
"text/template"

Expand Down Expand Up @@ -77,14 +78,24 @@ func isFirstTimeUpgradeOfThisVersion(app *GaiaApp, ctx sdk.Context) bool {
return true
}

func buildProposalStepWithArgs(moduleName string, entrypoint string, opts map[string]any) (vm.CoreProposalStep, error) {
func buildProposalStepWithArgs(moduleName string, entrypoint string, opts any) (vm.CoreProposalStep, error) {
t := template.Must(template.New("").Parse(`{
"module": "{{.moduleName}}",
"entrypoint": "{{.entrypoint}}",
"args": [ {{.optsArg}} ]
}`))
"module": "{{.moduleName}}",
"entrypoint": "{{.entrypoint}}",
"args": {{.args}}
}`))

optsArg, err := json.Marshal(opts)
var args []byte
var err error
if opts == nil {
args = []byte(`[]`)
} else if reflect.TypeOf(opts).Kind() == reflect.Slice {
args, err = json.Marshal(opts)
} else if reflect.TypeOf(opts).Kind() == reflect.Map && reflect.TypeOf(opts).Key().Kind() == reflect.String {
args, err = json.Marshal([]any{opts})
} else {
return nil, fmt.Errorf("proposal opts must be nil, array, or string map, not %v", opts)
}
if err != nil {
return nil, err
}
Expand All @@ -93,7 +104,7 @@ func buildProposalStepWithArgs(moduleName string, entrypoint string, opts map[st
err = t.Execute(&result, map[string]any{
"moduleName": moduleName,
"entrypoint": entrypoint,
"optsArg": string(optsArg),
"args": string(args),
})
if err != nil {
return nil, err
Expand Down

0 comments on commit aa6b8e8

Please sign in to comment.