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(examples): added check for overflow/underflow #2874

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 7 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,8 @@
package grc20

import (
"errors"
"math"
"std"
"strconv"

Expand Down Expand Up @@ -55,7 +57,11 @@ func (b *Banker) Mint(address std.Address, amount uint64) error {
return ErrInvalidAddress
}

// TODO: check for overflow
// check for overflow
if diff := math.MaxUint64 - b.totalSupply; diff < amount {
err := ufmt.Sprintf("you can't mint more than %d tokens", diff)
return errors.New(err)
Comment on lines +62 to +63
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This lines can be shortened using ufmt.Errorf.

Suggested change
err := ufmt.Sprintf("you can't mint more than %d tokens", diff)
return errors.New(err)
return ufmt.Errorf("you can't mint more than %d tokens", diff)

}

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 amount is higher than the total supply")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this supposed to be used somewhere? It should be deleted if not.

)

type Token interface {
Expand Down
21 changes: 21 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,21 @@
package main

import (
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")
// 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
Comment on lines +1 to +21
Copy link
Contributor

@leohhhn leohhhn Oct 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest, filetests should be discouraged. We should strive to write proper tests, that are more Go idiomatic.
They are more readable & more understandable by the broader community, and provide actual type checking on the errors. On the other hand, filetests are supereasy to write, but in the end just provide a println output.

Please write a unit test for this.

Loading