Skip to content

Commit

Permalink
Fix cjs build
Browse files Browse the repository at this point in the history
  • Loading branch information
garywang authored and armaniferrante committed Nov 15, 2020
1 parent dd9cd7b commit 41adbc9
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 657 deletions.
12 changes: 12 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"presets": [
[ "@babel/preset-env", { "modules": "auto" } ]
],
"plugins": [
"@babel/transform-runtime",
["@babel/plugin-proposal-class-properties", { "loose": true }]
],
"ignore": [
"**/*.test.js"
]
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ node_modules
# builds
build
dist
lib
.rpt2_cache

# misc
Expand Down
24 changes: 16 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@
"description": "Library for interacting with the serum dex",
"license": "MIT",
"repository": "serum-foundation/serum-js",
"main": "dist/index.js",
"module": "dist/index.module.js",
"unpkg": "dist/index.umd.js",
"main": "lib/index.js",
"source": "src/index.js",
"engines": {
"node": ">=10"
},
"scripts": {
"build": "microbundle",
"start": "microbundle watch",
"build": "yarn babel src --out-dir lib",
"start": "yarn babel src --out-dir lib --watch",
"prepare": "run-s build",
"test": "run-s test:unit test:lint test:build",
"test:build": "run-s build",
Expand All @@ -24,6 +22,11 @@
"deploy": "gh-pages -d example/build"
},
"devDependencies": {
"@babel/cli": "^7.10.5",
"@babel/core": "^7.11.1",
"@babel/plugin-proposal-class-properties": "^7.10.4",
"@babel/plugin-transform-runtime": "^7.11.0",
"@babel/preset-env": "^7.11.0",
"@solana/web3.js": "^0.64.0",
"babel-eslint": "^10.0.3",
"cross-env": "^7.0.2",
Expand All @@ -38,15 +41,14 @@
"eslint-plugin-react": "^7.17.0",
"eslint-plugin-standard": "^4.0.1",
"gh-pages": "^2.2.0",
"microbundle": "^0.12.3",
"npm-run-all": "^4.1.5",
"prettier": "^2.0.4",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-scripts": "^3.4.1"
},
"files": [
"dist"
"lib"
],
"jest": {
"transformIgnorePatterns": [
Expand All @@ -61,5 +63,11 @@
"@solana/web3.js": "^0.64.0",
"bn.js": "^5.1.2",
"buffer-layout": "^1.2.0"
}
},
"browserslist": [
">0.2%",
"not dead",
"not op_mini all",
"maintained node versions"
]
}
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { Slab } from './slab';
export { MarketState, Orderbook } from './market-state';
26 changes: 1 addition & 25 deletions src/instructions.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,11 @@
import { blob, Layout, struct, u16, u32, u8, union } from 'buffer-layout';
import { u64 } from './layout';
import { u64, VersionedLayout } from './layout';
import { PublicKey } from '@solana/web3.js';

export const DEX_PROGRAM_ID = new PublicKey(
'6iM2JjaPVViB2u82aVjf3ZfHDNHQ4G635XgnvgN6CNhY',
);

class VersionedLayout extends Layout {
constructor(version, inner, property) {
super(inner.span > 0 ? inner.span + 1 : inner.span, property);
this.version = version;
this.inner = inner;
}

decode(b, offset = 0) {
if (b.readUInt8(offset) !== this._version) {
throw new Error('invalid version');
}
return this.inner.decode(b, offset + 1);
}

encode(src, b, offset = 0) {
b.writeUInt8(this.version, offset);
return 1 + this.inner.encode(src, b, offset + 1);
}

getSpan(b, offset = 0) {
return 1 + this.inner.getSpan(b, offset + 1);
}
}

export const INSTRUCTION_LAYOUT = new VersionedLayout(
0,
union(u32('instruction')),
Expand Down
32 changes: 32 additions & 0 deletions src/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,41 @@ export class WideBits extends Layout {
}
}

export class VersionedLayout extends Layout {
constructor(version, inner, property) {
super(inner.span > 0 ? inner.span + 1 : inner.span, property);
this.version = version;
this.inner = inner;
}

decode(b, offset = 0) {
if (b.readUInt8(offset) !== this._version) {
throw new Error('invalid version');
}
return this.inner.decode(b, offset + 1);
}

encode(src, b, offset = 0) {
b.writeUInt8(this.version, offset);
return 1 + this.inner.encode(src, b, offset + 1);
}

getSpan(b, offset = 0) {
return 1 + this.inner.getSpan(b, offset + 1);
}
}

export function setLayoutDecoder(layout, decoder) {
const originalDecode = layout.decode;
layout.decode = function decode(b, offset = 0) {
return decoder(originalDecode.call(this, b, offset));
};
}

export function setLayoutEncoder(layout, encoder) {
const originalEncode = layout.encode;
layout.encode = function encode(src, b, offset) {
return originalEncode(encoder(src), b, offset);
};
return layout;
}
Loading

0 comments on commit 41adbc9

Please sign in to comment.