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.

vault.gno

2.50 Kb · 89 lines
 1// Package vault is the demo vault, version 1. Users deposit ugnot and
 2// withdraw it later. It has a bug Bouncer finds and proves: Ledger hands out
 3// a live pointer to the balances tree (security guide §5.1).
 4package vault
 5
 6import (
 7	"chain"
 8	"chain/banker"
 9	"chain/runtime/unsafe"
10	"strconv"
11	"strings"
12
13	"gno.land/p/nt/avl/v0"
14)
15
16var (
17	self   address         // this realm's address
18	owner  address         // the deployer
19	ledger = avl.NewTree() // address -> int64 ugnot owed to the depositor
20)
21
22func init(cur realm) {
23	self = cur.Address()
24	owner = cur.Previous().Address()
25	// The deployer's opening deposit (addpkg -send) is the first entry.
26	ledger.Set(string(owner), unsafe.OriginSend().AmountOf("ugnot"))
27}
28
29// Deposit credits the caller with the ugnot sent with the call.
30func Deposit(cur realm) {
31	if !cur.IsCurrent() {
32		panic("not current")
33	}
34	if !cur.Previous().IsUserCall() {
35		panic("deposit with maketx call")
36	}
37	amount := unsafe.OriginSend().AmountOf("ugnot")
38	if amount <= 0 {
39		panic("send ugnot to deposit")
40	}
41	caller := string(cur.Previous().Address())
42	ledger.Set(caller, balanceOf(caller)+amount)
43	chain.Emit("Deposit", "from", caller, "amount", strconv.FormatInt(amount, 10))
44}
45
46// Withdraw pays the caller back up to their balance.
47func Withdraw(cur realm, amount int64) {
48	if !cur.IsCurrent() {
49		panic("not current")
50	}
51	caller := cur.Previous().Address()
52	bal := balanceOf(string(caller))
53	if amount <= 0 || amount > bal {
54		panic("insufficient balance")
55	}
56	ledger.Set(string(caller), bal-amount)
57	bnk := banker.NewBanker(banker.BankerTypeRealmSend, cur)
58	bnk.SendCoins(self, caller, chain.Coins{chain.NewCoin("ugnot", amount)})
59	chain.Emit("Withdraw", "to", caller.String(), "amount", strconv.FormatInt(amount, 10))
60}
61
62// BalanceOf returns addr's balance in ugnot.
63func BalanceOf(addr address) int64 {
64	return balanceOf(string(addr))
65}
66
67// Ledger returns the balances, for frontends that list depositors.
68func Ledger() *avl.Tree {
69	return ledger
70}
71
72func balanceOf(key string) int64 {
73	if v := ledger.Get(key); v != nil {
74		return v.(int64)
75	}
76	return 0
77}
78
79func Render(_ string) string {
80	var sb strings.Builder
81	sb.WriteString("# Vault v1\n\n")
82	held := banker.NewReadonlyBanker().GetCoins(self).AmountOf("ugnot")
83	sb.WriteString("Holds " + strconv.FormatInt(held, 10) + " ugnot.\n\n| Depositor | Balance (ugnot) |\n| --- | --- |\n")
84	ledger.Iterate("", "", func(key string, value any) bool {
85		sb.WriteString("| " + key + " | " + strconv.FormatInt(value.(int64), 10) + " |\n")
86		return false
87	})
88	return sb.String()
89}