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.

fees.gno

6.75 Kb · 229 lines
  1// Package fees is the entry-fee rate each account pays on Mimir's pool and VS
  2// markets (the gno.land port of contracts/MimirFees.sol).
  3//
  4// Everyone pays BaseBps (0.5%) of what they put in. A lower rate needs a fee
  5// ticket signed by the signer: "this account is tier T until `expires`". The
  6// account submits it with ApplyTicket, normally in the same transaction as its
  7// stake. Mimir's server signs no tickets.
  8//
  9// Signatures are ed25519 over TicketMessage(account, tier, expires), which
 10// binds the chain id and this realm's path, so a ticket cannot be replayed on
 11// another network or another deployment.
 12//
 13// Trust: the signer can only lower an account's rate (BaseBps is the most
 14// anyone pays), so a stolen signer key costs the protocol revenue, never a user
 15// money. The owner revokes it at once (RevokeSigner); a new signer waits
 16// SignerTimelock, so a stolen owner key cannot install one quietly. Both retire
 17// every ticket the old signer issued (each ticket carries the signer epoch it
 18// was applied under).
 19package fees
 20
 21import (
 22	"chain"
 23	"chain/runtime"
 24	"crypto/ed25519"
 25	"encoding/hex"
 26	"strconv"
 27	"time"
 28
 29	"gno.land/p/nt/avl/v0"
 30	"gno.land/p/nt/ufmt/v0"
 31)
 32
 33// Deployer is the initial owner, the address that deploys these realms.
 34const Deployer = address("g12dcsmwccgdxasqde2eumua88tlrkw83kqf6tcz")
 35
 36const (
 37	BaseBps   = int64(50) // 0.5%
 38	HolderBps = int64(25) // 0.25%: tier 1 ticket
 39	WhaleBps  = int64(10) // 0.1%: tier 2 ticket
 40
 41	// A ticket may not outlive this, so a discount ends within two days.
 42	MaxTicketSeconds         = int64(2 * 24 * 60 * 60)
 43	OwnershipTimelockSeconds = int64(2 * 24 * 60 * 60)
 44	SignerTimelockSeconds    = int64(2 * 24 * 60 * 60)
 45)
 46
 47type Ticket struct {
 48	Tier    int
 49	Expires int64
 50	Epoch   int64
 51}
 52
 53var (
 54	tickets avl.Tree // address -> *Ticket
 55
 56	signer          []byte // ed25519 public key; nil when revoked or unset
 57	owner           address
 58	pendingOwner    address
 59	pendingOwnerEta int64
 60	// Bumped on every signer change or revoke: tickets applied under an earlier epoch pay BaseBps.
 61	signerEpoch      int64
 62	pendingSigner    []byte
 63	pendingSignerEta int64
 64	signerEverSet    bool
 65	selfPath         string
 66)
 67
 68func init(cur realm) {
 69	owner = Deployer
 70	selfPath = cur.PkgPath()
 71	chain.Emit("OwnershipTransferred", "previous", "", "next", owner.String())
 72}
 73
 74func now() int64 { return time.Now().Unix() }
 75
 76func assertOwner(cur realm) {
 77	if cur.Previous().Address() != owner {
 78		panic("NotOwner")
 79	}
 80}
 81
 82func decodeKey(pubHex string) []byte {
 83	k, err := hex.DecodeString(pubHex)
 84	if err != nil || len(k) != 32 {
 85		panic("BadSignerKey")
 86	}
 87	return k
 88}
 89
 90// SetInitialSigner installs the first signer, once, as the Solidity
 91// constructor did. Every later change goes through the timelock.
 92func SetInitialSigner(cur realm, pubHex string) {
 93	assertOwner(cur)
 94	if signerEverSet {
 95		panic("SignerAlreadySet")
 96	}
 97	signer = decodeKey(pubHex)
 98	signerEverSet = true
 99	chain.Emit("SignerChanged", "previous", "", "next", pubHex)
100}
101
102// RevokeSigner is the emergency stop: no ticket verifies any more, and the
103// ones applied stop counting.
104func RevokeSigner(cur realm) {
105	assertOwner(cur)
106	chain.Emit("SignerChanged", "previous", hex.EncodeToString(signer), "next", "")
107	signer = nil
108	signerEverSet = true
109	signerEpoch++
110}
111
112func QueueSigner(cur realm, pubHex string) {
113	assertOwner(cur)
114	pendingSigner = decodeKey(pubHex)
115	pendingSignerEta = now() + SignerTimelockSeconds
116	chain.Emit("SignerQueued", "next", pubHex, "eta", strconv.FormatInt(pendingSignerEta, 10))
117}
118
119// ExecuteSigner is permissionless once due, like the market realms' queued changes.
120func ExecuteSigner(cur realm) {
121	if pendingSignerEta == 0 {
122		panic("NothingQueued")
123	}
124	if now() < pendingSignerEta {
125		panic("Timelocked")
126	}
127	chain.Emit("SignerChanged", "previous", hex.EncodeToString(signer), "next", hex.EncodeToString(pendingSigner))
128	signer = pendingSigner
129	signerEverSet = true
130	signerEpoch++
131	pendingSigner = nil
132	pendingSignerEta = 0
133}
134
135func TransferOwnership(cur realm, next address) {
136	assertOwner(cur)
137	if !next.IsValid() {
138		panic("ZeroAddress")
139	}
140	pendingOwner = next
141	pendingOwnerEta = now() + OwnershipTimelockSeconds
142	chain.Emit("OwnershipTransferStarted", "previous", owner.String(), "next", next.String(),
143		"eta", strconv.FormatInt(pendingOwnerEta, 10))
144}
145
146func CancelOwnershipTransfer(cur realm) {
147	assertOwner(cur)
148	if pendingOwner == "" {
149		panic("NotPendingOwner")
150	}
151	chain.Emit("OwnershipTransferCancelled", "pending", pendingOwner.String())
152	pendingOwner = ""
153	pendingOwnerEta = 0
154}
155
156func AcceptOwnership(cur realm) {
157	caller := cur.Previous().Address()
158	if pendingOwner == "" || caller != pendingOwner {
159		panic("NotPendingOwner")
160	}
161	if now() < pendingOwnerEta {
162		panic("Timelocked")
163	}
164	chain.Emit("OwnershipTransferred", "previous", owner.String(), "next", caller.String())
165	owner = caller
166	pendingOwner = ""
167	pendingOwnerEta = 0
168}
169
170// TicketMessage is the exact byte string the signer signs for (account, tier, expires).
171func TicketMessage(account address, tier int, expires int64) string {
172	return ufmt.Sprintf("Mimir Fees v1|%s|%s|%s|%d|%d",
173		runtime.ChainID(), selfPath, account.String(), tier, expires)
174}
175
176// ApplyTicket stores a signed tier for the caller: a ticket is bound to the
177// account it was signed for.
178func ApplyTicket(cur realm, tier int, expires int64, sigHex string) {
179	account := cur.Previous().Address()
180	if tier < 0 || tier > 2 {
181		panic("BadTier")
182	}
183	if expires <= now() || expires > now()+MaxTicketSeconds {
184		panic("BadExpiry")
185	}
186	sig, err := hex.DecodeString(sigHex)
187	if err != nil || len(sig) != 64 || signer == nil {
188		panic("BadSignature")
189	}
190	if !ed25519.Verify(signer, []byte(TicketMessage(account, tier, expires)), sig) {
191		panic("BadSignature")
192	}
193	tickets.Set(account.String(), &Ticket{Tier: tier, Expires: expires, Epoch: signerEpoch})
194	chain.Emit("TicketApplied", "account", account.String(), "tier", strconv.Itoa(tier),
195		"expires", strconv.FormatInt(expires, 10))
196}
197
198// EntryBps is the account's entry fee in basis points right now.
199func EntryBps(account address) int64 {
200	v := tickets.Get(account.String())
201	if v == nil {
202		return BaseBps
203	}
204	t := v.(*Ticket)
205	if t.Expires <= now() || t.Epoch != signerEpoch {
206		return BaseBps
207	}
208	switch t.Tier {
209	case 2:
210		return WhaleBps
211	case 1:
212		return HolderBps
213	}
214	return BaseBps
215}
216
217func Owner() address        { return owner }
218func PendingOwner() address { return pendingOwner }
219func SignerEpoch() int64    { return signerEpoch }
220func Signer() string        { return hex.EncodeToString(signer) }
221
222func Render(path string) string {
223	if path != "" {
224		return ufmt.Sprintf("Entry fee for %s: %d bps\n", path, EntryBps(address(path)))
225	}
226	return ufmt.Sprintf("# Mimir fees\n\nBase entry fee: %d bps (0.5%%). Tier 1: %d bps. Tier 2: %d bps.\n\n"+
227		"Owner: %s\n\nSigner epoch: %d\n\nLook up an account: `:<address>`\n",
228		BaseBps, HolderBps, WhaleBps, owner.String(), signerEpoch)
229}