render.gno
2.83 Kb · 68 lines
1package heirrealm
2
3import (
4 "strconv"
5 "time"
6
7 "gno.land/p/nt/ufmt/v0"
8)
9
10// Render is the gnoweb homepage for the realm. It lists every vault
11// with its owner, beneficiary, balance, deadline and status, plus a
12// short how-to. A single vault can be viewed at /<id>.
13func Render(path string) string {
14 if path != "" {
15 id, err := strconv.Atoi(path)
16 if err != nil {
17 return "# HeirRealm\n\nUnknown vault."
18 }
19 v := vaults.Get(strconv.Itoa(id))
20 if v == nil {
21 return "# HeirRealm\n\nUnknown vault."
22 }
23 return renderVault(v.(*Vault))
24 }
25 out := "# HeirRealm — dead-man's switch for Gno\n\n"
26 out += "Name a beneficiary and a check-in window. Check in before the window ends and nothing happens. "
27 out += "Miss the window and any keeper can trigger the release: the beneficiary gets 98%, the keeper earns a 2% fee. "
28 out += "No custodian, no will, no trusted third party.\n\n"
29 out += "## Vaults\n\n"
30 if nextVault == 0 {
31 out += "No vaults yet. Create one with `CreateVault(beneficiary, intervalSeconds)` and attach ugnot with `-send`.\n"
32 return out
33 }
34 for i := 1; i <= nextVault; i++ {
35 v := vaults.Get(strconv.Itoa(i))
36 if v == nil {
37 continue
38 }
39 vv := v.(*Vault)
40 out += ufmt.Sprintf("- Vault #%d — %s — balance %d ugnot — deadline %s\n",
41 vv.ID, vv.Status(), vv.Balance, vv.Deadline.Format(time.RFC3339))
42 }
43 out += "\n## How it works\n\n"
44 out += "1. `CreateVault(beneficiary, intervalSeconds)` + `-send` ugnot — you are the owner.\n"
45 out += "2. `CheckIn(vaultID)` before the deadline to reset the timer.\n"
46 out += "3. `Heartbeat(vaultID)` during the post-deadline grace period to revive the vault and reset the deadline (owner only).\n"
47 out += "4. `Deposit(vaultID)` + `-send` ugnot to top up (owner only, timer unchanged).\n"
48 out += "5. After deadline + grace period, anyone except the owner or beneficiary calls `TriggerRelease(vaultID)` and earns 2%.\n"
49 out += "6. `Cancel(vaultID)` refunds the owner in full — owner only, before the deadline.\n"
50 return out
51}
52
53func renderVault(v *Vault) string {
54 out := ufmt.Sprintf("# HeirRealm — Vault #%d\n\n", v.ID)
55 out += ufmt.Sprintf("- Status: %s\n", v.Status())
56 out += ufmt.Sprintf("- Owner: %s\n", v.Owner)
57 out += ufmt.Sprintf("- Beneficiary: %s\n", v.Beneficiary)
58 out += ufmt.Sprintf("- Balance: %d ugnot\n", v.Balance)
59 out += ufmt.Sprintf("- Check-in interval: %d seconds\n", v.Interval)
60 out += ufmt.Sprintf("- Deadline: %s\n", v.Deadline.Format(time.RFC3339))
61 out += ufmt.Sprintf("- Grace period ends: %s (%d seconds)\n", v.GraceEnd().Format(time.RFC3339), v.GracePeriod)
62 out += ufmt.Sprintf("- Keeper fee at release: %d ugnot (2%%)\n", v.KeeperFee())
63 out += ufmt.Sprintf("- Beneficiary share at release: %d ugnot (98%%)\n", v.BeneficiaryShare())
64 if v.Released {
65 out += ufmt.Sprintf("- Released by keeper: %s at %s\n", v.Keeper, v.ReleasedAt.Format(time.RFC3339))
66 }
67 return out
68}