Parse and display serum dex v3 transactions (#114)

* Parse dex3 transactions

* Update serum-ts dependency
This commit is contained in:
Sebastian Conybeare 2021-02-28 05:47:53 +08:00 committed by GitHub
parent 469a3955d2
commit 658f7640ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 64 additions and 22 deletions

View File

@ -6,7 +6,7 @@
"@ledgerhq/hw-transport-webusb": "^5.34.0",
"@material-ui/core": "^4.11.2",
"@material-ui/icons": "^4.11.2",
"@project-serum/serum": "^0.13.23",
"@project-serum/serum": "^0.13.24",
"@solana/web3.js": "^0.87.2",
"@testing-library/jest-dom": "^5.11.6",
"@testing-library/react": "^11.2.2",

View File

@ -3,11 +3,9 @@ import Typography from '@material-ui/core/Typography';
import LabelValue from './LabelValue';
import { useWallet } from '../../utils/wallet';
export default function Neworder({ instruction, onOpenAddress }) {
export default function Neworder({ instruction, onOpenAddress, v3=false }) {
const wallet = useWallet();
const { data, market, marketInfo } = instruction;
const { side, limitPrice, maxQuantity, orderType, ownerPubkey } = data;
const marketLabel =
(marketInfo &&
marketInfo?.name + (marketInfo?.deprecated ? ' (deprecated)' : '')) ||
@ -19,6 +17,8 @@ export default function Neworder({ instruction, onOpenAddress }) {
return isOwner ? 'This wallet' : address?.toBase58() || 'Unknown';
};
const { side, limitPrice, orderType, ownerPubkey } = data;
const maxQuantity = v3 ? data.maxBaseQuantity : data.maxQuantity;
return (
<>
<Typography

View File

@ -330,7 +330,7 @@ function isSafeInstruction(publicKeys, owner, txInstructions) {
} else {
if (instruction.type === 'raydium') {
// Whitelist raydium for now.
} else if (['cancelOrder', 'matchOrders'].includes(instruction.type)) {
} else if (['cancelOrder', 'matchOrders', 'cancelOrderV3'].includes(instruction.type)) {
// It is always considered safe to cancel orders, match orders
} else if (instruction.type === 'systemCreate') {
let { newAccountPubkey } = instruction.data;
@ -339,7 +339,7 @@ function isSafeInstruction(publicKeys, owner, txInstructions) {
} else {
accountStates[newAccountPubkey.toBase58()] = states.CREATED;
}
} else if (instruction.type === 'newOrder') {
} else if (['newOrder', 'newOrderV3'].includes(instruction.type)) {
// New order instructions are safe if the owner is this wallet
let { openOrdersPubkey, ownerPubkey } = instruction.data;
if (ownerPubkey && owner.equals(ownerPubkey)) {
@ -465,6 +465,7 @@ function ApproveSignatureForm({
const getContent = (instruction) => {
switch (instruction?.type) {
case 'cancelOrder':
case 'cancelOrderV2':
case 'matchOrders':
case 'settleFunds':
return (
@ -496,6 +497,10 @@ function ApproveSignatureForm({
return (
<NewOrder instruction={instruction} onOpenAddress={onOpenAddress} />
);
case 'newOrderV3':
return (
<NewOrder instruction={instruction} onOpenAddress={onOpenAddress} v3={true} />
);
default:
return <UnknownInstruction instruction={instruction} />;
}

View File

@ -4,7 +4,6 @@ import {
SYSVAR_RENT_PUBKEY,
TransactionInstruction,
} from '@solana/web3.js';
import { publicKeyLayout } from '@project-serum/serum/lib/layout';
export const TOKEN_PROGRAM_ID = new PublicKey(
'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA',
@ -152,6 +151,25 @@ export function memoInstruction(memo) {
});
}
class PublicKeyLayout extends BufferLayout.Blob {
constructor(property) {
super(32, property);
}
decode(b, offset) {
return new PublicKey(super.decode(b, offset));
}
encode(src, b, offset) {
return super.encode(src.toBuffer(), b, offset);
}
}
function publicKeyLayout(property) {
return new PublicKeyLayout(property);
}
export const OWNER_VALIDATION_PROGRAM_ID = new PublicKey(
'4MNPdKu9wFMvEeZBMt3Eipfs5ovVWTJb31pEXDJAAxX5',
);

View File

@ -10,6 +10,8 @@ import {
SETTLE_FUNDS_QUOTE_WALLET_INDEX,
NEW_ORDER_OPEN_ORDERS_INDEX,
NEW_ORDER_OWNER_INDEX,
NEW_ORDER_V3_OPEN_ORDERS_INDEX,
NEW_ORDER_V3_OWNER_INDEX,
} from '@project-serum/serum';
import { PublicKey } from '@solana/web3.js';
import { TOKEN_PROGRAM_ID } from './tokens/instructions';
@ -237,6 +239,9 @@ const handleDexInstruction = async (
} else if (type === 'newOrder') {
const newOrderData = getNewOrderData(accounts, accountKeys);
data = { ...data, ...newOrderData };
} else if (type === 'newOrderV3') {
const newOrderData = getNewOrderV3Data(accounts, accountKeys);
data = { ...data, ...newOrderData };
}
return {
type,
@ -383,6 +388,20 @@ const getNewOrderData = (accounts, accountKeys) => {
return { openOrdersPubkey, ownerPubkey };
};
const getNewOrderV3Data = (accounts, accountKeys) => {
const openOrdersPubkey = getAccountByIndex(
accounts,
accountKeys,
NEW_ORDER_V3_OPEN_ORDERS_INDEX,
);
const ownerPubkey = getAccountByIndex(
accounts,
accountKeys,
NEW_ORDER_V3_OWNER_INDEX,
);
return { openOrdersPubkey, ownerPubkey };
}
const getSettleFundsData = (accounts, accountKeys) => {
const basePubkey = getAccountByIndex(
accounts,

View File

@ -1861,12 +1861,12 @@
schema-utils "^2.6.5"
source-map "^0.7.3"
"@project-serum/serum@^0.13.23":
version "0.13.23"
resolved "https://registry.yarnpkg.com/@project-serum/serum/-/serum-0.13.23.tgz#70a4206e3fe926ef2f6e41da1b562399a20b3c9d"
integrity sha512-Atg2sSdQLEp4JIg4jp3vy1T9u9O5wObVTcGe0hX7e7zDmDc4zxBbTJXxdvMRxaz4pfrETyithX//zIMyIHMA8Q==
"@project-serum/serum@^0.13.24":
version "0.13.24"
resolved "https://registry.yarnpkg.com/@project-serum/serum/-/serum-0.13.24.tgz#42ab28052924c4b754ed078790a192ae23fffab9"
integrity sha512-ux2MuO2kGGUV/QYBVx+e3roM/zATI6Fi2ThVaEo4aBdQKosbuNA60uAfSnYK9Du0kz7UjGoZHv2FnkCq8web3A==
dependencies:
"@solana/web3.js" "0.86.1"
"@solana/web3.js" "^0.90.0"
bn.js "^5.1.2"
buffer-layout "^1.2.0"
@ -1917,15 +1917,15 @@
dependencies:
"@sinonjs/commons" "^1.7.0"
"@solana/web3.js@0.86.1":
version "0.86.1"
resolved "https://registry.yarnpkg.com/@solana/web3.js/-/web3.js-0.86.1.tgz#034a2cef742569f74dfc9960dfbcabc92e674b08"
integrity sha512-9mjWs17ym7PIm7bHA37wnnYyD7rIVHwkx1RI6BzGhMO5h8E+HlZM8ISLgOx+NItg8XRCfFhlrVgJTzK4om1s0g==
"@solana/web3.js@^0.87.2":
version "0.87.2"
resolved "https://registry.yarnpkg.com/@solana/web3.js/-/web3.js-0.87.2.tgz#92c8d344695c6113d4e0eb3339117fbc6b22d0d2"
integrity sha512-BtrOGfg7zUud/zLL/BxXV3E84qpXK1HknqncNQDNr8JlJ4tGc+Xsip5DzSMP/fNUJgbAWblHqWWJxFdN2mtO6A==
dependencies:
"@babel/runtime" "^7.3.1"
bn.js "^5.0.0"
bs58 "^4.0.1"
buffer "^5.4.3"
buffer "^6.0.1"
buffer-layout "^1.2.0"
crypto-hash "^1.2.2"
esdoc-inject-style-plugin "^1.0.0"
@ -1940,10 +1940,10 @@
tweetnacl "^1.0.0"
ws "^7.0.0"
"@solana/web3.js@^0.87.2":
version "0.87.2"
resolved "https://registry.yarnpkg.com/@solana/web3.js/-/web3.js-0.87.2.tgz#92c8d344695c6113d4e0eb3339117fbc6b22d0d2"
integrity sha512-BtrOGfg7zUud/zLL/BxXV3E84qpXK1HknqncNQDNr8JlJ4tGc+Xsip5DzSMP/fNUJgbAWblHqWWJxFdN2mtO6A==
"@solana/web3.js@^0.90.0":
version "0.90.5"
resolved "https://registry.yarnpkg.com/@solana/web3.js/-/web3.js-0.90.5.tgz#5be7d78a19f0b5e01bf82c52e3cbf0bb72a38cfd"
integrity sha512-2elnwfIkdtrUjLdTBKu7Rc3mTv95k2cn+JtNtmO2fY/PFRZm7mnGS/gk/wWzGCpBVLiqz2o6o21oONqyDtLG/w==
dependencies:
"@babel/runtime" "^7.3.1"
bn.js "^5.0.0"
@ -3692,7 +3692,7 @@ buffer@^4.3.0:
ieee754 "^1.1.4"
isarray "^1.0.0"
buffer@^5.0.5, buffer@^5.4.3, buffer@^5.5.0, buffer@^5.6.0:
buffer@^5.0.5, buffer@^5.5.0, buffer@^5.6.0:
version "5.6.0"
resolved "https://registry.yarnpkg.com/buffer/-/buffer-5.6.0.tgz#a31749dc7d81d84db08abf937b6b8c4033f62786"
integrity sha512-/gDYp/UtU0eA1ys8bOs9J6a+E/KWIY+DZ+Q2WESNUA0jFRsJOc0SNUO6xJ5SGA1xueg3NL65W6s+NY5l9cunuw==