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 5 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
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
MalekLahbib marked this conversation as resolved.
Show resolved Hide resolved

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 {
MalekLahbib marked this conversation as resolved.
Show resolved Hide resolved
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 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