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(gnovm): add Coin constructor and more functionality #2104

Merged
merged 41 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
1ce96e2
match receivers
leohhhn May 14, 2024
723f489
Merge branch 'master' into feat/better-coins
leohhhn May 14, 2024
10939cc
Merge branch 'master' into feat/better-coins
leohhhn May 15, 2024
b4fdb4e
remove data
leohhhn May 15, 2024
ec43225
add functions & constructors
leohhhn May 15, 2024
8fd229c
fix test
leohhhn May 15, 2024
f9ee7ed
fix test
leohhhn May 15, 2024
3c6b0be
use NewCoin()
leohhhn May 15, 2024
c3c6db0
newcoins
leohhhn May 15, 2024
f58fe4d
string
leohhhn May 17, 2024
6431a0e
sav
leohhhn May 17, 2024
3f34cc6
save
leohhhn May 17, 2024
b7b58cf
save
leohhhn May 17, 2024
ddedaa3
save
leohhhn May 17, 2024
226027c
regexp
leohhhn May 17, 2024
c06b7c2
put regexp back
leohhhn May 17, 2024
46a8061
add txtar tests
leohhhn May 17, 2024
1ccae6c
remove unused
leohhhn May 17, 2024
f44f9a4
Merge branch 'master' into feat/better-coins
leohhhn May 17, 2024
de5f3a3
extract panic
leohhhn May 17, 2024
50a9696
remove isZero check
leohhhn May 18, 2024
22a171e
remvove ctor
leohhhn May 21, 2024
779ff9a
remove new coin objects
leohhhn May 21, 2024
16109c4
use loadpkg
leohhhn May 21, 2024
b423d29
copy to stdshim
leohhhn May 21, 2024
b367b8f
Merge branch 'master' into feat/better-coins
thehowl May 23, 2024
a5696e9
Merge branch 'master' into feat/better-coins
thehowl May 24, 2024
ae0fc4c
print errors from transpilePkg
thehowl May 24, 2024
e857291
do not use overflow in stdshim
thehowl May 24, 2024
c343ef3
before milos nitpicks on this
thehowl May 24, 2024
d33a0d5
Merge branch 'master' into feat/better-coins
leohhhn May 26, 2024
07cf1c9
remove unsafe methods, add comments]
leohhhn May 26, 2024
c1a27c1
fix test
leohhhn May 26, 2024
c82680a
add docs, modify coins ctor, modify txtar tests
leohhhn May 26, 2024
3edd473
Merge branch 'master' into feat/better-coins
leohhhn May 26, 2024
a7b13ac
copy to stdshim
leohhhn May 26, 2024
62631ad
remove overflow from stdshim
leohhhn May 26, 2024
3762497
better comment
leohhhn May 27, 2024
41c2675
fix tests
leohhhn May 27, 2024
43a6d22
fix stdshim
leohhhn May 27, 2024
2b6fb4a
Merge branch 'master' into feat/better-coins
thehowl May 27, 2024
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
2 changes: 1 addition & 1 deletion examples/gno.land/r/demo/boards/public.gno
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func CreateBoard(name string) BoardID {

func checkAnonFee() bool {
sent := std.GetOrigSend()
anonFeeCoin := std.Coin{"ugnot", int64(gDefaultAnonFee)}
anonFeeCoin := std.NewCoin("ugnot", int64(gDefaultAnonFee))
if len(sent) == 1 && sent[0].IsGTE(anonFeeCoin) {
return true
}
Expand Down
2 changes: 1 addition & 1 deletion examples/gno.land/r/demo/users/users.gno
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func Register(inviter std.Address, name string, profile string) {
panic("should not happen") // because std.AssertOrigCall().
}
sentCoins := std.GetOrigSend()
minCoin := std.Coin{"ugnot", minFee}
minCoin := std.NewCoin("ugnot", minFee)
if inviter == "" {
// banker := std.GetBanker(std.BankerTypeOrigSend)
if len(sentCoins) == 1 && sentCoins[0].IsGTE(minCoin) {
Expand Down
4 changes: 2 additions & 2 deletions examples/gno.land/r/demo/users/z_0_filetest.gno
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
)

func main() {
std.TestSetOrigSend(std.Coins{{"dontcare", 1}}, nil)
std.TestSetOrigSend(std.Coins{std.NewCoin("dontcare", 1)}, nil)
users.Register("", "gnouser", "my profile")
println("done")
}

// Error:
// invalid coin denominations: dontcare
// comparing different denominations: dontcare, ugnot
2 changes: 1 addition & 1 deletion examples/gno.land/r/gnoland/faucet/admin.gno
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func AdminSetTransferLimit(amount int64) string {
if err := assertIsAdmin(); err != nil {
return err.Error()
}
gLimit = std.Coin{Denom: "ugnot", Amount: amount}
gLimit = std.NewCoin("ugnot", amount)
return ""
}

Expand Down
4 changes: 2 additions & 2 deletions examples/gno.land/r/gnoland/faucet/faucet.gno
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var (
gTotalTransfers = uint(0)

// per request limit, 350 gnot
gLimit std.Coin = std.Coin{"ugnot", 350000000}
gLimit std.Coin = std.NewCoin("ugnot", 350000000)
)

func Transfer(to std.Address, send int64) string {
Expand All @@ -37,7 +37,7 @@ func Transfer(to std.Address, send int64) string {
if send > gLimit.Amount {
return errors.New("Per request limit " + gLimit.String() + " exceed").Error()
}
sendCoins := std.Coins{std.Coin{Denom: "ugnot", Amount: send}}
sendCoins := std.Coins{std.NewCoin("ugnot", send)}

gTotalTransferred = gTotalTransferred.Add(sendCoins)
gTotalTransfers++
Expand Down
66 changes: 62 additions & 4 deletions gnovm/stdlibs/std/coins.gno
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package std

import "strconv"
import (
"strconv"
)

// NOTE: this is selectly copied over from pkgs/std/coin.go
// TODO: import all functionality(?).
Expand All @@ -13,21 +15,77 @@ type Coin struct {
Amount int64 `json:"amount"`
}

// NewCoin returns a new coin with a denomination and amount
func NewCoin(denom string, amount int64) Coin {
zivkovicmilos marked this conversation as resolved.
Show resolved Hide resolved
return Coin{
Denom: denom,
Amount: amount,
}
}

// String provides a human-readable representation of a coin
func (c Coin) String() string {
if c.IsZero() {
return ""
}
ajnavarro marked this conversation as resolved.
Show resolved Hide resolved

return strconv.Itoa(int(c.Amount)) + c.Denom
}

// IsGTE returns true if they are the same type and the receiver is
// an equal or greater value
func (c Coin) IsGTE(other Coin) bool {
if c.Denom != other.Denom {
panic("invalid coin denominations: " + c.Denom)
panic("comparing different denominations: " + c.Denom + ", " + other.Denom)
}
ajnavarro marked this conversation as resolved.
Show resolved Hide resolved
return c.Amount >= other.Amount
}

// IsLT returns true if they are the same type and the receiver is
// a smaller value
func (c Coin) IsLT(other Coin) bool {
if c.Denom != other.Denom {
panic("comparing different denominations: " + c.Denom + ", " + other.Denom)
}
return c.Amount < other.Amount
}

// IsEqual returns true if the two sets of Coins have the same value
func (c Coin) IsEqual(other Coin) bool {
if c.Denom != other.Denom {
panic("comparing different denominations: " + c.Denom + ", " + other.Denom)
}
zivkovicmilos marked this conversation as resolved.
Show resolved Hide resolved
return c.Amount == other.Amount
}

// IsZero returns if this represents no money
func (c Coin) IsZero() bool {
return c.Amount == 0
}

func NewCoins(val ...interface{}) Coins {
ajnavarro marked this conversation as resolved.
Show resolved Hide resolved
if len(val)%2 != 0 {
panic("invalid number of arguments")
}

cz := Coins{}
for i := 0; i < len(val)-1; i++ {
denom := val[i].(string)
amt := val[i+1].(int64)
cz = append(cz, NewCoin(denom, amt))
}

return cz
}

// Coins is a set of Coin, one per currency
type Coins []Coin

func (cz Coins) String() string {
if len(cz) == 0 {
return ""
}

res := ""
for i, c := range cz {
if i > 0 {
Expand All @@ -47,9 +105,9 @@ func (cz Coins) AmountOf(denom string) int64 {
return 0
}

func (a Coins) Add(b Coins) Coins {
func (cz Coins) Add(b Coins) Coins {
c := Coins{}
for _, ac := range a {
for _, ac := range cz {
bc := b.AmountOf(ac.Denom)
ac.Amount += bc
c = append(c, ac)
Expand Down
63 changes: 58 additions & 5 deletions gnovm/stdlibs/stdshim/coins.gno
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package std

import "strconv"
import (
"strconv"
)

// NOTE: this is selectly copied over from pkgs/std/coin.go
// TODO: import all functionality(?).
// TODO: implement Coin/Coins constructors.
zivkovicmilos marked this conversation as resolved.
Show resolved Hide resolved

// Coin hold some amount of one currency.
// A negative amount is invalid.
Expand All @@ -12,21 +15,62 @@ type Coin struct {
Amount int64 `json:"amount"`
}

// NewCoin returns a new coin with a denomination and amount
func NewCoin(denom string, amount int64) Coin {
return Coin{
Denom: denom,
Amount: amount,
}
}

// String provides a human-readable representation of a coin
func (c Coin) String() string {
if c.IsZero() {
return ""
}
zivkovicmilos marked this conversation as resolved.
Show resolved Hide resolved

return strconv.Itoa(int(c.Amount)) + c.Denom
}

// IsGTE returns true if they are the same type and the receiver is
// an equal or greater value
func (c Coin) IsGTE(other Coin) bool {
if c.Denom != other.Denom {
panic("invalid coin denominations: " + c.Denom)
panic("comparing different denominations: " + c.Denom + ", " + other.Denom)
}
return c.Amount >= other.Amount
}

// IsLT returns true if they are the same type and the receiver is
// a smaller value
func (c Coin) IsLT(other Coin) bool {
if c.Denom != other.Denom {
panic("comparing different denominations: " + c.Denom + ", " + other.Denom)
}
zivkovicmilos marked this conversation as resolved.
Show resolved Hide resolved
return c.Amount < other.Amount
}

// IsEqual returns true if the two sets of Coins have the same value
func (c Coin) IsEqual(other Coin) bool {
if c.Denom != other.Denom {
panic("comparing different denominations: " + c.Denom + ", " + other.Denom)
}
return c.Amount == other.Amount
}

// IsZero returns if this represents no money
leohhhn marked this conversation as resolved.
Show resolved Hide resolved
func (c Coin) IsZero() bool {
return c.Amount == 0
}

// Coins is a set of Coin, one per currency
type Coins []Coin

func (cz Coins) String() string {
if len(cz) == 0 {
return ""
}

res := ""
for i, c := range cz {
if i > 0 {
Expand All @@ -46,9 +90,9 @@ func (cz Coins) AmountOf(denom string) int64 {
return 0
}

func (a Coins) Add(b Coins) Coins {
func (cz Coins) Add(b Coins) Coins {
c := Coins{}
for _, ac := range a {
for _, ac := range cz {
bc := b.AmountOf(ac.Denom)
ac.Amount += bc
c = append(c, ac)
Expand All @@ -62,4 +106,13 @@ func (a Coins) Add(b Coins) Coins {
return c
}

// TODO implement Coin/Coins constructors.
// expand for usage within natively bound functions.
zivkovicmilos marked this conversation as resolved.
Show resolved Hide resolved
func (cz Coins) expandNative() (denoms []string, amounts []int64) {
denoms = make([]string, len(cz))
amounts = make([]int64, len(cz))
for i, coin := range cz {
denoms[i] = coin.Denom
amounts[i] = coin.Amount
}
return denoms, amounts
}
2 changes: 1 addition & 1 deletion gnovm/tests/files/zrealm_natbind0.gno
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func main() {
// "Closure": {
// "@type": "/gno.RefValue",
// "Escaped": true,
// "ObjectID": "a7f5397443359ea76c50be82c77f1f893a060925:7"
// "ObjectID": "a7f5397443359ea76c50be82c77f1f893a060925:8"
// },
// "FileName": "native.gno",
// "IsMethod": false,
Expand Down
Loading