-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInfluencerRegistry.cdc
110 lines (86 loc) · 4.22 KB
/
InfluencerRegistry.cdc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// The Influencer Registry stores the mappings from the name of an
// influencer to the vaults in which they'd like to receive tokens,
// as well as the cut they'd like to take from marketplace transactions.
import FungibleToken from 0xFUNGIBLETOKENADDRESS
pub contract InfluencerRegistry {
// Emitted when the contract is created
pub event ContractInitialized()
// Emitted when a FT-receiving capability for an influencer has been updated
// If address is nil, that means the capability has been removed.
pub event CapabilityUpdated(name: String, ftType: Type, address: Address?)
// Emitted when an influencer's cut percentage has been updated
// If the cutPercentage is nil, that means it has been removed.
pub event CutPercentageUpdated(name: String, cutPercentage: UFix64?)
// Emitted when the default cut percentage has been updated
pub event DefaultCutPercentageUpdated(cutPercentage: UFix64?)
// capabilities is a mapping from influencer name, to fungible token ID, to
// the capability for a receiver for the fungible token
pub var capabilities: {String: {String: Capability<&{FungibleToken.Receiver}>}}
// The mappings from the name of an influencer to the cut percentage
// that they are supposed to receive.
pub var cutPercentages: {String: UFix64}
// The default cut percentage
pub var defaultCutPercentage: UFix64
// Get the capability for depositing accounting tokens to the influencer
pub fun getCapability(name: String, ftType: Type): Capability? {
let ftId = ftType.identifier
if let caps = self.capabilities[name] {
return caps[ftId]
} else {
return nil
}
}
// Get the current cut percentage for the influencer
pub fun getCutPercentage(name: String): UFix64 {
if let cut = InfluencerRegistry.cutPercentages[name] {
return cut
} else {
return InfluencerRegistry.defaultCutPercentage
}
}
// Admin is an authorization resource that allows the contract owner to
// update values in the registry.
pub resource Admin {
// Update the FT-receiving capability for an influencer
pub fun setCapability(name: String, ftType: Type, capability: Capability<&{FungibleToken.Receiver}>?) {
let ftId = ftType.identifier
if let cap = capability {
if let caps = InfluencerRegistry.capabilities[name] {
caps[ftId] = cap
InfluencerRegistry.capabilities[name] = caps
} else {
InfluencerRegistry.capabilities[name] = {ftId: cap}
}
// This is the only way to get the address behind a capability from Cadence right
// now. It will panic if the capability is not pointing to anything, but in that
// case we should in fact panic anyways.
let addr = ((cap.borrow() ?? panic("Capability is empty"))
.owner ?? panic("Capability owner is empty"))
.address
emit CapabilityUpdated(name: name, ftType: ftType, address: addr)
} else {
if let caps = InfluencerRegistry.capabilities[name] {
caps.remove(key: ftId)
InfluencerRegistry.capabilities[name] = caps
}
emit CapabilityUpdated(name: name, ftType: ftType, address: nil)
}
}
// Update the cut percentage for the influencer
pub fun setCutPercentage(name: String, cutPercentage: UFix64?) {
InfluencerRegistry.cutPercentages[name] = cutPercentage
emit CutPercentageUpdated(name: name, cutPercentage: cutPercentage)
}
// Update the default cut percentage
pub fun setDefaultCutPercentage(cutPercentage: UFix64) {
InfluencerRegistry.defaultCutPercentage = cutPercentage
emit DefaultCutPercentageUpdated(cutPercentage: cutPercentage)
}
}
init() {
self.cutPercentages = {}
self.capabilities = {}
self.defaultCutPercentage = 0.04
self.account.save<@Admin>(<- create Admin(), to: /storage/EternalInfluencerRegistryAdmin)
}
}