Skip to content

Commit

Permalink
Merge pull request #113 from gnoswap-labs/GSW-627-add-non-register-token
Browse files Browse the repository at this point in the history
GSW-627 chore: add 2 non register grc20 tokens
  • Loading branch information
notJoon authored Dec 7, 2023
2 parents 04a0aeb + 6ec8955 commit 2bc0fc6
Show file tree
Hide file tree
Showing 7 changed files with 418 additions and 1 deletion.
73 changes: 73 additions & 0 deletions _setup/fred/fred_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package fred

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/fred/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/r/fred
129 changes: 129 additions & 0 deletions _setup/fred/thud.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package fred

import (
"std"
"strings"

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

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

func init() {
fred = grc20.NewAdminToken("Fred", "FRED", 4)
fred.Mint(admin, 1000000*10000) // @administrator (1M)
}

// method proxies as public functions.
//

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

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

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

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

// setters.

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

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

func TransferFrom(from, to users.AddressOrName, amount uint64) {
caller := std.PrevRealm().Addr()
err := fred.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.PrevRealm().Addr()
fred.Mint(caller, 1000*10000) // 1k
}

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

// administration.

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

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

// render.
//

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

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

func assertIsAdmin(address std.Address) {
if address != admin {
panic("restricted access")
}
}
1 change: 1 addition & 0 deletions _setup/thud/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/r/thud
129 changes: 129 additions & 0 deletions _setup/thud/thud.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package thud

import (
"std"
"strings"

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

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

func init() {
thud = grc20.NewAdminToken("Thud", "THUD", 4)
thud.Mint(admin, 1000000*10000) // @administrator (1M)
}

// method proxies as public functions.
//

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

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

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

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

// setters.

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

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

func TransferFrom(from, to users.AddressOrName, amount uint64) {
caller := std.PrevRealm().Addr()
err := thud.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.PrevRealm().Addr()
thud.Mint(caller, 1000*10000) // 1k
}

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

// administration.

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

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

// render.
//

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

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

func assertIsAdmin(address std.Address) {
if address != admin {
panic("restricted access")
}
}
Loading

0 comments on commit 2bc0fc6

Please sign in to comment.