Skip to content

Commit

Permalink
feat: add price-slippage indication to Orderbook chart
Browse files Browse the repository at this point in the history
  • Loading branch information
dib542 committed May 24, 2024
1 parent b3230b8 commit b9bc50f
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/components/cards/LimitOrderCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,12 @@ import {
} from '../../lib/web3/utils/limitOrders';
import {
DexTickUpdateEvent,
DexTickUpdateReservesEvent,
mapEventAttributes,
} from '../../lib/web3/utils/events';
import {
displayPriceToTickIndex,
tickIndexToDisplayPrice,
tickIndexToPrice,
} from '../../lib/web3/utils/ticks';
import { guessInvertedOrder } from '../../lib/web3/utils/pairs';
Expand Down Expand Up @@ -743,10 +745,46 @@ function LimitOrder({
const price = new BigNumber(simulationResult.response.coin_in.amount).div(
simulationResult.response.taker_coin_out.amount
);
// track last price state
setLastKnownPrice(price.toNumber());
}
}, [simulationResult?.response]);

// update slippage indication on chart
useEffect(() => {
if (simulationResult?.result) {
// send slippage indication to rest of Orderbook
if (priceTab === 'Swap' && currentPriceAtoB) {
const events = simulationResult.result?.events
.map<DexTickUpdateReservesEvent>(mapEventAttributes)
.filter((event) => event.attributes.TickIndex);
const endTickIndex = events?.at(-1)?.attributes.TickIndex;
const endPrice =
endTickIndex &&
tickIndexToDisplayPrice(
new BigNumber(endTickIndex),
tokenA,
tokenB
)?.toNumber();
setPriceIndication?.(
endPrice
? buyMode
? Math.max(currentPriceAtoB, endPrice)
: Math.min(currentPriceAtoB, endPrice)
: undefined
);
}
}
}, [
buyMode,
currentPriceAtoB,
priceTab,
setPriceIndication,
simulationResult?.result,
tokenA,
tokenB,
]);

// find coinIn and coinOut from the simulation results
const tranchedReserves =
(simulatedMsgPlaceLimitOrder &&
Expand Down

0 comments on commit b9bc50f

Please sign in to comment.