Skip to content

Commit c3eb585

Browse files
Update DistributorV2 with send slash logic
1 parent 81bf5c1 commit c3eb585

File tree

4 files changed

+70
-21
lines changed

4 files changed

+70
-21
lines changed

apps/distributor/src/distributorv2.test.ts

+2-7
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ describe('Distributor V2 Worker', () => {
103103
updated_at: '2024-04-06T16:49:02.569245+00:00',
104104
snapshot_block_num: 13261327,
105105
chain_id: 845337,
106+
send_slash_divisor: 150,
106107
distribution_verification_values: [
107108
{
108109
type: 'tag_referral',
@@ -112,7 +113,6 @@ describe('Distributor V2 Worker', () => {
112113
multiplier_max: 2.5,
113114
multiplier_step: 0.1,
114115
distribution_id: 4,
115-
mode: 'individual',
116116
},
117117
{
118118
type: 'total_tag_referrals',
@@ -121,7 +121,7 @@ describe('Distributor V2 Worker', () => {
121121
multiplier_min: 1.0,
122122
multiplier_max: 2.0,
123123
multiplier_step: 0.01,
124-
mode: 'aggregate',
124+
125125
created_at: '2024-04-06T16:49:02.569245+00:00',
126126
updated_at: '2024-04-06T16:49:02.569245+00:00',
127127
distribution_id: 4,
@@ -131,7 +131,6 @@ describe('Distributor V2 Worker', () => {
131131
fixed_value: 200,
132132
bips_value: 0,
133133
distribution_id: 4,
134-
mode: 'individual',
135134
},
136135
{
137136
type: 'tag_registration',
@@ -140,7 +139,6 @@ describe('Distributor V2 Worker', () => {
140139
distribution_id: 4,
141140
created_at: '2024-04-06T16:49:02.569245+00:00',
142141
updated_at: '2024-04-06T16:49:02.569245+00:00',
143-
mode: 'individual',
144142
},
145143
{
146144
type: 'send_ten',
@@ -149,7 +147,6 @@ describe('Distributor V2 Worker', () => {
149147
distribution_id: 4,
150148
created_at: '2024-04-06T16:49:02.569245+00:00',
151149
updated_at: '2024-04-06T16:49:02.569245+00:00',
152-
mode: 'individual',
153150
},
154151
{
155152
type: 'send_one_hundred',
@@ -158,7 +155,6 @@ describe('Distributor V2 Worker', () => {
158155
distribution_id: 4,
159156
created_at: '2024-04-06T16:49:02.569245+00:00',
160157
updated_at: '2024-04-06T16:49:02.569245+00:00',
161-
mode: 'individual',
162158
},
163159
{
164160
type: 'send_streak',
@@ -170,7 +166,6 @@ describe('Distributor V2 Worker', () => {
170166
multiplier_step: 0.2,
171167
created_at: '2024-04-06T16:49:02.569245+00:00',
172168
updated_at: '2024-04-06T16:49:02.569245+00:00',
173-
mode: 'aggregate',
174169
},
175170
],
176171
} as Tables<'distributions'> & {

apps/distributor/src/distributorv2.ts

+60-13
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import {
66
fetchAllHodlers,
77
fetchAllVerifications,
88
fetchDistribution,
9+
fetchDistributionShares,
10+
fetchSendSlash,
11+
fetchSendTransfers,
912
supabaseAdmin,
1013
} from './supabase'
1114
import { fetchAllBalances, isMerkleDropActive } from './wagmi'
@@ -165,7 +168,6 @@ export class DistributorV2Worker {
165168
acc[verification.type] = {
166169
fixedValue: BigInt(verification.fixed_value),
167170
bipsValue: BigInt(verification.bips_value),
168-
mode: verification.mode,
169171
multiplier_min: verification.multiplier_min,
170172
multiplier_max: verification.multiplier_max,
171173
multiplier_step: verification.multiplier_step,
@@ -177,7 +179,6 @@ export class DistributorV2Worker {
177179
{
178180
fixedValue?: bigint
179181
bipsValue?: bigint
180-
mode: Database['public']['Enums']['verification_value_mode']
181182
multiplier_min: number
182183
multiplier_max: number
183184
multiplier_step: number
@@ -276,6 +277,29 @@ export class DistributorV2Worker {
276277
})
277278
}
278279

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+
279303
// Calculate fixed pool share weights
280304
const distAmt = BigInt(distribution.amount)
281305
const fixedPoolAvailableAmount = distAmt
@@ -358,16 +382,27 @@ export class DistributorV2Worker {
358382
userFixedAmount =
359383
(userFixedAmount * BigInt(Math.round(finalMultiplier * Number(PERC_DENOM)))) / PERC_DENOM
360384

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')
368398

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+
}
371406
}
372407
}
373408

@@ -376,7 +411,20 @@ export class DistributorV2Worker {
376411

377412
let hodlerShares: { address: string; amount: bigint }[] = []
378413
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)
380428
hodlerShares = Object.values(weightedShares)
381429
}
382430

@@ -499,7 +547,6 @@ export class DistributorV2Worker {
499547
this.running = false
500548
return await this.workerPromise
501549
}
502-
503550
public async calculateDistribution(id: string) {
504551
const { data: distribution, error } = await fetchDistribution(id)
505552
if (error) {

apps/distributor/src/supabase.ts

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Database, Tables } from '@my/supabase/database.types'
22
import { createClient } from '@supabase/supabase-js'
33
import { selectAll } from 'app/utils/supabase/selectAll'
4+
import type { Address } from 'viem'
45

56
if (!process.env.NEXT_PUBLIC_SUPABASE_URL) {
67
throw new Error(
@@ -30,6 +31,12 @@ export async function fetchDistribution(id: string) {
3031
.single()
3132
}
3233

34+
export async function fetchSendSlash(distributionNumber: number) {
35+
return await supabaseAdmin
36+
.rpc('get_send_slash_calculations', { distribution_number: distributionNumber })
37+
.select()
38+
}
39+
3340
export async function fetchAllVerifications(distributionId: number) {
3441
return selectAll(
3542
supabaseAdmin

apps/distributor/src/weights.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export enum Mode {
2525
Exponential = 'exponential',
2626
}
2727

28-
export const PERC_DENOM = 10000n
28+
export const PERC_DENOM = 10000000n
2929

3030
export function calculatePercentageWithBips(value: bigint, bips: bigint) {
3131
const bps = bips * PERC_DENOM

0 commit comments

Comments
 (0)