Skip to content

Commit

Permalink
added check for overflow/underflow
Browse files Browse the repository at this point in the history
  • Loading branch information
MalekLahbib committed Sep 30, 2024
1 parent 1e27785 commit 1e6e2ac
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
9 changes: 8 additions & 1 deletion examples/gno.land/p/demo/grc/grc20/banker.gno
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package grc20

import (
"errors"
"std"
"strconv"

Expand All @@ -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")
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions examples/gno.land/p/demo/grc/grc20/types.gno
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
22 changes: 22 additions & 0 deletions examples/gno.land/p/demo/grc/grc20/z_0_filetest.gno
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 1e6e2ac

Please sign in to comment.