diff --git a/packages/serum/package.json b/packages/serum/package.json index cf7d920c..25cf85b0 100644 --- a/packages/serum/package.json +++ b/packages/serum/package.json @@ -29,6 +29,7 @@ "@typescript-eslint/eslint-plugin": "^4.6.0", "@typescript-eslint/parser": "^4.6.0", "babel-eslint": "^10.0.3", + "bs58": "^4.0.1", "cross-env": "^7.0.2", "eslint": "^7.6.0", "eslint-config-prettier": "^6.11.0", @@ -46,8 +47,8 @@ "trailingComma": "all" }, "dependencies": { - "@solana/spl-token": "^0.1.6", "@project-serum/anchor": "^0.11.1", + "@solana/spl-token": "^0.1.6", "@solana/web3.js": "^1.21.0", "bn.js": "^5.1.2", "buffer-layout": "^1.2.0" diff --git a/packages/serum/src/instructions.js b/packages/serum/src/instructions.js index 7cae60ec..b056ae33 100644 --- a/packages/serum/src/instructions.js +++ b/packages/serum/src/instructions.js @@ -104,8 +104,17 @@ export function encodeInstruction(instruction) { return b.slice(0, INSTRUCTION_LAYOUT.encode(instruction, b)); } -export function decodeInstruction(message) { - return INSTRUCTION_LAYOUT.decode(message); +/** + * Accepts raw instruction data as a buffer of raw bytes + * eg. from [@solana/web3.js.TransactionInstruction.data](https://solana-labs.github.io/solana-web3.js/classes/TransactionInstruction.html) + * or from decoding the base58 data from [@solana/web3.js.PartiallyDecodedInstruction.data](https://solana-labs.github.io/solana-web3.js/modules.html#PartiallyDecodedInstruction) + * Returns an object that describes the instruction parameters + * + * @param {Uint8Array} instructionData + * @returns {Object} decoded Instruction Object + */ +export function decodeInstruction(instructionData) { + return INSTRUCTION_LAYOUT.decode(instructionData); } export class DexInstructions { diff --git a/packages/serum/src/instructions.test.js b/packages/serum/src/instructions.test.js index db1d9400..df893d7a 100644 --- a/packages/serum/src/instructions.test.js +++ b/packages/serum/src/instructions.test.js @@ -1,5 +1,6 @@ -import { encodeInstruction } from './instructions'; +import { encodeInstruction, decodeInstruction } from './instructions'; import BN from 'bn.js'; +import base58 from 'bs58'; describe('instruction', () => { it('encodes initialize market', () => { @@ -30,4 +31,12 @@ describe('instruction', () => { '0001000000010000000a000000000000000500000000000000020000000000000000000000', ); }); + + it('decodes new order from rawbytes', () => { + const newOrderExpectedJSON = '{"newOrderV3":{"side":"buy","limitPrice":"011d28","maxBaseQuantity":"0122","maxQuoteQuantity":"7e2edb40","selfTradeBehavior":"decrementTake","orderType":"limit","clientId":"00","limit":65535}}' + const newOrderInstData58 = '189VEfQCdeLeDg3Y6qq3iVwwij5GobKfSvh42MPYfkCpGVM4TkjdwRK9FdLswwBvABt5k' + const newOrderInstDataBytes = base58.decode(newOrderInstData58) + const orderObj = decodeInstruction(newOrderInstDataBytes); + expect(JSON.stringify(orderObj)).toEqual(newOrderExpectedJSON); + }); });