Skip to content

Commit

Permalink
fix: parsing of scientific notation in SOR (#272)
Browse files Browse the repository at this point in the history
  • Loading branch information
gmbronco authored May 28, 2024
1 parent a1fc7bd commit 8871f4a
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions src/modules/sor/sor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ export function orderKindToSwapType(orderKind: string): SwapType {
}

export function sdkToSorSwapType(swapType: SwapType): SwapTypes {
return swapType === SwapType.SwapExactIn ? SwapTypes.SwapExactIn : SwapTypes.SwapExactOut;
return swapType === SwapType.SwapExactIn
? SwapTypes.SwapExactIn
: SwapTypes.SwapExactOut;
}


export function serializeSwapInfo(swapInfo: SwapInfo): SerializedSwapInfo {
const serializedSwapInfo: SerializedSwapInfo = {
tokenAddresses: swapInfo.tokenAddresses,
Expand Down Expand Up @@ -65,7 +66,7 @@ interface SorSwapOptions {
export async function getSorSwap(
chainId: number,
request: SorRequest,
options: SorSwapOptions = {},
options: SorSwapOptions = {}
): Promise<SwapInfo> {
log(`Getting swap: ${JSON.stringify(request)}`);
const rpcUrl = getRpcUrl(chainId);
Expand All @@ -77,18 +78,18 @@ export async function getSorSwap(

let sorSettings;
if (useDb) {
log("Using DynamoDB for SOR data.");
log('Using DynamoDB for SOR data.');
log(`Minimum Liquidity: ${minLiquidity}`);

// SDK/SOR will use this to retrieve pool list from db (default uses onchain call which will be slow)
const dbPoolDataService = new DatabasePoolDataService({
chainId,
minLiquidity
minLiquidity,
});

sorSettings = {
poolDataService: dbPoolDataService
}
poolDataService: dbPoolDataService,
};
}

const balancer = new BalancerSDK({
Expand All @@ -101,17 +102,9 @@ export async function getSorSwap(
const { sellToken, buyToken, orderKind, amount, gasPrice } = request;

const sellTokenDetails: Token = await getToken(chainId, sellToken);
log(
`Sell token details: ${JSON.stringify(
sellTokenDetails
)}`
);
log(`Sell token details: ${JSON.stringify(sellTokenDetails)}`);
const buyTokenDetails: Token = await getToken(chainId, buyToken);
log(
`Buy token details: ${JSON.stringify(
buyTokenDetails
)}`
);
log(`Buy token details: ${JSON.stringify(buyTokenDetails)}`);

const nativeAssetPriceSymbol = getNativeAssetPriceSymbol(chainId);

Expand All @@ -120,6 +113,11 @@ export async function getSorSwap(
if (typeof sellTokenDetails.price !== 'object') {
priceOfNativeAssetInSellToken = sellTokenDetails.price;
} else if (sellTokenDetails.price[nativeAssetPriceSymbol]) {
if (sellTokenDetails.price[nativeAssetPriceSymbol].match(/e/)) {
sellTokenDetails.price[nativeAssetPriceSymbol] = Number(
sellTokenDetails.price[nativeAssetPriceSymbol]
).toFixed(18);
}
priceOfNativeAssetInSellToken = Number(
formatFixed(
parseFixed('1', 72).div(
Expand All @@ -130,7 +128,9 @@ export async function getSorSwap(
);
}
}
log(`Price of ${sellToken} in native asset: ${priceOfNativeAssetInSellToken}`);
log(
`Price of ${sellToken} in native asset: ${priceOfNativeAssetInSellToken}`
);
balancer.sor.swapCostCalculator.setNativeAssetPriceInToken(
sellToken,
priceOfNativeAssetInSellToken.toString()
Expand All @@ -141,6 +141,11 @@ export async function getSorSwap(
if (typeof buyTokenDetails.price !== 'object') {
priceOfNativeAssetInBuyToken = buyTokenDetails.price;
} else if (buyTokenDetails.price[nativeAssetPriceSymbol]) {
if (buyTokenDetails.price[nativeAssetPriceSymbol].match(/e/)) {
buyTokenDetails.price[nativeAssetPriceSymbol] = Number(
buyTokenDetails.price[nativeAssetPriceSymbol]
).toFixed(18);
}
priceOfNativeAssetInBuyToken = Number(
formatFixed(
parseFixed('1', 72).div(
Expand Down Expand Up @@ -188,4 +193,3 @@ export async function getSorSwap(

return swapInfo;
}

0 comments on commit 8871f4a

Please sign in to comment.