Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

gsdash.gno

7.81 Kb · 240 lines
  1// Package gsdash renders one address's whole GnoSwap footprint on chain, with
  2// no indexer and no off-chain query: balances, liquidity positions, what each
  3// one is worth right now, and what it has earned but not collected.
  4//
  5// It reads GnoSwap through the unversioned proxy paths (r/gnoswap/position and
  6// friends) rather than their /vN implementations, so a GnoSwap implementation
  7// upgrade does not reach this realm.
  8package gsdash
  9
 10import (
 11	"chain"
 12	"strconv"
 13	"strings"
 14
 15	"gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/kit/ui/v0"
 16	"gno.land/p/nt/ufmt/v0"
 17
 18	"gno.land/r/gnoswap/gnft"
 19	"gno.land/r/gnoswap/gns"
 20	"gno.land/r/gnoswap/gov/xgns"
 21	"gno.land/r/gnoswap/position"
 22	"gno.land/r/gnoswap/staker"
 23)
 24
 25// scanWindow is how many stored position records one page walks.
 26//
 27// There is no address-to-positions index anywhere in GnoSwap: gnft is a plain
 28// grc721 with no enumerable extension, so the only way to find an address's
 29// positions is to walk the records and ask each one who owns it. GnoSwap's own
 30// position realm renders a keyed lookup and says out loud that it "does not
 31// enumerate positions", which is the same ceiling talking. Queries get
 32// 3,000,000,000 gas (maxGasQuery, gno.land/pkg/sdk/vm/keeper.go), so the walk
 33// is paged rather than unbounded, and the page footer links the next one.
 34const scanWindow = 250
 35
 36// stakerPath is the realm that custodies a staked position's NFT. Staking
 37// transfers the token to it, so for anything staked the grc721 owner is this
 38// address and not a person: see resolveOwner.
 39const stakerPath = "gno.land/r/gnoswap/staker"
 40
 41// Render serves three routes: the index, an address page, and the next page of
 42// an address whose records did not fit in one scan window.
 43//
 44//	""                  the index
 45//	"g1..."             that address, scanning from record 0
 46//	"g1.../from/500"    the same, scanning from record 500
 47func Render(path string) string {
 48	if path == "" {
 49		return renderIndex()
 50	}
 51
 52	addrPart, offset, ok := parsePath(path)
 53	if !ok {
 54		return ui.Empty("Not a route. Try `" + shortSelf + ":g1...`.")
 55	}
 56	addr := address(addrPart)
 57	if !addr.IsValid() {
 58		// Escaped rather than echoed: the path is whatever the reader typed.
 59		return ui.Empty("Not a valid address: " + ui.Inline(addrPart))
 60	}
 61	return renderAddress(addr, offset)
 62}
 63
 64// shortSelf is this realm's own render prefix, used in the links it prints.
 65const shortSelf = "/r/moul/x/gsdash/v0"
 66
 67func renderIndex() string {
 68	s := "# GnoSwap dashboard\n\n"
 69	s += "Everything one address has on GnoSwap, read live from the chain: GNS and xGNS " +
 70		"balances, every liquidity position with what it holds right now, the fees it has " +
 71		"accrued, and the emission rewards waiting to be collected.\n\n"
 72	s += ufmt.Sprintf("Open an address: `%s:g1...`\n\n", shortSelf)
 73
 74	t := ui.NewTable("What", "Now")
 75	t.Row("Stored position records", ufmt.Sprintf("%d", position.GetPositions().Size()))
 76	t.Row("Position NFTs in circulation", ufmt.Sprintf("%d", gnft.TotalSupply()))
 77	t.Row("Records walked per page", ufmt.Sprintf("%d", scanWindow))
 78	s += t.String()
 79
 80	s += "\nRecords include burned positions, so the count is not a count of live liquidity.\n"
 81	return s
 82}
 83
 84func renderAddress(addr address, offset int) string {
 85	s := ufmt.Sprintf("# %s\n\n", ui.AddrFull(addr))
 86
 87	nfts, err := gnft.BalanceOf(addr)
 88	if err != nil {
 89		nfts = 0
 90	}
 91	t := ui.NewTable("GNS", "xGNS", "Position NFTs held")
 92	t.Row(
 93		ufmt.Sprintf("%d", gns.BalanceOf(addr)),
 94		ufmt.Sprintf("%d", xgns.BalanceOf(addr)),
 95		ufmt.Sprintf("%d", nfts),
 96	)
 97	s += t.String() + "\n"
 98
 99	// The offset comes from the path, so it reaches maxInt for free, and
100	// offset+scanWindow would wrap negative. Clamp it to what exists first.
101	size := position.GetPositions().Size()
102	if offset > size {
103		offset = size
104	}
105	end := offset + scanWindow
106	if end > size {
107		end = size
108	}
109	s += ufmt.Sprintf("## Positions\n\nRecords %d to %d of %d.\n\n", offset, end, size)
110	s += renderPositions(addr, offset)
111
112	if next := offset + scanWindow; next < size {
113		s += ufmt.Sprintf("\n[Next %d records](%s:%s/from/%d)\n", scanWindow, shortSelf, addr.String(), next)
114	}
115	return s
116}
117
118// renderPositions walks one window of the position records and prints the rows
119// belonging to addr.
120//
121// The walk is deliberately cheap per record and expensive per hit: identifying
122// the owner is one or two tree lookups, while the price math behind IsInRange,
123// the token balances and the unclaimed fees costs far more. Only a row that is
124// actually printed pays for it.
125func renderPositions(addr address, offset int) string {
126	t := ui.NewTable("ID", "Pool", "Ticks", "Liquidity", "In range", "Holds", "Uncollected fees", "Staked", "Reward")
127
128	position.GetPositions().IterateByOffset(offset, scanWindow, func(key string, _ any) bool {
129		id, err := strconv.ParseUint(key, 10, 64)
130		if err != nil {
131			return false
132		}
133		owner, staked, ok := resolveOwner(id)
134		if !ok || owner != addr {
135			return false
136		}
137		t.Row(row(id, staked)...)
138		return false
139	})
140
141	return t.OrEmpty("No positions in this window.")
142}
143
144// row is the expensive half: everything here reads pool price state or recomputes
145// a fee, so it runs for a matched position and never for a scanned one.
146func row(id uint64, staked bool) []string {
147	poolKey, _ := position.GetPositionPoolKey(id)
148	lower, upper, _ := position.GetPositionTicks(id)
149	liquidity, _ := position.GetPositionLiquidity(id)
150	inRange, _ := position.IsInRange(id)
151	amount0, amount1, _ := position.GetPositionTokenBalances(id)
152	fee0, fee1, _ := position.GetUnclaimedFee(id)
153
154	reward := "-"
155	if staked {
156		if r, err := staker.CollectableEmissionReward(id); err == nil {
157			reward = ufmt.Sprintf("%d", r)
158		}
159	}
160
161	return []string{
162		ufmt.Sprintf("%d", id),
163		ui.Cell(shortPool(poolKey)),
164		ufmt.Sprintf("%d to %d", lower, upper),
165		ui.Cell(liquidity),
166		yesNo(inRange),
167		ufmt.Sprintf("%d / %d", amount0, amount1),
168		ui.Cell(fee0) + " / " + ui.Cell(fee1),
169		yesNo(staked),
170		reward,
171	}
172}
173
174// resolveOwner answers who a position really belongs to, and whether it is staked.
175//
176// Staking transfers the position NFT to the staker realm, so for every staked
177// position the grc721 owner is that realm's address and not the person's. A
178// dashboard that trusts the NFT owner alone shows an empty page to everyone who
179// staked, which is most of them. The deposit record is what still names the
180// person.
181func resolveOwner(id uint64) (owner address, staked bool, ok bool) {
182	held, err := position.GetPositionOwner(id)
183	if err != nil {
184		return "", false, false
185	}
186	if held != chain.PackageAddress(stakerPath) {
187		return held, false, true
188	}
189	deposited, err := staker.GetDepositOwner(id)
190	if err != nil {
191		// Custodied by the staker with no deposit behind it: in flight, or a
192		// record this realm does not understand. Better to say nothing than to
193		// attribute it to the staker realm as if it were a user.
194		return "", true, false
195	}
196	return deposited, true, true
197}
198
199// parsePath splits an address route from its optional scan offset.
200func parsePath(path string) (addr string, offset int, ok bool) {
201	addr, rest, found := strings.Cut(path, "/")
202	if !found {
203		return addr, 0, true
204	}
205	after, found := strings.CutPrefix(rest, "from/")
206	if !found {
207		return "", 0, false
208	}
209	n, err := strconv.Atoi(after)
210	if err != nil || n < 0 {
211		return "", 0, false
212	}
213	return addr, n, true
214}
215
216// shortPool turns a pool key ("<token0 path>.<symbol>:<token1 path>.<symbol>:<fee>")
217// into something that fits a table cell.
218func shortPool(poolKey string) string {
219	parts := strings.Split(poolKey, ":")
220	if len(parts) != 3 {
221		return poolKey
222	}
223	return symbolOf(parts[0]) + "/" + symbolOf(parts[1]) + " " + parts[2]
224}
225
226// symbolOf takes the symbol off a GnoSwap token key, which is the grc20 registry
227// form "<pkgpath>.<Symbol>".
228func symbolOf(tokenKey string) string {
229	if i := strings.LastIndex(tokenKey, "."); i >= 0 {
230		return tokenKey[i+1:]
231	}
232	return tokenKey
233}
234
235func yesNo(b bool) string {
236	if b {
237		return "yes"
238	}
239	return "no"
240}