Skip to content

Commit 628a607

Browse files
committed
feat(pool): add cumulative reward and fee factors
Add `cumulativeFeeFactor` and `cumulativeRewardFactor` fields to the Pool entity to improve reward and fee calculations for delegators. This enables more precise tracking and distribution of rewards and fees over time.
1 parent bb88ef2 commit 628a607

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

schema.graphql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ type Pool @entity {
133133
rewardCut: BigInt!
134134
"Transcoder's fee share during the earnings pool's round"
135135
feeShare: BigInt!
136+
"Cumulative fee factor for delegator fees calculation"
137+
cumulativeFeeFactor: BigDecimal
138+
"Cumulative reward factor for delegator rewards calculation"
139+
cumulativeRewardFactor: BigDecimal
136140
}
137141

138142
"""

src/mappings/bondingManager.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { store } from "@graphprotocol/graph-ts";
1+
import { store, Address } from "@graphprotocol/graph-ts";
22
import {
33
convertToDecimal,
44
createOrLoadDelegator,
@@ -8,6 +8,7 @@ import {
88
createOrLoadTranscoder,
99
EMPTY_ADDRESS,
1010
getBlockNum,
11+
integerFromString,
1112
makeEventId,
1213
makePoolId,
1314
makeUnbondingLockId,
@@ -500,6 +501,13 @@ export function reward(event: Reward): void {
500501
pool!.feeShare = transcoder.feeShare;
501502
pool!.rewardCut = transcoder.rewardCut;
502503

504+
let bondingManager = BondingManager.bind(event.address);
505+
let earningsPool = bondingManager.getTranscoderEarningsPoolForRound(
506+
Address.fromString(transcoder.id),
507+
integerFromString(round.id)
508+
);
509+
pool!.cumulativeRewardFactor = convertToDecimal(earningsPool.cumulativeRewardFactor);
510+
503511
transcoder.save();
504512
delegate.save();
505513
pool!.save();

src/mappings/ticketBroker.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
createOrLoadTranscoderDay,
1111
getBlockNum,
1212
getEthPriceUsd,
13+
integerFromString,
1314
makeEventId,
1415
makePoolId,
1516
ZERO_BD,
@@ -29,6 +30,9 @@ import {
2930
WinningTicketRedeemed,
3031
Withdrawal,
3132
} from "../types/TicketBroker/TicketBroker";
33+
import {
34+
BondingManager,
35+
} from "../types/BondingManager/BondingManager";
3236

3337
export function winningTicketRedeemed(event: WinningTicketRedeemed): void {
3438
let round = createOrLoadRound(getBlockNum());
@@ -97,9 +101,17 @@ export function winningTicketRedeemed(event: WinningTicketRedeemed): void {
97101
protocol.winningTicketCount = protocol.winningTicketCount + 1;
98102
protocol.save();
99103

100-
// update the transcoder pool fees
104+
// update the transcoder pool fees and cummulative factors
101105
if (pool) {
102106
pool.fees = pool.fees.plus(faceValue);
107+
108+
let bondingManager = BondingManager.bind(event.address);
109+
let earningsPool = bondingManager.getTranscoderEarningsPoolForRound(
110+
Address.fromString(transcoder.id),
111+
integerFromString(round.id)
112+
);
113+
pool.cumulativeFeeFactor = convertToDecimal(earningsPool.cumulativeFeeFactor);
114+
103115
pool.save();
104116
}
105117

0 commit comments

Comments
 (0)