From aa1e70b809fe602cb3960d92492e60710b24fe50 Mon Sep 17 00:00:00 2001 From: Armani Ferrante Date: Thu, 20 May 2021 23:53:58 -0700 Subject: [PATCH] Eliminate unnecessary market network requests (#20) --- src/swap/context/Dex.tsx | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/swap/context/Dex.tsx b/src/swap/context/Dex.tsx index f85ab92..b220b21 100644 --- a/src/swap/context/Dex.tsx +++ b/src/swap/context/Dex.tsx @@ -161,12 +161,22 @@ export function useMarket(market?: PublicKey): Market | undefined { if (_MARKET_CACHE.get(market.toString())) { return _MARKET_CACHE.get(market.toString()); } - const marketClient = Market.load( - swapClient.program.provider.connection, - market, - undefined, - DEX_PID - ); + + const marketClient = new Promise(async (resolve) => { + const marketAccount = + await swapClient.program.provider.connection.getAccountInfo(market); + if (marketAccount === null) { + throw new Error("Invalid market"); + } + const marketClient = new Market( + Market.getLayout(DEX_PID).decode(marketAccount.data), + -1, + -1, + swapClient.program.provider.opts, + DEX_PID + ); + resolve(marketClient); + }); _MARKET_CACHE.set(market.toString(), marketClient); return marketClient;