Eliminate unnecessary market network requests (#20)

This commit is contained in:
Armani Ferrante 2021-05-20 23:53:58 -07:00 committed by GitHub
parent d7b8200b9d
commit aa1e70b809
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 6 deletions

View File

@ -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<Market>(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;