Smart Contract
StarlyCardStaking
A.29fcd0b5e444242a.StarlyCardStaking
1import NonFungibleToken from 0x1d7e57aa55817448
2import MetadataViews from 0x1d7e57aa55817448
3import StarlyIDParser from 0x5b82f21c0edf76e3
4import StarlyMetadata from 0x5b82f21c0edf76e3
5import StarlyMetadataViews from 0x5b82f21c0edf76e3
6
7access(all) contract StarlyCardStaking {
8
9 access(all) struct CollectionData {
10 access(all) let editions: {String: UFix64}
11
12 init(editions: {String: UFix64}) {
13 self.editions = editions
14 }
15
16 access(all) fun setRemainingResource(starlyID: String, remainingResource: UFix64) {
17 self.editions.insert(key: starlyID, remainingResource)
18 }
19 }
20
21 access(contract) var collections: {String: CollectionData}
22
23 access(all) let AdminStoragePath: StoragePath
24 access(all) let EditorStoragePath: StoragePath
25 access(all) let EditorProxyStoragePath: StoragePath
26 access(all) let EditorProxyPublicPath: PublicPath
27
28 access(all) view fun getRemainingResource(collectionID: String, starlyID: String): UFix64? {
29 if let collection = StarlyCardStaking.collections[collectionID] {
30 return collection.editions[starlyID]
31 } else {
32 return nil
33 }
34 }
35
36 access(all) fun getRemainingResourceWithDefault(starlyID: String): UFix64 {
37 let metadata = StarlyMetadata.getCardEdition(starlyID: starlyID) ?? panic("Missing metadata")
38 let collectionID = metadata.collection.id
39 let initialResource = metadata.score ?? 0.0
40 return StarlyCardStaking.getRemainingResource(collectionID: collectionID, starlyID: starlyID) ?? initialResource
41 }
42
43 access(all) resource interface IEditor {
44 access(all) fun setRemainingResource(collectionID: String, starlyID: String, remainingResource: UFix64)
45 }
46
47 access(all) resource Editor: IEditor {
48 access(all) fun setRemainingResource(collectionID: String, starlyID: String, remainingResource: UFix64) {
49 if let collection = StarlyCardStaking.collections[collectionID] {
50 StarlyCardStaking.collections[collectionID]!.setRemainingResource(starlyID: starlyID, remainingResource: remainingResource)
51 } else {
52 StarlyCardStaking.collections.insert(key: collectionID, CollectionData(editions: {starlyID: remainingResource}))
53 }
54 }
55 }
56
57 access(all) resource interface EditorProxyPublic {
58 access(all) fun setEditorCapability(cap: Capability<&Editor>)
59 }
60
61 access(all) resource EditorProxy: IEditor, EditorProxyPublic {
62 access(self) var editorCapability: Capability<&Editor>?
63
64 access(all) fun setEditorCapability(cap: Capability<&Editor>) {
65 self.editorCapability = cap
66 }
67
68 access(all) fun setRemainingResource(collectionID: String, starlyID: String, remainingResource: UFix64) {
69 self.editorCapability!.borrow()!
70 .setRemainingResource(collectionID: collectionID, starlyID: starlyID, remainingResource: remainingResource)
71 }
72
73 init() {
74 self.editorCapability = nil
75 }
76 }
77
78 access(all) fun createEditorProxy(): @EditorProxy {
79 return <- create EditorProxy()
80 }
81
82 access(all) resource Admin {
83 access(all) fun createNewEditor(): @Editor {
84 return <- create Editor()
85 }
86 }
87
88 init() {
89 self.collections = {}
90
91 self.AdminStoragePath = /storage/starlyCardStakingAdmin
92 self.EditorStoragePath = /storage/starlyCardStakingEditor
93 self.EditorProxyPublicPath = /public/starlyCardStakingEditorProxy
94 self.EditorProxyStoragePath = /storage/starlyCardStakingEditorProxy
95
96 let admin <- create Admin()
97 let editor <- admin.createNewEditor()
98 self.account.storage.save(<-admin, to: self.AdminStoragePath)
99 self.account.storage.save(<-editor, to: self.EditorStoragePath)
100 }
101}
102