render.gno
8.41 Kb · 232 lines
1package portbook
2
3import (
4 "strconv"
5 "strings"
6
7 "gno.land/p/moul/txlink/v0"
8 "gno.land/p/nt/ufmt/v0"
9)
10
11// Render exposes the interactive Markdown UI in gnoweb with clickable Adena txlinks.
12func Render(path string) string {
13 path = strings.Trim(path, "/")
14
15 switch {
16 case path == "":
17 return renderHome()
18 case path == "ports":
19 return renderAllPorts()
20 case path == "requests":
21 return renderAllRequests()
22 case strings.HasPrefix(path, "port/"):
23 id, err := strconv.ParseUint(strings.TrimPrefix(path, "port/"), 10, 64)
24 if err != nil {
25 return "# Invalid Port ID\n\n[← Back to PortBook](:)\n"
26 }
27 return renderPortDetail(id)
28 case strings.HasPrefix(path, "request/"):
29 id, err := strconv.ParseUint(strings.TrimPrefix(path, "request/"), 10, 64)
30 if err != nil {
31 return "# Invalid Request ID\n\n[← Back to PortBook](:)\n"
32 }
33 return renderRequestDetail(id)
34 default:
35 return "# Smelt PortBook — 404\n\nUnknown route `" + path + "`.\n\n[← Back to PortBook](:)\n"
36 }
37}
38
39func renderHome() string {
40 var b strings.Builder
41
42 b.WriteString("# Smelt PortBook\n\n")
43 b.WriteString("**On-Chain Solidity → Native Gno Migration Registry & Community Port Board.**\n\n")
44 b.WriteString("Powered by **[Smelt](https://smeltcli.xyz)** (`smelt analyze` · `smelt convert` · `smelt verify` · `smelt deploy`) — *independent community tool for Gno.land.*\n\n")
45
46 // KPI summary
47 b.WriteString("| Registered Ports | Open Requests | Community Votes | Unique Contributors |\n")
48 b.WriteString("| :---: | :---: | :---: | :---: |\n")
49 b.WriteString(ufmt.Sprintf("| **%d** | **%d** | **%d** | **%d** |\n\n",
50 nextPortID, openReqCount, totalVotes, contributors.Size()))
51
52 // Quick actions via txlink (Adena 1-click)
53 regURL := txlink.NewLink("RegisterPort").
54 AddArgs("name", "", "category", "Utility", "sourceURL", "", "solHash", "", "realmPath", "gno.land/r/", "mode", "idiomatic", "cleanFuncs", "1", "totalFuncs", "1", "warns", "0", "holes", "0", "summary", "").
55 URL()
56 reqURL := txlink.NewLink("RequestPort").
57 AddArgs("contractName", "", "category", "DeFi", "sourceURL", "", "notes", "").
58 URL()
59
60 b.WriteString(ufmt.Sprintf("**Actions:** [+ Register a Port](%s) · [+ Request a Solidity Contract](%s) · [All Ports](:ports) · [All Requests](:requests)\n\n",
61 regURL, reqURL))
62
63 b.WriteString("---\n\n")
64 b.WriteString("## Live Transpiled Contracts\n\n")
65 b.WriteString(renderPortsTable(10))
66
67 b.WriteString("\n---\n\n")
68 b.WriteString("## Community Port Requests\n\n")
69 b.WriteString("Vote on which Ethereum/Solidity protocol should be ported to native Gno next:\n\n")
70 b.WriteString(renderRequestsTable(10))
71
72 b.WriteString("\n---\n\n")
73 b.WriteString("### Built with Smelt\n")
74 b.WriteString("- Shared on-chain runtime: [`gno.land/p/nym-smelt001/solrt`](/p/nym-smelt001/solrt)\n")
75 b.WriteString("- CLI & Docs: [smeltcli.xyz](https://smeltcli.xyz) · [github.com/aber-dev-crypto/smelt](https://github.com/aber-dev-crypto/smelt)\n")
76
77 return b.String()
78}
79
80func renderAllPorts() string {
81 var b strings.Builder
82 b.WriteString("# Smelt PortBook — All Registered Ports\n\n")
83 b.WriteString("[← Home](:) · [Requests Board](:requests)\n\n")
84 b.WriteString(renderPortsTable(0))
85 return b.String()
86}
87
88func renderAllRequests() string {
89 var b strings.Builder
90 b.WriteString("# Smelt PortBook — Community Port Requests\n\n")
91 b.WriteString("[← Home](:) · [Registered Ports](:ports)\n\n")
92 b.WriteString(renderRequestsTable(0))
93 return b.String()
94}
95
96func renderPortsTable(limit int) string {
97 if portsByID.Size() == 0 {
98 return "_No ports registered yet. Be the first to publish a Solidity → Gno migration!_\n"
99 }
100
101 var b strings.Builder
102 b.WriteString("| # | Contract | Category | Mode | Deterministic | Live Deployment | Upvotes |\n")
103 b.WriteString("| :---: | :--- | :--- | :---: | :---: | :--- | :---: |\n")
104
105 count := 0
106 portsByID.ReverseIterate("", "", func(_ string, v interface{}) bool {
107 if limit > 0 && count >= limit {
108 return true
109 }
110 p := v.(*PortEntry)
111 relRealm := strings.TrimPrefix(p.RealmPath, "gno.land")
112 upURL := txlink.NewLink("UpvotePort").AddArgs("portID", strconv.FormatUint(p.ID, 10)).URL()
113 b.WriteString(ufmt.Sprintf(
114 "| [#%d](:port/%d) | **%s** | %s | `%s` | %d%% (%d/%d) | [`%s`](%s) | %d [▲](%s) |\n",
115 p.ID, p.ID, escapeCell(p.Name), escapeCell(p.Category), p.Mode,
116 p.DeterministicPct(), p.CleanFuncs, p.TotalFuncs,
117 p.RealmPath, relRealm, p.Upvotes, upURL,
118 ))
119 count++
120 return false
121 })
122
123 return b.String()
124}
125
126func renderRequestsTable(limit int) string {
127 if requestsByID.Size() == 0 {
128 return "_No open requests yet. Submit a Solidity contract you want ported to Gno!_\n"
129 }
130
131 var b strings.Builder
132 b.WriteString("| # | Solidity Contract | Category | Status | Votes | Action |\n")
133 b.WriteString("| :---: | :--- | :--- | :---: | :---: | :---: |\n")
134
135 count := 0
136 requestsByID.ReverseIterate("", "", func(_ string, v interface{}) bool {
137 if limit > 0 && count >= limit {
138 return true
139 }
140 r := v.(*PortRequest)
141 status := "Open"
142 action := ufmt.Sprintf("[▲ Vote](%s)", txlink.NewLink("VoteRequest").AddArgs("requestID", strconv.FormatUint(r.ID, 10)).URL())
143 if r.FulfilledPortID != 0 {
144 status = ufmt.Sprintf("Ported → [#%d](:port/%d)", r.FulfilledPortID, r.FulfilledPortID)
145 action = "✓"
146 }
147 b.WriteString(ufmt.Sprintf(
148 "| [#%d](:request/%d) | **%s** | %s | %s | **%d** | %s |\n",
149 r.ID, r.ID, escapeCell(r.ContractName), escapeCell(r.Category), status, r.Votes, action,
150 ))
151 count++
152 return false
153 })
154
155 return b.String()
156}
157
158func renderPortDetail(id uint64) string {
159 p := GetPort(id)
160 if p == nil {
161 return "# Port Not Found\n\n[← Back to PortBook](:)\n"
162 }
163
164 relRealm := strings.TrimPrefix(p.RealmPath, "gno.land")
165 upURL := txlink.NewLink("UpvotePort").AddArgs("portID", strconv.FormatUint(p.ID, 10)).URL()
166
167 var b strings.Builder
168 b.WriteString(ufmt.Sprintf("# Port #%d — %s\n\n", p.ID, p.Name))
169 b.WriteString("[← Home](:) · [All Ports](:ports)\n\n")
170
171 if p.Summary != "" {
172 b.WriteString("> " + p.Summary + "\n\n")
173 }
174
175 b.WriteString("| Field | Value |\n| :--- | :--- |\n")
176 b.WriteString(ufmt.Sprintf("| **Live Deployment** | [`%s`](%s) |\n", p.RealmPath, relRealm))
177 b.WriteString(ufmt.Sprintf("| **Category** | %s |\n", escapeCell(p.Category)))
178 b.WriteString(ufmt.Sprintf("| **Transpile Mode** | `%s` |\n", p.Mode))
179 b.WriteString(ufmt.Sprintf("| **Deterministic Score** | **%d%%** (%d/%d clean functions) |\n", p.DeterministicPct(), p.CleanFuncs, p.TotalFuncs))
180 b.WriteString(ufmt.Sprintf("| **Analyzer Diagnostics** | %d warns · %d holes |\n", p.Warns, p.Holes))
181 if p.SolHash != "" {
182 b.WriteString(ufmt.Sprintf("| **Solidity SHA-256** | `%s` |\n", p.SolHash))
183 }
184 if p.SourceURL != "" {
185 b.WriteString(ufmt.Sprintf("| **Solidity Source** | [%s](%s) |\n", p.SourceURL, p.SourceURL))
186 }
187 b.WriteString(ufmt.Sprintf("| **Author** | `%s` |\n", string(p.Author)))
188 b.WriteString(ufmt.Sprintf("| **Block Height** | #%d |\n", p.CreatedAt))
189 b.WriteString(ufmt.Sprintf("| **Upvotes** | **%d** ([▲ Upvote this Port](%s)) |\n", p.Upvotes, upURL))
190
191 return b.String()
192}
193
194func renderRequestDetail(id uint64) string {
195 r := GetRequest(id)
196 if r == nil {
197 return "# Request Not Found\n\n[← Back to PortBook](:)\n"
198 }
199
200 var b strings.Builder
201 b.WriteString(ufmt.Sprintf("# Request #%d — %s\n\n", r.ID, r.ContractName))
202 b.WriteString("[← Home](:) · [All Requests](:requests)\n\n")
203
204 if r.Notes != "" {
205 b.WriteString("> " + r.Notes + "\n\n")
206 }
207
208 b.WriteString("| Field | Value |\n| :--- | :--- |\n")
209 b.WriteString(ufmt.Sprintf("| **Category** | %s |\n", escapeCell(r.Category)))
210 if r.SourceURL != "" {
211 b.WriteString(ufmt.Sprintf("| **Reference Source** | [%s](%s) |\n", r.SourceURL, r.SourceURL))
212 }
213 b.WriteString(ufmt.Sprintf("| **Requester** | `%s` |\n", string(r.Requester)))
214 b.WriteString(ufmt.Sprintf("| **Block Height** | #%d |\n", r.CreatedAt))
215 b.WriteString(ufmt.Sprintf("| **Votes** | **%d** |\n", r.Votes))
216
217 if r.FulfilledPortID != 0 {
218 b.WriteString(ufmt.Sprintf("| **Status** | Fulfilled by [Port #%d](:port/%d) |\n", r.FulfilledPortID, r.FulfilledPortID))
219 } else {
220 voteURL := txlink.NewLink("VoteRequest").AddArgs("requestID", strconv.FormatUint(r.ID, 10)).URL()
221 fulfillURL := txlink.NewLink("FulfillRequest").AddArgs("requestID", strconv.FormatUint(r.ID, 10), "portID", "").URL()
222 b.WriteString(ufmt.Sprintf("| **Status** | Open — [▲ Vote](%s) · [Mark Fulfilled](%s) |\n", voteURL, fulfillURL))
223 }
224
225 return b.String()
226}
227
228func escapeCell(s string) string {
229 s = strings.ReplaceAll(s, "|", "\\|")
230 s = strings.ReplaceAll(s, "\n", " ")
231 return s
232}