Check for empty orderbook (#58)

This commit is contained in:
Armani Ferrante 2021-06-22 21:40:18 -07:00 committed by GitHub
parent 15c9845bdc
commit a3e841bf8d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 4 deletions

View File

@ -356,6 +356,9 @@ export function useBbo(market?: PublicKey): Bbo | undefined {
}
const bestBid = orderbook.bids.items(true).next().value;
const bestOffer = orderbook.asks.items(false).next().value;
if (!bestBid && !bestOffer) {
return {};
}
if (!bestBid) {
return { bestOffer: bestOffer.price };
}

View File

@ -89,9 +89,10 @@ export function SwapContextProvider(props: any) {
assert.ok(slippage >= 0);
useEffect(() => {
if (fair) {
setFromAmount(fromAmount);
if (!fair) {
return;
}
setFromAmount(fromAmount);
}, [fair]);
const swapToFromMints = () => {
@ -105,7 +106,9 @@ export function SwapContextProvider(props: any) {
const setFromAmount = (amount: number) => {
if (fair === undefined) {
throw new Error("Fair price not found");
_setFromAmount(0);
_setToAmount(0);
return;
}
_setFromAmount(amount);
_setToAmount(FEE_MULTIPLIER * (amount / fair));
@ -113,7 +116,9 @@ export function SwapContextProvider(props: any) {
const setToAmount = (amount: number) => {
if (fair === undefined) {
throw new Error("Fair price not found");
_setFromAmount(0);
_setToAmount(0);
return;
}
_setToAmount(amount);
_setFromAmount((amount * fair) / FEE_MULTIPLIER);
@ -175,12 +180,16 @@ export function useCanSwap(): boolean {
const { fromMint, toMint, fromAmount, toAmount } = useSwapContext();
const { swapClient } = useDexContext();
const { wormholeMap, solletMap } = useTokenListContext();
const fair = useSwapFair();
const route = useRouteVerbose(fromMint, toMint);
if (route === null) {
return false;
}
return (
// Fair price is defined.
fair !== undefined &&
fair > 0 &&
// Mints are distinct.
fromMint.equals(toMint) === false &&
// Wallet is connected.