diff --git a/js/packages/common/src/contexts/accounts.tsx b/js/packages/common/src/contexts/accounts.tsx index 487389d..9a8424d 100644 --- a/js/packages/common/src/contexts/accounts.tsx +++ b/js/packages/common/src/contexts/accounts.tsx @@ -13,10 +13,8 @@ import { TokenAccount } from '../models'; import { chunks } from '../utils/utils'; import { EventEmitter } from '../utils/eventEmitter'; import { useUserAccounts } from '../hooks/useUserAccounts'; -import { - WRAPPED_SOL_MINT, - programIds, -} from '../utils/ids'; +import { WRAPPED_SOL_MINT, programIds } from '../utils/ids'; +import { AuctionParser } from '../actions'; const AccountsContext = React.createContext(null); @@ -146,6 +144,7 @@ export const cache = { id: PublicKey | string, obj: AccountInfo, parser?: AccountParser, + isActive?: boolean | undefined | ((parsed: any) => boolean), ) => { if (obj.data.length === 0) { return; @@ -166,10 +165,13 @@ export const cache = { return; } + if (isActive == undefined) isActive = true; + else if (isActive instanceof Function) isActive = isActive(account); + const isNew = !genericCache.has(address); genericCache.set(address, account); - cache.emitter.raiseCacheUpdated(address, isNew, deserialize); + cache.emitter.raiseCacheUpdated(address, isNew, deserialize, isActive); return account; }, get: (pubKey: string | PublicKey) => { @@ -320,7 +322,7 @@ const UseNativeAccount = () => { const id = wallet.publicKey?.toBase58(); cache.registerParser(id, TokenAccountParser); genericCache.set(id, wrapped as TokenAccount); - cache.emitter.raiseCacheUpdated(id, false, TokenAccountParser); + cache.emitter.raiseCacheUpdated(id, false, TokenAccountParser, true); } } }, @@ -341,7 +343,7 @@ const UseNativeAccount = () => { return; } - const account = await connection.getAccountInfo(wallet.publicKey) + const account = await connection.getAccountInfo(wallet.publicKey); updateAccount(account); subId = connection.onAccountChange(wallet.publicKey, updateAccount); @@ -351,7 +353,7 @@ const UseNativeAccount = () => { if (subId) { connection.removeAccountChangeListener(subId); } - } + }; }, [setNativeAccount, wallet, wallet?.publicKey, connection, updateCache]); return { nativeAccount }; @@ -405,7 +407,7 @@ export function AccountsProvider({ children = null as any }) { useEffect(() => { const subs: number[] = []; cache.emitter.onCache(args => { - if (args.isNew) { + if (args.isNew && args.isActive) { let id = args.id; let deserialize = args.parser; connection.onAccountChange(new PublicKey(id), info => { @@ -435,7 +437,7 @@ export function AccountsProvider({ children = null as any }) { programIds().token, info => { // TODO: fix type in web3.js - const id = (info.accountId as unknown) as string; + const id = info.accountId as unknown as string; // TODO: do we need a better way to identify layout (maybe a enum identifing type?) if (info.accountInfo.data.length === AccountLayout.span) { const data = deserializeAccount(info.accountInfo.data); diff --git a/js/packages/common/src/contexts/connection.tsx b/js/packages/common/src/contexts/connection.tsx index 5cc8052..71b074b 100644 --- a/js/packages/common/src/contexts/connection.tsx +++ b/js/packages/common/src/contexts/connection.tsx @@ -63,7 +63,6 @@ const DEFAULT = ENDPOINTS[0].endpoint; interface ConnectionConfig { connection: Connection; - sendConnection: Connection; endpoint: string; env: ENV; setEndpoint: (val: string) => void; @@ -75,7 +74,6 @@ const ConnectionContext = React.createContext({ endpoint: DEFAULT, setEndpoint: () => {}, connection: new Connection(DEFAULT, 'recent'), - sendConnection: new Connection(DEFAULT, 'recent'), env: ENDPOINTS[0].name, tokens: [], tokenMap: new Map(), @@ -91,10 +89,6 @@ export function ConnectionProvider({ children = undefined as any }) { () => new Connection(endpoint, 'recent'), [endpoint], ); - const sendConnection = useMemo( - () => new Connection(endpoint, 'recent'), - [endpoint], - ); const env = ENDPOINTS.find(end => end.endpoint === endpoint)?.name || ENDPOINTS[0].name; @@ -144,30 +138,12 @@ export function ConnectionProvider({ children = undefined as any }) { }; }, [connection]); - useEffect(() => { - const id = sendConnection.onAccountChange( - Keypair.generate().publicKey, - () => {}, - ); - return () => { - sendConnection.removeAccountChangeListener(id); - }; - }, [sendConnection]); - - useEffect(() => { - const id = sendConnection.onSlotChange(() => null); - return () => { - sendConnection.removeSlotChangeListener(id); - }; - }, [sendConnection]); - return ( ({ export function MetaProvider({ children = null as any }) { const connection = useConnection(); + const { wallet } = useWallet(); const { env } = useConnectionConfig(); const [metadata, setMetadata] = useState[]>([]); @@ -226,6 +230,7 @@ export function MetaProvider({ children = null as any }) { processAuctions( accounts[i], + wallet, (cb: any) => (tempCache.auctions = cb(tempCache.auctions)), (cb: any) => (tempCache.bidderMetadataByAuctionAndBidder = cb( @@ -327,6 +332,7 @@ export function MetaProvider({ children = null as any }) { setWhitelistedCreatorsByCreator, updateMints, env, + wallet, ]); useEffect(() => { @@ -408,6 +414,7 @@ export function MetaProvider({ children = null as any }) { pubkey, account: info.accountInfo, }, + wallet, setAuctions, setBidderMetadataByAuctionAndBidder, setBidderPotsByAuctionAndBidder, @@ -537,6 +544,7 @@ const queryExtendedMetadata = async ( key, mintAccount, MintParser, + false, ) as ParsedAccount; if (mint.info.supply.gt(new BN(1)) || mint.info.decimals !== 0) { // naive not NFT check @@ -576,6 +584,7 @@ function isValidHttpUrl(text: string) { const processAuctions = ( a: PublicKeyAndAccount, + wallet: any, setAuctions: any, setBidderMetadataByAuctionAndBidder: any, setBidderPotsByAuctionAndBidder: any, @@ -587,6 +596,7 @@ const processAuctions = ( a.pubkey, a.account, AuctionParser, + false, ) as ParsedAccount; setAuctions((e: any) => ({ @@ -603,6 +613,7 @@ const processAuctions = ( a.pubkey, a.account, BidderMetadataParser, + false, ) as ParsedAccount; setBidderMetadataByAuctionAndBidder((e: any) => ({ ...e, @@ -621,6 +632,7 @@ const processAuctions = ( a.pubkey, a.account, BidderPotParser, + false, ) as ParsedAccount; setBidderPotsByAuctionAndBidder((e: any) => ({ @@ -714,6 +726,7 @@ const processMetaplexAccounts = async ( a.pubkey, a.account, WhitelistedCreatorParser, + false, ) as ParsedAccount; const nameInfo = (names as any)[account.info.address.toBase58()];