Search Apps Documentation Source Content File Folder Download Copy Actions Download

render.gno

6.21 Kb ยท 200 lines
  1package gnopensea8
  2
  3import (
  4	"strconv"
  5	"strings"
  6
  7	"gno.land/p/nt/ufmt"
  8)
  9
 10// ============= RENDER =============
 11
 12func Render(path string) string {
 13	if path == "" {
 14		return renderHome()
 15	}
 16
 17	if strings.HasPrefix(path, "listing/") {
 18		idStr := strings.TrimPrefix(path, "listing/")
 19		id, _ := strconv.Atoi(idStr)
 20		return renderListing(id)
 21	}
 22
 23	if strings.HasPrefix(path, "sale/") {
 24		idStr := strings.TrimPrefix(path, "sale/")
 25		id, _ := strconv.Atoi(idStr)
 26		return renderSale(id)
 27	}
 28
 29	if path == "stats" {
 30		return renderStats()
 31	}
 32
 33	return "Page not found"
 34}
 35
 36func renderHome() string {
 37	output := "# ๐Ÿช GNOPENSEA\n\n"
 38	output += "Marketplace compatible **GRC-721** + **GRC-2981** (Automatic Royalties)\n\n"
 39	output += "---\n\n"
 40	output += ufmt.Sprintf("**Marketplace Address:** %s\n\n", marketplaceAddr.String())
 41	output += "---\n\n"
 42
 43	output += "## Statistics\n\n"
 44	output += ufmt.Sprintf("- **Active listings:** %d\n", GetActiveListingsCount())
 45	output += ufmt.Sprintf("- **Total sales:** %d\n", GetTotalSales())
 46	output += ufmt.Sprintf("- **Total volume:** %s\n", formatPrice(GetTotalVolume()))
 47	output += ufmt.Sprintf("- **Royalties paid:** %s\n", formatPrice(GetTotalRoyaltiesPaid()))
 48	output += ufmt.Sprintf("- **Marketplace fee:** %s\n", formatFee(marketplaceFee))
 49	output += ufmt.Sprintf("- **Balance:** %s\n\n", formatPrice(GetBalance()))
 50
 51	output += "[View detailed statistics](/r/pierre115/gnopensea:stats)\n\n"
 52	output += "---\n\n"
 53
 54	// Active listings
 55	if GetActiveListingsCount() > 0 {
 56		output += "## NFTs for sale\n\n"
 57
 58		listings.Iterate("", "", func(key string, value interface{}) bool {
 59			listing := value.(*Listing)
 60			if listing.Active {
 61				output += renderListingPreview(listing)
 62			}
 63			return false
 64		})
 65	} else {
 66		output += "## No NFTs for sale\n\n"
 67		output += "*Be the first to list an NFT!*\n"
 68	}
 69
 70	return output
 71}
 72
 73func renderListingPreview(listing *Listing) string {
 74	// Calculate breakdown
 75	sellerGets, marketFee, royalty, royaltyAddr := GetRoyaltyBreakdown(listing.ListingId)
 76
 77	output := ufmt.Sprintf("### ๐Ÿท๏ธ Listing #%d\n\n", listing.ListingId)
 78	output += ufmt.Sprintf("**Token ID:** %s\n\n", listing.TokenId.String())
 79	output += ufmt.Sprintf("**Price:** %s\n\n", formatPrice(listing.Price))
 80
 81	if royalty > 0 {
 82		output += ufmt.Sprintf("Royalty: %s (%s)\n\n",
 83			formatPrice(royalty),
 84			formatPercentage(royalty, listing.Price))
 85	}
 86
 87	output += ufmt.Sprintf("**Seller receives:** %s\n\n", formatPrice(sellerGets))
 88	output += ufmt.Sprintf("**Market Fees:** %s\n\n", formatPrice(marketFee))
 89	output += ufmt.Sprintf("**Royalty Address:** %s\n\n", royaltyAddr.String())
 90	output += ufmt.Sprintf("**Marketplace Address:** %s\n\n", marketplaceAddr.String())
 91	output += ufmt.Sprintf("[View details](/r/pierre115/gnopensea:listing/%d)\n\n", listing.ListingId)
 92	output += "---\n\n"
 93	return output
 94}
 95
 96func renderListing(listingId int) string {
 97	listing := getListing(listingId)
 98	if listing == nil {
 99		return "# Listing not found"
100	}
101
102	status := "Active"
103	if !listing.Active {
104		status = "Sold/Cancelled"
105	}
106
107	output := ufmt.Sprintf("# Listing #%d - %s\n\n", listingId, status)
108
109	output += "## Details\n\n"
110	output += ufmt.Sprintf("**Token ID:** %s\n\n", listing.TokenId.String())
111	output += ufmt.Sprintf("**Price:** %s\n\n", formatPrice(listing.Price))
112	output += ufmt.Sprintf("**Seller:** `%s`\n\n", listing.Seller.String())
113	output += ufmt.Sprintf("**Listed at block:** %d\n\n", listing.ListedAt)
114
115	if listing.Active {
116		sellerGets, marketFee, royalty, royaltyAddr := GetRoyaltyBreakdown(listingId)
117
118		output += "---\n\n"
119		output += "## Price breakdown\n\n"
120		output += ufmt.Sprintf("- **Total price:** %s (100%%)\n", formatPrice(listing.Price))
121		output += ufmt.Sprintf("- **Marketplace fee (%s):** %s\n",
122			formatFee(marketplaceFee), formatPrice(marketFee))
123
124		if royalty > 0 {
125			output += ufmt.Sprintf("- **Creator royalty (%s):** %s\n",
126				formatPercentage(royalty, listing.Price), formatPrice(royalty))
127			output += ufmt.Sprintf("  - Beneficiary: `%s`\n", royaltyAddr.String())
128		} else {
129			output += "- **Royalty:** None\n"
130		}
131
132		output += ufmt.Sprintf("- **Seller receives:** %s (%s)\n\n",
133			formatPrice(sellerGets), formatPercentage(sellerGets, listing.Price))
134
135		output += "## Purchase\n\n"
136		output += "```bash\n"
137		output += "gnokey maketx call \\\n"
138		output += "  -pkgpath \"gno.land/r/pierre115/gnopensea\" \\\n"
139		output += "  -func \"BuyNFT\" \\\n"
140		output += ufmt.Sprintf("  -args \"%d\" \\\n", listingId)
141		output += ufmt.Sprintf("  -send \"%dugnot\" \\\n", listing.Price)
142		output += "  -broadcast yourkey\n"
143		output += "```\n"
144	}
145
146	output += "\n[โ† Back](/r/pierre115/gnopensea)\n"
147
148	return output
149}
150
151func renderSale(saleId int) string {
152	value, exists := sales.Get(strconv.Itoa(saleId))
153	if !exists {
154		return "# Sale not found"
155	}
156
157	sale := value.(*Sale)
158
159	output := ufmt.Sprintf("# Sale #%d\n\n", saleId)
160	output += ufmt.Sprintf("**Token ID:** %s\n\n", sale.TokenId)
161	output += ufmt.Sprintf("**Price:** %s\n\n", formatPrice(sale.Price))
162	output += ufmt.Sprintf("**Buyer:** `%s`\n\n", sale.Buyer.String())
163	output += ufmt.Sprintf("**Seller:** `%s`\n\n", sale.Seller.String())
164	output += ufmt.Sprintf("**Block:** %d\n\n", sale.SoldAt)
165
166	output += "## ๐Ÿ’ธ Distribution\n\n"
167	output += ufmt.Sprintf("- **Marketplace fee:** %s\n", formatPrice(sale.MarketplaceFee))
168
169	if sale.RoyaltyFee > 0 {
170		output += ufmt.Sprintf("- **Royalty:** %s โ†’ `%s`\n",
171			formatPrice(sale.RoyaltyFee), sale.RoyaltyReceiver.String())
172	}
173
174	sellerReceived := sale.Price - sale.MarketplaceFee - sale.RoyaltyFee
175	output += ufmt.Sprintf("- **Seller received:** %s\n", formatPrice(sellerReceived))
176
177	return output
178}
179
180func renderStats() string {
181	output := "# ๐Ÿ“Š Marketplace Statistics\n\n"
182
183	totalVolume := GetTotalVolume()
184	totalSales := GetTotalSales()
185	totalRoyalties := GetTotalRoyaltiesPaid()
186
187	output += ufmt.Sprintf("**Total volume:** %s\n\n", formatPrice(totalVolume))
188	output += ufmt.Sprintf("**Number of sales:** %d\n\n", totalSales)
189	output += ufmt.Sprintf("**Royalties paid:** %s (%s of volume)\n\n",
190		formatPrice(totalRoyalties), formatPercentage(totalRoyalties, totalVolume))
191
192	if totalSales > 0 {
193		avgPrice := totalVolume / int64(totalSales)
194		output += ufmt.Sprintf("**Average price:** %s\n\n", formatPrice(avgPrice))
195	}
196
197	output += "\n[โ† Back](/r/pierre115/gnopensea)\n"
198
199	return output
200}