Skip to content

Commit

Permalink
hide token lock for stable pools
Browse files Browse the repository at this point in the history
  • Loading branch information
MattPereira committed Nov 27, 2024
1 parent 077243b commit a43e798
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
23 changes: 12 additions & 11 deletions packages/nextjs/app/v3/_components/ChooseToken.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,18 @@ export function ChooseToken({ index }: { index: number }) {
<div className="bg-base-100 border border-neutral p-4 rounded-xl flex flex-col gap-3">
<div className="flex gap-3 w-full items-center">
<div className="flex flex-col gap-5 justify-between items-center">
{isWeightLocked ? (
<LockClosedIcon
onClick={() => updateTokenConfig(index, { isWeightLocked: false })}
className="w-5 h-5 cursor-pointer"
/>
) : (
<LockOpenIcon
onClick={() => updateTokenConfig(index, { isWeightLocked: true })}
className="w-5 h-5 cursor-pointer"
/>
)}
{poolType === PoolType.Weighted &&
(isWeightLocked ? (
<LockClosedIcon
onClick={() => updateTokenConfig(index, { isWeightLocked: false })}
className="w-5 h-5 cursor-pointer"
/>
) : (
<LockOpenIcon
onClick={() => updateTokenConfig(index, { isWeightLocked: true })}
className="w-5 h-5 cursor-pointer"
/>
))}
{tokenConfigs.length > 2 && (
<div className="cursor-pointer" onClick={handleRemoveToken}>
<TrashIcon className="w-5 h-5" />
Expand Down
20 changes: 13 additions & 7 deletions packages/nextjs/app/v3/_components/ChooseTokens.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,24 @@ export function ChooseTokens() {

// Beware of javascript floating point precision issues if 100 % number of tokens is not equal to zero
function handleAddToken() {
const updatedTokenCount = tokenConfigs.length + 1;
// Calculate equal weights ensuring they sum to exactly 100
const baseWeight = Math.floor(100 / updatedTokenCount);
const remainder = 100 - baseWeight * updatedTokenCount;
// Count unlocked tokens (including the new one we're adding)
const unlockedTokenCount = tokenConfigs.filter(token => !token.isWeightLocked).length + 1;

// Update existing tokens with equal weights
// Calculate remaining weight to distribute (100 minus sum of locked weights)
const lockedWeightSum = tokenConfigs.reduce((sum, token) => (token.isWeightLocked ? sum + token.weight : sum), 0);
const remainingWeight = 100 - lockedWeightSum;

// Calculate base weight for unlocked tokens
const baseWeight = Math.floor(remainingWeight / unlockedTokenCount);
const remainder = remainingWeight - baseWeight * unlockedTokenCount;

// Update tokens, preserving locked weights
const updatedPoolTokens = tokenConfigs.map(token => ({
...token,
weight: baseWeight,
weight: token.isWeightLocked ? token.weight : baseWeight,
}));

// Add the new token with any remaining weight to ensure sum is 100
// Add the new token with any remaining weight
updatedPoolTokens.push({
...initialTokenConfig,
weight: baseWeight + remainder,
Expand Down

0 comments on commit a43e798

Please sign in to comment.