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

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

escrow_filetest.gno

2.81 Kb · 74 lines
 1// PKGPATH: gno.land/r/test/escrowfixture/main
 2
 3package main
 4
 5import (
 6	"chain"
 7	"chain/banker"
 8	"testing"
 9
10	"gno.land/r/g132vz03ycpaagtef4jmxjakstkppgyc5wn3r807/erratabank/v0"
11)
12
13const (
14	owner  = address("g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0")
15	reader = address("g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c")
16)
17
18func main(cur realm) {
19	bank := banker.NewReadonlyBanker()
20	bankAddr := chain.PackageAddress("gno.land/r/g132vz03ycpaagtef4jmxjakstkppgyc5wn3r807/erratabank/v0")
21	println("initial realm balance:", bank.GetCoin(bankAddr, "ugnot"))
22
23	stageSend(cur, bankAddr, 900)
24	testing.SetRealm(testing.NewUserRealm(owner))
25	bookID := erratabank.CreateBook(cross(cur), "Quickstart", "First", "https://example.org/guide", 1000)
26	book, _ := erratabank.GetBook(bookID)
27	println("created escrow and realm balance:", book.Escrow, bank.GetCoin(bankAddr, "ugnot"))
28
29	stageSend(cur, bankAddr, 500)
30	testing.SetRealm(testing.NewUserRealm(reader))
31	erratabank.FundBook(cross(cur), bookID)
32	book, _ = erratabank.GetBook(bookID)
33	println("funded escrow and realm balance:", book.Escrow, bank.GetCoin(bankAddr, "ugnot"))
34
35	testing.SetOriginSend(nil)
36	first := erratabank.Submit(cross(cur), bookID, "Page 1", "Fix the example.")
37	second := erratabank.Submit(cross(cur), bookID, "Page 2", "Fix the note.")
38	testing.SetRealm(testing.NewUserRealm(owner))
39	erratabank.Accept(cross(cur), first, "Confirmed")
40	report, _ := erratabank.GetErratum(first)
41	book, _ = erratabank.GetBook(bookID)
42	println("paid, escrow, realm, finder:", report.Paid, book.Escrow,
43		bank.GetCoin(bankAddr, "ugnot"), bank.GetCoin(reader, "ugnot"))
44
45	erratabank.Accept(cross(cur), second, "Confirmed")
46	report, _ = erratabank.GetErratum(second)
47	count, earned := erratabank.FinderStats(reader)
48	book, _ = erratabank.GetBook(bookID)
49	println("credit only, escrow, realm, finder:", report.Status, report.Paid, book.Escrow,
50		bank.GetCoin(bankAddr, "ugnot"), bank.GetCoin(reader, "ugnot"))
51	println("finder stats:", count, earned)
52
53	erratabank.Withdraw(cross(cur), bookID, 200)
54	book, _ = erratabank.GetBook(bookID)
55	println("withdrawal escrow, realm, owner:", book.Escrow, bank.GetCoin(bankAddr, "ugnot"), bank.GetCoin(owner, "ugnot"))
56}
57
58// stageSend reproduces the chain's transfer and origin-send envelope before MsgCall.
59func stageSend(cur realm, target address, amount int64) {
60	sent := chain.NewCoins(chain.NewCoin("ugnot", amount))
61	testing.IssueCoins(cur.Address(), sent)
62	b := banker.NewBanker(banker.BankerTypeRealmSend, cur)
63	b.SendCoins(cur.Address(), target, sent)
64	testing.SetOriginSend(sent)
65}
66
67// Output:
68// initial realm balance: 0
69// created escrow and realm balance: 900 900
70// funded escrow and realm balance: 1400 1400
71// paid, escrow, realm, finder: 1000 400 400 1000
72// credit only, escrow, realm, finder: Accepted 0 400 400 1000
73// finder stats: 2 1000
74// withdrawal escrow, realm, owner: 200 200 200