-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
12 changed files
with
686 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module gno.land/r/baz |
Oops, something went wrong.