Parse instructions based on program id

This commit is contained in:
Nishad 2020-09-23 02:13:33 +08:00
parent 18ca87638e
commit 340621918e
4 changed files with 46 additions and 51 deletions

View File

@ -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]}
</Typography>
<LabelValue
label="Market"

View File

@ -5,7 +5,7 @@ import { useWallet } from '../../utils/wallet';
export default function Neworder({ instruction, onOpenAddress }) {
const wallet = useWallet();
const { data, market, marketInfo, knownProgramId } = instruction;
const { data, market, marketInfo } = instruction;
const { side, limitPrice, maxQuantity, orderType, ownerPubkey } = data;
const marketLabel =
@ -26,7 +26,7 @@ export default function Neworder({ instruction, onOpenAddress }) {
style={{ fontWeight: 'bold' }}
gutterBottom
>
Place an order {!knownProgramId ? '(Unknown DEX program)' : null}
Place an order
</Typography>
<LabelValue
label="Market"

View File

@ -293,16 +293,6 @@ function isSafeInstruction(publicKeys, owner, instructions) {
if (!instruction) {
unsafe = true;
} else {
if (
['cancelOrder', 'matchOrders', 'newOrder', 'settleFunds'].includes(
instruction.type,
)
) {
// Verify that the DEX program ID is known
if (!instruction.knownProgramId) {
unsafe = true;
}
}
if (['cancelOrder', 'matchOrders'].includes(instruction.type)) {
// It is always considered safe to cancel orders, match orders
} else if (instruction.type === 'systemCreate') {

View File

@ -1,5 +1,5 @@
import bs58 from 'bs58';
import { Message, SystemInstruction } from '@solana/web3.js';
import { Message, SystemInstruction, SystemProgram } from '@solana/web3.js';
import {
decodeInstruction,
decodeTokenInstructionData,
@ -11,6 +11,7 @@ import {
NEW_ORDER_OPEN_ORDERS_INDEX,
NEW_ORDER_OWNER_INDEX,
} from '@project-serum/serum';
import { TOKEN_PROGRAM_ID } from './tokens/instructions';
const marketCache = {};
let marketCacheConnection = null;
@ -52,51 +53,58 @@ const toInstruction = async (
instruction,
index,
) => {
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,