Smart Contract
WonderPartnerRewardAlgorithm
A.35f9e7ac86111b22.WonderPartnerRewardAlgorithm
1import RewardAlgorithm from 0x35f9e7ac86111b22
2
3access(all)
4contract WonderPartnerRewardAlgorithm: RewardAlgorithm{
5
6 // -----------------------------------------------------------------------
7 // Events
8 // -----------------------------------------------------------------------
9 access(all)
10 event ContractInitialized()
11
12 // -----------------------------------------------------------------------
13 // Paths
14 // -----------------------------------------------------------------------
15 access(all)
16 let AlgorithmStoragePath: StoragePath
17
18 access(all)
19 let AlgorithmPublicPath: PublicPath
20
21 access(all)
22 resource Algorithm: RewardAlgorithm.Algorithm{
23 access(all)
24 fun randomAlgorithm(): Int{
25 // Generate a random number between 0 and 100_000_000
26 let randomNum = Int(revertibleRandom<UInt64>() % 100_000_000)
27 let threshold1 = 55_000_000 // for 55%
28
29 let threshold2 = 91_000_000 // for 36%, cumulative 91%
30
31 let threshold3 = 99_000_000 // for 8%, cumulative 99%
32
33
34 // Return reward based on generated random number
35 if randomNum < threshold1{
36 return 1
37 } else if randomNum < threshold2{
38 return 2
39 } else if randomNum < threshold3{
40 return 3
41 } else{
42 return 4
43 } // for remaining 1%
44
45 }
46 }
47
48 access(all)
49 fun createAlgorithm(): @Algorithm{
50 return <-create WonderPartnerRewardAlgorithm.Algorithm()
51 }
52
53 access(all)
54 fun borrowAlgorithm(): &Algorithm{
55 return self.account.capabilities.get<&WonderPartnerRewardAlgorithm.Algorithm>(self.AlgorithmPublicPath).borrow()!
56 }
57
58 init(){
59 self.AlgorithmStoragePath = /storage/WonderPartnerRewardAlgorithm
60 self.AlgorithmPublicPath = /public/WonderPartnerRewardAlgorithm
61 self.account.storage.save(<-self.createAlgorithm(), to: self.AlgorithmStoragePath)
62 var capability_1 = self.account.capabilities.storage.issue<&WonderPartnerRewardAlgorithm.Algorithm>(self.AlgorithmStoragePath)
63 self.account.capabilities.publish(capability_1, at: self.AlgorithmPublicPath)
64 emit ContractInitialized()
65 }
66}
67