Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

caller_teller_sub_realm_filetest.gno

1.92 Kb · 63 lines
 1// PKGPATH: gno.land/r/demo/grc20subrealm
 2
 3// A frame-relative teller must keep working when the token's OWN realm
 4// operates under an identity minted by its own cur.Sub(). The sub's synthesized
 5// pkgpath is "<host>#vault", which is not origRealm verbatim, so a raw string
 6// comparison would refuse the token's own realm — a false positive that would
 7// break the one property the home binding promises unconditionally.
 8package grc20subrealm
 9
10import (
11	"chain"
12
13	"gno.land/p/nt/grc20/v0"
14)
15
16var (
17	token  *grc20.Token
18	ledger *grc20.PrivateLedger
19	teller grc20.Teller
20)
21
22func init(cur realm) {
23	token, ledger = grc20.NewToken("SubRealm", "SUBR", 4, 0, cur)
24	teller = ledger.CallerTeller()
25}
26
27func main(cur realm) {
28	payer := cur.Previous().Address() // whom the frame-relative teller debits
29	bob := chain.PackageAddress("bob")
30	ledger.Mint(payer, 1_000)
31
32	// Control: the primary identity moves the caller's funds.
33	if err := teller.Transfer(0, cur, bob, 100); err != nil {
34		panic("primary identity rejected: " + err.Error())
35	}
36
37	// Same realm, same teller, under this realm's own "vault" sub identity.
38	if err := teller.Transfer(0, cur.Sub("vault"), bob, 100); err != nil {
39		panic("own sub identity rejected: " + err.Error())
40	}
41
42	if got := token.BalanceOf(bob); got != 200 {
43		panic("unexpected bob balance")
44	}
45
46	// The mirror image: a token CREATED from a sub frame must still be usable
47	// from its host realm. origRealm drops the subpath at construction, so the
48	// token belongs to the host rather than being stranded in the sub.
49	subTok, subLedger := grc20.NewToken("FromSub", "FSUB", 4, 1, cur.Sub("vault"))
50	subTeller := subLedger.CallerTeller()
51	subLedger.Mint(payer, 500)
52	if err := subTeller.Transfer(0, cur, bob, 50); err != nil {
53		panic("host realm rejected for a sub-created token: " + err.Error())
54	}
55	if got := subTok.BalanceOf(bob); got != 50 {
56		panic("unexpected bob balance on the sub-created token")
57	}
58
59	println("ok")
60}
61
62// Output:
63// ok