diff --git a/src/components/instructions/DexInstruction.js b/src/components/instructions/DexInstruction.js index 086d338..22233b6 100644 --- a/src/components/instructions/DexInstruction.js +++ b/src/components/instructions/DexInstruction.js @@ -21,7 +21,7 @@ const DATA_LABELS = { export default function DexInstruction({ instruction, onOpenAddress }) { const wallet = useWallet(); const [publicKeys] = useWalletPublicKeys(); - const { type, data, market, marketInfo, knownProgramId } = instruction; + const { type, data, market, marketInfo } = instruction; const marketLabel = (marketInfo && @@ -44,7 +44,7 @@ export default function DexInstruction({ instruction, onOpenAddress }) { style={{ fontWeight: 'bold' }} gutterBottom > - {TYPE_LABELS[type]} {!knownProgramId ? '(Unknown DEX program)' : null} + {TYPE_LABELS[type]} - Place an order {!knownProgramId ? '(Unknown DEX program)' : null} + Place an order { - if (!instruction?.data || !instruction?.accounts) { + if ( + !instruction?.data || + !instruction?.accounts || + !instruction?.programIdIndex + ) { return; } // get instruction data const decoded = bs58.decode(instruction.data); - let decodedInstruction; - // try dex instruction decoding - try { - decodedInstruction = decodeInstruction(decoded); - console.log('[' + index + '] Handled as dex instruction'); - return await handleDexInstruction( - connection, - instruction, - accountKeys, - decodedInstruction, - ); - } catch {} + const programId = getAccountByIndex( + [instruction.programIdIndex], + accountKeys, + 0, + ); + if (!programId) { + return null; + } - // try token decoding try { - decodedInstruction = decodeTokenInstruction(decoded); - console.log('[' + index + '] Handled as token instruction'); - return handleTokenInstruction( - publicKey, - instruction.accounts, - decodedInstruction, - accountKeys, - ); - } catch {} - - // try system instruction decoding - try { - const systemInstruction = handleSystemInstruction( - publicKey, - instruction, - accountKeys, - ); - console.log('[' + index + '] Handled as system instruction'); - return systemInstruction; + if (programId.equals(SystemProgram.programId)) { + console.log('[' + index + '] Handled as system instruction'); + return handleSystemInstruction(publicKey, instruction, accountKeys); + } else if (programId.equals(TOKEN_PROGRAM_ID)) { + console.log('[' + index + '] Handled as token instruction'); + let decodedInstruction = decodeTokenInstruction(decoded); + return handleTokenInstruction( + publicKey, + instruction.accounts, + decodedInstruction, + accountKeys, + ); + } else if ( + MARKETS.some( + (market) => market.programId && market.programId.equals(programId), + ) + ) { + console.log('[' + index + '] Handled as dex instruction'); + let decodedInstruction = decodeInstruction(decoded); + return await handleDexInstruction( + connection, + instruction, + accountKeys, + decodedInstruction, + ); + } } catch {} // all decodings failed console.log('[' + index + '] Failed, data: ' + JSON.stringify(decoded)); + return; }; @@ -171,9 +179,6 @@ const handleDexInstruction = async ( data = { ...data, ...newOrderData }; } return { - knownProgramId: MARKETS.some( - ({ programId }) => programIdAddress && programIdAddress.equals(programId), - ), type, data, market,