6
6
fetchAllHodlers ,
7
7
fetchAllVerifications ,
8
8
fetchDistribution ,
9
+ fetchDistributionShares ,
10
+ fetchSendSlash ,
11
+ fetchSendTransfers ,
9
12
supabaseAdmin ,
10
13
} from './supabase'
11
14
import { fetchAllBalances , isMerkleDropActive } from './wagmi'
@@ -165,7 +168,6 @@ export class DistributorV2Worker {
165
168
acc [ verification . type ] = {
166
169
fixedValue : BigInt ( verification . fixed_value ) ,
167
170
bipsValue : BigInt ( verification . bips_value ) ,
168
- mode : verification . mode ,
169
171
multiplier_min : verification . multiplier_min ,
170
172
multiplier_max : verification . multiplier_max ,
171
173
multiplier_step : verification . multiplier_step ,
@@ -177,7 +179,6 @@ export class DistributorV2Worker {
177
179
{
178
180
fixedValue ?: bigint
179
181
bipsValue ?: bigint
180
- mode : Database [ 'public' ] [ 'Enums' ] [ 'verification_value_mode' ]
181
182
multiplier_min : number
182
183
multiplier_max : number
183
184
multiplier_step : number
@@ -276,6 +277,29 @@ export class DistributorV2Worker {
276
277
} )
277
278
}
278
279
280
+ const { data : sendSlashes , error : slashesError } = await fetchSendSlash ( distribution . id )
281
+
282
+ if ( slashesError ) {
283
+ throw slashesError
284
+ }
285
+
286
+ const slashPercentageByUserId = sendSlashes . reduce (
287
+ ( acc , { user_id, slash_percentage } ) => {
288
+ acc [ user_id ] = slash_percentage
289
+ return acc
290
+ } ,
291
+ { } as Record < string , number >
292
+ )
293
+ console . log ( 'slashPercentageByUserId: ' , slashPercentageByUserId )
294
+
295
+ if ( log . isLevelEnabled ( 'debug' ) ) {
296
+ await Bun . write ( 'dist/slashes.json' , JSON . stringify ( sendSlashes , jsonBigint , 2 ) ) . catch (
297
+ ( e ) => {
298
+ log . error ( e , 'Error writing slashes.json' )
299
+ }
300
+ )
301
+ }
302
+
279
303
// Calculate fixed pool share weights
280
304
const distAmt = BigInt ( distribution . amount )
281
305
const fixedPoolAvailableAmount = distAmt
@@ -358,16 +382,27 @@ export class DistributorV2Worker {
358
382
userFixedAmount =
359
383
( userFixedAmount * BigInt ( Math . round ( finalMultiplier * Number ( PERC_DENOM ) ) ) ) / PERC_DENOM
360
384
361
- if (
362
- userFixedAmount > 0n &&
363
- fixedPoolAllocatedAmount + userFixedAmount <= fixedPoolAvailableAmount
364
- ) {
365
- fixedPoolAmountsByAddress [ address ] =
366
- ( fixedPoolAmountsByAddress [ address ] || 0n ) + userFixedAmount
367
- fixedPoolAllocatedAmount += userFixedAmount
385
+ // Then modify the fixed pool calculation:
386
+ if ( userFixedAmount > 0n ) {
387
+ const slashPercentage = slashPercentageByUserId [ userId ] || 0
388
+ console . log ( 'Before slash:' , {
389
+ userId,
390
+ userFixedAmount,
391
+ slashPercentage,
392
+ calculation : Math . round ( slashPercentage * Number ( PERC_DENOM ) ) ,
393
+ } )
394
+ userFixedAmount =
395
+ ( userFixedAmount * BigInt ( Math . round ( slashPercentage * Number ( PERC_DENOM ) ) ) ) / PERC_DENOM
396
+
397
+ log . debug ( { userId, address, slashPercentage } , 'User send slash' )
368
398
369
- // Log or save the multipliers for each verification type
370
- log . debug ( { userId, address, multipliers, finalMultiplier } , 'User multipliers' )
399
+ if ( fixedPoolAllocatedAmount + userFixedAmount <= fixedPoolAvailableAmount ) {
400
+ fixedPoolAmountsByAddress [ address ] =
401
+ ( fixedPoolAmountsByAddress [ address ] || 0n ) + userFixedAmount
402
+ fixedPoolAllocatedAmount += userFixedAmount
403
+ // Log or save the multipliers for each verification type
404
+ log . debug ( { userId, address, multipliers, finalMultiplier } , 'User multipliers' )
405
+ }
371
406
}
372
407
}
373
408
@@ -376,7 +411,20 @@ export class DistributorV2Worker {
376
411
377
412
let hodlerShares : { address : string ; amount : bigint } [ ] = [ ]
378
413
if ( hodlerPoolAvailableAmount > 0n ) {
379
- const { weightedShares } = calculateWeights ( minBalanceAddresses , hodlerPoolAvailableAmount )
414
+ const slashedBalances = minBalanceAddresses . map ( ( balance ) => {
415
+ const slashPercentage =
416
+ slashPercentageByUserId [ hodlerUserIdByAddress [ balance . address ] ?? '' ] || 0
417
+ const slashedBalance =
418
+ ( BigInt ( balance . balance ) * BigInt ( Math . round ( slashPercentage * Number ( PERC_DENOM ) ) ) ) /
419
+ PERC_DENOM
420
+
421
+ return {
422
+ address : balance . address ,
423
+ balance : slashedBalance . toString ( ) ,
424
+ }
425
+ } )
426
+
427
+ const { weightedShares } = calculateWeights ( slashedBalances , hodlerPoolAvailableAmount )
380
428
hodlerShares = Object . values ( weightedShares )
381
429
}
382
430
@@ -499,7 +547,6 @@ export class DistributorV2Worker {
499
547
this . running = false
500
548
return await this . workerPromise
501
549
}
502
-
503
550
public async calculateDistribution ( id : string ) {
504
551
const { data : distribution , error } = await fetchDistribution ( id )
505
552
if ( error ) {
0 commit comments