Rebase serum-js
This commit is contained in:
parent
b6c49b80a6
commit
ff6688a94e
|
@ -17,7 +17,7 @@
|
|||
},
|
||||
"rules": {
|
||||
"no-constant-condition": ["error", { "checkLoops": false }],
|
||||
"@typescript-eslint/explicit-module-boundary-types": "off",
|
||||
"no-empty-function": "off",
|
||||
"@typescript-eslint/ban-ts-comment": "off",
|
||||
"@typescript-eslint/no-explicit-any": "off"
|
||||
}
|
||||
|
|
|
@ -1,22 +1,10 @@
|
|||
# dependencies
|
||||
node_modules
|
||||
|
||||
# builds
|
||||
build
|
||||
dist
|
||||
lib
|
||||
.rpt2_cache
|
||||
|
||||
# misc
|
||||
node_modules/
|
||||
build/
|
||||
dist/
|
||||
lib/
|
||||
.DS_Store
|
||||
.env
|
||||
.env.local
|
||||
.env.development.local
|
||||
.env.test.local
|
||||
.env.production.local
|
||||
*~
|
||||
.idea
|
||||
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
language: node_js
|
||||
node_js:
|
||||
- 12
|
||||
- 10
|
||||
node_js: 12
|
||||
dist: bionic
|
||||
cache: yarn
|
||||
before_script:
|
||||
- yarn build
|
||||
|
|
155
README.md
155
README.md
|
@ -1,98 +1,87 @@
|
|||
[![npm (scoped)](https://img.shields.io/npm/v/@project-serum/serum)](https://www.npmjs.com/package/@project-serum/serum)
|
||||
[![Build Status](https://travis-ci.com/project-serum/serum-js.svg?branch=master)](https://travis-ci.com/project-serum/serum-js)
|
||||
<div align="center">
|
||||
<img height="170" src="http://github.com/project-serum/awesome-serum/blob/master/logo-serum.png?raw=true" />
|
||||
|
||||
# Serum JS Client Library
|
||||
<h1>serum-ts</h1>
|
||||
|
||||
JavaScript client library for interacting with the Project Serum DEX.
|
||||
<p>
|
||||
<strong>Project Serum Monorepo</strong>
|
||||
</p>
|
||||
|
||||
## Installation
|
||||
<p>
|
||||
<a href="https://travis-ci.com/project-serum/serum-ts"><img alt="Build Status" src="https://travis-ci.com/project-serum/serum-ts.svg?branch=master" /></a>
|
||||
<a href="https://discord.com/channels/739225212658122886"><img alt="Discord Chat" src="https://img.shields.io/discord/739225212658122886?color=blueviolet" /></a>
|
||||
<a href="https://opensource.org/licenses/Apache-2.0"><img alt="License" src="https://img.shields.io/github/license/project-serum/serum-dex?color=blue" /></a>
|
||||
</p>
|
||||
|
||||
Using npm:
|
||||
<h4>
|
||||
<a href="https://projectserum.com/">Website</a>
|
||||
<span> | </span>
|
||||
<a href="https://serum-academy.com/en/">Academy</a>
|
||||
<span> | </span>
|
||||
<a href="https://github.com/project-serum/awesome-serum">Awesome</a>
|
||||
<span> | </span>
|
||||
<a href="https://dex.projectserum.com/#/">DEX</a>
|
||||
<span> | </span>
|
||||
<a href="https://github.com/project-serum/serum-dex">Rust</a>
|
||||
</h4>
|
||||
</div>
|
||||
|
||||
## Packages
|
||||
|
||||
| Package | Version | Description |
|
||||
| --------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------- |
|
||||
| [`@project-serum/borsh`](/packages/borsh) | [![npm](https://img.shields.io/npm/v/@project-serum/borsh.svg)](https://www.npmjs.com/package/@project-serum/borsh) | Borsh serialization primitives |
|
||||
| [`@project-serum/common`](/packages/common) | [![npm](https://img.shields.io/npm/v/@project-serum/common.svg)](https://www.npmjs.com/package/@project-serum/common) | Common utilities |
|
||||
| [`@project-serum/serum`](/packages/serum) | [![npm](https://img.shields.io/npm/v/@project-serum/tokens.svg)](https://www.npmjs.com/package/@project-serum/tserum) | Library for interacting with the Serum DEX |
|
||||
| [`@project-serum/lockup`](/packages/lockup) | [![npm](https://img.shields.io/npm/v/@project-serum/lockup.svg)](https://www.npmjs.com/package/@project-serum/lockup) | Client for interacting with the Lockup Program |
|
||||
| [`@project-serum/pool`](/packages/pool) | [![npm](https://img.shields.io/npm/v/@project-serum/pool.svg)](https://www.npmjs.com/package/@project-serum/pool) | Client for interacting with Pools |
|
||||
| [`@project-serum/registry`](/packages/registry) | [![npm](https://img.shields.io/npm/v/@project-serum/registry.svg)](https://www.npmjs.com/package/@project-serum/registry) | Client for interacting with the Registry Program |
|
||||
| [`@project-serum/swap`](/packages/swap) | [![npm](https://img.shields.io/npm/v/@project-serum/swap.svg)](https://www.npmjs.com/package/@project-serum/swap) | Client for interacting witht the Swap Program |
|
||||
| [`@project-serum/tokens`](/packages/tokens) | [![npm](https://img.shields.io/npm/v/@project-serum/tokens.svg)](https://www.npmjs.com/package/@project-serum/tokens) | Solana token addresses |
|
||||
|
||||
## Contributing
|
||||
|
||||
### Installing
|
||||
|
||||
To get started first install the required build tools:
|
||||
|
||||
```
|
||||
npm install @solana/web3.js @project-serum/serum
|
||||
npm install -g lerna
|
||||
npm install -g yarn
|
||||
```
|
||||
|
||||
Using yarn:
|
||||
Then bootstrap the workspace:
|
||||
|
||||
```
|
||||
yarn add @solana/web3.js @project-serum/serum
|
||||
yarn
|
||||
```
|
||||
|
||||
## Usage
|
||||
### Building
|
||||
|
||||
```js
|
||||
import { Account, Connection, PublicKey } from '@solana/web3.js';
|
||||
import { Market } from '@project-serum/serum';
|
||||
To build the workspace:
|
||||
|
||||
let connection = new Connection('https://testnet.solana.com');
|
||||
let marketAddress = new PublicKey('...');
|
||||
let market = await Market.load(connection, marketAddress);
|
||||
|
||||
// Fetching orderbooks
|
||||
let bids = await market.loadBids(connection);
|
||||
let asks = await market.loadAsks(connection);
|
||||
// L2 orderbook data
|
||||
for (let [price, size] of bids.getL2(20)) {
|
||||
console.log(price, size);
|
||||
}
|
||||
// Full orderbook data
|
||||
for (let order of asks) {
|
||||
console.log(
|
||||
order.orderId,
|
||||
order.price,
|
||||
order.size,
|
||||
order.side, // 'buy' or 'sell'
|
||||
);
|
||||
}
|
||||
|
||||
// Placing orders
|
||||
let owner = new Account('...');
|
||||
let payer = new PublicKey('...'); // spl-token account
|
||||
await market.placeOrder(connection, {
|
||||
owner,
|
||||
payer,
|
||||
side: 'buy', // 'buy' or 'sell'
|
||||
price: 123.45,
|
||||
size: 17.0,
|
||||
orderType: 'limit', // 'limit', 'ioc', 'postOnly'
|
||||
});
|
||||
|
||||
// Retrieving open orders by owner
|
||||
let myOrders = await market.loadOrdersForOwner(connection, owner.publicKey);
|
||||
|
||||
// Cancelling orders
|
||||
for (let order of myOrders) {
|
||||
await market.cancelOrder(connection, owner, order);
|
||||
}
|
||||
|
||||
// Retrieving fills
|
||||
for (let fill of await market.loadFills(connection)) {
|
||||
console.log(
|
||||
fill.orderId,
|
||||
fill.price,
|
||||
fill.size,
|
||||
fill.side,
|
||||
);
|
||||
}
|
||||
|
||||
// Settle funds
|
||||
for (let openOrders of await market.findOpenOrdersAccountsForOwner(
|
||||
connection,
|
||||
owner.publicKey,
|
||||
)) {
|
||||
if (openOrders.baseTokenFree > 0 || openOrders.quoteTokenFree > 0) {
|
||||
// spl-token accounts to which to send the proceeds from trades
|
||||
let baseTokenAccount = new PublicKey('...');
|
||||
let quoteTokenAccount = new PublicKey('...');
|
||||
|
||||
await market.settleFunds(
|
||||
connection,
|
||||
owner,
|
||||
openOrders,
|
||||
baseTokenAccount,
|
||||
quoteTokenAccount,
|
||||
);
|
||||
}
|
||||
}
|
||||
```
|
||||
yarn build
|
||||
```
|
||||
|
||||
### Testing
|
||||
|
||||
To run all tests:
|
||||
|
||||
```
|
||||
yarn test
|
||||
```
|
||||
|
||||
### Linting
|
||||
|
||||
To lint:
|
||||
|
||||
```
|
||||
yarn lint
|
||||
```
|
||||
|
||||
To apply lint fixes:
|
||||
|
||||
```
|
||||
yarn lint:fix
|
||||
```
|
||||
|
|
|
@ -1,9 +0,0 @@
|
|||
root = true
|
||||
|
||||
[*]
|
||||
charset = utf-8
|
||||
indent_style = space
|
||||
indent_size = 2
|
||||
end_of_line = lf
|
||||
insert_final_newline = true
|
||||
trim_trailing_whitespace = true
|
|
@ -1,10 +0,0 @@
|
|||
node_modules/
|
||||
build/
|
||||
dist/
|
||||
lib/
|
||||
.DS_Store
|
||||
*~
|
||||
.idea
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
|
@ -1,6 +0,0 @@
|
|||
language: node_js
|
||||
node_js: 12
|
||||
dist: bionic
|
||||
cache: yarn
|
||||
before_script:
|
||||
- yarn build
|
|
@ -1,86 +0,0 @@
|
|||
<div align="center">
|
||||
<img height="170" src="http://github.com/project-serum/awesome-serum/blob/master/logo-serum.png?raw=true" />
|
||||
|
||||
<h1>serum-ts</h1>
|
||||
|
||||
<p>
|
||||
<strong>Project Serum Monorepo</strong>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<a href="https://travis-ci.com/project-serum/serum-ts"><img alt="Build Status" src="https://travis-ci.com/project-serum/serum-ts.svg?branch=master" /></a>
|
||||
<a href="https://discord.com/channels/739225212658122886"><img alt="Discord Chat" src="https://img.shields.io/discord/739225212658122886?color=blueviolet" /></a>
|
||||
<a href="https://opensource.org/licenses/Apache-2.0"><img alt="License" src="https://img.shields.io/github/license/project-serum/serum-dex?color=blue" /></a>
|
||||
</p>
|
||||
|
||||
<h4>
|
||||
<a href="https://projectserum.com/">Website</a>
|
||||
<span> | </span>
|
||||
<a href="https://serum-academy.com/en/">Academy</a>
|
||||
<span> | </span>
|
||||
<a href="https://github.com/project-serum/awesome-serum">Awesome</a>
|
||||
<span> | </span>
|
||||
<a href="https://dex.projectserum.com/#/">DEX</a>
|
||||
<span> | </span>
|
||||
<a href="https://github.com/project-serum/serum-dex">Rust</a>
|
||||
</h4>
|
||||
</div>
|
||||
|
||||
## Packages
|
||||
|
||||
| Package | Version | Description |
|
||||
| --------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------- |
|
||||
| [`@project-serum/borsh`](/packages/borsh) | [![npm](https://img.shields.io/npm/v/@project-serum/borsh.svg)](https://www.npmjs.com/package/@project-serum/borsh) | Borsh serialization primitives |
|
||||
| [`@project-serum/common`](/packages/common) | [![npm](https://img.shields.io/npm/v/@project-serum/common.svg)](https://www.npmjs.com/package/@project-serum/common) | Common utilities |
|
||||
| [`@project-serum/lockup`](/packages/lockup) | [![npm](https://img.shields.io/npm/v/@project-serum/lockup.svg)](https://www.npmjs.com/package/@project-serum/lockup) | Client for interacting with the Lockup Program |
|
||||
| [`@project-serum/pool`](/packages/pool) | [![npm](https://img.shields.io/npm/v/@project-serum/pool.svg)](https://www.npmjs.com/package/@project-serum/pool) | Client for interacting with Pools |
|
||||
| [`@project-serum/registry`](/packages/registry) | [![npm](https://img.shields.io/npm/v/@project-serum/registry.svg)](https://www.npmjs.com/package/@project-serum/registry) | Client for interacting with the Registry Program |
|
||||
| [`@project-serum/swap`](/packages/swap) | [![npm](https://img.shields.io/npm/v/@project-serum/swap.svg)](https://www.npmjs.com/package/@project-serum/swap) | Client for interacting witht the Swap Program |
|
||||
| [`@project-serum/tokens`](/packages/tokens) | [![npm](https://img.shields.io/npm/v/@project-serum/tokens.svg)](https://www.npmjs.com/package/@project-serum/tokens) | Solana token addresses |
|
||||
|
||||
## Contributing
|
||||
|
||||
### Installing
|
||||
|
||||
To get started first install the required build tools:
|
||||
|
||||
```
|
||||
npm install -g lerna
|
||||
npm install -g yarn
|
||||
```
|
||||
|
||||
Then bootstrap the workspace:
|
||||
|
||||
```
|
||||
yarn
|
||||
```
|
||||
|
||||
### Building
|
||||
|
||||
To build the workspace:
|
||||
|
||||
```
|
||||
yarn build
|
||||
```
|
||||
|
||||
### Testing
|
||||
|
||||
To run all tests:
|
||||
|
||||
```
|
||||
yarn test
|
||||
```
|
||||
|
||||
### Linting
|
||||
|
||||
To lint:
|
||||
|
||||
```
|
||||
yarn lint
|
||||
```
|
||||
|
||||
To apply lint fixes:
|
||||
|
||||
```
|
||||
yarn lint:fix
|
||||
```
|
|
@ -1,67 +0,0 @@
|
|||
{
|
||||
"private": true,
|
||||
"keywords": [],
|
||||
"workspaces": {
|
||||
"packages": [
|
||||
"packages/*"
|
||||
]
|
||||
},
|
||||
"license": "Apache-2.0",
|
||||
"engines": {
|
||||
"node": ">=6.0.0"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "lerna run build",
|
||||
"lint": "eslint 'packages/*/{src,test}/**/*.ts' && prettier -c 'packages/*/{src,test}/**/*.ts'",
|
||||
"lint:fix": "eslint --fix 'packages/*/{src,test}/**/*.ts' && prettier --write 'packages/*/{src,test}/**/*.ts'",
|
||||
"deploy:docs": "lerna run docs && gh-pages -d docs",
|
||||
"test": "lerna run test --concurrency 1 --stream"
|
||||
},
|
||||
"lint-staged": {
|
||||
"packages/*/{src,test}/**/*.ts": [
|
||||
"prettier --write"
|
||||
]
|
||||
},
|
||||
"husky": {
|
||||
"hooks": {
|
||||
"pre-commit": "lint-staged"
|
||||
}
|
||||
},
|
||||
"prettier": {
|
||||
"arrowParens": "avoid",
|
||||
"semi": true,
|
||||
"singleQuote": true,
|
||||
"trailingComma": "all"
|
||||
},
|
||||
"commitlint": {
|
||||
"extends": [
|
||||
"@commitlint/config-conventional"
|
||||
]
|
||||
},
|
||||
"devDependencies": {
|
||||
"@commitlint/cli": "^8.2.0",
|
||||
"@commitlint/config-conventional": "^8.2.0",
|
||||
"@types/jest": "^26.0.15",
|
||||
"@typescript-eslint/eslint-plugin": "^4.6.0",
|
||||
"@typescript-eslint/parser": "^4.6.0",
|
||||
"eslint": "^7.12.1",
|
||||
"eslint-config-prettier": "^6.15.0",
|
||||
"gh-pages": "^3.1.0",
|
||||
"husky": "^4.3.0",
|
||||
"jest": "^26.6.1",
|
||||
"jest-config": "^26.6.1",
|
||||
"lerna": "3.22.1",
|
||||
"lint-staged": "^10.5.0",
|
||||
"prettier": "^2.1.2",
|
||||
"rollup": "^1.23.1",
|
||||
"rollup-plugin-commonjs": "^10.1.0",
|
||||
"rollup-plugin-json": "^4.0.0",
|
||||
"rollup-plugin-node-resolve": "^5.2.0",
|
||||
"rollup-plugin-sourcemaps": "^0.4.2",
|
||||
"rollup-plugin-terser": "^5.1.3",
|
||||
"rollup-plugin-typescript2": "^0.25.2",
|
||||
"ts-jest": "^26.4.3",
|
||||
"ts-node": "^9.0.0",
|
||||
"typescript": "^4.0.5"
|
||||
}
|
||||
}
|
|
@ -1,9 +0,0 @@
|
|||
import MAINNET_TOKENS from "./mainnet-beta.json";
|
||||
import DEVNET_TOKENS from "./devnet.json";
|
||||
import TESTNET_TOKENS from "./testnet.json";
|
||||
|
||||
export const TOKENS = {
|
||||
'mainnet': MAINNET_TOKENS,
|
||||
'devnet': DEVNET_TOKENS,
|
||||
'testnet': TESTNET_TOKENS,
|
||||
};
|
|
@ -1,17 +0,0 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"moduleResolution": "node",
|
||||
"target": "es6",
|
||||
"module": "es2015",
|
||||
"lib": ["es2015", "es2016", "es2017", "dom"],
|
||||
"strict": true,
|
||||
"sourceMap": true,
|
||||
"declaration": true,
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"experimentalDecorators": true,
|
||||
"emitDecoratorMetadata": true,
|
||||
"noImplicitAny": true,
|
||||
"typeRoots": ["types/", "node_modules/@types"]
|
||||
},
|
||||
"include": ["src/**/*"]
|
||||
}
|
9395
migrate/yarn.lock
9395
migrate/yarn.lock
File diff suppressed because it is too large
Load Diff
102
package.json
102
package.json
|
@ -1,59 +1,67 @@
|
|||
{
|
||||
"name": "@project-serum/serum",
|
||||
"version": "0.13.11",
|
||||
"description": "Library for interacting with the serum dex",
|
||||
"license": "MIT",
|
||||
"repository": "project-serum/serum-js",
|
||||
"main": "lib/index.js",
|
||||
"source": "src/index.js",
|
||||
"types": "lib/index.d.ts",
|
||||
"private": true,
|
||||
"keywords": [],
|
||||
"workspaces": {
|
||||
"packages": [
|
||||
"packages/*"
|
||||
]
|
||||
},
|
||||
"license": "Apache-2.0",
|
||||
"engines": {
|
||||
"node": ">=10"
|
||||
"node": ">=6.0.0"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"start": "tsc --watch",
|
||||
"clean": "rm -rf lib",
|
||||
"prepare": "run-s clean build",
|
||||
"shell": "node -e \"$(< shell)\" -i --experimental-repl-await",
|
||||
"test": "run-s test:unit test:lint test:build",
|
||||
"test:build": "run-s build",
|
||||
"test:lint": "eslint src",
|
||||
"test:unit": "jest",
|
||||
"test:watch": "jest --watch"
|
||||
"build": "lerna run build",
|
||||
"lint": "eslint 'packages/*/{src,test}/**/*.ts' && prettier -c 'packages/*/{src,test}/**/*.ts'",
|
||||
"lint:fix": "eslint --fix 'packages/*/{src,test}/**/*.ts' && prettier --write 'packages/*/{src,test}/**/*.ts'",
|
||||
"deploy:docs": "lerna run docs && gh-pages -d docs",
|
||||
"test": "lerna run test --concurrency 1 --stream"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@tsconfig/node12": "^1.0.7",
|
||||
"@types/bn.js": "^4.11.6",
|
||||
"@types/jest": "^26.0.9",
|
||||
"@typescript-eslint/eslint-plugin": "^4.6.0",
|
||||
"@typescript-eslint/parser": "^4.6.0",
|
||||
"babel-eslint": "^10.0.3",
|
||||
"cross-env": "^7.0.2",
|
||||
"eslint": "^7.6.0",
|
||||
"eslint-config-prettier": "^6.11.0",
|
||||
"jest": "^26.4.0",
|
||||
"npm-run-all": "^4.1.5",
|
||||
"prettier": "^2.0.5",
|
||||
"ts-jest": "^26.2.0",
|
||||
"typescript": "^4.0.5"
|
||||
"lint-staged": {
|
||||
"packages/*/{src,test}/**/*.ts": [
|
||||
"prettier --write"
|
||||
]
|
||||
},
|
||||
"husky": {
|
||||
"hooks": {
|
||||
"pre-commit": "lint-staged"
|
||||
}
|
||||
},
|
||||
"files": [
|
||||
"lib"
|
||||
],
|
||||
"prettier": {
|
||||
"arrowParens": "avoid",
|
||||
"semi": true,
|
||||
"singleQuote": true,
|
||||
"trailingComma": "all"
|
||||
},
|
||||
"dependencies": {
|
||||
"@solana/web3.js": "0.86.1",
|
||||
"bn.js": "^5.1.2",
|
||||
"buffer-layout": "^1.2.0"
|
||||
"commitlint": {
|
||||
"extends": [
|
||||
"@commitlint/config-conventional"
|
||||
]
|
||||
},
|
||||
"browserslist": [
|
||||
">0.2%",
|
||||
"not dead",
|
||||
"not op_mini all",
|
||||
"maintained node versions"
|
||||
]
|
||||
"devDependencies": {
|
||||
"@commitlint/cli": "^8.2.0",
|
||||
"@commitlint/config-conventional": "^8.2.0",
|
||||
"@types/jest": "^26.0.15",
|
||||
"@typescript-eslint/eslint-plugin": "^4.6.0",
|
||||
"@typescript-eslint/parser": "^4.6.0",
|
||||
"eslint": "^7.12.1",
|
||||
"eslint-config-prettier": "^6.15.0",
|
||||
"gh-pages": "^3.1.0",
|
||||
"husky": "^4.3.0",
|
||||
"jest": "^26.6.1",
|
||||
"jest-config": "^26.6.1",
|
||||
"lerna": "3.22.1",
|
||||
"lint-staged": "^10.5.0",
|
||||
"prettier": "^2.1.2",
|
||||
"rollup": "^1.23.1",
|
||||
"rollup-plugin-commonjs": "^10.1.0",
|
||||
"rollup-plugin-json": "^4.0.0",
|
||||
"rollup-plugin-node-resolve": "^5.2.0",
|
||||
"rollup-plugin-sourcemaps": "^0.4.2",
|
||||
"rollup-plugin-terser": "^5.1.3",
|
||||
"rollup-plugin-typescript2": "^0.25.2",
|
||||
"ts-jest": "^26.4.3",
|
||||
"ts-node": "^9.0.0",
|
||||
"typescript": "^4.0.5"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,7 +17,7 @@
|
|||
},
|
||||
"rules": {
|
||||
"no-constant-condition": ["error", { "checkLoops": false }],
|
||||
"no-empty-function": "off",
|
||||
"@typescript-eslint/explicit-module-boundary-types": "off",
|
||||
"@typescript-eslint/ban-ts-comment": "off",
|
||||
"@typescript-eslint/no-explicit-any": "off"
|
||||
}
|
|
@ -0,0 +1,98 @@
|
|||
[![npm (scoped)](https://img.shields.io/npm/v/@project-serum/serum)](https://www.npmjs.com/package/@project-serum/serum)
|
||||
[![Build Status](https://travis-ci.com/project-serum/serum-js.svg?branch=master)](https://travis-ci.com/project-serum/serum-js)
|
||||
|
||||
# Serum JS Client Library
|
||||
|
||||
JavaScript client library for interacting with the Project Serum DEX.
|
||||
|
||||
## Installation
|
||||
|
||||
Using npm:
|
||||
|
||||
```
|
||||
npm install @solana/web3.js @project-serum/serum
|
||||
```
|
||||
|
||||
Using yarn:
|
||||
|
||||
```
|
||||
yarn add @solana/web3.js @project-serum/serum
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
import { Account, Connection, PublicKey } from '@solana/web3.js';
|
||||
import { Market } from '@project-serum/serum';
|
||||
|
||||
let connection = new Connection('https://testnet.solana.com');
|
||||
let marketAddress = new PublicKey('...');
|
||||
let market = await Market.load(connection, marketAddress);
|
||||
|
||||
// Fetching orderbooks
|
||||
let bids = await market.loadBids(connection);
|
||||
let asks = await market.loadAsks(connection);
|
||||
// L2 orderbook data
|
||||
for (let [price, size] of bids.getL2(20)) {
|
||||
console.log(price, size);
|
||||
}
|
||||
// Full orderbook data
|
||||
for (let order of asks) {
|
||||
console.log(
|
||||
order.orderId,
|
||||
order.price,
|
||||
order.size,
|
||||
order.side, // 'buy' or 'sell'
|
||||
);
|
||||
}
|
||||
|
||||
// Placing orders
|
||||
let owner = new Account('...');
|
||||
let payer = new PublicKey('...'); // spl-token account
|
||||
await market.placeOrder(connection, {
|
||||
owner,
|
||||
payer,
|
||||
side: 'buy', // 'buy' or 'sell'
|
||||
price: 123.45,
|
||||
size: 17.0,
|
||||
orderType: 'limit', // 'limit', 'ioc', 'postOnly'
|
||||
});
|
||||
|
||||
// Retrieving open orders by owner
|
||||
let myOrders = await market.loadOrdersForOwner(connection, owner.publicKey);
|
||||
|
||||
// Cancelling orders
|
||||
for (let order of myOrders) {
|
||||
await market.cancelOrder(connection, owner, order);
|
||||
}
|
||||
|
||||
// Retrieving fills
|
||||
for (let fill of await market.loadFills(connection)) {
|
||||
console.log(
|
||||
fill.orderId,
|
||||
fill.price,
|
||||
fill.size,
|
||||
fill.side,
|
||||
);
|
||||
}
|
||||
|
||||
// Settle funds
|
||||
for (let openOrders of await market.findOpenOrdersAccountsForOwner(
|
||||
connection,
|
||||
owner.publicKey,
|
||||
)) {
|
||||
if (openOrders.baseTokenFree > 0 || openOrders.quoteTokenFree > 0) {
|
||||
// spl-token accounts to which to send the proceeds from trades
|
||||
let baseTokenAccount = new PublicKey('...');
|
||||
let quoteTokenAccount = new PublicKey('...');
|
||||
|
||||
await market.settleFunds(
|
||||
connection,
|
||||
owner,
|
||||
openOrders,
|
||||
baseTokenAccount,
|
||||
quoteTokenAccount,
|
||||
);
|
||||
}
|
||||
}
|
||||
```
|
|
@ -0,0 +1,59 @@
|
|||
{
|
||||
"name": "@project-serum/serum",
|
||||
"version": "0.13.11",
|
||||
"description": "Library for interacting with the serum dex",
|
||||
"license": "MIT",
|
||||
"repository": "project-serum/serum-js",
|
||||
"main": "lib/index.js",
|
||||
"source": "src/index.js",
|
||||
"types": "lib/index.d.ts",
|
||||
"engines": {
|
||||
"node": ">=10"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"start": "tsc --watch",
|
||||
"clean": "rm -rf lib",
|
||||
"prepare": "run-s clean build",
|
||||
"shell": "node -e \"$(< shell)\" -i --experimental-repl-await",
|
||||
"test": "run-s test:unit test:lint test:build",
|
||||
"test:build": "run-s build",
|
||||
"test:lint": "eslint src",
|
||||
"test:unit": "jest",
|
||||
"test:watch": "jest --watch"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@tsconfig/node12": "^1.0.7",
|
||||
"@types/bn.js": "^4.11.6",
|
||||
"@types/jest": "^26.0.9",
|
||||
"@typescript-eslint/eslint-plugin": "^4.6.0",
|
||||
"@typescript-eslint/parser": "^4.6.0",
|
||||
"babel-eslint": "^10.0.3",
|
||||
"cross-env": "^7.0.2",
|
||||
"eslint": "^7.6.0",
|
||||
"eslint-config-prettier": "^6.11.0",
|
||||
"jest": "^26.4.0",
|
||||
"npm-run-all": "^4.1.5",
|
||||
"prettier": "^2.0.5",
|
||||
"ts-jest": "^26.2.0",
|
||||
"typescript": "^4.0.5"
|
||||
},
|
||||
"files": [
|
||||
"lib"
|
||||
],
|
||||
"prettier": {
|
||||
"singleQuote": true,
|
||||
"trailingComma": "all"
|
||||
},
|
||||
"dependencies": {
|
||||
"@solana/web3.js": "0.86.1",
|
||||
"bn.js": "^5.1.2",
|
||||
"buffer-layout": "^1.2.0"
|
||||
},
|
||||
"browserslist": [
|
||||
">0.2%",
|
||||
"not dead",
|
||||
"not op_mini all",
|
||||
"maintained node versions"
|
||||
]
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
import { PublicKey } from '@solana/web3.js';
|
||||
import Markets from "./markets.json";
|
||||
import TokenMints from "./token-mints.json";
|
||||
import Markets from './markets.json';
|
||||
import TokenMints from './token-mints.json';
|
||||
|
||||
export function getLayoutVersion(programId: PublicKey) {
|
||||
if (
|
||||
|
@ -16,23 +16,26 @@ export function getLayoutVersion(programId: PublicKey) {
|
|||
return 2;
|
||||
}
|
||||
|
||||
export const TOKEN_MINTS: Array<{ address: PublicKey; name: string }> = TokenMints.map(mint => {
|
||||
export const TOKEN_MINTS: Array<{
|
||||
address: PublicKey;
|
||||
name: string;
|
||||
}> = TokenMints.map((mint) => {
|
||||
return {
|
||||
address: new PublicKey(mint.address),
|
||||
name: mint.name
|
||||
}
|
||||
})
|
||||
name: mint.name,
|
||||
};
|
||||
});
|
||||
|
||||
export const MARKETS: Array<{
|
||||
address: PublicKey;
|
||||
name: string;
|
||||
programId: PublicKey;
|
||||
deprecated: boolean;
|
||||
}> = Markets.map(market => {
|
||||
}> = Markets.map((market) => {
|
||||
return {
|
||||
address: new PublicKey(market.address),
|
||||
name: market.name,
|
||||
programId: new PublicKey(market.programId),
|
||||
deprecated: market.deprecated,
|
||||
}
|
||||
})
|
||||
};
|
||||
});
|
|
@ -0,0 +1,15 @@
|
|||
{
|
||||
"extends": "@tsconfig/node12/tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "./lib",
|
||||
"allowJs": true,
|
||||
"checkJs": true,
|
||||
"declaration": true,
|
||||
"declarationMap": true,
|
||||
"noImplicitAny": false,
|
||||
"resolveJsonModule": true,
|
||||
"sourceMap": true
|
||||
},
|
||||
"include": ["./src/**/*"],
|
||||
"exclude": ["./src/**/*.test.js", "node_modules", "**/node_modules"]
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue