Skip to content

Commit f6cf7fc

Browse files
committed
wip: use args as input
Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com>
1 parent 3e6c14c commit f6cf7fc

File tree

4 files changed

+64
-93
lines changed

4 files changed

+64
-93
lines changed

gno.land/pkg/keyscli/call.go

+5-13
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,6 @@ func (c *MakeCallCfg) RegisterFlags(fs *flag.FlagSet) {
6969
"args",
7070
"arguments to contract",
7171
)
72-
73-
fs.StringVar(
74-
&c.Request,
75-
"request",
76-
"",
77-
"arguments to contract",
78-
)
7972
}
8073

8174
func execMakeCall(cfg *MakeCallCfg, args []string, io commands.IO) error {
@@ -126,12 +119,11 @@ func execMakeCall(cfg *MakeCallCfg, args []string, io commands.IO) error {
126119

127120
// construct msg & tx and marshal.
128121
msg := vm.MsgCall{
129-
Caller: caller,
130-
Send: send,
131-
PkgPath: cfg.PkgPath,
132-
Func: fnc,
133-
Args: cfg.Args,
134-
JSONRequest: []byte(cfg.Request),
122+
Caller: caller,
123+
Send: send,
124+
PkgPath: cfg.PkgPath,
125+
Func: fnc,
126+
Args: cfg.Args,
135127
}
136128
tx := std.Tx{
137129
Msgs: []std.Msg{msg},

gno.land/pkg/sdk/vm/convert_json.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package vm
22

33
import (
4-
"encoding/json"
5-
"fmt"
64
"reflect"
75

86
"github.com/gnolang/gno/gnovm/pkg/gnolang"
@@ -39,7 +37,6 @@ func UnmarshalNativeValueJSON(b []byte, t gno.Type) (*gno.TypedValue, error) {
3937
}
4038

4139
func MarshalJSON(tv *gno.TypedValue) ([]byte, error) {
42-
fmt.Println("ret tv:", tv)
4340
rv := gnolang.Gno2GoValue(tv, reflect.Value{})
44-
return json.Marshal(rv.Interface())
41+
return amino.Marshal(rv.Interface())
4542
}

gno.land/pkg/sdk/vm/keeper.go

+53-70
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"os"
99
"regexp"
1010
"strings"
11-
"unicode"
1211

1312
gno "github.com/gnolang/gno/gnovm/pkg/gnolang"
1413
"github.com/gnolang/gno/gnovm/stdlibs"
@@ -220,40 +219,30 @@ func (vm *VMKeeper) Call(ctx sdk.Context, msg MsgCall) (res string, err error) {
220219
mpn.Define("pkg", gno.TypedValue{T: &gno.PackageType{}, V: pv})
221220
mpv := mpn.NewPackage()
222221

223-
fmt.Println("request", string(msg.JSONRequest))
224-
225-
// Request
226-
capitalize := func(str string) string {
227-
runes := []rune(str)
228-
runes[0] = unicode.ToUpper(runes[0])
229-
return string(runes)
230-
}
222+
if len(msg.Args) != len(ft.Params) {
223+
return "", fmt.Errorf("wrong number of arguments in call to %s: want %d got %d", fnc, len(ft.Params), len(msg.Args))
224+
}
225+
226+
request := make([]*gno.TypedValue, len(ft.Params))
227+
for i, arg := range msg.Args {
228+
pt := ft.Params[i].Type
229+
arg = strings.TrimSpace(arg)
230+
if len(arg) > 2 &&
231+
((arg[0] == '{' && arg[len(arg)-1] == '}') ||
232+
(arg[0] == '[' && arg[len(arg)-1] == ']')) {
233+
// Handle JSON argument
234+
request[i], err = UnmarshalJSON(store.GetAllocator(), store, []byte(arg), pt)
235+
if err != nil {
236+
return "", fmt.Errorf("unable to unmarshal arg#%d: %w", err)
237+
}
231238

232-
// Iterate through params
233-
requestParam := &gno.StructType{}
234-
// define gno type
235-
requestNF := len(ft.Params)
236-
requestFS := make([]gno.FieldType, requestNF)
237-
for i, param := range ft.Params {
238-
name := gno.Name(capitalize(string(param.Name)))
239-
if name == "" {
240-
name = gno.Name(fmt.Sprintf("Args%d", i))
239+
continue
241240
}
242241

243-
fmt.Println(name)
244-
requestFS[i] = gno.FieldType{
245-
Name: name,
246-
Type: param.Type,
247-
}
248-
}
249-
requestParam.PkgPath = mpn.PkgPath
250-
requestParam.Fields = requestFS
242+
tv := convertArgToGno(arg, pt)
243+
request[i] = &tv
251244

252-
requestTv, err := UnmarshalJSON(store.GetAllocator(), store, msg.JSONRequest, requestParam)
253-
if err != nil {
254-
return "", fmt.Errorf("unable to unmarshall json: %w", err)
255245
}
256-
fmt.Println("request tv", requestTv)
257246

258247
// Create expresion
259248
argslist := ""
@@ -265,7 +254,7 @@ func (vm *VMKeeper) Call(ctx sdk.Context, msg MsgCall) (res string, err error) {
265254
}
266255

267256
expr := fmt.Sprintf(`pkg.%s(%s)`, fnc, argslist)
268-
fmt.Println("expr", expr)
257+
fmt.Printf("expr%v\r\n", expr)
269258
xn := gno.MustParseExpr(expr)
270259

271260
// Send send-coins to pkg from caller.
@@ -283,14 +272,9 @@ func (vm *VMKeeper) Call(ctx sdk.Context, msg MsgCall) (res string, err error) {
283272
panic("variadic calls not yet supported")
284273
}
285274

286-
// if len(msg.Args) != len(ft.Params) {
287-
// panic(fmt.Sprintf("wrong number of arguments in call to %s: want %d got %d", fnc, len(ft.Params), len(msg.Args)))
288-
// }
289-
290-
fmt.Println("request tv type", requestTv.T.String())
291-
for i, arg := range requestTv.V.(*gno.StructValue).Fields {
275+
for i, arg := range request {
292276
cx.Args[i] = &gno.ConstExpr{
293-
TypedValue: arg,
277+
TypedValue: *arg,
294278
}
295279
}
296280

@@ -333,54 +317,53 @@ func (vm *VMKeeper) Call(ctx sdk.Context, msg MsgCall) (res string, err error) {
333317
// Result
334318

335319
// Iterate through params
336-
var result gno.TypedValue
320+
var response gno.TypedValue
337321
{
338322
var sv gno.StructValue
339323
var st gno.StructType
340324

341-
// define gno type
342-
resultNF := len(rtvs)
343-
resultFS := make([]gno.FieldType, resultNF)
344-
resultTV := make([]gno.TypedValue, resultNF)
345-
for i, rtv := range rtvs {
346-
name := gno.Name(fmt.Sprintf("Args%d", i))
347-
resultFS[i] = gno.FieldType{
348-
Name: name,
349-
Type: rtv.T,
350-
}
325+
// Generate result Typed Valye
326+
result := gno.TypedValue{
327+
V: &gno.ArrayValue{
328+
List: rtvs,
329+
},
330+
T: &gno.ArrayType{
331+
Elt: &gno.InterfaceType{},
332+
Len: len(rtvs),
333+
},
334+
}
351335

352-
fmt.Println(name, rtv.String())
353-
resultTV[i] = rtv
336+
// Define response fields
337+
responseFS := []gno.FieldType{
338+
gno.FieldType{
339+
Name: gno.Name("Result"),
340+
Type: result.T,
341+
},
342+
gno.FieldType{
343+
Name: gno.Name("CPUCycles"),
344+
Type: gno.Int64Type,
345+
},
354346
}
355-
resultFS = append(resultFS, gno.FieldType{
356-
Name: gno.Name("NumCycles"),
357-
Type: gno.Int64Type,
358-
})
347+
responseTV := make([]gno.TypedValue, 0, len(responseFS))
348+
349+
// Add result value
350+
responseTV = append(responseTV, result)
359351

360352
// Add cpucycle to responses
361353
cycle := gno.TypedValue{T: gno.Int64Type}
362354
cycle.SetInt64(m.Cycles)
363-
resultTV = append(resultTV, cycle)
355+
responseTV = append(responseTV, cycle)
364356

365357
st.PkgPath = mpn.PkgPath
366-
st.Fields = resultFS
367-
sv.Fields = resultTV
358+
st.Fields = responseFS
359+
sv.Fields = responseTV
368360

369-
result = gno.TypedValue{
370-
T: &st,
371-
V: &sv,
372-
}
361+
response.T = &st
362+
response.V = &sv
373363
}
374364

375365
ctx.Logger().Info("CPUCYCLES call", "num-cycles", m.Cycles)
376-
// for i, rtv := range rtvs {
377-
// res = res + rtv.String()
378-
// if i < len(rtvs)-1 {
379-
// res += "\n"
380-
// }
381-
// }
382-
383-
resraw, err := MarshalJSON(&result)
366+
resraw, err := MarshalJSON(&response)
384367
if err != nil {
385368
return "", fmt.Errorf("unable to unarmsahll result: %w", err)
386369
}

gno.land/pkg/sdk/vm/msgs.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,11 @@ func (msg MsgAddPackage) GetReceived() std.Coins {
8383

8484
// MsgCall - executes a Gno statement.
8585
type MsgCall struct {
86-
Caller crypto.Address `json:"caller" yaml:"caller"`
87-
Send std.Coins `json:"send" yaml:"send"`
88-
PkgPath string `json:"pkg_path" yaml:"pkg_path"`
89-
Func string `json:"func" yaml:"func"`
90-
Args []string `json:"args" yaml:"args"`
91-
JSONRequest []byte `json:"jsonRequest" yaml:"jsonRequest"`
86+
Caller crypto.Address `json:"caller" yaml:"caller"`
87+
Send std.Coins `json:"send" yaml:"send"`
88+
PkgPath string `json:"pkg_path" yaml:"pkg_path"`
89+
Func string `json:"func" yaml:"func"`
90+
Args []string `json:"args" yaml:"args"`
9291
}
9392

9493
var _ std.Msg = MsgCall{}

0 commit comments

Comments
 (0)