github.com/DapperCollectives/CAST/backend@v0.0.0-20230921221157-1350c8be7c96/main/cadence/contracts/CommunityVoting.cdc (about) 1 // CommunityVoting.cdc 2 // A straightforward community storage contract 3 4 pub contract CommunityVoting { 5 // constants 6 pub let COMMUNITY_STORAGE_PATH: StoragePath 7 pub let COMMUNITY_PUBLIC_PATH: PublicPath 8 pub let COLLECTION_MINTER_PATH: StoragePath 9 pub let SUPER_ADMIN_PATH: PrivatePath 10 11 // global variables 12 access(contract) var currentCommunityId: UInt64 13 14 // events 15 pub event CommunityCreated() 16 pub event CommunityCollectionIssued() 17 18 // main community resource 19 pub resource Community { 20 pub let id: UInt64 21 pub(set) var results: [String] 22 23 init() { 24 self.id = CommunityVoting.currentCommunityId + 1 25 CommunityVoting.currentCommunityId = CommunityVoting.currentCommunityId + 1 26 self.results = [] 27 } 28 } 29 30 // community struct for getting/reporting 31 pub struct CommunityStruct { 32 pub let id: UInt64 33 pub let results: [String] 34 35 init(community: &Community) { 36 self.id = community.id 37 self.results = community.results 38 } 39 } 40 41 // receiver, mostly representing "Admin" capabilites 42 pub resource interface CommunityReceiver { 43 pub fun receiveCommunity(community: @Community) 44 pub fun receiveResults(communityId: UInt64, results: [String]) 45 } 46 47 // main collection of communities 48 pub resource CommunityCollection: CommunityReceiver { 49 pub let communities: @{UInt64: Community} 50 51 init() { 52 self.communities <- {} 53 } 54 55 pub fun createCommunity(): @Community { 56 let community <- create Community() 57 emit CommunityCreated() 58 return <- community 59 } 60 61 pub fun receiveCommunity(community: @Community) { 62 let oldCommunity <- self.communities[community.id] <- community 63 destroy oldCommunity 64 } 65 66 pub fun receiveResults(communityId: UInt64, results: [String]) { 67 pre { 68 self.communities[communityId] != nil : "community does not exist" 69 } 70 let communityRef = &self.communities[communityId] as &Community 71 communityRef.results = results 72 } 73 74 destroy() { 75 destroy self.communities 76 } 77 } 78 79 // SuperAdmin interface in order to link capabilities for distributing CommunityCollections which are basically Admin capabilites 80 pub resource interface SuperAdmin { 81 pub fun mintCollection(): @CommunityCollection 82 } 83 84 // resource for super admin stuff for now... may refactor 85 pub resource CollectionMinter: SuperAdmin { 86 pub fun mintCollection(): @CommunityCollection { 87 emit CommunityCollectionIssued() 88 return <- create CommunityCollection() 89 } 90 } 91 92 init() { 93 // set initial values 94 self.COMMUNITY_STORAGE_PATH = /storage/DapperCollectivesCommunityVotingTool_Communities 95 self.COMMUNITY_PUBLIC_PATH = /public/DapperCollectivesCommunityVotingTool_Communities 96 self.COLLECTION_MINTER_PATH = /storage/DapperCollectivesCommunityVotingTool_CollectionMinter 97 self.SUPER_ADMIN_PATH = /private/DapperCollectivesCommunityVotingTool_SuperAdmin 98 99 100 self.currentCommunityId = 0 101 102 // set up owner's account with empty collection and CollectionMinter resource 103 self.account.save<@CommunityCollection>(<- create CommunityCollection(), to: self.COMMUNITY_STORAGE_PATH) 104 self.account.save<@CollectionMinter>(<- create CollectionMinter(), to: self.COLLECTION_MINTER_PATH) 105 106 // link capabilities 107 self.account.link<&CollectionMinter{SuperAdmin}>(self.SUPER_ADMIN_PATH, target: self.COLLECTION_MINTER_PATH) 108 } 109 }