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.

mandate source realm

Package mandate is capped, revocable direct debit on native GNOT, built on Gno session keys.

Overview

Package mandate is capped, revocable direct debit on native GNOT, built on Gno session keys.

A payee publishes a plan ("1 GNOT every 7 days"). A payer subscribes with two things signed by the payer's own key:

  1. a session on chain for the payee's collector key, scoped to this realm only, with a spend limit of the plan's amount plus a small fee allowance per period, and an expiry;
  2. Subscribe here, which records the terms for that session.

The payee's collector then calls Pull once per period, signed with the session key on behalf of the payer and sending exactly the plan's amount. The realm is the rulebook: it checks who, how much and when, then forwards the coins to the payee in the same transaction. It never keeps funds.

Honest limits, also shown on the home page:

  • It is direct debit, not escrow. A payer can revoke the session, cancel, or empty the account at any time; the payee is then not paid.
  • The chain caps what the payer can lose per period at the session's spend limit: amount plus fee allowance. A hostile payee can burn the allowance as gas fees (it goes to the network, not to the payee).
  • The chain allows at most 16 sessions per account, so a wallet can have at most 16 mandates at a time.
  • A realm cannot see an on-chain session revoke. A service deciding whether someone has paid reads PaidThrough, which moves only when a payment succeeds; HasMandate is only the realm's view (recorded, not cancelled, not expired), and the chain's own view of the session is the free query auth/accounts/<payer>/session/<session address>.
  • A session scoped to vm/exec:<this realm> also allows every realm under this path (the chain's scope match is prefix-based). Nothing must ever be deployed under a sub-path of this realm. Pull also refuses any session whose allow list is not exactly this realm, but that cannot protect a payer from a sub-realm called directly.

There is no admin, owner, pause, upgrade or sweep.

Constants 7

const MinAmount, MaxAmount, MinPeriod, MaxPeriod, MaxTerm, FeeAllowance, MaxTitleLen, PageSize, MaxSessionsPerWallet

 1const (
 2	// MinAmount is the smallest plan amount, 0.1 GNOT. A Pull uses about
 3	// 8-9M gas on a local chain, and the chain charges the whole fee the
 4	// signer sets, not the gas used: the pages set 20,000 ugnot for 20M gas
 5	// wanted (Onyx's minimum price of 1 ugnot per 1,000 gas). At 0.1 GNOT that
 6	// fee is already a fifth of the payment; below it, fees would dominate.
 7	MinAmount int64 = 100_000
 8	// MaxAmount is 1,000,000 GNOT per period: far above any real plan, and
 9	// low enough that totals cannot overflow int64 for millions of pulls.
10	MaxAmount int64 = 1_000_000_000_000
11	// MinPeriod is one minute: block times are a few seconds, and the
12	// chain's spend period is counted in seconds. Short periods exist for
13	// demos and tests; pages flag any period under a day.
14	MinPeriod int64 = 60
15	// MaxPeriod is 30 days, exactly the chain's limit on a session's spend
16	// period (tm2 std.MaxSpendPeriod). The session's spend period is always
17	// the plan's period, so the cap the pages promise ("at most price +
18	// allowance per period") is exactly what the chain enforces. No yearly
19	// plans: the chain refuses a session whose spend period is longer.
20	MaxPeriod int64 = 30 * 24 * 60 * 60
21	// MaxTerm is the longest a mandate may run from the moment it is
22	// created: 366 days. A payer renews once a year at most; the chain's own
23	// cap on a session's expiry is 4 x 365 days (tm2 std.MaxSessionDuration).
24	MaxTerm int64 = 366 * 24 * 60 * 60
25	// FeeAllowance is what the pages add to the session's spend limit for
26	// fees and storage (0.1 GNOT). The chain accepts a fee when fee / gas
27	// wanted is at least the current gas price (1 ugnot per 1,000 gas on
28	// Onyx, raised by the chain when blocks are full), and counts the Pull's
29	// storage deposit against the same limit. With the pages' 20M gas wanted
30	// the minimum fee is 20,000 ugnot. A Pull creates no object: a first
31	// payment adds at most 78 bytes measured (7,800 ugnot at 100 ugnot per
32	// byte), later ones nothing. So the worst pull costs about 28,000 ugnot
33	// and the allowance covers a gas price up to about 4.5x Onyx's current
34	// one (docs/design.md, "Pull's budget").
35	FeeAllowance int64 = 100_000
36	// MaxTitleLen is the longest plan title, in characters.
37	MaxTitleLen = 80
38	// PageSize is the number of rows per list page and per read call.
39	PageSize = 20
40	// MaxSessionsPerWallet is the chain's limit (tm2 std.MaxSessionsPerAccount).
41	MaxSessionsPerWallet = 16
42)
source

Limits. See docs/design.md for the reasoning behind each value.

const ScanLimit, WalkLimit

1const (
2	ScanLimit = 200
3	WalkLimit = 1000
4)
source

ScanLimit is the most supporter-list entries one free read resolves (looks up in its payer's record); WalkLimit is the most it reads at all. Entries carry their status, so cancelled ones, and for some reads unpaid ones, are skipped without a lookup. Reads that filter return a cursor to continue from, so junk can delay but never hide a real entry.

const ChainID, Remote

1const (
2	ChainID = "onyx-1"
3	Remote  = "https://rpc.onyx.testnets.gno.land:443"
4)
source

Network the copy-ready commands are pinned to.

const Denom

1const Denom = "ugnot"
source

Denom is the only coin a plan can be paid in.

const MaxActivePerPayer

1const MaxActivePerPayer = MaxSessionsPerWallet
source

MaxActivePerPayer is how many active mandates one payer may hold: the chain allows 16 sessions per account (tm2 std.MaxSessionsPerAccount), and each active mandate needs its own session key.

const MaxCreditsPerPayer

1const MaxCreditsPerPayer = 16
source

MaxCreditsPerPayer bounds a payer's kept credits: 16 plans left while each is still paid ahead. At the bound, the payer's own Cancel or expiry lets the credit ending soonest give way to a later one (that plan's time is lost); a payee's Cancel never evicts: its new credit is not kept.

const SourceRepo

1const SourceRepo = "https://github.com/elangrr/mandate"
source

SourceRepo is where the source code is published, shown in the footer of every page.

Functions 22

func Cancel

crossing Action
1func Cancel(cur realm, payer address, sessionAddr address)
source

Cancel ends a mandate in the realm. The payer or the payee may cancel, each with their own key (never a session). A payer should also revoke the session on chain (the payer page shows the command); cancelling alone already makes every later Pull fail.

func CollectorPlans

Action
1func CollectorPlans(payee address, page int) string
source

CollectorPlans lists a payee's plans for a collector script, one per line:

Example
1<plan id> <amount ugnot> <period seconds> <mandates>

PageSize per page; the last line is "more" or "end".

func CollectorScan

Action
1func CollectorScan(planID uint64, sessionAddr address, from int) string
source

CollectorScan walks at most ScanLimit entries of a plan's supporter list, from index `from` (0 = oldest), and returns the active mandates whose session key is sessionAddr, one per line:

Example
1<index> <mandate id> <payer> <amount ugnot> <next pull unix> <due|wait> <pulls> <paid>

<paid> is 1 when the payer has paid for this plan (this mandate pulled, or it started with paid time carried over from an earlier one), 0 when it never has. The last line is "next <index>" when more entries follow, "end" otherwise. Cancelled and expired mandates, and mandates for other keys, are skipped here, so a collector never needs to read them. Anyone can record a mandate without a session, so a collector should check on chain that the session exists, at least before the first pull of a mandate whose payer never paid.

func HasMandate

Action
1func HasMandate(payer address, planID uint64) bool
source

HasMandate reports whether payer has a mandate for the plan that is neither cancelled nor expired in the realm. It says nothing about payment (a mandate costs only a storage deposit to record) and cannot see a session revoked on chain: do not grant anything on it; use PaidThrough.

func IsPaidUp

Action
1func IsPaidUp(payer address, planID uint64) bool
source

IsPaidUp reports whether payer's latest payment for the plan still covers now (now < PaidThrough). See PaidThrough.

func NextPullAt

Action
1func NextPullAt(payer address, planID uint64) int64
source

NextPullAt returns when the next pull of payer's active mandate for the plan is allowed (unix seconds), or 0 if there is no active mandate.

func Offer

crossing Action
1func Offer(cur realm, title string, amountUgnot, periodSeconds int64) uint64
source

Offer publishes a plan: amountUgnot every periodSeconds, paid to the caller. Signed with the payee's own key; no session, no coins.

func PaidThrough

Action
1func PaidThrough(payer address, planID uint64) int64
source

PaidThrough is the read for services that deliver something to paying supporters. It returns the unix time until which the payer's payments for the plan cover them: the latest payment's time plus one period. Paid time stays the payer's when a mandate is cancelled (by either side) or expires, and a new mandate for the same plan starts when it ends, so it is not charged twice. The one exception: a payer who already holds 16 kept periods (MaxCreditsPerPayer). 0 when they never paid for the plan. Deliver while now < PaidThrough.

It turns false at the due time and moves on only when the payee's collector pulls the next payment: allow a grace equal to how often the collector runs (an hour, say) before treating a supporter as lapsed.

A payment is proof; a mandate is not: anyone can record a mandate without creating a session and without paying. A realm also cannot see a session revoked on chain: a revoked supporter simply stops paying, so PaidThrough stops moving and runs out within one period.

func PlanCounts

Action
1func PlanCounts(planID uint64) (paidThisPeriod, paidLastPeriod, unpaid int, unpaidExact bool)
source

PlanCounts returns the numbers the gnoweb plan page shows: how many supporters paid in the plan's current period window and in the one before (as in GetPlan), and how many active mandates have never paid, among the latest ScanLimit entries (unpaidExact is false when there were more entries than that).

func Pull

crossing Action
1func Pull(cur realm)
source

Pull collects one period's payment. It must be signed with the mandate's session key on behalf of the payer (gnokey ... -master <payer> <session key>), as a direct call, sending exactly the plan's amount. The realm forwards the coins to the plan's payee in the same transaction. At most one pull per period; the next one is due one period after this one, the same rule the chain applies to the session's spend period.

func Render

1func Render(path string) string
source

Render returns the gnoweb page for path, with the footer.

func SetCollectorKey

crossing Action
1func SetCollectorKey(cur realm, pubKey string)
source

SetCollectorKey publishes the public key (bech32, gpub1...) of the key the payee's collector signs pulls with. Plan pages show it in the copy-ready commands, so payers create their session for the right key. Signed with the payee's own key. It does not change existing mandates.

func Subscribe

crossing Action
1func Subscribe(cur realm, planID uint64, sessionAddr address, expiresAtUnix int64) uint64
source

Subscribe records the terms for one session: the payer allows the holder of sessionAddr to pull the plan's amount once per period until expiresAtUnix, through this realm only. Signed with the payer's own key (never a session), no coins. Create the session on chain in the same transaction or just before (see the plan page for the exact command).

The first payment is collected by the first Pull, which is due at once (billing in advance: each period is paid at its start).

func Totals

Action
1func Totals() (plansCount, mandatesCount int, pulls, paid int64)
source

Totals returns the number of plans and mandates, and all pulls so far.

func MandateOf

Action
1func MandateOf(payer, sessionAddr address) (MandateInfo, bool)
source

MandateOf returns the latest mandate of payer for this session address.

func MandatesOf

Action
1func MandatesOf(payer address, page int) []MandateInfo
source

MandatesOf lists a payer's mandates (all states), newest first.

func PayingSupporters

Action
1func PayingSupporters(planID uint64, from int) ([]MandateInfo, int)
source

PayingSupporters lists a plan's supporters the way its gnoweb page does: mandates that have paid at least once and are active now, newest first, at most PageSize per call. Pass from = -1 for the newest; the second result is where to continue. A client must stop when it is -1 (the list is exhausted): -1 passed back in starts again from the newest, so a client that feeds the cursor back unchecked loops for ever. Each call walks at most WalkLimit entries and looks up at most ScanLimit of them, so a call may return fewer than PageSize (even none) and still a cursor.

func SupportersOf

Action
1func SupportersOf(planID uint64, page int) []MandateInfo
source

SupportersOf lists a plan's active mandates (not cancelled, replaced or expired), oldest first. Page n covers entries (n-1)*PageSize to n*PageSize-1 of the plan's list, so a page may hold fewer rows than PageSize; inactive ones are on the payers' own pages.

func GetPayee

Action
1func GetPayee(payee address) (PayeeInfo, bool)
source

GetPayee returns a payee's totals and collector key.

func GetPlan

Action
1func GetPlan(id uint64) (PlanInfo, bool)
source

GetPlan returns the plan with this id.

func Plans

Action
1func Plans(page int) []PlanInfo
source

Plans lists all plans, newest first, PageSize per page (page 1 first).

func PlansOf

Action
1func PlansOf(payee address, page int) []PlanInfo
source

PlansOf lists a payee's plans, oldest first.

Types 7

type Mandate

struct
 1type Mandate struct {
 2	ID         uint64
 3	Payer      address
 4	Session    address // address of the session key the payee holds
 5	CreatedAt  int64
 6	ExpiresAt  int64 // unix seconds; pulls are refused from this time on
 7	NextPullAt int64 // unix seconds; the first pull is due at once
 8	Pulls      int64
 9	Index      int // position in the plan's supporter list
10
11	CancelledAt int64 // 0 = not cancelled
12	CancelledBy address
13
14	plan        *Plan    // direct link, so Pull needs no lookup
15	prevOfPayer *Mandate // the payer's previous mandate (history, newest first)
16}
source

Mandate is one payer's permission for one session key on one plan. It keeps only what cannot be derived: the amount paid is Pulls * the plan's amount, and the last pull was one period before NextPullAt.

type MandateInfo

struct
 1type MandateInfo struct {
 2	ID            uint64
 3	PlanID        uint64
 4	Payer         address
 5	Payee         address
 6	Session       address
 7	SessionPubKey string
 8	Amount        int64
 9	Period        int64
10	CreatedAt     int64
11	ExpiresAt     int64
12	NextPullAt    int64
13	LastPullAt    int64
14	// PaidThrough is until when this mandate's payments, or paid time
15	// carried over from the payer's earlier mandate for the plan, cover the
16	// payer (0 if neither). A mandate that starts with carried time has its
17	// first pull at that time.
18	PaidThrough int64
19	Pulls       int64
20	Paid        int64
21	CancelledAt int64
22	CancelledBy address
23	// Status is "active", "expired" or "cancelled": the realm's view only.
24	Status string
25	// Due is true when the mandate is active and a pull is allowed now.
26	Due bool
27	// SessionPubKey is the session key's public key when it is the payee's
28	// current collector key, "" otherwise.
29	//
30	// SessionQuery is the free query an off-chain checker must also run:
31	// the realm cannot see a session revoked or expired on chain.
32	SessionQuery string
33}
source

MandateInfo is a copy of a mandate with the realm's view of it.

type Payee

struct
 1type Payee struct {
 2	Addr          address
 3	Plans         int
 4	planIDs       fixedList // the payee's plan ids, padded (values, not pointers)
 5	Pulls         int64
 6	Paid          int64
 7	LastPullAt    int64
 8	CollectorKey  string  // bech32 public key (gpub1...), empty if not set
 9	CollectorAddr address // address derived from CollectorKey by the chain
10}
source

Payee holds a payee's plans, totals and published collector key.

type PayeeInfo

struct
 1type PayeeInfo struct {
 2	Addr          address
 3	Plans         int
 4	Pulls         int64
 5	Paid          int64
 6	LastPullAt    int64
 7	CollectorKey  string
 8	CollectorAddr address
 9	// CollectorFunded is true when the collector key's address holds coins:
10	// the chain then refuses every new session for that key, so nobody can
11	// subscribe until the creator publishes a new key. (An address that
12	// received coins and was emptied is also refused by the chain; only the
13	// free query auth/accounts/<CollectorAddr> shows that.)
14	CollectorFunded bool
15}
source

PayeeInfo is a copy of a payee's totals and collector key.

type Payer

struct
1type Payer struct {
2	Active  []activeRef
3	Head    *Mandate
4	Credits []credit
5}
source

Payer holds one payer's own lookups and nothing of anyone else's, so no address can make another payer's Pull, Subscribe or Cancel read or write more than the payer tree's depth.

  • Active: the payer's current mandates, at most MaxActivePerPayer that are not expired. Cancel removes an entry at once; an expired entry is removed at the payer's next Subscribe. Each entry carries the session address and plan id inline, so finding a mandate reads one small array and loads only the mandate found.
  • Head: the payer's newest mandate; older ones follow through prevOfPayer (history, walked only by free reads).
  • Credits: time the payer has paid for on a plan where they no longer have an active mandate (cancelled by either side, or expired and pruned), at most MaxCreditsPerPayer, each still in the future. A new mandate for that plan starts when the credit ends, so paid time is not lost or charged twice, unless 16 credits are already held (see MaxCreditsPerPayer for who can then lose what).

type Plan

struct
 1type Plan struct {
 2	ID        uint64
 3	Payee     address
 4	Title     string
 5	Amount    int64 // ugnot per period
 6	Period    int64 // seconds
 7	CreatedAt int64 // unix seconds
 8
 9	Supporters fixedList // every mandate for this plan, oldest first (see encodeEntry)
10	Cancelled  int       // how many of them were cancelled
11	Pulls      int64     // successful pulls (paid = Pulls * Amount)
12	LastPullAt int64     // unix seconds, 0 = never
13
14	// Payments per period window: window k is [k*Period, (k+1)*Period). A
15	// mandate pays at most once in a window, and paid time carries over to a
16	// payer's next mandate for the plan, so a payer is not charged twice for
17	// one period (except when 16 kept periods are already held, see
18	// MaxCreditsPerPayer); but these count payments, not distinct people (a creator
19	// can pay its own plan from a second wallet). Read with paidWindows, which
20	// turns them into counts for the window containing "now" and the one
21	// before it, so they are true at any time without a transaction.
22	payWindow int64 // index of the window of the last payment
23	paidThis  int   // payments in window payWindow
24	paidPrev  int   // payments in window payWindow-1
25
26	payee *Payee // direct link, so Pull needs no lookup
27}
source

Plan is published by a payee and never changes. New terms mean a new plan.

type PlanInfo

struct
 1type PlanInfo struct {
 2	ID        uint64
 3	Payee     address
 4	Title     string
 5	Amount    int64
 6	Period    int64
 7	CreatedAt int64
 8	Mandates  int // mandates ever created for this plan
 9	Cancelled int // how many of them were cancelled
10	// PaidThisPeriod and PaidLastPeriod count the payments in the plan's
11	// current period window, which started at PeriodStart, and in the window
12	// before it. Windows are [k*Period, (k+1)*Period) in unix time. They count
13	// payments, not distinct people (a creator can pay its own plan from a
14	// second wallet). Historical facts, true at any time, whatever has
15	// expired or been cancelled since.
16	PaidThisPeriod int
17	PaidLastPeriod int
18	PeriodStart    int64
19	Pulls          int64
20	Paid           int64
21	LastPullAt     int64
22}
source

PlanInfo is a copy of a plan.

Dependencies

Source Files 11