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

refactor: GRC20 #2314

Draft
wants to merge 32 commits into
base: master
Choose a base branch
from
Draft

refactor: GRC20 #2314

wants to merge 32 commits into from

Conversation

leohhhn
Copy link
Contributor

@leohhhn leohhhn commented Jun 10, 2024

Description

Closes: #2294

This PR refactors the GRC20 folder to follow the ERC20 spec as close as possibly.

Contributors' checklist...
  • Added new tests, or not needed, or not feasible
  • Provided an example (e.g. screenshot) to aid review or the PR is self-explanatory
  • Updated the official documentation or not needed
  • No breaking changes were made, or a BREAKING CHANGE: xxx message was included in the description
  • Added references to related issues and PRs
  • Provided any useful hints for running manual tests
  • Added new benchmarks to generated graphs, if any. More info here.

@github-actions github-actions bot added the 🧾 package/realm Tag used for new Realms or Packages. label Jun 10, 2024
@leohhhn
Copy link
Contributor Author

leohhhn commented Jun 10, 2024

Interesting discussion point: #2316

@leohhhn
Copy link
Contributor Author

leohhhn commented Jun 12, 2024

Opening up for reviews - grc20 package, foo20, wugnot & grc20 factory realms have been refactored, need to fix up some other stuff that is failing due to the changed API, awaiting a reply from @moul

@leohhhn leohhhn marked this pull request as ready for review June 12, 2024 12:24
@leohhhn leohhhn requested review from a team as code owners June 12, 2024 12:24
@leohhhn leohhhn requested review from deelawn and mvertes and removed request for a team June 12, 2024 12:24
Copy link
Contributor

@tbruyelle tbruyelle left a comment

Choose a reason for hiding this comment

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

I really like the simplicity of this version.

With grc20factory that acts as a grc20 multiplexer, I wonder if we still need to have the IGRC20 interface. This interface was initially designed to make *grc20.Token implementation shareable between realms, but now these realms can just use the grc20factory right ?

@leohhhn
Copy link
Contributor Author

leohhhn commented Jun 17, 2024

@tbruyelle I think both have valid use cases. People can choose to use the factory, or simply write their own realm importing the GRC20 package.

ApprovalEvent = "Approval"
)

func NewGRC20Token(name, symbol string, decimals uint8) *Token {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
func NewGRC20Token(name, symbol string, decimals uint8) *Token {
func NewToken(name, symbol string, decimals uint8) *Token {

grc20 is the package name.

Copy link
Member

Choose a reason for hiding this comment

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

i think we should not have a NewToken helper, but only a NewBanker one.

"gno.land/p/demo/ufmt"
)

type Token struct {
Copy link
Member

Choose a reason for hiding this comment

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

This looks good, but you need to have a type Banker interface { Token /* embedded */, Mint() } which is what you initializes first.

Copy link
Member

Choose a reason for hiding this comment

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

the banker should also have all the "admin" helpers, such as ways to call Transfer() and specifying a custom from.

}

func (t *Token) BalanceOf(owner std.Address) (balance uint64) {
mustBeValid(owner)
Copy link
Member

Choose a reason for hiding this comment

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

we can remove this one.

return rawAllowance.(uint64)
}

func (t *Token) Mint(account std.Address, amount uint64) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
func (t *Token) Mint(account std.Address, amount uint64) {
func (t *Banker) Mint(account std.Address, amount uint64) {

panic(err)
}

t.totalSupply = sum
Copy link
Member

Choose a reason for hiding this comment

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

shouldn't you also mint the tokens to the to address?

Comment on lines +24 to +25
o = ownable.New()
o.TransferOwnership("g125em6arxsnj49vx35f0n0z34putv5ty3376fg5") // @admin
Copy link
Member

Choose a reason for hiding this comment

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

Let's remove the token owner completely and expose a "Faucet" function for the demonstration.

See #2388 for an example.

Copy link
Member

Choose a reason for hiding this comment

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

I prefer if you keep this file as a test, or else, please implement it completely using standard unit tests.

if err != nil {
panic(err)
}
wugnot.Approve(users.Resolve(spender), amount)
Copy link
Member

Choose a reason for hiding this comment

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

why removing the panic?

return ufmt.Sprintf("%d\n", balance)
case c == 2 && parts[0] == "balance": // pkgpath:balance/address_or_username
owner := pusers.AddressOrName(parts[1])
return ufmt.Sprintf("%d\n", wugnot.BalanceOf(users.Resolve(owner)))
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
return ufmt.Sprintf("%d\n", wugnot.BalanceOf(users.Resolve(owner)))
balance := wugnot.BalanceOf(users.Resolve(owner))
return ufmt.Sprintf("%d\n", balance)

func (t *Token) spendAllowance(owner, spender std.Address, value uint64) {
currentAllowance := t.Allowance(owner, spender)

if currentAllowance != math.MaxUint64 {
Copy link
Member

Choose a reason for hiding this comment

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

This exception should be documented. If I'm not mistaken, you consider a MaxUint64 allowance to be equivalent to an infinite allowance, correct?

unknown := std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") // valid but never used.

admin := testutils.TestAddress("admin") // g1v9kxjcm9ta047h6lta047h6lta047h6lzd40gh
manfred := testutils.TestAddress("manfred") // g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
manfred := testutils.TestAddress("manfred") // g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm
alice := testutils.TestAddress("alice") // xxx

let's keep "manfred" when it's really my address.

t := tokenWithAdmin{
token: newToken,
admin: admin,
fauceted: false, // faucet disabled by default
Copy link
Member

Choose a reason for hiding this comment

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

If disabled by default, please register one or two tokens in init() that will be fauceted as true by default, so this package can be easily tested.

moul added a commit that referenced this pull request Jun 19, 2024
```go
// Package bar20 is similar to foo20 but exposes a safe-object that can be used
// by `maketx run`, another contract importing foo20, and in the future when
// we'll support `maketx call Token.XXX`.
```

This package currently has limited functionality, but it should become
more useful in the future.

I'm using it to demonstrate the rationale for having two implementations
in the `grc20` package - one with a banker, and one with a safe object.

Related with #2314

---------

Signed-off-by: moul <94029+moul@users.noreply.github.com>
@moul
Copy link
Member

moul commented Jul 7, 2024

#2529

moul added a commit that referenced this pull request Jul 7, 2024
Main changes:
- rename `AdminToken` -> `Banker`
- rename `GRC20` -> `Token`
- remove unused helpers
- remove vault (temporarily, will be reimplemented)
- remove the returner ˋerror` when unnecessary
- use `std.Emit`
- use uassert for testing
- better file naming and organization for improved readability

Fixes #2294
Replaces #2314 (h/t @leohhhn)
~Depends on #2534~
BREAKING CHANGE

---------

Signed-off-by: moul <94029+moul@users.noreply.github.com>
Co-authored-by: Leon Hudak <33522493+leohhhn@users.noreply.github.com>
@moul moul marked this pull request as draft July 8, 2024 11:43
gfanton pushed a commit to gfanton/gno that referenced this pull request Jul 23, 2024
```go
// Package bar20 is similar to foo20 but exposes a safe-object that can be used
// by `maketx run`, another contract importing foo20, and in the future when
// we'll support `maketx call Token.XXX`.
```

This package currently has limited functionality, but it should become
more useful in the future.

I'm using it to demonstrate the rationale for having two implementations
in the `grc20` package - one with a banker, and one with a safe object.

Related with gnolang#2314

---------

Signed-off-by: moul <94029+moul@users.noreply.github.com>
gfanton pushed a commit to gfanton/gno that referenced this pull request Jul 23, 2024
Main changes:
- rename `AdminToken` -> `Banker`
- rename `GRC20` -> `Token`
- remove unused helpers
- remove vault (temporarily, will be reimplemented)
- remove the returner ˋerror` when unnecessary
- use `std.Emit`
- use uassert for testing
- better file naming and organization for improved readability

Fixes gnolang#2294
Replaces gnolang#2314 (h/t @leohhhn)
~Depends on gnolang#2534~
BREAKING CHANGE

---------

Signed-off-by: moul <94029+moul@users.noreply.github.com>
Co-authored-by: Leon Hudak <33522493+leohhhn@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🧾 package/realm Tag used for new Realms or Packages.
Projects
Status: No status
Status: In Progress
Development

Successfully merging this pull request may close these issues.

[GRC] Refactor GRC20
4 participants