From 1e6e2acdf39421b3f01e62f0c0c416ddeb75753d Mon Sep 17 00:00:00 2001 From: malek Date: Mon, 30 Sep 2024 18:40:48 +0200 Subject: [PATCH 1/5] added check for overflow/underflow --- examples/gno.land/p/demo/grc/grc20/banker.gno | 9 +++++++- examples/gno.land/p/demo/grc/grc20/types.gno | 1 + .../p/demo/grc/grc20/z_0_filetest.gno | 22 +++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 examples/gno.land/p/demo/grc/grc20/z_0_filetest.gno diff --git a/examples/gno.land/p/demo/grc/grc20/banker.gno b/examples/gno.land/p/demo/grc/grc20/banker.gno index f643d3e2635..68823342719 100644 --- a/examples/gno.land/p/demo/grc/grc20/banker.gno +++ b/examples/gno.land/p/demo/grc/grc20/banker.gno @@ -1,6 +1,7 @@ package grc20 import ( + "errors" "std" "strconv" @@ -24,6 +25,8 @@ type Banker struct { token *token // to share the same pointer } +const uint64_max = 1<<64 - 1 + func NewBanker(name, symbol string, decimals uint) *Banker { if name == "" { panic("name should not be empty") @@ -55,7 +58,11 @@ func (b *Banker) Mint(address std.Address, amount uint64) error { return ErrInvalidAddress } - // TODO: check for overflow + // check for overflow + if uint64_max - b.totalSupply < amount { + err := ufmt.Sprintf("you can't mint more than %d tokens", uint64_max - b.totalSupply) + return errors.New(err) + } b.totalSupply += amount currentBalance := b.BalanceOf(address) diff --git a/examples/gno.land/p/demo/grc/grc20/types.gno b/examples/gno.land/p/demo/grc/grc20/types.gno index fe3aef349d9..2f43fd68f83 100644 --- a/examples/gno.land/p/demo/grc/grc20/types.gno +++ b/examples/gno.land/p/demo/grc/grc20/types.gno @@ -12,6 +12,7 @@ var ( ErrInsufficientAllowance = errors.New("insufficient allowance") ErrInvalidAddress = errors.New("invalid address") ErrCannotTransferToSelf = errors.New("cannot send transfer to self") + ErrUnderflow = errors.New("the a;ount is higher than the total supply") ) type Token interface { diff --git a/examples/gno.land/p/demo/grc/grc20/z_0_filetest.gno b/examples/gno.land/p/demo/grc/grc20/z_0_filetest.gno new file mode 100644 index 00000000000..e7a686c043f --- /dev/null +++ b/examples/gno.land/p/demo/grc/grc20/z_0_filetest.gno @@ -0,0 +1,22 @@ +package main + +import ( + "std" + "gno.land/p/demo/testutils" + + banker "gno.land/p/demo/grc/grc20" +) +const uint64_max = 1<<64 - 1 + +func main() { + b := banker.NewBanker("Dummy", "DUMMY", 6) + var owner = testutils.TestAddress("owner") + // Mint uint64_max - 1000 tokens for owner + b.Mint(owner, uint64_max - 1000) + // Try to mint 1001 tokens for owner, should fail because it exceeds the limit + println(b.Mint(owner, 1001).Error()) + +} + +// Output: +// you can't mint more than 1000 tokens From 295ab0d4b1a1954508a38c9db533d72690683b2d Mon Sep 17 00:00:00 2001 From: malek Date: Mon, 30 Sep 2024 18:52:40 +0200 Subject: [PATCH 2/5] gno fmt --- examples/gno.land/p/demo/grc/grc20/z_0_filetest.gno | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/examples/gno.land/p/demo/grc/grc20/z_0_filetest.gno b/examples/gno.land/p/demo/grc/grc20/z_0_filetest.gno index e7a686c043f..c712d2f9f35 100644 --- a/examples/gno.land/p/demo/grc/grc20/z_0_filetest.gno +++ b/examples/gno.land/p/demo/grc/grc20/z_0_filetest.gno @@ -1,18 +1,17 @@ package main import ( - "std" - "gno.land/p/demo/testutils" - banker "gno.land/p/demo/grc/grc20" + "gno.land/p/demo/testutils" ) + const uint64_max = 1<<64 - 1 func main() { b := banker.NewBanker("Dummy", "DUMMY", 6) - var owner = testutils.TestAddress("owner") + var owner = testutils.TestAddress("owner") // Mint uint64_max - 1000 tokens for owner - b.Mint(owner, uint64_max - 1000) + b.Mint(owner, uint64_max-1000) // Try to mint 1001 tokens for owner, should fail because it exceeds the limit println(b.Mint(owner, 1001).Error()) From 8fa25efee2804432058ab62743d99bc814c6fac3 Mon Sep 17 00:00:00 2001 From: malek Date: Tue, 1 Oct 2024 09:11:15 +0200 Subject: [PATCH 3/5] gnofmt --- examples/gno.land/p/demo/grc/grc20/banker.gno | 4 ++-- examples/gno.land/p/demo/grc/grc20/types.gno | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/gno.land/p/demo/grc/grc20/banker.gno b/examples/gno.land/p/demo/grc/grc20/banker.gno index 68823342719..5561eed60b3 100644 --- a/examples/gno.land/p/demo/grc/grc20/banker.gno +++ b/examples/gno.land/p/demo/grc/grc20/banker.gno @@ -59,8 +59,8 @@ func (b *Banker) Mint(address std.Address, amount uint64) error { } // check for overflow - if uint64_max - b.totalSupply < amount { - err := ufmt.Sprintf("you can't mint more than %d tokens", uint64_max - b.totalSupply) + if uint64_max-b.totalSupply < amount { + err := ufmt.Sprintf("you can't mint more than %d tokens", uint64_max-b.totalSupply) return errors.New(err) } diff --git a/examples/gno.land/p/demo/grc/grc20/types.gno b/examples/gno.land/p/demo/grc/grc20/types.gno index 2f43fd68f83..bcbe970f466 100644 --- a/examples/gno.land/p/demo/grc/grc20/types.gno +++ b/examples/gno.land/p/demo/grc/grc20/types.gno @@ -12,7 +12,7 @@ var ( ErrInsufficientAllowance = errors.New("insufficient allowance") ErrInvalidAddress = errors.New("invalid address") ErrCannotTransferToSelf = errors.New("cannot send transfer to self") - ErrUnderflow = errors.New("the a;ount is higher than the total supply") + ErrUnderflow = errors.New("the amount is higher than the total supply") ) type Token interface { From 7e174beee7983376f87195554ca3f6c6a5a87360 Mon Sep 17 00:00:00 2001 From: malek Date: Tue, 1 Oct 2024 10:43:09 +0200 Subject: [PATCH 4/5] gno fmt 3 --- examples/gno.land/p/demo/grc/grc20/types.gno | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/gno.land/p/demo/grc/grc20/types.gno b/examples/gno.land/p/demo/grc/grc20/types.gno index bcbe970f466..5911f495265 100644 --- a/examples/gno.land/p/demo/grc/grc20/types.gno +++ b/examples/gno.land/p/demo/grc/grc20/types.gno @@ -12,7 +12,7 @@ var ( ErrInsufficientAllowance = errors.New("insufficient allowance") ErrInvalidAddress = errors.New("invalid address") ErrCannotTransferToSelf = errors.New("cannot send transfer to self") - ErrUnderflow = errors.New("the amount is higher than the total supply") + ErrUnderflow = errors.New("the amount is higher than the total supply") ) type Token interface { From 0356eca4975bd7a11ce676b7e9cd7bff49ed59da Mon Sep 17 00:00:00 2001 From: malek Date: Thu, 3 Oct 2024 09:18:13 +0200 Subject: [PATCH 5/5] use math.MaxUint64, modify if condition --- examples/gno.land/p/demo/grc/grc20/banker.gno | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/examples/gno.land/p/demo/grc/grc20/banker.gno b/examples/gno.land/p/demo/grc/grc20/banker.gno index 5561eed60b3..1fae31f9283 100644 --- a/examples/gno.land/p/demo/grc/grc20/banker.gno +++ b/examples/gno.land/p/demo/grc/grc20/banker.gno @@ -2,6 +2,7 @@ package grc20 import ( "errors" + "math" "std" "strconv" @@ -25,8 +26,6 @@ type Banker struct { token *token // to share the same pointer } -const uint64_max = 1<<64 - 1 - func NewBanker(name, symbol string, decimals uint) *Banker { if name == "" { panic("name should not be empty") @@ -59,8 +58,8 @@ func (b *Banker) Mint(address std.Address, amount uint64) error { } // check for overflow - if uint64_max-b.totalSupply < amount { - err := ufmt.Sprintf("you can't mint more than %d tokens", uint64_max-b.totalSupply) + if diff := math.MaxUint64 - b.totalSupply; diff < amount { + err := ufmt.Sprintf("you can't mint more than %d tokens", diff) return errors.New(err) }