Check for empty orderbook (#58)
This commit is contained in:
parent
15c9845bdc
commit
a3e841bf8d
|
@ -356,6 +356,9 @@ export function useBbo(market?: PublicKey): Bbo | undefined {
|
||||||
}
|
}
|
||||||
const bestBid = orderbook.bids.items(true).next().value;
|
const bestBid = orderbook.bids.items(true).next().value;
|
||||||
const bestOffer = orderbook.asks.items(false).next().value;
|
const bestOffer = orderbook.asks.items(false).next().value;
|
||||||
|
if (!bestBid && !bestOffer) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
if (!bestBid) {
|
if (!bestBid) {
|
||||||
return { bestOffer: bestOffer.price };
|
return { bestOffer: bestOffer.price };
|
||||||
}
|
}
|
||||||
|
|
|
@ -89,9 +89,10 @@ export function SwapContextProvider(props: any) {
|
||||||
assert.ok(slippage >= 0);
|
assert.ok(slippage >= 0);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (fair) {
|
if (!fair) {
|
||||||
setFromAmount(fromAmount);
|
return;
|
||||||
}
|
}
|
||||||
|
setFromAmount(fromAmount);
|
||||||
}, [fair]);
|
}, [fair]);
|
||||||
|
|
||||||
const swapToFromMints = () => {
|
const swapToFromMints = () => {
|
||||||
|
@ -105,7 +106,9 @@ export function SwapContextProvider(props: any) {
|
||||||
|
|
||||||
const setFromAmount = (amount: number) => {
|
const setFromAmount = (amount: number) => {
|
||||||
if (fair === undefined) {
|
if (fair === undefined) {
|
||||||
throw new Error("Fair price not found");
|
_setFromAmount(0);
|
||||||
|
_setToAmount(0);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
_setFromAmount(amount);
|
_setFromAmount(amount);
|
||||||
_setToAmount(FEE_MULTIPLIER * (amount / fair));
|
_setToAmount(FEE_MULTIPLIER * (amount / fair));
|
||||||
|
@ -113,7 +116,9 @@ export function SwapContextProvider(props: any) {
|
||||||
|
|
||||||
const setToAmount = (amount: number) => {
|
const setToAmount = (amount: number) => {
|
||||||
if (fair === undefined) {
|
if (fair === undefined) {
|
||||||
throw new Error("Fair price not found");
|
_setFromAmount(0);
|
||||||
|
_setToAmount(0);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
_setToAmount(amount);
|
_setToAmount(amount);
|
||||||
_setFromAmount((amount * fair) / FEE_MULTIPLIER);
|
_setFromAmount((amount * fair) / FEE_MULTIPLIER);
|
||||||
|
@ -175,12 +180,16 @@ export function useCanSwap(): boolean {
|
||||||
const { fromMint, toMint, fromAmount, toAmount } = useSwapContext();
|
const { fromMint, toMint, fromAmount, toAmount } = useSwapContext();
|
||||||
const { swapClient } = useDexContext();
|
const { swapClient } = useDexContext();
|
||||||
const { wormholeMap, solletMap } = useTokenListContext();
|
const { wormholeMap, solletMap } = useTokenListContext();
|
||||||
|
const fair = useSwapFair();
|
||||||
const route = useRouteVerbose(fromMint, toMint);
|
const route = useRouteVerbose(fromMint, toMint);
|
||||||
if (route === null) {
|
if (route === null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
// Fair price is defined.
|
||||||
|
fair !== undefined &&
|
||||||
|
fair > 0 &&
|
||||||
// Mints are distinct.
|
// Mints are distinct.
|
||||||
fromMint.equals(toMint) === false &&
|
fromMint.equals(toMint) === false &&
|
||||||
// Wallet is connected.
|
// Wallet is connected.
|
||||||
|
|
Loading…
Reference in New Issue