Smart Contract

BagDistributionRatioV2

A.c649103ebbc38926.BagDistributionRatioV2

Valid From

131,562,306

Deployed

1w ago
Feb 16, 2026, 11:23:01 AM UTC

Dependents

4 imports
1import FlowIDTableStaking from 0x8624b52f9ddcd04a
2
3access(all) contract BagDistributionRatioV2 {
4
5    access(all) event EpochSnapshotCreated(epochNumber: UInt64, stakedAmount: UFix64);
6    access(all) event RewardsCalculated(epochNumber: UInt64, stakedAmount: UFix64)
7    
8    access(all) var targetAPY: UFix64
9    access(all) var totalWeek: UFix64
10    access(all) var weeklyDistributionSnapshots: {UInt64: BagDistributionDetails}
11
12    access(all) let AdminStoragePath: StoragePath
13    
14    access(all) struct BagDistributionDetails {
15        access(all) var epochId: UInt64
16        access(all) var lotteryId: UInt64
17        access(all) var tokenStaked: UFix64
18        access(all) var stakingReward: UFix64
19        access(all) var lotteryExecutedTime: UFix64
20
21        init(epochId:UInt64, tokenStaked:UFix64){
22            self.epochId = epochId
23            self.tokenStaked = tokenStaked
24            self.lotteryId = 0
25            self.stakingReward = 0.0
26            self.lotteryExecutedTime = 0.0
27        }
28
29        access(all) fun updateLotteryId(id:UInt64){
30            self.lotteryId = id
31        }
32
33        access(all) fun updateStakingReward(newReward:UFix64){
34            self.stakingReward = newReward
35        }
36
37        access(all) fun updateLotteryExecutedTime(newTime:UFix64){
38            self.lotteryExecutedTime = newTime
39        }
40    }
41
42    access(all) resource Admin {
43        
44        access(all) fun recordStakeSnapshot(nodeID: String, delegatorID: UInt32, epochId:UInt64) {
45            let currentStake = self.getTokenStaked(nodeID: nodeID, delegatorID: delegatorID)
46            let data = BagDistributionRatioV2.BagDistributionDetails(epochId:epochId, tokenStaked:currentStake)
47            BagDistributionRatioV2.weeklyDistributionSnapshots[epochId] = data
48            emit EpochSnapshotCreated(epochNumber:epochId, stakedAmount: currentStake);
49        }
50
51        access(all) fun calculateWeeklyDistribution(epochId:UInt64): UFix64 {
52            let data = BagDistributionRatioV2.weeklyDistributionSnapshots[epochId]!
53            let weeklyReward = (data.tokenStaked * BagDistributionRatioV2.targetAPY) / BagDistributionRatioV2.totalWeek
54            data.updateStakingReward(newReward: weeklyReward)
55            BagDistributionRatioV2.weeklyDistributionSnapshots[epochId] = data
56            emit RewardsCalculated(epochNumber: epochId, stakedAmount: data.tokenStaked)
57            return weeklyReward
58        }
59
60        access(all) fun addLotteryDetails(epochId:UInt64, lotteryId:UInt64) {
61            let data = BagDistributionRatioV2.weeklyDistributionSnapshots[epochId]!
62            let newTime = getCurrentBlock().timestamp
63            data.updateLotteryExecutedTime(newTime: newTime)
64            data.updateLotteryId(id:lotteryId)
65            BagDistributionRatioV2.weeklyDistributionSnapshots[epochId] = data
66        }
67
68        access(all) fun getTokenStaked(nodeID: String, delegatorID: UInt32): UFix64 {
69            let delInfo = FlowIDTableStaking.DelegatorInfo(nodeID: nodeID, delegatorID: delegatorID)
70            return delInfo.tokensStaked
71        }
72
73        access(all) fun updateTargetAPY(newAPY: UFix64) {
74            BagDistributionRatioV2.targetAPY = newAPY
75        }
76
77        access(all) fun updateTotalWeek(newTotalWeek: UFix64) {
78            BagDistributionRatioV2.totalWeek = newTotalWeek
79        }
80    }
81
82    access(all) fun getStakeSnapshots(epochId:UInt64): BagDistributionDetails{
83        return self.weeklyDistributionSnapshots[epochId]!
84    }
85
86    init(targetAPY: UFix64, totalWeek: UFix64) {
87        self.targetAPY = targetAPY
88        self.totalWeek = totalWeek
89        self.weeklyDistributionSnapshots = {}
90        self.AdminStoragePath = /storage/BagDistributionRatioV2Admin
91
92        let admin <- create Admin()
93        self.account.storage.save(<- admin, to: self.AdminStoragePath)
94    }
95}