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