cli: allow to provide data
for nep17 transfer commands
This commit is contained in:
parent
28b74cb647
commit
db868f033e
4 changed files with 55 additions and 10 deletions
|
@ -539,7 +539,7 @@ func invokeInternal(ctx *cli.Context, signAndPush bool) error {
|
|||
paramsStart++
|
||||
|
||||
if len(args) > paramsStart {
|
||||
cosignersOffset, params, err = parseParams(args[paramsStart:], true)
|
||||
cosignersOffset, params, err = ParseParams(args[paramsStart:], true)
|
||||
if err != nil {
|
||||
return cli.NewExitError(err, 1)
|
||||
}
|
||||
|
@ -624,12 +624,12 @@ func invokeInternal(ctx *cli.Context, signAndPush bool) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// parseParams extracts array of smartcontract.Parameter from the given args and
|
||||
// ParseParams extracts array of smartcontract.Parameter from the given args and
|
||||
// returns the number of handled words, the array itself and an error.
|
||||
// `calledFromMain` denotes whether the method was called from the outside or
|
||||
// recursively and used to check if cosignersSeparator and closing bracket are
|
||||
// allowed to be in `args` sequence.
|
||||
func parseParams(args []string, calledFromMain bool) (int, []smartcontract.Parameter, error) {
|
||||
func ParseParams(args []string, calledFromMain bool) (int, []smartcontract.Parameter, error) {
|
||||
res := []smartcontract.Parameter{}
|
||||
for k := 0; k < len(args); {
|
||||
s := args[k]
|
||||
|
@ -640,7 +640,7 @@ func parseParams(args []string, calledFromMain bool) (int, []smartcontract.Param
|
|||
}
|
||||
return 0, []smartcontract.Parameter{}, errors.New("invalid array syntax: missing closing bracket")
|
||||
case arrayStartSeparator:
|
||||
numWordsRead, array, err := parseParams(args[k+1:], false)
|
||||
numWordsRead, array, err := ParseParams(args[k+1:], false)
|
||||
if err != nil {
|
||||
return 0, nil, fmt.Errorf("failed to parse array: %w", err)
|
||||
}
|
||||
|
@ -888,6 +888,26 @@ func contractDeploy(ctx *cli.Context) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// GetDataFromContext returns data parameter from context args.
|
||||
func GetDataFromContext(ctx *cli.Context) (interface{}, *cli.ExitError) {
|
||||
var data interface{}
|
||||
args := ctx.Args()
|
||||
if args.Present() {
|
||||
_, params, err := ParseParams(args, true)
|
||||
if err != nil {
|
||||
return nil, cli.NewExitError(fmt.Errorf("unable to parse 'data' parameter: %w", err), 1)
|
||||
}
|
||||
if len(params) != 1 {
|
||||
return nil, cli.NewExitError("'data' should be represented as a single parameter", 1)
|
||||
}
|
||||
data, err = smartcontract.ExpandParameterToEmitable(params[0])
|
||||
if err != nil {
|
||||
return nil, cli.NewExitError(fmt.Sprintf("failed to convert 'data' to emitable type: %s", err.Error()), 1)
|
||||
}
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// ParseContractConfig reads contract configuration file (.yaml) and returns unmarshalled ProjectConfig.
|
||||
func ParseContractConfig(confFile string) (ProjectConfig, error) {
|
||||
conf := ProjectConfig{}
|
||||
|
|
|
@ -202,7 +202,7 @@ func TestParseParams_CalledFromItself(t *testing.T) {
|
|||
|
||||
for str, expected := range testCases {
|
||||
input := strings.Split(str, " ")
|
||||
offset, actual, err := parseParams(input, false)
|
||||
offset, actual, err := ParseParams(input, false)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, expected.WordsRead, offset)
|
||||
require.Equal(t, expected.Value, actual)
|
||||
|
@ -218,7 +218,7 @@ func TestParseParams_CalledFromItself(t *testing.T) {
|
|||
|
||||
for _, str := range errorCases {
|
||||
input := strings.Split(str, " ")
|
||||
_, _, err := parseParams(input, false)
|
||||
_, _, err := ParseParams(input, false)
|
||||
require.Error(t, err)
|
||||
}
|
||||
}
|
||||
|
@ -400,7 +400,7 @@ func TestParseParams_CalledFromOutside(t *testing.T) {
|
|||
}
|
||||
for str, expected := range testCases {
|
||||
input := strings.Split(str, " ")
|
||||
offset, arr, err := parseParams(input, true)
|
||||
offset, arr, err := ParseParams(input, true)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, expected.WordsRead, offset)
|
||||
require.Equal(t, expected.Parameters, arr)
|
||||
|
@ -415,7 +415,7 @@ func TestParseParams_CalledFromOutside(t *testing.T) {
|
|||
}
|
||||
for _, str := range errorCases {
|
||||
input := strings.Split(str, " ")
|
||||
_, _, err := parseParams(input, true)
|
||||
_, _, err := ParseParams(input, true)
|
||||
require.Error(t, err)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue