From 20a776ef649f2612f4cd15eab8b2ec6fc0758303 Mon Sep 17 00:00:00 2001 From: gotjoshua Date: Thu, 30 Sep 2021 09:00:33 +0100 Subject: [PATCH 1/4] #172 WIP add test for decode --- packages/serum/package.json | 3 ++- packages/serum/src/instructions.test.js | 15 ++++++++++++++- yarn.lock | 5 +++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/packages/serum/package.json b/packages/serum/package.json index cf7d920c..20db1caf 100644 --- a/packages/serum/package.json +++ b/packages/serum/package.json @@ -46,9 +46,10 @@ "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", + "basex-encoder": "^0.0.10", "bn.js": "^5.1.2", "buffer-layout": "^1.2.0" }, diff --git a/packages/serum/src/instructions.test.js b/packages/serum/src/instructions.test.js index db1d9400..dec2748b 100644 --- a/packages/serum/src/instructions.test.js +++ b/packages/serum/src/instructions.test.js @@ -1,5 +1,10 @@ -import { encodeInstruction } from './instructions'; +import { encodeInstruction, decodeInstruction } from './instructions'; import BN from 'bn.js'; +import { encoder } from "basex-encoder"; + +const base58 = encoder( + "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" +); describe('instruction', () => { it('encodes initialize market', () => { @@ -30,4 +35,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.decodeToBuffer(newOrderInstData58) + const orderObj = decodeInstruction(newOrderInstDataBytes); + expect(JSON.stringify(orderObj)).toEqual(newOrderExpectedJSON); + }); }); diff --git a/yarn.lock b/yarn.lock index 7b775b59..7c8d6e60 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2515,6 +2515,11 @@ base@^0.11.1: mixin-deep "^1.2.0" pascalcase "^0.1.1" +basex-encoder@^0.0.10: + version "0.0.10" + resolved "https://registry.yarnpkg.com/basex-encoder/-/basex-encoder-0.0.10.tgz#3c89e54a49650133a6920cf4a57506032cd86ab4" + integrity sha512-7C/hMsE+cWNTxy+N1heJ5e540eIHIUpF5jTRz9sdSRvCYDDvaSNZ1/tDUqm9fwDxhAZu6jkOexxlEvyoTzaRMg== + bcrypt-pbkdf@^1.0.0: version "1.0.2" resolved "https://registry.yarnpkg.com/bcrypt-pbkdf/-/bcrypt-pbkdf-1.0.2.tgz#a4301d389b6a43f9b67ff3ca11a3f6637e360e9e" From a628aada8e478d69d59d06646d55c2d6a559363a Mon Sep 17 00:00:00 2001 From: gotjoshua Date: Thu, 30 Sep 2021 09:39:54 +0100 Subject: [PATCH 2/4] #172 WIP decode jsdocs --- packages/serum/src/instructions.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) 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 { From 92e9c5f5608c083989a70f751783cca5596770a2 Mon Sep 17 00:00:00 2001 From: gotjoshua Date: Thu, 30 Sep 2021 10:06:24 +0100 Subject: [PATCH 3/4] #172 use bs58 lib --- packages/serum/package.json | 2 +- packages/serum/src/instructions.test.js | 8 ++------ yarn.lock | 5 ----- 3 files changed, 3 insertions(+), 12 deletions(-) diff --git a/packages/serum/package.json b/packages/serum/package.json index 20db1caf..9e53825d 100644 --- a/packages/serum/package.json +++ b/packages/serum/package.json @@ -49,8 +49,8 @@ "@project-serum/anchor": "^0.11.1", "@solana/spl-token": "^0.1.6", "@solana/web3.js": "^1.21.0", - "basex-encoder": "^0.0.10", "bn.js": "^5.1.2", + "bs58": "^4.0.1", "buffer-layout": "^1.2.0" }, "browserslist": [ diff --git a/packages/serum/src/instructions.test.js b/packages/serum/src/instructions.test.js index dec2748b..df893d7a 100644 --- a/packages/serum/src/instructions.test.js +++ b/packages/serum/src/instructions.test.js @@ -1,10 +1,6 @@ import { encodeInstruction, decodeInstruction } from './instructions'; import BN from 'bn.js'; -import { encoder } from "basex-encoder"; - -const base58 = encoder( - "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" -); +import base58 from 'bs58'; describe('instruction', () => { it('encodes initialize market', () => { @@ -39,7 +35,7 @@ describe('instruction', () => { 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.decodeToBuffer(newOrderInstData58) + const newOrderInstDataBytes = base58.decode(newOrderInstData58) const orderObj = decodeInstruction(newOrderInstDataBytes); expect(JSON.stringify(orderObj)).toEqual(newOrderExpectedJSON); }); diff --git a/yarn.lock b/yarn.lock index 7c8d6e60..7b775b59 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2515,11 +2515,6 @@ base@^0.11.1: mixin-deep "^1.2.0" pascalcase "^0.1.1" -basex-encoder@^0.0.10: - version "0.0.10" - resolved "https://registry.yarnpkg.com/basex-encoder/-/basex-encoder-0.0.10.tgz#3c89e54a49650133a6920cf4a57506032cd86ab4" - integrity sha512-7C/hMsE+cWNTxy+N1heJ5e540eIHIUpF5jTRz9sdSRvCYDDvaSNZ1/tDUqm9fwDxhAZu6jkOexxlEvyoTzaRMg== - bcrypt-pbkdf@^1.0.0: version "1.0.2" resolved "https://registry.yarnpkg.com/bcrypt-pbkdf/-/bcrypt-pbkdf-1.0.2.tgz#a4301d389b6a43f9b67ff3ca11a3f6637e360e9e" From e7eb4ce07466fd55f439f2ca38dafbc3c7338891 Mon Sep 17 00:00:00 2001 From: gotjoshua Date: Thu, 30 Sep 2021 17:39:10 +0100 Subject: [PATCH 4/4] move bs58 to devDeps --- packages/serum/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/serum/package.json b/packages/serum/package.json index 9e53825d..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", @@ -50,7 +51,6 @@ "@solana/spl-token": "^0.1.6", "@solana/web3.js": "^1.21.0", "bn.js": "^5.1.2", - "bs58": "^4.0.1", "buffer-layout": "^1.2.0" }, "browserslist": [