block.gno
2.29 Kb · 105 lines
1package block
2
3import (
4 "chain"
5
6 "gno.land/p/akkadia/v0/accesscontrol"
7)
8
9const (
10 CreateBlockEvent = "CreateBlock"
11)
12
13func CreateBlock(cur realm, propKeys string, propValues string) uint32 {
14 assertNotFrozen()
15 caller := accesscontrol.MustGetUserCaller(0, cur)
16
17 props := parsePropsCSV(propKeys, propValues)
18 createValidator.MustValidate(props)
19 assertBlockName(props["name"])
20 maxSupply := mustParseUint32Field(props, "maxSupply")
21 assertNormalMaxSupply(maxSupply)
22 assertInstallerBPS(mustParseUint32Field(props, "installerBps"))
23 props["creator"] = caller.String()
24 blockID := blockStore.Create(props)
25 blockIDStr := blockIDToString(blockID)
26 creator := caller.String()
27
28 chain.Emit(
29 CreateBlockEvent,
30 "id", blockIDStr,
31 "name", props["name"],
32 "creator", creator,
33 )
34
35 return blockID
36}
37
38func GetCurrentBlockID() uint32 {
39 assertMigrationStateAvailable()
40 return blockStore.NextID()
41}
42
43func GetTotalBlockSize() int {
44 assertMigrationStateAvailable()
45 return blockStore.Total()
46}
47
48func GetSystemBlockSize() int {
49 assertMigrationStateAvailable()
50 return blockStore.SystemTotal()
51}
52
53func GetNormalBlockSize() int {
54 assertMigrationStateAvailable()
55 return blockStore.NormalTotal()
56}
57
58func GetBlockByName(name string) map[string]string {
59 assertMigrationStateAvailable()
60 assertBlockName(name)
61 return blockStore.MustGetByName(name)
62}
63
64func ListBlocks(page, count int) []map[string]string {
65 assertMigrationStateAvailable()
66 assertListPageCount(page, count)
67
68 return blockStore.List(page, count)
69}
70
71func ListBlocksFromTo(from, to, limit uint32) []map[string]string {
72 assertMigrationStateAvailable()
73 assertListLimit("limit", int(limit))
74
75 if from > to {
76 panic("from is greater than to")
77 }
78
79 var end *uint32
80 if to < ^uint32(0) {
81 endValue := to + 1
82 end = &endValue
83 }
84
85 return blockStore.ListFromTo(&from, end, limit)
86}
87
88func ListBlocksByIDs(blockIDs ...uint32) []map[string]string {
89 assertMigrationStateAvailable()
90 assertListLimit("blockIDs", len(blockIDs))
91
92 return blockStore.ListByIDs(blockIDs...)
93}
94
95func GetBlockMetadata(blockID uint32) string {
96 assertMigrationStateAvailable()
97 return blockStore.GetBlockMetadata(blockID)
98}
99
100func ListBlockMetadataByIDs(blockIDs ...uint32) []string {
101 assertMigrationStateAvailable()
102 assertListLimit("blockIDs", len(blockIDs))
103
104 return blockStore.ListBlockMetadataByIDs(blockIDs...)
105}