@@ -319,8 +319,12 @@ export class DistributorV2Worker {
319
319
const sendCeilingVerifications = verifications . filter ( ( v ) => v . type === 'send_ceiling' )
320
320
const sendCeilingByUserId = sendCeilingVerifications . reduce (
321
321
( acc , v ) => {
322
+ const previousReward =
323
+ previousSharesByUserId [ v . user_id ] || BigInt ( distribution . hodler_min_balance )
324
+ const maxWeight = previousReward / BigInt ( sendSlash . scaling_divisor )
322
325
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 ) ,
324
328
// @ts -expect-error @todo metadata is untyped but value is the convention
325
329
ceiling : BigInt ( v . metadata ?. value || 0 ) ,
326
330
}
@@ -454,7 +458,10 @@ export class DistributorV2Worker {
454
458
455
459
// Calculate time adjustment for slashed amounts
456
460
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
458
465
459
466
// First calculate slashed balances for everyone
460
467
const balances = minBalanceAddresses . map ( ( balance ) => {
@@ -465,7 +472,12 @@ export class DistributorV2Worker {
465
472
if ( sendCeilingData && sendCeilingData . weight > 0n ) {
466
473
const previousReward =
467
474
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
469
481
}
470
482
471
483
const balanceAfterSlash = (
@@ -529,7 +541,10 @@ export class DistributorV2Worker {
529
541
// Non-slashed amounts
530
542
const hodlerPoolAmount = share . amount
531
543
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
533
548
534
549
// Slashed amounts - ensure we always have a value
535
550
const hodlerPoolAmountAfterSlash = share . amountAfterSlash || 0n
@@ -616,11 +631,17 @@ export class DistributorV2Worker {
616
631
617
632
while ( this . running ) {
618
633
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 )
619
641
await this . calculateDistributions ( )
620
642
} catch ( error ) {
621
643
this . log . error ( error , `Error processing block. ${ ( error as Error ) . message } ` )
622
644
}
623
- await sleep ( process . env . NODE_ENV !== 'production' ? 10_000 : 21_600_000 )
624
645
}
625
646
626
647
this . log . info ( 'Distributor stopped.' )
0 commit comments