Smart Contract
FindMarketInfrastructureCut
A.097bafa4e0b48eef.FindMarketInfrastructureCut
1import FindMarketCutInterface from 0x097bafa4e0b48eef
2import FindMarketCutStruct from 0x097bafa4e0b48eef
3import FindMarketCut from 0x097bafa4e0b48eef
4
5access(all) contract FindMarketInfrastructureCut : FindMarketCutInterface {
6
7 access(all) let contractName: String
8 access(all) let category: String
9
10 // tenant to {ruleId to cut}
11 access(contract) let cuts : {String : {String : FindMarketCutStruct.Cuts}}
12 access(contract) let cutsCache : {String : {String : FindMarketCutStruct.Cuts}}
13
14 access(all) event Cut(tenant: String, type: String, cutInfo: [FindMarketCutStruct.EventSafeCut], action: String, remark: String?)
15
16 access(all) fun getCut(tenant: String, listingType: Type, nftType: Type, ftType: Type) : FindMarketCutStruct.Cuts? {
17
18 let ruleId = FindMarketCut.getRuleId(listingType: listingType, nftType: nftType, ftType: ftType)
19 if let cache = self.getTenantRulesCache(tenant: tenant, ruleId: ruleId) {
20 return cache
21 }
22
23 let types = [listingType.identifier, nftType.identifier, ftType.identifier]
24 var cuts : [{FindMarketCutStruct.Cut}] = []
25 var returningCut : FindMarketCutStruct.Cuts? = nil
26 for t in types {
27 let cutMapping = self.cuts[tenant] ?? {}
28 if let c = cutMapping[t] {
29 cuts.appendAll(c.cuts)
30 }
31 }
32 if cuts.length > 0 {
33 returningCut = FindMarketCutStruct.Cuts(cuts:cuts)
34
35 // set cache if not already
36 // we do not need to check if cache exist here, because if it is, it will be returned earlier
37 self.setTenantRulesCache(tenant: tenant, ruleId: ruleId, result: returningCut!)
38 }
39
40 return returningCut
41 }
42
43 access(account) fun setTenantCuts(tenant: String, types: [Type], cuts: FindMarketCutStruct.Cuts) {
44 let old = self.cuts[tenant] ?? {}
45 for t in types {
46 old[t.identifier] = cuts
47 emit Cut(tenant: tenant, type: t.identifier, cutInfo: cuts.getEventSafeCuts(), action: "add", remark: nil)
48 }
49 self.cuts[tenant] = old
50 }
51
52 access(account) fun removeTenantCuts(tenant: String, types: [Type]) : [FindMarketCutStruct.Cuts] {
53 var panicMsg = "tenant infrastructure cut is not registered. Tenant : ".concat(tenant)
54 let old = self.cuts[tenant] ?? panic(panicMsg)
55 let cutsReturn : [FindMarketCutStruct.Cuts] = []
56 for t in types {
57 let detailedPanicMsg = panicMsg.concat(" Type : ".concat(t.identifier))
58 let cuts = old.remove(key: t.identifier) ?? panic(detailedPanicMsg)
59 cutsReturn.append(cuts)
60 emit Cut(tenant: tenant, type: t.identifier, cutInfo: cuts.getEventSafeCuts(), action: "remove", remark: nil)
61 }
62 self.cuts[tenant] = old
63 return cutsReturn
64 }
65
66 access(account) fun setTenantRulesCache(tenant: String, ruleId: String, result: FindMarketCutStruct.Cuts) {
67
68 let old = self.cutsCache[tenant] ?? {}
69 if old[ruleId] != nil {
70 panic("There is already a cache for this find rule. RuleId : ".concat(ruleId))
71 }
72 old[ruleId] = result
73 self.cutsCache[tenant] = old
74 }
75
76 access(all) fun getTenantRulesCache(tenant: String, ruleId: String) : FindMarketCutStruct.Cuts? {
77 if self.cutsCache[tenant] == nil {
78 return nil
79 }
80 return self.cutsCache[tenant]![ruleId]
81 }
82
83 access(account) fun resetTenantRulesCache(_ tenant: String) {
84 self.cutsCache.remove(key: tenant)
85 }
86
87
88 init() {
89 self.cuts = {}
90 self.cutsCache = {}
91
92 self.contractName = "FindMarketInfrastructureCut"
93 self.category = "infrastructure"
94 FindMarketCut.setCategory(category: self.category, contractName: self.contractName)
95 }
96
97}
98