Skip to content

Commit

Permalink
optimize with promise all
Browse files Browse the repository at this point in the history
  • Loading branch information
vuonghuuhung committed Sep 16, 2024
1 parent 62e2544 commit 537d17c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 30 deletions.
2 changes: 1 addition & 1 deletion packages/oraiswap-v3/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@oraichain/oraiswap-v3",
"version": "0.1.6-beta.20",
"version": "0.1.6-beta.21",
"main": "build/index.js",
"files": [
"build/"
Expand Down
22 changes: 4 additions & 18 deletions packages/oraiswap-v3/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,24 +272,10 @@ export const _calculateTokenAmounts = (pool: Pool, position: Position, sign: boo
};

export const shiftDecimal = (value: bigint, decimals: number): BigDecimal => {
const valueStr = value.toString();
const len = valueStr.length;
const splitPos = len - decimals;

let result: string;

if (splitPos > 0) {
// When there are more digits than decimals, insert the decimal point accordingly
const intStr = valueStr.slice(0, splitPos);
const decStr = valueStr.slice(splitPos);
result = `${intStr}.${decStr}`;
} else {
// When digits are fewer than or equal to decimals, pad with leading zeros
const paddedDecStr = valueStr.padStart(decimals, "0");
result = `0.${paddedDecStr}`;
}

return new BigDecimal(result, decimals);
const valueStr = value.toString().padStart(decimals + 1, "0"); // Pad with leading zeros if needed
const intStr = valueStr.slice(0, -decimals) || "0"; // Retrieve integral part or default to "0"
const decStr = valueStr.slice(-decimals); // Retrieve decimal part
return new BigDecimal(`${intStr}.${decStr}`, decimals);
};

export const parseAsset = (token: TokenItemType, amount: string): Asset => {
Expand Down
2 changes: 1 addition & 1 deletion packages/oraiswap-v3/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async function main() {
// `${OSMOSIS_ORAICHAIN_DENOM}-orai-3000000000-100`,
`orai-${ORAIX_CONTRACT}-3000000000-100`
];
const tokenIn = oraichainTokens.find((t) => t.denom === "NTMPI");
const tokenIn = oraichainTokens.find((t) => extractAddress(t) === USDT_CONTRACT) as TokenItemType;

const zapper = new ZapConsumer({
routerApi: "https://osor.oraidex.io/smart-router/alpha-router",
Expand Down
29 changes: 19 additions & 10 deletions packages/oraiswap-v3/src/zap-consumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ export class ZapConsumer {
false,
isXToY ? getMinSqrtPrice(poolKey.fee_tier.tick_spacing) : getMaxSqrtPrice(poolKey.fee_tier.tick_spacing)
);

pool.sqrt_price = ((BigInt(pool.sqrt_price) + BigInt(swapResult.target_sqrt_price)) / 2n).toString();
const tick = getTickAtSqrtPrice(BigInt(pool.sqrt_price), poolKey.fee_tier.tick_spacing);
pool.current_tick_index = (simualteNextTick + tick) / 2;
Expand Down Expand Up @@ -436,15 +436,18 @@ export class ZapConsumer {

// API time: need to check
console.time("[ZAPPER] getPriceInfo");
const getXPriceByTokenIn = await this.getPriceInfo({
const getXPriceByTokenInPromise = this.getPriceInfo({
sourceAsset: tokenX,
destAsset: tokenIn
});

const getYPriceByTokenIn = await this.getPriceInfo({
const getYPriceByTokenInPromise = this.getPriceInfo({
sourceAsset: tokenY,
destAsset: tokenIn
});
const [getXPriceByTokenIn, getYPriceByTokenIn] = await Promise.all([
getXPriceByTokenInPromise,
getYPriceByTokenInPromise
]);
console.timeEnd("[ZAPPER] getPriceInfo");

// separate case if tokenIn is on of the pool tokens
Expand Down Expand Up @@ -473,16 +476,20 @@ export class ZapConsumer {
// re-check
// API time: need to check
console.time("[ZAPPER] findRoute");
const actualAmountXReceived = await this.findRoute({
const actualAmountXReceivedPromise = this.findRoute({
sourceAsset: tokenIn,
destAsset: tokenX,
amount: amountInToX
});
const actualAmountYReceived = await this.findRoute({
const actualAmountYReceivedPromise = this.findRoute({
sourceAsset: tokenIn,
destAsset: tokenY,
amount: amountInToY
});
const [actualAmountXReceived, actualAmountYReceived] = await Promise.all([
actualAmountXReceivedPromise,
actualAmountYReceivedPromise
]);
console.timeEnd("[ZAPPER] findRoute");

// get all routes
Expand Down Expand Up @@ -645,16 +652,17 @@ export class ZapConsumer {
console.time("[ZAPPER] findRoute");
let xRouteInfo: SmartRouteResponse;
let yRouteInfo: SmartRouteResponse;
xRouteInfo = await this.findRoute({
const xRouteInfoPromise = this.findRoute({
sourceAsset: tokenIn,
destAsset: tokenX,
amount: amountInToX
});
yRouteInfo = await this.findRoute({
const yRouteInfoPromise = this.findRoute({
sourceAsset: tokenIn,
destAsset: tokenY,
amount: amountInToY
});
[xRouteInfo, yRouteInfo] = await Promise.all([xRouteInfoPromise, yRouteInfoPromise]);
console.timeEnd("[ZAPPER] findRoute");

// create message
Expand Down Expand Up @@ -753,16 +761,17 @@ export class ZapConsumer {
// }

// const routes: SmartRouteResponse[] = [];
const xRouteInfo = await this.findRoute({
const xRouteInfoPromise = this.findRoute({
sourceAsset: oraichainTokens.find((t) => extractAddress(t) === pool.pool_key.token_x),
destAsset: tokenOut,
amount: rewardAmounts[pool.pool_key.token_x]
});
const yRouteInfo = await this.findRoute({
const yRouteInfoPromise = this.findRoute({
sourceAsset: oraichainTokens.find((t) => extractAddress(t) === pool.pool_key.token_y),
destAsset: tokenOut,
amount: rewardAmounts[pool.pool_key.token_y]
});
const [xRouteInfo, yRouteInfo] = await Promise.all([xRouteInfoPromise, yRouteInfoPromise]);

// build messages
const messages: ZapOutLiquidityResponse = {} as ZapOutLiquidityResponse;
Expand Down

0 comments on commit 537d17c

Please sign in to comment.