Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rounding errors in distributor with hack #775

Merged
merged 1 commit into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading