adder.gno

1.03 Kb ยท 42 lines
 1package adder
 2
 3import (
 4	"strconv"
 5	"time"
 6
 7	"gno.land/p/moul/txlink"
 8)
 9
10// Global variables to store the current number and last update timestamp
11var (
12	number     int
13	lastUpdate time.Time
14)
15
16// Add function to update the number and timestamp
17func Add(n int) {
18	number += n
19	lastUpdate = time.Now()
20}
21
22// Render displays the current number value, last update timestamp, and a link to call Add with 42
23func Render(path string) string {
24	// Display the current number and formatted last update time
25	result := "# Add Example\n\n"
26	result += "Current Number: " + strconv.Itoa(number) + "\n\n"
27	result += "Last Updated: " + formatTimestamp(lastUpdate) + "\n\n"
28
29	// Generate a transaction link to call Add with 42 as the default parameter
30	txLink := txlink.Call("Add", "n", "42")
31	result += "[Increase Number](" + txLink + ")\n"
32
33	return result
34}
35
36// Helper function to format the timestamp for readability
37func formatTimestamp(timestamp time.Time) string {
38	if timestamp.IsZero() {
39		return "Never"
40	}
41	return timestamp.Format("2006-01-02 15:04:05")
42}