Initial commit

This commit is contained in:
Gary Wang 2020-08-06 10:17:24 -07:00 committed by Armani Ferrante
parent 1c948f7a7e
commit 92f86f039a
10 changed files with 12306 additions and 0 deletions

9
.editorconfig Normal file
View File

@ -0,0 +1,9 @@
root = true
[*]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

5
.eslintignore Normal file
View File

@ -0,0 +1,5 @@
build/
dist/
node_modules/
.snapshots/
*.min.js

34
.eslintrc Normal file
View File

@ -0,0 +1,34 @@
{
"parser": "babel-eslint",
"extends": [
"standard",
"standard-react",
"plugin:prettier/recommended",
"prettier/standard",
"prettier/react"
],
"env": {
"node": true,
"jest": true
},
"parserOptions": {
"ecmaVersion": 2020,
"ecmaFeatures": {
"legacyDecorators": true,
"jsx": true
}
},
"settings": {
"react": {
"version": "16"
}
},
"rules": {
"space-before-function-paren": 0,
"react/prop-types": 0,
"react/jsx-handler-names": 0,
"react/jsx-fragments": 0,
"react/no-unused-prop-types": 0,
"import/export": 0
}
}

21
.gitignore vendored Normal file
View File

@ -0,0 +1,21 @@
# dependencies
node_modules
# builds
build
dist
.rpt2_cache
# misc
.DS_Store
.env
.env.local
.env.development.local
.env.test.local
.env.production.local
*~
.idea
npm-debug.log*
yarn-debug.log*
yarn-error.log*

5
.travis.yml Normal file
View File

@ -0,0 +1,5 @@
language: node_js
node_js:
- 12
- 10
dist: bionic

64
package.json Normal file
View File

@ -0,0 +1,64 @@
{
"name": "@project-serum/serum",
"version": "0.1.0",
"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",
"source": "src/index.js",
"engines": {
"node": ">=10"
},
"scripts": {
"build": "microbundle",
"start": "microbundle watch",
"prepare": "run-s build",
"test": "run-s test:unit test:lint test:build",
"test:build": "run-s build",
"test:lint": "eslint .",
"test:unit": "cross-env CI=1 react-scripts test --env=jsdom",
"test:watch": "react-scripts test --env=jsdom",
"predeploy": "cd example && yarn install && yarn run build",
"deploy": "gh-pages -d example/build"
},
"devDependencies": {
"@solana/web3.js": "^0.64.0",
"babel-eslint": "^10.0.3",
"cross-env": "^7.0.2",
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.7.0",
"eslint-config-standard": "^14.1.0",
"eslint-config-standard-react": "^9.2.0",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-node": "^11.0.0",
"eslint-plugin-prettier": "^3.1.1",
"eslint-plugin-promise": "^4.2.1",
"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"
],
"jest": {
"transformIgnorePatterns": [
"^.+\\.cjs$"
]
},
"prettier": {
"singleQuote": true,
"trailingComma": "all"
},
"dependencies": {
"@solana/web3.js": "^0.64.0",
"buffer-layout": "^1.2.0"
}
}

54
src/index.js Normal file
View File

@ -0,0 +1,54 @@
import { u8, nu64, u32, seq, blob, struct, union, offset } from 'buffer-layout';
import { zeros, publicKeyLayout } from './layout';
const SLAB_HEADER_LAYOUT = struct(
[
u32('bumpIndex'),
zeros(4),
u32('freeListLen'),
zeros(4),
u32('freeListHead'),
u32('rootNode'),
u32('leafCount'),
zeros(4),
],
'header',
);
const SLAB_NODE_LAYOUT = union(u32('tag'), blob(60), 'node');
SLAB_NODE_LAYOUT.addVariant(0, struct([]), 'uninitialized');
SLAB_NODE_LAYOUT.addVariant(
1,
struct([u32('prefixLen'), blob(16, 'key'), seq(u32(), 2, 'children')]),
'innerNode',
);
SLAB_NODE_LAYOUT.addVariant(
2,
struct([
u8('ownerSlot'),
blob(3),
blob(16, 'key'),
publicKeyLayout('owner'),
nu64('quantity'),
]),
'leafNode',
);
SLAB_NODE_LAYOUT.addVariant(3, struct([u32('next')]), 'freeNode');
SLAB_NODE_LAYOUT.addVariant(4, struct([]), 'lastFreeNode');
const SLAB_LAYOUT = struct([
SLAB_HEADER_LAYOUT,
seq(
SLAB_NODE_LAYOUT,
offset(
SLAB_HEADER_LAYOUT.layoutFor('bumpIndex'),
SLAB_HEADER_LAYOUT.offsetOf('bumpIndex') - SLAB_HEADER_LAYOUT.span,
),
'nodes',
),
]);
export function parseSlab(slab) {
const { header, nodes } = SLAB_LAYOUT.decode(slab);
return { header, nodes };
}

15
src/index.test.js Normal file
View File

@ -0,0 +1,15 @@
import { parseSlab } from './index';
describe('slab', () => {
it('parses', () => {
const slab = parseSlab(
Buffer.from(
'0900000000000000020000000000000008000000000000000400000000000000010000001e00000000000040952fe4da5c1f3c860200000004000000030000000d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d7b000000000000000200000002000000000000a0ca17726dae0f1e4301000000111111111111111111111111111111111111111111111111111111111111111141010000000000000200000001000000d20a3f4eeee073c3f60fe98e010000000d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d7b00000000000000020000000300000000000040952fe4da5c1f3c8602000000131313131313131313131313131313131313131313131313131313131313131340e2010000000000010000001f0000000500000000000000000000000000000005000000060000000d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d7b00000000000000020000000400000004000000000000000000000000000000171717171717171717171717171717171717171717171717171717171717171702000000000000000100000020000000000000a0ca17726dae0f1e430100000001000000020000000d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d7b000000000000000400000000000000040000000000000000000000000000001717171717171717171717171717171717171717171717171717171717171717020000000000000003000000070000000500000000000000000000000000000017171717171717171717171717171717171717171717171717171717171717170200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
'hex',
),
);
console.log(slab);
console.log(slab.nodes[1]);
console.log(slab.nodes[1].leafNode.owner.toBase58());
});
});

34
src/layout.js Normal file
View File

@ -0,0 +1,34 @@
import { Blob } from 'buffer-layout';
import { PublicKey } from '@solana/web3.js';
class Zeros extends Blob {
decode(b, offset) {
const slice = super.decode(b, offset);
if (!slice.every((v) => v === 0)) {
throw new Error('nonzero padding bytes');
}
return slice;
}
}
export function zeros(length) {
return new Zeros(length);
}
class PublicKeyLayout extends 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);
}
}
export function publicKeyLayout(property) {
return new PublicKeyLayout(property);
}

12065
yarn.lock Normal file

File diff suppressed because it is too large Load Diff