migration_research_store.gno
4.94 Kb · 199 lines
1package block
2
3import (
4 "strconv"
5 "strings"
6
7 "gno.land/p/g1nqnrt3aldzhu6zzeg75yw97wvavqy7wr77g56q/deploy-test/v0/v0/grc1155"
8 oldblock "gno.land/r/g1nqnrt3aldzhu6zzeg75yw97wvavqy7wr77g56q/deploy-test/v0/v0/block"
9)
10
11type BlockStore = oldblock.BlockStore
12type InstalledBlockStore = oldblock.InstalledBlockStore
13type MintedBlockStore = oldblock.MintedBlockStore
14
15var (
16 blockStore *BlockStore
17 installedBlockStore *InstalledBlockStore
18 mintedBlockStore *MintedBlockStore
19 creatorBPS int
20 listLimit int
21 frozen bool
22)
23
24func GetTotalBlockSize() int {
25 return blockStore.Total()
26}
27
28func IsFrozen() bool {
29 return frozen
30}
31
32func Unfreeze(cur realm) {
33 frozen = false
34}
35
36func GetCreatorBPS() int {
37 return creatorBPS
38}
39
40func GetListLimit() int {
41 return listLimit
42}
43
44func ListBlocksByIDs(blockIDs ...uint32) []map[string]string {
45 return blockStore.ListByIDs(blockIDs...)
46}
47
48func GetBlockByName(name string) map[string]string {
49 return blockStore.MustGetByName(name)
50}
51
52func GetBlockMetadata(blockID uint32) string {
53 return blockStore.GetBlockMetadata(blockID)
54}
55
56func SetBlockMetadata(cur realm, blockID uint32, metadata string) {
57 blockStore.SetMetadata(blockID, metadata)
58}
59
60func CreateBlock(cur realm, propKeys string, propValues string) uint32 {
61 props := migrationResearchProperties(propKeys, propValues)
62 props["creator"] = cur.Previous().Address().String()
63 return blockStore.Create(props)
64}
65
66func BalanceOf(user address, tokenID grc1155.TokenID) int64 {
67 return mintedBlockStore.BalanceOf(user, tokenID)
68}
69
70func Mint(cur realm, to address, blockID uint32, amount int64) {
71 if amount < 1 {
72 panic("amount must be positive")
73 }
74 block := blockStore.MustGet(blockID)
75 maxSupply := migrationResearchMintableSupply(block)
76 mintedBlockStore.Mint(to, to, blockIDToTokenID(blockID), amount, maxSupply)
77}
78
79func GetInventory(user address) []map[string]string {
80 return mintedBlockStore.GetInventory(user)
81}
82
83func SetMintAllowlist(cur realm, blockID uint32, minters string) {
84 blockStore.MustGet(blockID)
85 mintedBlockStore.SetMintAllowlist(blockID, migrationResearchAddressCSV(minters))
86}
87
88func GetMintAllowlist(blockID uint32) []string {
89 return mintedBlockStore.GetMintAllowlist(blockID)
90}
91
92func IsAcrRewardEnabled(blockID uint32) bool {
93 return blockStore.IsAcrRewardEnabled(blockID)
94}
95
96func ListInstalled(positions ...string) []map[string]string {
97 return installedBlockStore.ListInstalled(positions...)
98}
99
100func Read(position string) string {
101 info, found := installedBlockStore.GetInstalled(position)
102 if !found {
103 panic("block not found at position: " + position)
104 }
105 return info["content"]
106}
107
108func Use(cur realm, position string) {
109 info, found := installedBlockStore.GetInstalled(position)
110 if !found {
111 panic("block not found at position: " + position)
112 }
113 user := migrationResearchPositionOwner(position)
114 block := blockStore.MustGet(stringToBlockID(info["blockId"]))
115 installedBlockStore.RecordUse(user, map[string]string{
116 "position": position,
117 "blockId": info["blockId"],
118 "user": user.String(),
119 "fee": block["usePrice"],
120 "installer": info["installer"],
121 "installerShare": "0",
122 "feeCollector": "",
123 "feeCollectorShare": "0",
124 })
125}
126
127func ListUseLogs(user address, limit int) []map[string]string {
128 return installedBlockStore.ListUseLogs(user, limit)
129}
130
131func blockIDToString(blockID uint32) string {
132 return strconv.FormatUint(uint64(blockID), 10)
133}
134
135func stringToBlockID(blockIDStr string) uint32 {
136 value, err := strconv.ParseUint(blockIDStr, 10, 32)
137 if err != nil {
138 panic("invalid blockID: " + err.Error())
139 }
140 return uint32(value)
141}
142
143func blockIDToTokenID(blockID uint32) grc1155.TokenID {
144 return grc1155.TokenID(blockIDToString(blockID))
145}
146
147func migrationResearchMintableSupply(block map[string]string) int64 {
148 maxSupply, err := strconv.ParseInt(block["maxSupply"], 10, 64)
149 if err != nil {
150 panic("maxSupply must be int")
151 }
152 if maxSupply < 0 {
153 panic("block not mintable")
154 }
155 return maxSupply
156}
157
158func migrationResearchPositionOwner(position string) address {
159 parts := strings.Split(position, "|")
160 if len(parts) != 6 {
161 panic("invalid position format: " + position)
162 }
163 return address(parts[2])
164}
165
166func migrationResearchAddressCSV(value string) []address {
167 if value == "" {
168 return []address{}
169 }
170 items := strings.Split(value, ",")
171 result := []address{}
172 for _, item := range items {
173 item = strings.TrimSpace(item)
174 if item == "" {
175 continue
176 }
177 result = append(result, address(item))
178 }
179 return result
180}
181
182func migrationResearchProperties(keys string, values string) map[string]string {
183 if keys == "" {
184 panic("properties keys must be not empty")
185 }
186 keyList := strings.Split(keys, ",")
187 valueList := strings.Split(values, ",")
188 if len(keyList) != len(valueList) {
189 panic("properties keys and values count mismatch")
190 }
191 result := map[string]string{}
192 for i, key := range keyList {
193 if key == "" {
194 panic("properties key must be not empty")
195 }
196 result[key] = valueList[i]
197 }
198 return result
199}