Skip to content Search Apps Documentation Source Content 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.

notify.gno

3.01 Kb · 85 lines
 1package gnosplit
 2
 3import (
 4	"chain"
 5
 6	"gno.land/p/nym-alexiscolin000/gnosplit/ledger/v0"
 7	gnotif "gno.land/r/nym-gfanton001/gnotif/v0"
 8)
 9
10// Events. The first ones say what changed, for indexers and apps. The notify
11// ones each carry "to", a wallet: gnotif turns them into a browser
12// notification for whoever opted in with that address. A notify event goes
13// only to members with a wallet, never to the member who acted.
14const (
15	EventGroupCreated = "GroupCreated"
16	EventMemberAdded  = "MemberAdded"
17	EventMemberLinked = "MemberLinked"
18	EventEntryAdded   = "EntryAdded"
19	EventEntryDeleted = "EntryDeleted"
20	EventGroupMoved   = "GroupMoved"
21	EventOwnerOffered = "OwnerOffered"
22	EventOwnerChanged = "OwnerChanged"
23	EventSuccessorSet = "SuccessorSet"
24
25	NotifyExpense  = "Expense"  // to, group, gname, by, title, share
26	NotifyIncome   = "Income"   // to, group, gname, by, title, share
27	NotifyTransfer = "Transfer" // to, group, gname, from, peer, amount
28	NotifyReminder = "Reminder" // to, group, gname, by, amount
29	NotifyJoined   = "Joined"   // to, group, gname, by
30)
31
32// declareTriggers registers the notify events with gnotif at deploy. The
33// registry marks them verified, since this realm declares its own.
34// ponytail: gnotif is a v0 on onyx; a new registry version means a new
35// release of this realm, which is why every gnotif call lives in this file.
36func declareTriggers(cur realm) {
37	triggers := [][2]string{
38		{NotifyExpense, "{by} added {title}. Your share: {share}"},
39		{NotifyIncome, "{by} recorded {title}. You get back {share}"},
40		{NotifyTransfer, "{from} paid {peer} {amount}"},
41		{NotifyReminder, "{by} reminds you that you owe {amount}"},
42		{NotifyJoined, "{by} added you to the group"},
43	}
44	for _, t := range triggers {
45		gnotif.Declare(cross(cur), self, t[0], "", "to", "{gname}", t[1], hub+":{group}")
46	}
47}
48
49func notifyEntry(g *Group, e *Entry, by int) {
50	if e.Kind == KindTransfer {
51		for _, i := range []int{e.Payer, e.Who[0]} {
52			if to := g.Members[i].Addr; to != "" && i != by {
53				chain.Emit(NotifyTransfer, "to", to.String(), "group", itoa(g.ID), "gname", g.Name,
54					"from", g.Members[e.Payer].Name, "peer", g.Members[e.Who[0]].Name, "amount", g.money(e.Total))
55			}
56		}
57		return
58	}
59	event := NotifyExpense
60	if e.Kind == KindIncome {
61		event = NotifyIncome
62	}
63	for k, i := range e.Who {
64		if to := g.Members[i].Addr; to != "" && i != by {
65			share := e.Owe[k]
66			if share < 0 {
67				share = -share
68			}
69			chain.Emit(event, "to", to.String(), "group", itoa(g.ID), "gname", g.Name,
70				"by", g.Members[by].Name, "title", e.Title, "share", g.money(share))
71		}
72	}
73}
74
75func notifyReminder(g *Group, m *Member, by string) {
76	chain.Emit(NotifyReminder, "to", m.Addr.String(), "group", itoa(g.ID), "gname", g.Name,
77		"by", by, "amount", g.money(-m.Bal))
78}
79
80func notifyJoined(g *Group, a address, by string) {
81	chain.Emit(NotifyJoined, "to", a.String(), "group", itoa(g.ID), "gname", g.Name, "by", by)
82}
83
84// money writes cents with the group's currency: "42.50 €".
85func (g *Group) money(c int64) string { return ledger.Format(c) + " " + g.Currency }