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

store.gno

2.13 Kb · 66 lines
 1package sales
 2
 3import (
 4	"strconv"
 5
 6	"gno.land/p/nt/avl/v0"
 7)
 8
 9// Per-token, per-buyer and per-claim state lives in AVL trees: a write touches
10// one entry and the path to it. A realm-level Go map is stored as one object,
11// so each write would cost gas in proportion to everything the map holds, and
12// a busy sale could price its own buyers out of their claims. The four
13// per-currency liabilities stay maps: they hold one entry per currency, and
14// only the config admin gives a currency terms.
15var (
16	vestings         = avl.NewTree() // "<token id>:<index>" -> VestingRecord
17	vestingCounts    = avl.NewTree() // token id -> int
18	tokenLiabilities = avl.NewTree() // token id -> int64
19	airdrops         = avl.NewTree() // token id -> AirdropRecord
20	airdropClaims    = avl.NewTree() // "<token id>:<index>" -> true
21	fairSales        = avl.NewTree() // token id -> FairSaleRecord
22	fairBuyers       = avl.NewTree() // "<token id>:<buyer>" -> FairBuyerRecord
23)
24
25// A token id is "T" and digits, so ":" separates it from what follows.
26func indexKey(id string, index int) string { return id + ":" + strconv.Itoa(index) }
27func buyerKey(id string, buyer address) string { return id + ":" + string(buyer) }
28
29func vestingCount(id string) int {
30	n, _ := vestingCounts.Get(id).(int)
31	return n
32}
33
34// vestingAt returns record index of a token, which the caller has checked
35// to be below vestingCount.
36func vestingAt(id string, index int) VestingRecord {
37	return vestings.Get(indexKey(id, index)).(VestingRecord)
38}
39
40func setVesting(id string, index int, r VestingRecord) { vestings.Set(indexKey(id, index), r) }
41
42func appendVesting(id string, r VestingRecord) {
43	n := vestingCount(id)
44	vestings.Set(indexKey(id, n), r)
45	vestingCounts.Set(id, n+1)
46}
47
48func tokenOwed(id string) int64 {
49	v, _ := tokenLiabilities.Get(id).(int64)
50	return v
51}
52
53// A token that is owed nothing has no entry.
54func setTokenOwed(id string, amount int64) {
55	if amount == 0 {
56		tokenLiabilities.Remove(id)
57		return
58	}
59	tokenLiabilities.Set(id, amount)
60}
61
62// airdropOf returns the zero record for a token without a campaign.
63func airdropOf(id string) AirdropRecord {
64	r, _ := airdrops.Get(id).(AirdropRecord)
65	return r
66}