Skip to content

Commit

Permalink
Fix rounding errors in distributor with hack
Browse files Browse the repository at this point in the history
  • Loading branch information
youngkidwarrior committed Oct 19, 2024
1 parent 9e77ea2 commit e66a766
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
3 changes: 1 addition & 2 deletions apps/distributor/src/distributorv2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,7 @@ export class DistributorV2Worker {
}

// Calculate hodler pool share weights
// -500 to account for rounding errors
const hodlerPoolAvailableAmount = distAmt - fixedPoolAllocatedAmount - 500n
const hodlerPoolAvailableAmount = distAmt - fixedPoolAllocatedAmount

let hodlerShares: { address: string; amount: bigint }[] = []
if (hodlerPoolAvailableAmount > 0n) {
Expand Down
16 changes: 16 additions & 0 deletions apps/distributor/src/weights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,22 @@ export function calculateWeights(
}
}

//@todo: this is a hack to ensure the total distributed amount is equal to the amount
// We really should handle these rounding errors instead
let totalDistributed = 0n
for (const share of Object.values(weightedShares)) {
totalDistributed += share.amount
}

if (totalDistributed !== amount) {
const difference = amount - totalDistributed
// Add or subtract the difference from the largest share
const largestShare = Object.values(weightedShares).reduce((a, b) =>
a.amount > b.amount ? a : b
)
largestShare.amount += difference
}

return { totalWeight, weightPerSend, poolWeights, weightedShares }
}

Expand Down

0 comments on commit e66a766

Please sign in to comment.