Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sdk/vm): support float as arguments to maketx call #1434

Merged
merged 7 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions gno.land/cmd/gnoland/testdata/float-arg.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# test for float args

## start a new node
gnoland start

gnokey maketx addpkg -pkgdir $WORK -pkgpath gno.land/r/demo/float_realm -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1

gnokey maketx call -pkgpath gno.land/r/demo/float_realm --func AddF32 -args 10.5 --args 20 --gas-fee 1000000ugnot --gas-wanted 2000000 --broadcast -chainid=tendermint_test test1
stdout '(30.5 float32)'

gnokey maketx call -pkgpath gno.land/r/demo/float_realm --func AddF64 -args 3.1 --args 2.2 --gas-fee 1000000ugnot --gas-wanted 2000000 --broadcast -chainid=tendermint_test test1
stdout '(5.3[0-9]* float64)'

-- float_realm.gno --
package float_realm

func AddF32(x, y float32) float32 {
return x + y
}

func AddF64(x, y float64) float64 {
return x + y
}

70 changes: 40 additions & 30 deletions gno.land/pkg/sdk/vm/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@
"fmt"
"strconv"

"github.com/cockroachdb/apd/v3"
gno "github.com/gnolang/gno/gnovm/pkg/gnolang"
)

func assertCharNotPlus(b byte) {
if b == '+' {
panic("numbers cannot start with +")

Check warning on line 14 in gno.land/pkg/sdk/vm/convert.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/sdk/vm/convert.go#L12-L14

Added lines #L12 - L14 were not covered by tests
}
}
deelawn marked this conversation as resolved.
Show resolved Hide resolved

// These convert string representations of public-facing arguments to GNO types.
// The limited set of input types available should map 1:1 to types supported
// in FunctionSignature{}.
Expand All @@ -34,9 +41,7 @@
tv.SetString(gno.StringValue(arg))
return
case gno.IntType:
if arg[0] == '+' {
panic("numbers cannot start with +")
}
assertCharNotPlus(arg[0])

Check warning on line 44 in gno.land/pkg/sdk/vm/convert.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/sdk/vm/convert.go#L44

Added line #L44 was not covered by tests
ajnavarro marked this conversation as resolved.
Show resolved Hide resolved
i64, err := strconv.ParseInt(arg, 10, 64)
if err != nil {
panic(fmt.Sprintf(
Expand All @@ -46,9 +51,7 @@
tv.SetInt(int(i64))
return
case gno.Int8Type:
if arg[0] == '+' {
panic("numbers cannot start with +")
}
assertCharNotPlus(arg[0])

Check warning on line 54 in gno.land/pkg/sdk/vm/convert.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/sdk/vm/convert.go#L54

Added line #L54 was not covered by tests
i8, err := strconv.ParseInt(arg, 10, 8)
if err != nil {
panic(fmt.Sprintf(
Expand All @@ -58,9 +61,7 @@
tv.SetInt8(int8(i8))
return
case gno.Int16Type:
if arg[0] == '+' {
panic("numbers cannot start with +")
}
assertCharNotPlus(arg[0])

Check warning on line 64 in gno.land/pkg/sdk/vm/convert.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/sdk/vm/convert.go#L64

Added line #L64 was not covered by tests
i16, err := strconv.ParseInt(arg, 10, 16)
if err != nil {
panic(fmt.Sprintf(
Expand All @@ -70,9 +71,7 @@
tv.SetInt16(int16(i16))
return
case gno.Int32Type:
if arg[0] == '+' {
panic("numbers cannot start with +")
}
assertCharNotPlus(arg[0])

Check warning on line 74 in gno.land/pkg/sdk/vm/convert.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/sdk/vm/convert.go#L74

Added line #L74 was not covered by tests
i32, err := strconv.ParseInt(arg, 10, 32)
if err != nil {
panic(fmt.Sprintf(
Expand All @@ -82,9 +81,7 @@
tv.SetInt32(int32(i32))
return
case gno.Int64Type:
if arg[0] == '+' {
panic("numbers cannot start with +")
}
assertCharNotPlus(arg[0])

Check warning on line 84 in gno.land/pkg/sdk/vm/convert.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/sdk/vm/convert.go#L84

Added line #L84 was not covered by tests
i64, err := strconv.ParseInt(arg, 10, 64)
if err != nil {
panic(fmt.Sprintf(
Expand All @@ -94,9 +91,7 @@
tv.SetInt64(i64)
return
case gno.UintType:
if arg[0] == '+' {
panic("numbers cannot start with +")
}
assertCharNotPlus(arg[0])

Check warning on line 94 in gno.land/pkg/sdk/vm/convert.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/sdk/vm/convert.go#L94

Added line #L94 was not covered by tests
u64, err := strconv.ParseUint(arg, 10, 64)
if err != nil {
panic(fmt.Sprintf(
Expand All @@ -106,9 +101,7 @@
tv.SetUint(uint(u64))
return
case gno.Uint8Type:
if arg[0] == '+' {
panic("numbers cannot start with +")
}
assertCharNotPlus(arg[0])

Check warning on line 104 in gno.land/pkg/sdk/vm/convert.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/sdk/vm/convert.go#L104

Added line #L104 was not covered by tests
u8, err := strconv.ParseUint(arg, 10, 8)
if err != nil {
panic(fmt.Sprintf(
Expand All @@ -118,9 +111,7 @@
tv.SetUint8(uint8(u8))
return
case gno.Uint16Type:
if arg[0] == '+' {
panic("numbers cannot start with +")
}
assertCharNotPlus(arg[0])

Check warning on line 114 in gno.land/pkg/sdk/vm/convert.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/sdk/vm/convert.go#L114

Added line #L114 was not covered by tests
u16, err := strconv.ParseUint(arg, 10, 16)
if err != nil {
panic(fmt.Sprintf(
Expand All @@ -130,9 +121,7 @@
tv.SetUint16(uint16(u16))
return
case gno.Uint32Type:
if arg[0] == '+' {
panic("numbers cannot start with +")
}
assertCharNotPlus(arg[0])

Check warning on line 124 in gno.land/pkg/sdk/vm/convert.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/sdk/vm/convert.go#L124

Added line #L124 was not covered by tests
u32, err := strconv.ParseUint(arg, 10, 32)
if err != nil {
panic(fmt.Sprintf(
Expand All @@ -142,9 +131,7 @@
tv.SetUint32(uint32(u32))
return
case gno.Uint64Type:
if arg[0] == '+' {
panic("numbers cannot start with +")
}
assertCharNotPlus(arg[0])

Check warning on line 134 in gno.land/pkg/sdk/vm/convert.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/sdk/vm/convert.go#L134

Added line #L134 was not covered by tests
u64, err := strconv.ParseUint(arg, 10, 64)
if err != nil {
panic(fmt.Sprintf(
Expand All @@ -153,6 +140,14 @@
}
tv.SetUint64(u64)
return
case gno.Float32Type:
value := convertFloat(arg, 32)
tv.SetFloat32(float32(value))
return
case gno.Float64Type:
value := convertFloat(arg, 64)
tv.SetFloat64(value)
return

Check warning on line 150 in gno.land/pkg/sdk/vm/convert.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/sdk/vm/convert.go#L143-L150

Added lines #L143 - L150 were not covered by tests
default:
panic(fmt.Sprintf("unexpected primitive type %s", bt.String()))
}
Expand Down Expand Up @@ -195,3 +190,18 @@
panic(fmt.Sprintf("unexpected type in contract arg: %v", argT))
}
}

func convertFloat(value string, precision int) float64 {
assertCharNotPlus(value[0])
dec, _, err := apd.NewFromString(value)
if err != nil {
panic(fmt.Sprintf("error parsing float%d %s: %v", precision, value, err))

Check warning on line 198 in gno.land/pkg/sdk/vm/convert.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/sdk/vm/convert.go#L194-L198

Added lines #L194 - L198 were not covered by tests
}

f64, err := strconv.ParseFloat(dec.String(), precision)
if err != nil {
panic(fmt.Sprintf("error value exceeds float%d precision %s: %v", precision, value, err))

Check warning on line 203 in gno.land/pkg/sdk/vm/convert.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/sdk/vm/convert.go#L201-L203

Added lines #L201 - L203 were not covered by tests
}

return f64

Check warning on line 206 in gno.land/pkg/sdk/vm/convert.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/sdk/vm/convert.go#L206

Added line #L206 was not covered by tests
}
Loading