Skip to content

Commit

Permalink
Merge pull request #80 from gnoswap-labs/GSW-516-feat-call-grc-20-fun…
Browse files Browse the repository at this point in the history
…ction-with-registered-interface

GSW-516 feat: call grc 20 function with registered interface
  • Loading branch information
r3v4s authored Nov 1, 2023
2 parents 95d1df9 + 5cc8440 commit fa5d90d
Show file tree
Hide file tree
Showing 12 changed files with 686 additions and 169 deletions.
22 changes: 8 additions & 14 deletions _setup/bar/bar.gno
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ func init() {
bar.Mint(tr01, 50000000)

bar.Approve(lp01, poolAddr, 50000000)
bar.Approve(lp02, poolAddr, 50000000)
bar.Approve(lp03, poolAddr, 50000000)
// bar.Approve(lp02, poolAddr, 50000000)
// bar.Approve(lp03, poolAddr, 50000000)
bar.Approve(tr01, poolAddr, 50000000)

bar.Approve(lp01, lp01, 50000000)
bar.Approve(lp02, lp02, 50000000)
bar.Approve(lp03, lp03, 50000000)
bar.Approve(tr01, tr01, 50000000)
// bar.Approve(lp01, lp01, 50000000)
// bar.Approve(lp02, lp02, 50000000)
// bar.Approve(lp03, lp03, 50000000)
// bar.Approve(tr01, tr01, 50000000)

bar.Approve(posAddr, poolAddr, 50000000)
// bar.Approve(poolAddr, registryAddr, 50000000)
}

// method proxies as public functions.
Expand Down Expand Up @@ -80,7 +80,6 @@ func Allowance(owner, spender users.AddressOrName) uint64 {
// setters.

func Transfer(to users.AddressOrName, amount uint64) {
// caller := std.GetOrigCaller()
caller := std.PrevRealm().Addr()
err := bar.Transfer(caller, to.Resolve(), amount)
if err != nil {
Expand All @@ -89,7 +88,6 @@ func Transfer(to users.AddressOrName, amount uint64) {
}

func Approve(spender users.AddressOrName, amount uint64) {
// caller := std.GetOrigCaller()
caller := std.PrevRealm().Addr()
err := bar.Approve(caller, spender.Resolve(), amount)
if err != nil {
Expand All @@ -98,8 +96,8 @@ func Approve(spender users.AddressOrName, amount uint64) {
}

func TransferFrom(from, to users.AddressOrName, amount uint64) {
// caller := std.GetOrigCaller()
caller := std.PrevRealm().Addr()

err := bar.TransferFrom(caller, from.Resolve(), to.Resolve(), amount)
if err != nil {
panic(err.Error())
Expand All @@ -111,30 +109,26 @@ func TransferFrom(from, to users.AddressOrName, amount uint64) {
func Faucet() {
// FIXME: add limits?
// FIXME: add payment in gnot?
// caller := std.GetOrigCaller()
caller := std.PrevRealm().Addr()
bar.Mint(caller, 1000*10000) // 1k
}

func FaucetL() {
// FIXME: add limits?
// FIXME: add payment in gnot?
// caller := std.GetOrigCaller()
caller := std.PrevRealm().Addr()
bar.Mint(caller, 50000000000) // 50_000_000_000
}

// administration.

func Mint(address users.AddressOrName, amount uint64) {
// caller := std.GetOrigCaller()
caller := std.PrevRealm().Addr()
assertIsAdmin(caller)
bar.Mint(address.Resolve(), amount)
}

func Burn(address users.AddressOrName, amount uint64) {
// caller := std.GetOrigCaller()
caller := std.PrevRealm().Addr()
assertIsAdmin(caller)
bar.Burn(address.Resolve(), amount)
Expand Down
166 changes: 166 additions & 0 deletions _setup/baz/baz.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package baz

import (
"std"
"strings"

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

// for swap
"gno.land/p/demo/testutils"
)

var (
baz *grc20.AdminToken
admin std.Address = "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5" // TODO: helper to change admin
)

func init() {
baz = grc20.NewAdminToken("Baz", "BAZ", 4)
baz.Mint(admin, 1000000*10000) // @administrator (1M)

// for swap > pool
var (
lp01 = testutils.TestAddress("lp01") // Liquidity Provider 01
lp02 = testutils.TestAddress("lp02") // Liquidity Provider 02
lp03 = testutils.TestAddress("lp03") // Liquidity Provider 03
tr01 = testutils.TestAddress("tr01") // Trader 01
poolAddr = std.DerivePkgAddr("gno.land/r/pool")
posAddr = std.DerivePkgAddr("gno.land/r/position")
)

baz.Mint(lp01, 50000000)
baz.Mint(lp02, 50000000)
baz.Mint(lp03, 50000000)
baz.Mint(tr01, 50000000)

baz.Approve(lp01, poolAddr, 50000000)
// baz.Approve(lp02, poolAddr, 50000000)
// baz.Approve(lp03, poolAddr, 50000000)
baz.Approve(tr01, poolAddr, 50000000)

// baz.Approve(lp01, lp01, 50000000)
// baz.Approve(lp02, lp02, 50000000)
// baz.Approve(lp03, lp03, 50000000)
// baz.Approve(tr01, tr01, 50000000)

// baz.Approve(posAddr, poolAddr, 50000000)
}

// method proxies as public functions.
//

// getters.
func GetGRC20() *grc20.AdminToken {
return baz
}

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

func BalanceOf(owner users.AddressOrName) uint64 {
balance, err := baz.BalanceOf(owner.Resolve())
if err != nil {
panic(err)
}
return balance
}

func Allowance(owner, spender users.AddressOrName) uint64 {
allowance, err := baz.Allowance(owner.Resolve(), spender.Resolve())
if err != nil {
panic(err)
}
return allowance
}

// setters.

func Transfer(to users.AddressOrName, amount uint64) {
// caller := std.GetOrigCaller()
caller := std.PrevRealm().Addr()
err := baz.Transfer(caller, to.Resolve(), amount)
if err != nil {
panic(err.Error())
}
}

func Approve(spender users.AddressOrName, amount uint64) {
// caller := std.GetOrigCaller()
caller := std.PrevRealm().Addr()
err := baz.Approve(caller, spender.Resolve(), amount)
if err != nil {
panic(err.Error())
}
}

func TransferFrom(from, to users.AddressOrName, amount uint64) {
// caller := std.GetOrigCaller()
caller := std.PrevRealm().Addr()
err := baz.TransferFrom(caller, from.Resolve(), to.Resolve(), amount)
if err != nil {
panic(err.Error())
}
}

// faucet.

func Faucet() {
// FIXME: add limits?
// FIXME: add payment in gnot?
// caller := std.GetOrigCaller()
caller := std.PrevRealm().Addr()
baz.Mint(caller, 1000*10000) // 1k
}

func FaucetL() {
// FIXME: add limits?
// FIXME: add payment in gnot?
// caller := std.GetOrigCaller()
caller := std.PrevRealm().Addr()
baz.Mint(caller, 50000000000) // 50_000_000_000
}

// administration.

func Mint(address users.AddressOrName, amount uint64) {
// caller := std.GetOrigCaller()
caller := std.PrevRealm().Addr()
assertIsAdmin(caller)
baz.Mint(address.Resolve(), amount)
}

func Burn(address users.AddressOrName, amount uint64) {
// caller := std.GetOrigCaller()
caller := std.PrevRealm().Addr()
assertIsAdmin(caller)
baz.Burn(address.Resolve(), amount)
}

// render.
//

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

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

func assertIsAdmin(address std.Address) {
if address != admin {
panic("restricted access")
}
}
73 changes: 73 additions & 0 deletions _setup/baz/baz_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package foo

import (
"std"
"testing"

"gno.land/p/demo/testutils"

"gno.land/r/demo/users"
)

var (
a1 = testutils.TestAddress("a1")
a2 = testutils.TestAddress("a2")
a3 = testutils.TestAddress("a3")
a4 = testutils.TestAddress("a4")
)

func init() {
std.TestSetOrigCaller(a1)
Faucet()
}

func TestTransfer(t *testing.T) {
std.TestSetOrigCaller(a1)
Transfer(a2u(a2), 100)

std.TestSetOrigCaller(a2)
Transfer(a2u(a3), 95)

shouldPanicWithMsg(t, func() { Transfer(a2u(a3), 10) }, "insufficient balance")
}

func TestApprove(t *testing.T) {
std.TestSetOrigCaller(std.Address(""))
shouldPanicWithMsg(t, func() { Approve(a2u(a2), 1000) }, "invalid address")

std.TestSetOrigCaller(a1)
shouldPanicWithMsg(t, func() { Approve(a2u(std.Address("")), 1000) }, "invalid address")
}

func TestTransferFrom(t *testing.T) {
std.TestSetOrigCaller(a1)
Approve(a2u(a2), 1000)

std.TestSetOrigCaller(a2)
TransferFrom(a2u(a1), a2u(a3), 100)

// not enough allowance
shouldPanicWithMsg(t, func() { TransferFrom(a2u(a1), a2u(a3), 901) }, "insufficient allowance")

// didn't approve
std.TestSetOrigCaller(a3)
shouldPanicWithMsg(t, func() { TransferFrom(a2u(a1), a2u(a4), 100) }, "insufficient allowance")

}

func a2u(addr std.Address) users.AddressOrName {
return users.AddressOrName(addr)
}

func shouldPanicWithMsg(t *testing.T, f func(), msg string) {
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
} else {
if r != msg {
t.Errorf("excepted panic(%v), got(%v)", msg, r)
}
}
}()
f()
}
1 change: 1 addition & 0 deletions _setup/baz/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/r/baz
Loading

0 comments on commit fa5d90d

Please sign in to comment.