Skip to content

Commit 56f6fa3

Browse files
Cap distributor maxWeight and run every hour
1 parent d80fe64 commit 56f6fa3

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

apps/distributor/src/distributorv2.ts

+26-5
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,12 @@ export class DistributorV2Worker {
319319
const sendCeilingVerifications = verifications.filter((v) => v.type === 'send_ceiling')
320320
const sendCeilingByUserId = sendCeilingVerifications.reduce(
321321
(acc, v) => {
322+
const previousReward =
323+
previousSharesByUserId[v.user_id] || BigInt(distribution.hodler_min_balance)
324+
const maxWeight = previousReward / BigInt(sendSlash.scaling_divisor)
322325
acc[v.user_id] = {
323-
weight: BigInt(v.weight || 0),
326+
// Cap the weight to maxWeight
327+
weight: BigInt(v.weight || 0) > maxWeight ? maxWeight : BigInt(v.weight || 0),
324328
// @ts-expect-error @todo metadata is untyped but value is the convention
325329
ceiling: BigInt(v.metadata?.value || 0),
326330
}
@@ -454,7 +458,10 @@ export class DistributorV2Worker {
454458

455459
// Calculate time adjustment for slashed amounts
456460
const hourlyHodlerAmount = (hodlerPoolAvailableAmount * PERC_DENOM) / BigInt(hoursInMonth)
457-
const timeAdjustedAmount = (hourlyHodlerAmount * BigInt(currentHour + 1)) / PERC_DENOM
461+
const timeAdjustedAmount =
462+
(hourlyHodlerAmount * BigInt(currentHour + 1)) / PERC_DENOM > hodlerPoolAvailableAmount
463+
? hodlerPoolAvailableAmount
464+
: (hourlyHodlerAmount * BigInt(currentHour + 1)) / PERC_DENOM
458465

459466
// First calculate slashed balances for everyone
460467
const balances = minBalanceAddresses.map((balance) => {
@@ -465,7 +472,12 @@ export class DistributorV2Worker {
465472
if (sendCeilingData && sendCeilingData.weight > 0n) {
466473
const previousReward =
467474
previousSharesByUserId[userId] || BigInt(distribution.hodler_min_balance)
468-
slashPercentage = (sendCeilingData.weight * PERC_DENOM) / previousReward
475+
const scaledPreviousReward = previousReward / BigInt(sendSlash.scaling_divisor)
476+
const cappedWeight =
477+
sendCeilingData.weight > scaledPreviousReward
478+
? scaledPreviousReward
479+
: sendCeilingData.weight
480+
slashPercentage = (cappedWeight * PERC_DENOM) / scaledPreviousReward
469481
}
470482

471483
const balanceAfterSlash = (
@@ -529,7 +541,10 @@ export class DistributorV2Worker {
529541
// Non-slashed amounts
530542
const hodlerPoolAmount = share.amount
531543
const fixedPoolAmount = fixedPoolAmountsByAddress[share.address]?.amount || 0n
532-
const amount = hodlerPoolAmount + fixedPoolAmount > distAmt ? distAmt : hodlerPoolAmount
544+
const amount =
545+
hodlerPoolAmount + fixedPoolAmount > distAmt
546+
? distAmt
547+
: hodlerPoolAmount + fixedPoolAmount
533548

534549
// Slashed amounts - ensure we always have a value
535550
const hodlerPoolAmountAfterSlash = share.amountAfterSlash || 0n
@@ -616,11 +631,17 @@ export class DistributorV2Worker {
616631

617632
while (this.running) {
618633
try {
634+
const now = new Date()
635+
const nextHour = new Date(now)
636+
nextHour.setHours(nextHour.getHours() + 1, 0, 0, 0)
637+
const targetTime = new Date(nextHour.getTime() - 60000) // 1 minute before next hour
638+
const waitTime = targetTime.getTime() - now.getTime()
639+
640+
process.env.NODE_ENV === 'development' ? await sleep(10_000) : await sleep(waitTime)
619641
await this.calculateDistributions()
620642
} catch (error) {
621643
this.log.error(error, `Error processing block. ${(error as Error).message}`)
622644
}
623-
await sleep(process.env.NODE_ENV !== 'production' ? 10_000 : 21_600_000)
624645
}
625646

626647
this.log.info('Distributor stopped.')

apps/distributor/src/weights.ts

+6-10
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,6 @@ export function calculatePercentageWithBips(value: bigint, bips: bigint) {
3333
return percentage / PERC_DENOM
3434
}
3535

36-
const calculateSlashPercentage = (
37-
weight: bigint,
38-
scaledPreviousReward: bigint,
39-
percDenom: bigint = PERC_DENOM
40-
): bigint => {
41-
return (weight * percDenom) / scaledPreviousReward > percDenom
42-
? percDenom
43-
: (weight * percDenom) / scaledPreviousReward
44-
}
45-
4636
/**
4737
* Given a list of balances and a distribution amount, calculate the distribution weights and share amounts.
4838
*/
@@ -99,6 +89,12 @@ export function calculateWeights(
9989
const poolWeight = poolWeights[address] ?? 0n
10090
const poolWeightAfterSlash = poolWeightsAfterSlash[address] ?? 0n
10191
const totalWeight = totalWeightAfterSlash - poolWeightAfterSlash + poolWeight
92+
93+
// with capped slashed balances:
94+
// totalWeightAfterSlash = sum of all capped slashed balances
95+
// -poolWeightAfterSlash = remove this user's capped slashed balance
96+
// +poolWeight = add their full unslashed balance
97+
10298
const potentialAmount = (poolWeight * amount) / totalWeight
10399
const slashedAmount = (poolWeightAfterSlash * timeAdjustedAmount) / totalWeightAfterSlash
104100

0 commit comments

Comments
 (0)