txlink.gno
2.20 Kb ยท 81 lines
1package txlink
2
3import (
4 "std"
5 "time"
6
7 "gno.land/p/moul/txlink"
8)
9
10type Message struct {
11 Author string
12 Text string
13 Time time.Time
14}
15
16var messages []Message
17
18// Post saves a message with the caller as the author
19func Post(cur realm, text string) {
20 msg := Message{
21 Author: std.PreviousRealm().Address().String(),
22 Text: text,
23 Time: time.Now(),
24 }
25 messages = append(messages, msg)
26}
27
28func Render(_ string) string {
29 out := "# Transaction Links\n\n"
30 out += `Transaction links ("txlinks" for short) are a standardized way of creating
31URLs that Gno-enabled wallets can interpret and generate transactions from.
32
33They allow developers to turn basic hyperlinks into realm calls
34with optional arguments and funds attached.
35
36Arguments with empty values are treated as _wallet-settable_, allowing the
37user to customize them before submitting the transaction.
38
39You can also link to functions in _other realms_, using the pkgpath of the realm in question.
40
41Check out the links below.
42
43`
44
45 out += "## Post a Message\n"
46
47 // Basic call
48 out += "- [Say Hello](" + txlink.Call("Post", "text", "Hello from txlink!") + ") - A simple, static argument call\n"
49
50 // A link builder including some funds specified in the string format (std.Coin.String())
51 custom := txlink.NewLink("Post").
52 AddArgs("text", "Support this board with 1ugnot").
53 SetSend("1ugnot").
54 URL()
55 out += "- [Send Supportive Message](" + custom + ") - A more complex call with coins included\n"
56
57 // Up to the user to set the argument
58 out += "- [Write Your Own Message](" + txlink.NewLink("Post").
59 AddArgs("text", ""). // Should be editable in the user's wallet
60 URL() + ") - A user-defined argument call\n"
61
62 // Cross-realm example
63 crossRealm := txlink.Realm("gno.land/r/docs/minisocial/v2").
64 NewLink("CreatePost").
65 AddArgs("text", "Hello from `/r/docs/txlink`!").
66 URL()
67 out += "- [Say Hello on MiniSocial V2!](" + crossRealm + ") - A call to another realm\n"
68
69 out += "\n## Recent Messages\n"
70 if len(messages) == 0 {
71 out += "_No messages yet._\n\n"
72 return out
73 }
74
75 for i := len(messages) - 1; i >= 0 && i >= len(messages)-5; i-- {
76 msg := messages[i]
77 out += "- **" + msg.Author + "**: " + msg.Text + " (" + msg.Time.Format("15:04:05") + ")\n"
78 }
79
80 return out
81}