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

tests: vault tests #9

Merged
merged 6 commits into from
Oct 24, 2024
Merged
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: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ This is called Automated Liquidity Management (ALM).

## Get Started

### Contracts
### Realms

To tests the contracts, you first need to clone the gnoswap repository and add it to the gno root directory in the `examples` folder with the corresponding path between /p and /r.
To tests the realms, you first need to clone the gnoswap repository and add it to the gno root directory in the `examples` folder with the corresponding path between /p and /r.

You also need to do the same for foo and bar located at `realms/mock/foo` and `realms/mock/bar`

Then you can run the following commands:

```bash
gno test ./contracts
gno test ./realms
```

### Frontend
Expand Down
58 changes: 0 additions & 58 deletions contracts/vault_test.gno

This file was deleted.

File renamed without changes.
File renamed without changes.
82 changes: 82 additions & 0 deletions realms/mock/bar/bar.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package bar

import (
"strings"

"gno.land/p/demo/grc/grc20"
"gno.land/p/demo/ownable"
"gno.land/p/demo/ufmt"
pusers "gno.land/p/demo/users"
"gno.land/r/demo/users"
)

var (
banker *grc20.Banker
admin *ownable.Ownable
token grc20.Token
)

func init() {
admin = ownable.NewWithAddress("g1lmvrrrr4er2us84h2732sru76c9zl2nvknha8c") // ADMIN
banker = grc20.NewBanker("Bar", "BAR", 6)
banker.Mint(admin.Owner(), 100_000_000_000_000)
token = banker.Token()
}

func TotalSupply() uint64 { return token.TotalSupply() }

func BalanceOf(owner pusers.AddressOrName) uint64 {
ownerAddr := users.Resolve(owner)
return token.BalanceOf(ownerAddr)
}

func Allowance(owner, spender pusers.AddressOrName) uint64 {
ownerAddr := users.Resolve(owner)
spenderAddr := users.Resolve(spender)
return token.Allowance(ownerAddr, spenderAddr)
}

func Transfer(to pusers.AddressOrName, amount uint64) {
toAddr := users.Resolve(to)
checkErr(token.Transfer(toAddr, amount))
}

func Approve(spender pusers.AddressOrName, amount uint64) {
spenderAddr := users.Resolve(spender)
checkErr(token.Approve(spenderAddr, amount))
}

func TransferFrom(from, to pusers.AddressOrName, amount uint64) {
fromAddr := users.Resolve(from)
toAddr := users.Resolve(to)
checkErr(token.TransferFrom(fromAddr, toAddr, amount))
}

func Burn(from pusers.AddressOrName, amount uint64) {
admin.AssertCallerIsOwner()
fromAddr := users.Resolve(from)
checkErr(banker.Burn(fromAddr, amount))
}

func Render(path string) string {
parts := strings.Split(path, "/")
c := len(parts)

switch {
case path == "":
return banker.RenderHome()
case c == 2 && parts[0] == "balance":
owner := pusers.AddressOrName(parts[1])
ownerAddr := users.Resolve(owner)
balance := banker.BalanceOf(ownerAddr)
return ufmt.Sprintf("%d\n", balance)
default:
return "404\n"
}
}

func checkErr(err error) {
if err != nil {
panic(err)
}
}
9 changes: 9 additions & 0 deletions realms/mock/bar/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module gno.land/r/onbloc/bar

require (
gno.land/p/demo/grc/grc20 v0.0.0-latest
gno.land/p/demo/ownable v0.0.0-latest
gno.land/p/demo/ufmt v0.0.0-latest
gno.land/p/demo/users v0.0.0-latest
gno.land/r/demo/users v0.0.0-latest
)
82 changes: 82 additions & 0 deletions realms/mock/foo/foo.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package foo

import (
"strings"

"gno.land/p/demo/grc/grc20"
"gno.land/p/demo/ownable"
"gno.land/p/demo/ufmt"
pusers "gno.land/p/demo/users"
"gno.land/r/demo/users"
)

var (
banker *grc20.Banker
admin *ownable.Ownable
token grc20.Token
)

func init() {
admin = ownable.NewWithAddress("g1lmvrrrr4er2us84h2732sru76c9zl2nvknha8c") // ADMIN
banker = grc20.NewBanker("Foo", "FOO", 6)
banker.Mint(admin.Owner(), 100_000_000_000_000)
token = banker.Token()
}

func TotalSupply() uint64 { return token.TotalSupply() }

func BalanceOf(owner pusers.AddressOrName) uint64 {
ownerAddr := users.Resolve(owner)
return token.BalanceOf(ownerAddr)
}

func Allowance(owner, spender pusers.AddressOrName) uint64 {
ownerAddr := users.Resolve(owner)
spenderAddr := users.Resolve(spender)
return token.Allowance(ownerAddr, spenderAddr)
}

func Transfer(to pusers.AddressOrName, amount uint64) {
toAddr := users.Resolve(to)
checkErr(token.Transfer(toAddr, amount))
}

func Approve(spender pusers.AddressOrName, amount uint64) {
spenderAddr := users.Resolve(spender)
checkErr(token.Approve(spenderAddr, amount))
}

func TransferFrom(from, to pusers.AddressOrName, amount uint64) {
fromAddr := users.Resolve(from)
toAddr := users.Resolve(to)
checkErr(token.TransferFrom(fromAddr, toAddr, amount))
}

func Burn(from pusers.AddressOrName, amount uint64) {
admin.AssertCallerIsOwner()
fromAddr := users.Resolve(from)
checkErr(banker.Burn(fromAddr, amount))
}

func Render(path string) string {
parts := strings.Split(path, "/")
c := len(parts)

switch {
case path == "":
return banker.RenderHome()
case c == 2 && parts[0] == "balance":
owner := pusers.AddressOrName(parts[1])
ownerAddr := users.Resolve(owner)
balance := banker.BalanceOf(ownerAddr)
return ufmt.Sprintf("%d\n", balance)
default:
return "404\n"
}
}

func checkErr(err error) {
if err != nil {
panic(err)
}
}
9 changes: 9 additions & 0 deletions realms/mock/foo/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module gno.land/r/onbloc/foo

require (
gno.land/p/demo/grc/grc20 v0.0.0-latest
gno.land/p/demo/ownable v0.0.0-latest
gno.land/p/demo/ufmt v0.0.0-latest
gno.land/p/demo/users v0.0.0-latest
gno.land/r/demo/users v0.0.0-latest
)
59 changes: 33 additions & 26 deletions contracts/testutils_test.gno → realms/testutils_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (

pusers "gno.land/p/demo/users"

"gno.land/p/demo/testutils"
"gno.land/r/gnoswap/v2/bar"
"gno.land/r/gnoswap/v2/common"
"gno.land/r/gnoswap/v2/consts"
"gno.land/r/demo/wugnot"
"gno.land/r/gnoswap/v2/gns"
"gno.land/p/demo/testutils"
"gno.land/r/gnoswap/v2/foo"
pl "gno.land/r/gnoswap/v2/pool"
)

Expand All @@ -24,52 +24,59 @@ var (

func initPool() {
std.TestSetRealm(adminRealm)

bar.Transfer(a2u(fresh01), 10000000)
foo.Transfer(a2u(fresh01), 10000000)

pl.SetPoolCreationFeeByAdmin(0)
pl.CreatePool(consts.GNOT, consts.GNS_PATH, 500, common.TickMathGetSqrtRatioAtTick(-10000).ToString())
pl.CreatePool("gno.land/r/gnoswap/v2/bar", "gno.land/r/gnoswap/v2/foo", 500, common.TickMathGetSqrtRatioAtTick(1).ToString())
}

type WugnotToken struct{}
type FooToken struct{}

func (WugnotToken) Transfer() func(to pusers.AddressOrName, amount uint64) {
return wugnot.Transfer
func (FooToken) Transfer() func(to pusers.AddressOrName, amount uint64) {
return foo.Transfer
}
func (WugnotToken) TransferFrom() func(from, to pusers.AddressOrName, amount uint64) {
return wugnot.TransferFrom

func (FooToken) TransferFrom() func(from, to pusers.AddressOrName, amount uint64) {
return foo.TransferFrom
}
func (WugnotToken) BalanceOf() func(owner pusers.AddressOrName) uint64 {
return wugnot.BalanceOf

func (FooToken) BalanceOf() func(owner pusers.AddressOrName) uint64 {
return foo.BalanceOf
}
func (WugnotToken) Approve() func(spender pusers.AddressOrName, amount uint64) {
return wugnot.Approve

func (FooToken) Approve() func(spender pusers.AddressOrName, amount uint64) {
return foo.Approve
}

type GNSToken struct{}
type BarToken struct{}

func (GNSToken) Transfer() func(to pusers.AddressOrName, amount uint64) {
return gns.Transfer
func (BarToken) Transfer() func(to pusers.AddressOrName, amount uint64) {
return bar.Transfer
}

func (GNSToken) TransferFrom() func(from, to pusers.AddressOrName, amount uint64) {
return gns.TransferFrom
func (BarToken) TransferFrom() func(from, to pusers.AddressOrName, amount uint64) {
return bar.TransferFrom
}

func (GNSToken) BalanceOf() func(owner pusers.AddressOrName) uint64 {
return gns.BalanceOf
func (BarToken) BalanceOf() func(owner pusers.AddressOrName) uint64 {
return bar.BalanceOf
}

func (GNSToken) Approve() func(spender pusers.AddressOrName, amount uint64) {
return gns.Approve
func (BarToken) Approve() func(spender pusers.AddressOrName, amount uint64) {
return bar.Approve
}

func initTokenRegistries() {
RegisterGRC20Interface("gno.land/r/demo/wugnot", WugnotToken{})
RegisterGRC20Interface("gno.land/r/gnoswap/v2/gns", GNSToken{})
RegisterGRC20Interface("gno.land/r/gnoswap/v2/foo", FooToken{})
RegisterGRC20Interface("gno.land/r/gnoswap/v2/bar", BarToken{})

tokenRegisterRealm := std.NewUserRealm(consts.TOKEN_REGISTER)
std.TestSetRealm(tokenRegisterRealm)

pl.RegisterGRC20Interface("gno.land/r/demo/wugnot", WugnotToken{})
pl.RegisterGRC20Interface("gno.land/r/gnoswap/v2/gns", GNSToken{})
pl.RegisterGRC20Interface("gno.land/r/gnoswap/v2/foo", FooToken{})
pl.RegisterGRC20Interface("gno.land/r/gnoswap/v2/bar", BarToken{})
}

func a2u(addr std.Address) pusers.AddressOrName {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ func TestRegisterGRC20InterfaceNotAsAdmin(t *testing.T) {
std.TestSetRealm(fresh02Realm)

shouldPanic(t, func() {
RegisterGRC20Interface("pkgPath", GNSToken{})
RegisterGRC20Interface("pkgPath", FooToken{})
})
}

func TestRegisterGRC20Interface(t *testing.T) {
RegisterGRC20Interface("pkgPath", GNSToken{})
RegisterGRC20Interface("pkgPath", FooToken{})

grc20, found := registered.Get("pkgPath")

shouldEQ(t, found, true)
shouldEQ(t, grc20, GNSToken{})
shouldEQ(t, grc20, FooToken{})
}

func TestRegisterGRC20InterfaceAlreadyRegistered(t *testing.T) {
shouldPanic(t, func() {
RegisterGRC20Interface("pkgPath", GNSToken{})
RegisterGRC20Interface("pkgPath", FooToken{})
})
}

Expand Down
File renamed without changes.
File renamed without changes.
Loading