forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
hieu.ha
committed
Feb 13, 2024
1 parent
9aa7cd7
commit 2145d9b
Showing
7 changed files
with
251 additions
and
3 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
14 changes: 14 additions & 0 deletions
14
examples/gno.land/r/demo/teritori/escrow/escrow_local_testnet.sh
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,14 @@ | ||
# Contractor | ||
gnokey maketx call \ | ||
-pkgpath "gno.land/r/demo/users" \ | ||
-func "Register" \ | ||
-gas-fee 1000000ugnot \ | ||
-gas-wanted 2000000 \ | ||
-send="200000000ugnot" \ | ||
-broadcast \ | ||
-chainid "dev" \ | ||
-args "" \ | ||
-args "contractor" \ | ||
-args "contractor profile" \ | ||
-remote "127.0.0.1:26657" \ | ||
test1 |
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
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,7 @@ | ||
module gno.land/r/demo/tori20 | ||
|
||
require ( | ||
"gno.land/p/demo/ufmt" v0.0.0-latest | ||
"gno.land/p/demo/grc/grc20" v0.0.0-latest | ||
"gno.land/r/demo/users" v0.0.0-latest | ||
) |
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,133 @@ | ||
package tori20 | ||
|
||
import ( | ||
"std" | ||
"strings" | ||
|
||
"gno.land/p/demo/grc/grc20" | ||
"gno.land/p/demo/ufmt" | ||
"gno.land/r/demo/users" | ||
) | ||
|
||
var ( | ||
foo *grc20.AdminToken | ||
admin std.Address = "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj" // TODO: helper to change admin | ||
) | ||
|
||
func init() { | ||
foo = grc20.NewAdminToken("Foo", "FOO", 4) | ||
foo.Mint(admin, 1000000*10000) // @administrator (1M) | ||
foo.Mint("g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq", 10000*10000) // @manfred (10k) | ||
|
||
// My accounts | ||
foo.Mint("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5", 10000*10000) // @Yo account 1 (10k) | ||
foo.Mint("g1kcdd3n0d472g2p5l8svyg9t0wq6h5857nq992f", 10000*10000) // @Yo account 2 (10k) | ||
} | ||
|
||
// method proxies as public functions. | ||
// | ||
|
||
// getters. | ||
|
||
func TotalSupply() uint64 { | ||
return foo.TotalSupply() | ||
} | ||
|
||
func BalanceOf(owner users.AddressOrName) uint64 { | ||
balance, err := foo.BalanceOf(owner.Resolve()) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return balance | ||
} | ||
|
||
func Allowance(owner, spender users.AddressOrName) uint64 { | ||
allowance, err := foo.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 := foo.Transfer(caller, to.Resolve(), amount) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func Approve(spender users.AddressOrName, amount uint64) { | ||
caller := std.PrevRealm().Addr() | ||
err := foo.Approve(caller, spender.Resolve(), amount) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func TransferFrom(from, to users.AddressOrName, amount uint64) { | ||
caller := std.PrevRealm().Addr() | ||
err := foo.TransferFrom(caller, from.Resolve(), to.Resolve(), amount) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
// faucet. | ||
|
||
func Faucet() { | ||
// FIXME: add limits? | ||
// FIXME: add payment in gnot? | ||
caller := std.PrevRealm().Addr() | ||
err := foo.Mint(caller, 1000*10000) // 1k | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
// administration. | ||
|
||
func Mint(address users.AddressOrName, amount uint64) { | ||
caller := std.PrevRealm().Addr() | ||
assertIsAdmin(caller) | ||
err := foo.Mint(address.Resolve(), amount) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func Burn(address users.AddressOrName, amount uint64) { | ||
caller := std.PrevRealm().Addr() | ||
assertIsAdmin(caller) | ||
err := foo.Burn(address.Resolve(), amount) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
// render. | ||
// | ||
|
||
func Render(path string) string { | ||
parts := strings.Split(path, "/") | ||
c := len(parts) | ||
|
||
switch { | ||
case path == "": | ||
return foo.RenderHome() | ||
case c == 2 && parts[0] == "balance": | ||
owner := users.AddressOrName(parts[1]) | ||
balance, _ := foo.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,92 @@ | ||
package tori20 | ||
|
||
import ( | ||
"std" | ||
"testing" | ||
|
||
"gno.land/r/demo/users" | ||
) | ||
|
||
func TestReadOnlyPublicMethods(t *testing.T) { | ||
admin := users.AddressOrName("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj") | ||
manfred := users.AddressOrName("g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq") | ||
unknown := std.Address("g1u0000000000000000000000000000000000000") | ||
|
||
type test struct { | ||
name string | ||
balance uint64 | ||
fn func() uint64 | ||
} | ||
|
||
// check balances #1. | ||
{ | ||
tests := []test{ | ||
{"TotalSupply", 10100000000, func() uint64 { return TotalSupply() }}, | ||
{"BalanceOf(admin)", 10000000000, func() uint64 { return BalanceOf(admin) }}, | ||
{"BalanceOf(manfred)", 100000000, func() uint64 { return BalanceOf(manfred) }}, | ||
{"Allowance(admin, manfred)", 0, func() uint64 { return Allowance(admin, manfred) }}, | ||
{"BalanceOf(unknown)", 0, func() uint64 { return BalanceOf(users.AddressOrName(unknown)) }}, | ||
} | ||
for _, tc := range tests { | ||
if tc.fn() != tc.balance { | ||
t.Errorf("%s: have: %d want: %d", tc.name, tc.fn(), tc.balance) | ||
} | ||
} | ||
} | ||
|
||
// unknown uses the faucet. | ||
std.TestSetOrigCaller(unknown) | ||
Faucet() | ||
|
||
// check balances #2. | ||
{ | ||
tests := []test{ | ||
{"TotalSupply", 10110000000, func() uint64 { return TotalSupply() }}, | ||
{"BalanceOf(admin)", 10000000000, func() uint64 { return BalanceOf(admin) }}, | ||
{"BalanceOf(manfred)", 100000000, func() uint64 { return BalanceOf(manfred) }}, | ||
{"Allowance(admin, manfred)", 0, func() uint64 { return Allowance(admin, manfred) }}, | ||
{"BalanceOf(unknown)", 10000000, func() uint64 { return BalanceOf(users.AddressOrName(unknown)) }}, | ||
} | ||
for _, tc := range tests { | ||
if tc.fn() != tc.balance { | ||
t.Errorf("%s: have: %d want: %d", tc.name, tc.fn(), tc.balance) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func TestErrConditions(t *testing.T) { | ||
admin := std.Address("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj") | ||
empty := std.Address("") | ||
|
||
type test struct { | ||
name string | ||
msg string | ||
fn func() | ||
} | ||
|
||
std.TestSetOrigCaller(admin) | ||
{ | ||
tests := []test{ | ||
{"Transfer(admin, 1)", "cannot send transfer to self", func() { Transfer(users.AddressOrName(admin), 1) }}, | ||
{"Approve(empty, 1))", "invalid address", func() { Approve(users.AddressOrName(empty), 1) }}, | ||
} | ||
for _, tc := range tests { | ||
shouldPanicWithMsg(t, tc.fn, tc.msg) | ||
} | ||
} | ||
} | ||
|
||
func shouldPanicWithMsg(t *testing.T, f func(), msg string) { | ||
defer func() { | ||
if r := recover(); r == nil { | ||
t.Errorf("The code did not panic") | ||
} else { | ||
errMsg := error(r).Error() | ||
if errMsg != msg { | ||
t.Errorf("excepted panic(%v), got(%v)", msg, errMsg) | ||
} | ||
} | ||
}() | ||
f() | ||
} |