Fix for store

This commit is contained in:
Jordan Prince 2021-06-02 11:00:08 -05:00
parent 485c6314b5
commit 7845381d34
1 changed files with 102 additions and 65 deletions

View File

@ -175,12 +175,14 @@ export function MetaProvider({ children = null as any }) {
useEffect(() => { useEffect(() => {
let dispose = () => {}; let dispose = () => {};
(async () => { (async () => {
const accounts = (await Promise.all([ const accounts = (
connection.getProgramAccounts(programIds().vault), await Promise.all([
connection.getProgramAccounts(programIds().auction), connection.getProgramAccounts(programIds().vault),
connection.getProgramAccounts(programIds().metadata), connection.getProgramAccounts(programIds().auction),
connection.getProgramAccounts(programIds().metaplex), connection.getProgramAccounts(programIds().metadata),
])).flat(); connection.getProgramAccounts(programIds().metaplex),
])
).flat();
const tempCache = { const tempCache = {
metadata: {}, metadata: {},
@ -200,53 +202,86 @@ export function MetaProvider({ children = null as any }) {
bidderMetadataByAuctionAndBidder: {}, bidderMetadataByAuctionAndBidder: {},
bidderPotsByAuctionAndBidder: {}, bidderPotsByAuctionAndBidder: {},
safetyDepositBoxesByVaultAndIndex: {}, safetyDepositBoxesByVaultAndIndex: {},
} };
for (let i = 0; i < accounts.length; i++) { for (let i = 0; i < accounts.length; i++) {
processVaultData( processVaultData(
accounts[i], accounts[i],
(cb: any) => tempCache.safetyDepositBoxesByVaultAndIndex = cb(tempCache.safetyDepositBoxesByVaultAndIndex), (cb: any) =>
(cb: any) => tempCache.vaults = cb(tempCache.vaults), (tempCache.safetyDepositBoxesByVaultAndIndex = cb(
tempCache.safetyDepositBoxesByVaultAndIndex,
)),
(cb: any) => (tempCache.vaults = cb(tempCache.vaults)),
); );
processAuctions( processAuctions(
accounts[i], accounts[i],
(cb: any) => tempCache.auctions = cb(tempCache.auctions), (cb: any) => (tempCache.auctions = cb(tempCache.auctions)),
(cb: any) => tempCache.bidderMetadataByAuctionAndBidder = cb(tempCache.bidderMetadataByAuctionAndBidder), (cb: any) =>
(cb: any) => tempCache.bidderPotsByAuctionAndBidder = cb(tempCache.bidderPotsByAuctionAndBidder), (tempCache.bidderMetadataByAuctionAndBidder = cb(
tempCache.bidderMetadataByAuctionAndBidder,
)),
(cb: any) =>
(tempCache.bidderPotsByAuctionAndBidder = cb(
tempCache.bidderPotsByAuctionAndBidder,
)),
); );
await processMetaData( await processMetaData(
accounts[i], accounts[i],
(cb: any) => tempCache.metadataByMint = cb(tempCache.metadataByMint), (cb: any) =>
(cb: any) => tempCache.metadataByMasterEdition = cb(tempCache.metadataByMasterEdition), (tempCache.metadataByMint = cb(tempCache.metadataByMint)),
(cb: any) => tempCache.editions = cb(tempCache.editions), (cb: any) =>
(cb: any) => tempCache.masterEditions = cb(tempCache.masterEditions), (tempCache.metadataByMasterEdition = cb(
(cb: any) => tempCache.masterEditionsByPrintingMint = cb(tempCache.masterEditionsByPrintingMint), tempCache.metadataByMasterEdition,
(cb: any) => tempCache.masterEditionsByOneTimeAuthMint = cb(tempCache.masterEditionsByOneTimeAuthMint), )),
(cb: any) => (tempCache.editions = cb(tempCache.editions)),
(cb: any) =>
(tempCache.masterEditions = cb(tempCache.masterEditions)),
(cb: any) =>
(tempCache.masterEditionsByPrintingMint = cb(
tempCache.masterEditionsByPrintingMint,
)),
(cb: any) =>
(tempCache.masterEditionsByOneTimeAuthMint = cb(
tempCache.masterEditionsByOneTimeAuthMint,
)),
); );
await processMetaplexAccounts( await processMetaplexAccounts(
accounts[i], accounts[i],
(cb: any) => tempCache.auctionManagersByAuction = cb(tempCache.auctionManagersByAuction), (cb: any) =>
(cb: any) => tempCache.bidRedemptions = cb(tempCache.bidRedemptions), (tempCache.auctionManagersByAuction = cb(
(cb: any) => tempCache.payoutTickets = cb(tempCache.payoutTickets), tempCache.auctionManagersByAuction,
(cb: any) => tempCache.store = cb(tempCache.store), )),
(cb: any) => tempCache.whitelistedCreatorsByCreator = cb(tempCache.whitelistedCreatorsByCreator), (cb: any) =>
(tempCache.bidRedemptions = cb(tempCache.bidRedemptions)),
(cb: any) => (tempCache.payoutTickets = cb(tempCache.payoutTickets)),
(obj: any) => (tempCache.store = obj),
(cb: any) =>
(tempCache.whitelistedCreatorsByCreator = cb(
tempCache.whitelistedCreatorsByCreator,
)),
); );
} }
setSafetyDepositBoxesByVaultAndIndex(tempCache.safetyDepositBoxesByVaultAndIndex); setSafetyDepositBoxesByVaultAndIndex(
tempCache.safetyDepositBoxesByVaultAndIndex,
);
setVaults(tempCache.vaults); setVaults(tempCache.vaults);
setAuctions(tempCache.auctions); setAuctions(tempCache.auctions);
setBidderMetadataByAuctionAndBidder(tempCache.bidderMetadataByAuctionAndBidder); setBidderMetadataByAuctionAndBidder(
tempCache.bidderMetadataByAuctionAndBidder,
);
setBidderPotsByAuctionAndBidder(tempCache.bidderPotsByAuctionAndBidder); setBidderPotsByAuctionAndBidder(tempCache.bidderPotsByAuctionAndBidder);
setMetadataByMint(tempCache.metadataByMint); setMetadataByMint(tempCache.metadataByMint);
setMetadataByMasterEdition(tempCache.metadataByMasterEdition); setMetadataByMasterEdition(tempCache.metadataByMasterEdition);
setEditions(tempCache.editions); setEditions(tempCache.editions);
setMasterEditions(tempCache.masterEditions); setMasterEditions(tempCache.masterEditions);
setmasterEditionsByPrintingMint(tempCache.masterEditionsByPrintingMint); setmasterEditionsByPrintingMint(tempCache.masterEditionsByPrintingMint);
setMasterEditionsByOneTimeAuthMint(tempCache.masterEditionsByOneTimeAuthMint); setMasterEditionsByOneTimeAuthMint(
tempCache.masterEditionsByOneTimeAuthMint,
);
setAuctionManagersByAuction(tempCache.auctionManagersByAuction); setAuctionManagersByAuction(tempCache.auctionManagersByAuction);
setBidRedemptions(tempCache.bidRedemptions); setBidRedemptions(tempCache.bidRedemptions);
setPayoutTickets(tempCache.payoutTickets); setPayoutTickets(tempCache.payoutTickets);
@ -261,15 +296,16 @@ export function MetaProvider({ children = null as any }) {
); );
setMetadata(m.metadata); setMetadata(m.metadata);
setMetadataByMint(m.mintToMetadata); setMetadataByMint(m.mintToMetadata);
} catch(er) { } catch (er) {
console.error(er) console.error(er);
} }
})(); })();
return () => { return () => {
dispose(); dispose();
}; };
}, [connection, }, [
connection,
setSafetyDepositBoxesByVaultAndIndex, setSafetyDepositBoxesByVaultAndIndex,
setVaults, setVaults,
setAuctions, setAuctions,
@ -285,7 +321,8 @@ export function MetaProvider({ children = null as any }) {
setBidRedemptions, setBidRedemptions,
setPayoutTickets, setPayoutTickets,
setStore, setStore,
setWhitelistedCreatorsByCreator]); setWhitelistedCreatorsByCreator,
]);
useEffect(() => { useEffect(() => {
let vaultSubId = connection.onProgramAccountChange( let vaultSubId = connection.onProgramAccountChange(
@ -301,7 +338,8 @@ export function MetaProvider({ children = null as any }) {
account: info.accountInfo, account: info.accountInfo,
}, },
setSafetyDepositBoxesByVaultAndIndex, setSafetyDepositBoxesByVaultAndIndex,
setVaults); setVaults,
);
}, },
); );
@ -313,15 +351,16 @@ export function MetaProvider({ children = null as any }) {
? new PublicKey(info.accountId as unknown as string) ? new PublicKey(info.accountId as unknown as string)
: info.accountId; : info.accountId;
await processMetaplexAccounts( await processMetaplexAccounts(
{ {
pubkey, pubkey,
account: info.accountInfo, account: info.accountInfo,
}, },
setAuctionManagersByAuction, setAuctionManagersByAuction,
setBidRedemptions, setBidRedemptions,
setPayoutTickets, setPayoutTickets,
setStore, setStore,
setWhitelistedCreatorsByCreator); setWhitelistedCreatorsByCreator,
);
}, },
); );
@ -342,7 +381,8 @@ export function MetaProvider({ children = null as any }) {
setEditions, setEditions,
setMasterEditions, setMasterEditions,
setmasterEditionsByPrintingMint, setmasterEditionsByPrintingMint,
setMasterEditionsByOneTimeAuthMint); setMasterEditionsByOneTimeAuthMint,
);
// setMetadataByMint(latest => { // setMetadataByMint(latest => {
// queryExtendedMetadata( // queryExtendedMetadata(
@ -368,7 +408,8 @@ export function MetaProvider({ children = null as any }) {
}, },
setAuctions, setAuctions,
setBidderMetadataByAuctionAndBidder, setBidderMetadataByAuctionAndBidder,
setBidderPotsByAuctionAndBidder); setBidderPotsByAuctionAndBidder,
);
}, },
); );
@ -395,7 +436,7 @@ export function MetaProvider({ children = null as any }) {
setBidRedemptions, setBidRedemptions,
setPayoutTickets, setPayoutTickets,
setStore, setStore,
setWhitelistedCreatorsByCreator setWhitelistedCreatorsByCreator,
]); ]);
const filteredMetadata = useMemo( const filteredMetadata = useMemo(
@ -403,7 +444,9 @@ export function MetaProvider({ children = null as any }) {
metadata.filter(m => metadata.filter(m =>
m?.info?.data?.creators?.find( m?.info?.data?.creators?.find(
c => c =>
c.verified && store && store.info && c.verified &&
store &&
store.info &&
(store.info.public || (store.info.public ||
whitelistedCreatorsByCreator[c.address.toBase58()]?.info whitelistedCreatorsByCreator[c.address.toBase58()]?.info
?.activated), ?.activated),
@ -508,7 +551,7 @@ const queryExtendedMetadata = async (
return { return {
metadata, metadata,
mintToMetadata, mintToMetadata,
} };
}; };
export const useMeta = () => { export const useMeta = () => {
@ -532,8 +575,8 @@ const processAuctions = (
a: PublicKeyAndAccount<Buffer>, a: PublicKeyAndAccount<Buffer>,
setAuctions: any, setAuctions: any,
setBidderMetadataByAuctionAndBidder: any, setBidderMetadataByAuctionAndBidder: any,
setBidderPotsByAuctionAndBidder: any setBidderPotsByAuctionAndBidder: any,
) => { ) => {
try { try {
const account = cache.add( const account = cache.add(
a.pubkey, a.pubkey,
@ -588,7 +631,6 @@ const processAuctions = (
} }
}; };
const processMetaplexAccounts = async ( const processMetaplexAccounts = async (
a: PublicKeyAndAccount<Buffer>, a: PublicKeyAndAccount<Buffer>,
setAuctionManagersByAuction: any, setAuctionManagersByAuction: any,
@ -604,9 +646,7 @@ const processMetaplexAccounts = async (
const auctionManager = decodeAuctionManager(a.account.data); const auctionManager = decodeAuctionManager(a.account.data);
// An initialized auction manager hasnt been validated, so we cant show it to users. // An initialized auction manager hasnt been validated, so we cant show it to users.
// Could have any kind of pictures in it. // Could have any kind of pictures in it.
if ( if (auctionManager.state.status !== AuctionManagerStatus.Initialized) {
auctionManager.state.status !== AuctionManagerStatus.Initialized
) {
const account: ParsedAccount<AuctionManager> = { const account: ParsedAccount<AuctionManager> = {
pubkey: a.pubkey, pubkey: a.pubkey,
account: a.account, account: a.account,
@ -642,22 +682,22 @@ const processMetaplexAccounts = async (
})); }));
} else if (a.account.data[0] === MetaplexKey.StoreV1) { } else if (a.account.data[0] === MetaplexKey.StoreV1) {
const store = decodeStore(a.account.data); const store = decodeStore(a.account.data);
console.log('Found store', store);
const account: ParsedAccount<Store> = { const account: ParsedAccount<Store> = {
pubkey: a.pubkey, pubkey: a.pubkey,
account: a.account, account: a.account,
info: store, info: store,
}; };
if (a.pubkey.toBase58() === programIds().store.toBase58()) if (a.pubkey.toBase58() === programIds().store.toBase58()) {
setStore(account); setStore(account);
}
} else if (a.account.data[0] === MetaplexKey.WhitelistedCreatorV1) { } else if (a.account.data[0] === MetaplexKey.WhitelistedCreatorV1) {
const whitelistedCreator = decodeWhitelistedCreator( const whitelistedCreator = decodeWhitelistedCreator(a.account.data);
a.account.data, const creatorKeyIfCreatorWasPartOfThisStore = await getWhitelistedCreator(
whitelistedCreator.address,
); );
const creatorKeyIfCreatorWasPartOfThisStore =
await getWhitelistedCreator(whitelistedCreator.address);
if ( if (
creatorKeyIfCreatorWasPartOfThisStore.toBase58() == creatorKeyIfCreatorWasPartOfThisStore.toBase58() == a.pubkey.toBase58()
a.pubkey.toBase58()
) { ) {
const account = cache.add( const account = cache.add(
a.pubkey, a.pubkey,
@ -684,7 +724,6 @@ const processMetaplexAccounts = async (
} }
}; };
const processMetaData = async ( const processMetaData = async (
meta: PublicKeyAndAccount<Buffer>, meta: PublicKeyAndAccount<Buffer>,
setMetadataByMint: any, setMetadataByMint: any,
@ -693,7 +732,7 @@ const processMetaData = async (
setMasterEditions: any, setMasterEditions: any,
setmasterEditionsByPrintingMint: any, setmasterEditionsByPrintingMint: any,
setMasterEditionsByOneTimeAuthMint: any, setMasterEditionsByOneTimeAuthMint: any,
) => { ) => {
try { try {
if (meta.account.data[0] === MetadataKey.MetadataV1) { if (meta.account.data[0] === MetadataKey.MetadataV1) {
const metadata = await decodeMetadata(meta.account.data); const metadata = await decodeMetadata(meta.account.data);
@ -741,8 +780,7 @@ const processMetaData = async (
})); }));
setMasterEditionsByOneTimeAuthMint((e: any) => ({ setMasterEditionsByOneTimeAuthMint((e: any) => ({
...e, ...e,
[masterEdition.oneTimePrintingAuthorizationMint.toBase58()]: [masterEdition.oneTimePrintingAuthorizationMint.toBase58()]: account,
account,
})); }));
} }
} catch { } catch {
@ -755,7 +793,7 @@ const processVaultData = (
a: PublicKeyAndAccount<Buffer>, a: PublicKeyAndAccount<Buffer>,
setSafetyDepositBoxesByVaultAndIndex: any, setSafetyDepositBoxesByVaultAndIndex: any,
setVaults: any, setVaults: any,
) => { ) => {
try { try {
if (a.account.data[0] === VaultKey.SafetyDepositBoxV1) { if (a.account.data[0] === VaultKey.SafetyDepositBoxV1) {
const safetyDeposit = decodeSafetyDeposit(a.account.data); const safetyDeposit = decodeSafetyDeposit(a.account.data);
@ -766,8 +804,7 @@ const processVaultData = (
}; };
setSafetyDepositBoxesByVaultAndIndex((e: any) => ({ setSafetyDepositBoxesByVaultAndIndex((e: any) => ({
...e, ...e,
[safetyDeposit.vault.toBase58() + '-' + safetyDeposit.order]: [safetyDeposit.vault.toBase58() + '-' + safetyDeposit.order]: account,
account,
})); }));
} else if (a.account.data[0] === VaultKey.VaultV1) { } else if (a.account.data[0] === VaultKey.VaultV1) {
const vault = decodeVault(a.account.data); const vault = decodeVault(a.account.data);