-
Notifications
You must be signed in to change notification settings - Fork 379
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
base: master
Are you sure you want to change the base?
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #2874 +/- ##
=======================================
Coverage 60.95% 60.95%
=======================================
Files 564 564
Lines 75273 75273
=======================================
+ Hits 45884 45886 +2
Misses 26017 26017
+ Partials 3372 3370 -2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
@@ -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") |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use the error(s) you defined, and also please write tests for these cases
@moul @deelawn @leohhhn I'm trying to figure out how is it possible to have underflow in Burn function, but for me it seems to be impossible as every function is protected with conditions. I'm doing some research to learn about erc20 known vulnerabilities/issues and figure out how to protect grc20 functions from them. I'm thinking about using "math/overflow' functions (as Add64, Sub64...) to add a protection/check layer. |
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 |
There was a problem hiding this comment.
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.
err := ufmt.Sprintf("you can't mint more than %d tokens", diff) | ||
return errors.New(err) |
There was a problem hiding this comment.
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
.
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) |
Contributors' checklist...
BREAKING CHANGE: xxx
message was included in the description