1package banktest
2
3import (
4 "std"
5 "time"
6)
7
8type activity struct {
9 caller std.Address
10 sent std.Coins
11 returned std.Coins
12 time time.Time
13}
14
15func (act *activity) String() string {
16 return act.caller.String() + " " +
17 act.sent.String() + " sent, " +
18 act.returned.String() + " returned, at " +
19 act.time.Format("2006-01-02 3:04pm MST")
20}
21
22var latest [10]*activity
23
24// Deposit will take the coins (to the realm's pkgaddr) or return them to user.
25func Deposit(returnDenom string, returnAmount int64) string {
26 std.AssertOriginCall()
27 caller := std.GetOrigCaller()
28 send := std.Coins{{returnDenom, returnAmount}}
29 // record activity
30 act := &activity{
31 caller: caller,
32 sent: std.GetOrigSend(),
33 returned: send,
34 time: time.Now(),
35 }
36 for i := len(latest) - 2; i >= 0; i-- {
37 latest[i+1] = latest[i] // shift by +1.
38 }
39 latest[0] = act
40 // return if any.
41 if returnAmount > 0 {
42 banker := std.GetBanker(std.BankerTypeOrigSend)
43 pkgaddr := std.GetOrigPkgAddr()
44 // TODO: use std.Coins constructors, this isn't generally safe.
45 banker.SendCoins(pkgaddr, caller, send)
46 return "returned!"
47 } else {
48 return "thank you!"
49 }
50}
51
52func Render(path string) string {
53 // get realm coins.
54 banker := std.GetBanker(std.BankerTypeReadonly)
55 coins := banker.GetCoins(std.GetOrigPkgAddr())
56
57 // render
58 res := ""
59 res += "## recent activity\n"
60 res += "\n"
61 for _, act := range latest {
62 if act == nil {
63 break
64 }
65 res += " * " + act.String() + "\n"
66 }
67 res += "\n"
68 res += "## total deposits\n"
69 res += coins.String()
70 return res
71}
banktest.gno
1.52 Kb ยท 71 lines