Skip to content

Commit

Permalink
feature(bex): always generate pool names and symbols so that the toke…
Browse files Browse the repository at this point in the history
…ns are named in the order they will be slotted in the pool (by sorted address)
  • Loading branch information
PaulMcInnis committed Jan 9, 2025
1 parent 7736de4 commit 6a73133
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions packages/berajs/src/utils/poolNamings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,20 @@ import { formatUnits } from "viem";

import { Token } from "~/types";

// Utility function to sort tokens by their address without modifying the original array
const sortTokensByAddress = (tokens: Token[]): Token[] => {
return [...tokens].sort((a, b) =>
a.address.toLowerCase().localeCompare(b.address.toLowerCase()),
);
};

export const generatePoolName = (tokens: Token[]): string => {
if (tokens.length === 0) {
return "";
}
return tokens.map((token) => token.symbol).join(" | ");
return sortTokensByAddress(tokens)
.map((token) => token.symbol)
.join(" | ");
};

export const generatePoolSymbol = (
Expand All @@ -20,13 +29,15 @@ export const generatePoolSymbol = (
if (weights.length === 0) {
return "";
}
return `${tokens
return `${sortTokensByAddress(tokens)
.map((token, index) => {
const weight = weights[index];
const weightPercentage = parseFloat(formatUnits(weight, 18)) * 100;
return `${weightPercentage.toFixed(0)}${token.symbol}`;
})
.join("-")}-${poolTypeString}`;
}
return `${tokens.map((token) => token.symbol).join("-")}-${poolTypeString}`;
return `${sortTokensByAddress(tokens)
.map((token) => token.symbol)
.join("-")}-${poolTypeString}`;
};

0 comments on commit 6a73133

Please sign in to comment.