strategy-tools 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/__mocks__/jose.js +17 -0
- package/dist/Signer.d.ts +4 -0
- package/dist/Signer.js +3 -0
- package/dist/Signer.js.map +1 -0
- package/dist/binance/BinanceApiSigner.d.ts +6 -0
- package/dist/binance/BinanceApiSigner.js +20 -0
- package/dist/binance/BinanceApiSigner.js.map +1 -0
- package/dist/binance/BinanceConnection.d.ts +21 -0
- package/dist/binance/BinanceConnection.js +77 -0
- package/dist/binance/BinanceConnection.js.map +1 -0
- package/dist/binance/BinanceSapiSigner.d.ts +6 -0
- package/dist/binance/BinanceSapiSigner.js +22 -0
- package/dist/binance/BinanceSapiSigner.js.map +1 -0
- package/dist/binance/BinanceTypes.d.ts +159 -0
- package/dist/binance/BinanceTypes.js +44 -0
- package/dist/binance/BinanceTypes.js.map +1 -0
- package/dist/binance/connection/BinanceOperationConnection.d.ts +121 -0
- package/dist/binance/connection/BinanceOperationConnection.js +83 -0
- package/dist/binance/connection/BinanceOperationConnection.js.map +1 -0
- package/dist/binance/connection/BinanceRfqConnection.d.ts +2 -0
- package/dist/binance/connection/BinanceRfqConnection.js +7 -0
- package/dist/binance/connection/BinanceRfqConnection.js.map +1 -0
- package/dist/cetus/CetusStrategyApiConnection.d.ts +25 -0
- package/dist/cetus/CetusStrategyApiConnection.js +22 -0
- package/dist/cetus/CetusStrategyApiConnection.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +20 -0
- package/dist/index.js.map +1 -0
- package/jest.config.js +19 -0
- package/package.json +44 -0
- package/src/Signer.ts +4 -0
- package/src/binance/BinanceApiSigner.ts +20 -0
- package/src/binance/BinanceConnection.ts +86 -0
- package/src/binance/BinanceSapiSigner.ts +21 -0
- package/src/binance/BinanceTypes.ts +168 -0
- package/src/binance/connection/BinanceOperationConnection.ts +122 -0
- package/src/binance/connection/BinanceRfqConnection.ts +1 -0
- package/src/cetus/CetusStrategyApiConnection.ts +30 -0
- package/src/index.ts +6 -0
- package/test/binance/BinanceOperationConnection.test.ts +73 -0
- package/test/binance/BinanceSigner.test.ts +45 -0
- package/test/cetus/CetusStrategyApiConnection.test.ts +27 -0
- package/tsconfig.json +23 -0
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { ExternalApiConnection, StandardKeyGroup } from "itrm-tools";
|
|
2
|
+
export interface CetusStrategy {
|
|
3
|
+
id: number;
|
|
4
|
+
name: string;
|
|
5
|
+
settings: any;
|
|
6
|
+
memory: any;
|
|
7
|
+
account: string;
|
|
8
|
+
pair: string;
|
|
9
|
+
intermediary: string;
|
|
10
|
+
status: string;
|
|
11
|
+
}
|
|
12
|
+
export declare enum StrategyStatus {
|
|
13
|
+
RUNNING = "RUNNING",
|
|
14
|
+
BUY_ONLY = "BUY_ONLY",
|
|
15
|
+
SELL_ONLY = "SELL_ONLY",
|
|
16
|
+
STOPPED = "STOPPED"
|
|
17
|
+
}
|
|
18
|
+
export declare class CetusStrategyApiConnection extends ExternalApiConnection {
|
|
19
|
+
constructor(apiUrl: string, keys: StandardKeyGroup);
|
|
20
|
+
getStrategies(params: {
|
|
21
|
+
name: string;
|
|
22
|
+
pair: string;
|
|
23
|
+
account: string;
|
|
24
|
+
}): Promise<CetusStrategy[]>;
|
|
25
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CetusStrategyApiConnection = exports.StrategyStatus = void 0;
|
|
4
|
+
const itrm_tools_1 = require("itrm-tools");
|
|
5
|
+
var StrategyStatus;
|
|
6
|
+
(function (StrategyStatus) {
|
|
7
|
+
StrategyStatus["RUNNING"] = "RUNNING";
|
|
8
|
+
StrategyStatus["BUY_ONLY"] = "BUY_ONLY";
|
|
9
|
+
StrategyStatus["SELL_ONLY"] = "SELL_ONLY";
|
|
10
|
+
StrategyStatus["STOPPED"] = "STOPPED";
|
|
11
|
+
})(StrategyStatus || (exports.StrategyStatus = StrategyStatus = {}));
|
|
12
|
+
class CetusStrategyApiConnection extends itrm_tools_1.ExternalApiConnection {
|
|
13
|
+
constructor(apiUrl, keys) {
|
|
14
|
+
super("cetus", apiUrl, keys);
|
|
15
|
+
}
|
|
16
|
+
async getStrategies(params) {
|
|
17
|
+
const queryString = new URLSearchParams(params).toString();
|
|
18
|
+
return this.request(itrm_tools_1.RequestMethod.GET, `/v1/strategies?${queryString}`);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
exports.CetusStrategyApiConnection = CetusStrategyApiConnection;
|
|
22
|
+
//# sourceMappingURL=CetusStrategyApiConnection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CetusStrategyApiConnection.js","sourceRoot":"","sources":["../../src/cetus/CetusStrategyApiConnection.ts"],"names":[],"mappings":";;;AAAA,2CAAoF;AAapF,IAAY,cAKX;AALD,WAAY,cAAc;IACtB,qCAAmB,CAAA;IACnB,uCAAqB,CAAA;IACrB,yCAAuB,CAAA;IACvB,qCAAmB,CAAA;AACvB,CAAC,EALW,cAAc,8BAAd,cAAc,QAKzB;AAED,MAAa,0BAA2B,SAAQ,kCAAqB;IACjE,YAAY,MAAc,EAAE,IAAsB;QAC9C,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,CAAC;IACjC,CAAC;IAEM,KAAK,CAAC,aAAa,CAAC,MAAuD;QAC9E,MAAM,WAAW,GAAG,IAAI,eAAe,CAAC,MAAa,CAAC,CAAC,QAAQ,EAAE,CAAC;QAClE,OAAO,IAAI,CAAC,OAAO,CAAkB,0BAAa,CAAC,GAAG,EAAE,kBAAkB,WAAW,EAAE,CAAC,CAAC;IAC7F,CAAC;CACJ;AATD,gEASC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { BinanceApiSigner as BinanceApiSigner } from './binance/BinanceApiSigner';
|
|
2
|
+
export { BinanceConnection as BinanceConnection } from './binance/BinanceConnection';
|
|
3
|
+
export { BinanceOperationConnection as BinanceOperationConnection } from './binance/connection/BinanceOperationConnection';
|
|
4
|
+
export { BinanceParams as BinanceParams, BinanceResponse as BinanceResponse, BinanceAccountResponse as BinanceAccountResponse, BinanceSymbolStatus as BinanceSymbolStatus, BinanceOrderSide as BinanceOrderSide, BinanceOrderType as BinanceOrderType, BinanceTimeInForce as BinanceTimeInForce, BinanceOrder as BinanceOrder, BinanceOrderStatus as BinanceOrderStatus, BinanceCancelRestrictions as BinanceCancelRestrictions } from './binance/BinanceTypes';
|
|
5
|
+
export { CetusStrategyApiConnection as CetusStrategyApiConnection, CetusStrategy as CetusStrategy, StrategyStatus as StrategyStatus } from './cetus/CetusStrategyApiConnection';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StrategyStatus = exports.CetusStrategyApiConnection = exports.BinanceCancelRestrictions = exports.BinanceOrderStatus = exports.BinanceTimeInForce = exports.BinanceOrderType = exports.BinanceOrderSide = exports.BinanceSymbolStatus = exports.BinanceOperationConnection = exports.BinanceConnection = exports.BinanceApiSigner = void 0;
|
|
4
|
+
var BinanceApiSigner_1 = require("./binance/BinanceApiSigner");
|
|
5
|
+
Object.defineProperty(exports, "BinanceApiSigner", { enumerable: true, get: function () { return BinanceApiSigner_1.BinanceApiSigner; } });
|
|
6
|
+
var BinanceConnection_1 = require("./binance/BinanceConnection");
|
|
7
|
+
Object.defineProperty(exports, "BinanceConnection", { enumerable: true, get: function () { return BinanceConnection_1.BinanceConnection; } });
|
|
8
|
+
var BinanceOperationConnection_1 = require("./binance/connection/BinanceOperationConnection");
|
|
9
|
+
Object.defineProperty(exports, "BinanceOperationConnection", { enumerable: true, get: function () { return BinanceOperationConnection_1.BinanceOperationConnection; } });
|
|
10
|
+
var BinanceTypes_1 = require("./binance/BinanceTypes");
|
|
11
|
+
Object.defineProperty(exports, "BinanceSymbolStatus", { enumerable: true, get: function () { return BinanceTypes_1.BinanceSymbolStatus; } });
|
|
12
|
+
Object.defineProperty(exports, "BinanceOrderSide", { enumerable: true, get: function () { return BinanceTypes_1.BinanceOrderSide; } });
|
|
13
|
+
Object.defineProperty(exports, "BinanceOrderType", { enumerable: true, get: function () { return BinanceTypes_1.BinanceOrderType; } });
|
|
14
|
+
Object.defineProperty(exports, "BinanceTimeInForce", { enumerable: true, get: function () { return BinanceTypes_1.BinanceTimeInForce; } });
|
|
15
|
+
Object.defineProperty(exports, "BinanceOrderStatus", { enumerable: true, get: function () { return BinanceTypes_1.BinanceOrderStatus; } });
|
|
16
|
+
Object.defineProperty(exports, "BinanceCancelRestrictions", { enumerable: true, get: function () { return BinanceTypes_1.BinanceCancelRestrictions; } });
|
|
17
|
+
var CetusStrategyApiConnection_1 = require("./cetus/CetusStrategyApiConnection");
|
|
18
|
+
Object.defineProperty(exports, "CetusStrategyApiConnection", { enumerable: true, get: function () { return CetusStrategyApiConnection_1.CetusStrategyApiConnection; } });
|
|
19
|
+
Object.defineProperty(exports, "StrategyStatus", { enumerable: true, get: function () { return CetusStrategyApiConnection_1.StrategyStatus; } });
|
|
20
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,+DAAkF;AAAzE,oHAAA,gBAAgB,OAAoB;AAC7C,iEAAqF;AAA5E,sHAAA,iBAAiB,OAAqB;AAC/C,8FAA2H;AAAlH,wIAAA,0BAA0B,OAA8B;AACjE,uDAAgc;AAAjU,mHAAA,mBAAmB,OAAuB;AAAE,gHAAA,gBAAgB,OAAoB;AAAE,gHAAA,gBAAgB,OAAoB;AAAE,kHAAA,kBAAkB,OAAsB;AAAgC,kHAAA,kBAAkB,OAAsB;AAAE,yHAAA,yBAAyB,OAA6B;AAE/Z,iFAAgL;AAAvK,wIAAA,0BAA0B,OAA8B;AAAkC,4HAAA,cAAc,OAAkB"}
|
package/jest.config.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
preset: "jest-puppeteer",
|
|
3
|
+
transform: {
|
|
4
|
+
'^.+\\.ts?$': [
|
|
5
|
+
'ts-jest',
|
|
6
|
+
{
|
|
7
|
+
transpilation: true,
|
|
8
|
+
}
|
|
9
|
+
],
|
|
10
|
+
},
|
|
11
|
+
testEnvironment: 'jest-environment-node',
|
|
12
|
+
testRegex: '/test/.*\\.(test|spec)?\\.(ts|tsx)$',
|
|
13
|
+
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
|
|
14
|
+
automock: false,
|
|
15
|
+
resetMocks: false,
|
|
16
|
+
moduleNameMapper: {
|
|
17
|
+
'^jose$': '<rootDir>/__mocks__/jose.js'
|
|
18
|
+
}
|
|
19
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "strategy-tools",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Librería para el desarrollo de estrategias de Cetus",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "rimraf ./dist && tsc --skipLibCheck",
|
|
8
|
+
"publish-lib": "npm run build && npm publish",
|
|
9
|
+
"publish-beta": "npm run build && npm publish --tag beta",
|
|
10
|
+
"publish-dryrun": "npm run build && npm publish --dry-run",
|
|
11
|
+
"link": "npm run build && npm link",
|
|
12
|
+
"test": "npm run build && set NODE_OPTIONS=--experimental-vm-modules && jest --runInBand --detectOpenHandles",
|
|
13
|
+
"test-silent": "npm run build && set NODE_OPTIONS=--experimental-vm-modules && jest --runInBand --detectOpenHandles --silent",
|
|
14
|
+
"test-watch": "npm run build && set NODE_OPTIONS=--experimental-vm-modules && jest --watch --runInBand --detectOpenHandles",
|
|
15
|
+
"test-watch-start": "npm run build && set NODE_OPTIONS=--experimental-vm-modules && jest --watch --runInBand --detectOpenHandles",
|
|
16
|
+
"test-coverage": "npm run build && set NODE_OPTIONS=--experimental-vm-modules && jest --runInBand --detectOpenHandles --silent --coverage"
|
|
17
|
+
},
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/ITRMachines/strategy-tools.git"
|
|
21
|
+
},
|
|
22
|
+
"keywords": [],
|
|
23
|
+
"author": "",
|
|
24
|
+
"license": "ISC",
|
|
25
|
+
"type": "commonjs",
|
|
26
|
+
"bugs": {
|
|
27
|
+
"url": "https://github.com/ITRMachines/strategy-tools/issues"
|
|
28
|
+
},
|
|
29
|
+
"homepage": "https://github.com/ITRMachines/strategy-tools#readme",
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"itrm-tools": "^1.3.28",
|
|
32
|
+
"jsrsasign": "^11.1.0"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@types/crypto-js": "^4.2.2",
|
|
36
|
+
"@types/jest": "^30.0.0",
|
|
37
|
+
"dotenv": "^17.2.3",
|
|
38
|
+
"jest": "^30.2.0",
|
|
39
|
+
"jest-puppeteer": "^11.0.0",
|
|
40
|
+
"rimraf": "^6.1.2",
|
|
41
|
+
"ts-jest": "^29.4.5",
|
|
42
|
+
"typescript": "^5.9.3"
|
|
43
|
+
}
|
|
44
|
+
}
|
package/src/Signer.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
const { KJUR, hextob64 } = require('jsrsasign');
|
|
2
|
+
//import { Signer } from "itrm-tools";
|
|
3
|
+
import { Signer } from "../Signer";
|
|
4
|
+
import { BinanceParams } from "./BinanceTypes";
|
|
5
|
+
|
|
6
|
+
export class BinanceApiSigner implements Signer<BinanceParams, string, string> {
|
|
7
|
+
public sign(params: BinanceParams, key: string): string {
|
|
8
|
+
const queryString = new URLSearchParams(params as URLSearchParams).toString();
|
|
9
|
+
const signer = new KJUR.crypto.Signature({"alg": "SHA256withRSA"});
|
|
10
|
+
signer.init(key);
|
|
11
|
+
signer.updateString(queryString);
|
|
12
|
+
const signatureHex = signer.sign();
|
|
13
|
+
const signature = hextob64(signatureHex);
|
|
14
|
+
return encodeURIComponent(signature);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
public verify(input: BinanceParams, key: string, signature: string): boolean {
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { BinanceApiSigner } from "./BinanceApiSigner";
|
|
2
|
+
import { BinanceSapiSignerBinanceApiSigner } from "./BinanceSapiSigner";
|
|
3
|
+
import { BinanceAccountResponse, BinanceParams, BinanceResponse } from "./BinanceTypes";
|
|
4
|
+
import { RequestAxiosCall } from "itrm-tools";
|
|
5
|
+
|
|
6
|
+
export interface BinanceConfig {
|
|
7
|
+
baseUrl: string;
|
|
8
|
+
secretKey: string;
|
|
9
|
+
privateKey: string;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export class BinanceConnection {
|
|
13
|
+
protected config: BinanceConfig;
|
|
14
|
+
private apiSigner: BinanceApiSigner = new BinanceApiSigner();
|
|
15
|
+
private sapiSigner: BinanceSapiSignerBinanceApiSigner = new BinanceSapiSignerBinanceApiSigner();
|
|
16
|
+
|
|
17
|
+
constructor(config: BinanceConfig) {
|
|
18
|
+
this.config= config;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
public async getAccount(params: { omitZeroBalances?: boolean; recvWindow?: number; }): Promise<BinanceAccountResponse> {
|
|
22
|
+
let response = await this.sendGetRequest('/api/v3/account', params);
|
|
23
|
+
return {
|
|
24
|
+
makerCommission: response.makerCommission!,
|
|
25
|
+
takerCommission: response.takerCommission!,
|
|
26
|
+
buyerCommission: response.buyerCommission!,
|
|
27
|
+
sellerCommission: response.sellerCommission!,
|
|
28
|
+
commissionRates: {
|
|
29
|
+
maker: parseFloat(response.commissionRates!.maker),
|
|
30
|
+
taker: parseFloat(response.commissionRates!.taker),
|
|
31
|
+
buyer: parseFloat(response.commissionRates!.buyer),
|
|
32
|
+
seller: parseFloat(response.commissionRates!.seller)
|
|
33
|
+
},
|
|
34
|
+
canTrade: response.canTrade!,
|
|
35
|
+
canWithdraw: response.canWithdraw!,
|
|
36
|
+
canDeposit: response.canDeposit!,
|
|
37
|
+
brokered: response.brokered!,
|
|
38
|
+
requireSelfTradePrevention: response.requireSelfTradePrevention!,
|
|
39
|
+
preventSor: response.preventSor!,
|
|
40
|
+
updateTime: response.updateTime!,
|
|
41
|
+
accountType: response.accountType!,
|
|
42
|
+
balances: this.parseBalances(response.balances!),
|
|
43
|
+
permissions: response.permissions!,
|
|
44
|
+
uid: response.uid!
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
private parseBalances(balances: { asset: string; free: string; locked: string; }[]) {
|
|
49
|
+
let result = [];
|
|
50
|
+
for (let balance of balances)
|
|
51
|
+
result.push({
|
|
52
|
+
asset: balance.asset,
|
|
53
|
+
free: parseFloat(balance.free),
|
|
54
|
+
locked: parseFloat(balance.locked)
|
|
55
|
+
});
|
|
56
|
+
return result;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
protected async sendGetRequest(endpoint: string, params: BinanceParams) {
|
|
60
|
+
params.timestamp = Date.now();
|
|
61
|
+
const queryString = new URLSearchParams(params as URLSearchParams).toString();
|
|
62
|
+
const url = `${this.config.baseUrl}${endpoint}?${queryString}&signature=${this.apiSigner.sign(params, this.config.privateKey)}`;
|
|
63
|
+
return await RequestAxiosCall.get<BinanceResponse>(url, { headers: { "X-MBX-APIKEY": this.config.secretKey } });
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
protected async sendPostRequest(endpoint: string, params: BinanceParams) {
|
|
67
|
+
params.timestamp = Date.now();
|
|
68
|
+
const queryString = new URLSearchParams(params as URLSearchParams).toString();
|
|
69
|
+
const requestBody = `${queryString}&signature=${this.apiSigner.sign(params, this.config.privateKey)}`;
|
|
70
|
+
return await RequestAxiosCall.post<BinanceResponse>(`${this.config.baseUrl}${endpoint}`, requestBody, { headers: { "X-MBX-APIKEY": this.config.secretKey } });
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
protected async sendDeleteRequest(endpoint: string, params: BinanceParams) {
|
|
74
|
+
params.timestamp = Date.now();
|
|
75
|
+
const queryString = new URLSearchParams(params as URLSearchParams).toString();
|
|
76
|
+
const url = `${this.config.baseUrl}${endpoint}?${queryString}&signature=${this.apiSigner.sign(params, this.config.privateKey)}`;
|
|
77
|
+
return await RequestAxiosCall.delete<BinanceResponse>(url, { headers: { "X-MBX-APIKEY": this.config.secretKey } });
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
protected async sendSapiPostRequest(endpoint: string, params: BinanceParams) {
|
|
81
|
+
params.timestamp = Date.now();
|
|
82
|
+
const queryString = new URLSearchParams(params as URLSearchParams).toString().replace(/\%40/g, '@');
|
|
83
|
+
const requestBody = `${queryString}&signature=${this.apiSigner.sign(params, this.config.privateKey)}`;
|
|
84
|
+
return await RequestAxiosCall.post<BinanceResponse>(`${this.config.baseUrl}${endpoint}`, requestBody, { headers: { "X-MBX-APIKEY": this.config.secretKey } });
|
|
85
|
+
}
|
|
86
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { BinanceParams } from "./BinanceTypes";
|
|
2
|
+
import { HmacSHA256, enc } from "crypto-js";
|
|
3
|
+
import { Signer } from "../Signer";
|
|
4
|
+
|
|
5
|
+
export class BinanceSapiSignerBinanceApiSigner implements Signer<BinanceParams, string, string> {
|
|
6
|
+
public sign(params: BinanceParams, key: string): string {
|
|
7
|
+
const queryString = new URLSearchParams(params as URLSearchParams).toString();
|
|
8
|
+
const signature = HmacSHA256(queryString, key).toString(enc.Hex);
|
|
9
|
+
/*const signer = new KJUR.crypto.Signature({"alg": "SHA256withRSA"});
|
|
10
|
+
signer.init(key);
|
|
11
|
+
signer.updateString(queryString);
|
|
12
|
+
const signatureHex = signer.sign();
|
|
13
|
+
const signature = hextob64(signatureHex);*/
|
|
14
|
+
console.log(">>> signature:", signature);
|
|
15
|
+
return encodeURIComponent(signature);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
public verify(input: BinanceParams, key: string, signature: string): boolean {
|
|
19
|
+
return true;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
export interface BinanceParams {
|
|
2
|
+
quoteId?: string;
|
|
3
|
+
rfqId?: string;
|
|
4
|
+
baseAsset?: string;
|
|
5
|
+
quoteAsset?: string;
|
|
6
|
+
quantity?: number;
|
|
7
|
+
price?: number;
|
|
8
|
+
settleTime?: number;
|
|
9
|
+
omitZeroBalances?: boolean;
|
|
10
|
+
recvWindow?: number;
|
|
11
|
+
timestamp?: number;
|
|
12
|
+
symbol?: string;
|
|
13
|
+
side?: BinanceOrderSide;
|
|
14
|
+
type?: BinanceOrderType;
|
|
15
|
+
timeInForce?: BinanceTimeInForce;
|
|
16
|
+
quoteOrderQty?: number;
|
|
17
|
+
orderId?: number;
|
|
18
|
+
origClientOrderId?: string;
|
|
19
|
+
newClientOrderId?: string;
|
|
20
|
+
cancelRestrictions?: BinanceCancelRestrictions;
|
|
21
|
+
toEmail?: string;
|
|
22
|
+
asset?: string;
|
|
23
|
+
amount?: number;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export interface BinanceResponse {
|
|
27
|
+
makerCommission?: number;
|
|
28
|
+
takerCommission?: number;
|
|
29
|
+
buyerCommission?: number,
|
|
30
|
+
sellerCommission?: number,
|
|
31
|
+
commissionRates?: {
|
|
32
|
+
maker: string;
|
|
33
|
+
taker: string;
|
|
34
|
+
buyer: string;
|
|
35
|
+
seller: string;
|
|
36
|
+
},
|
|
37
|
+
canTrade?: boolean;
|
|
38
|
+
canWithdraw?: boolean;
|
|
39
|
+
canDeposit?: boolean;
|
|
40
|
+
brokered?: boolean;
|
|
41
|
+
requireSelfTradePrevention?: boolean;
|
|
42
|
+
preventSor?: boolean;
|
|
43
|
+
updateTime?: number;
|
|
44
|
+
accountType?: string;
|
|
45
|
+
balances?: {
|
|
46
|
+
asset: string;
|
|
47
|
+
free: string;
|
|
48
|
+
locked: string;
|
|
49
|
+
} [];
|
|
50
|
+
permissions?: string[];
|
|
51
|
+
uid?: number;
|
|
52
|
+
symbol?: string;
|
|
53
|
+
orderId?: number;
|
|
54
|
+
orderListId?: number;
|
|
55
|
+
clientOrderId?: string;
|
|
56
|
+
transactTime?: number;
|
|
57
|
+
price?: string;
|
|
58
|
+
origQty?: string;
|
|
59
|
+
executedQty?: string;
|
|
60
|
+
origQuoteOrderQty?: string;
|
|
61
|
+
cummulativeQuoteQty?: string;
|
|
62
|
+
status?: BinanceOrderStatus;
|
|
63
|
+
timeInForce?: BinanceTimeInForce;
|
|
64
|
+
type?: BinanceOrderType;
|
|
65
|
+
side?: BinanceOrderSide;
|
|
66
|
+
workingTime?: number;
|
|
67
|
+
fills?: {
|
|
68
|
+
qty: string;
|
|
69
|
+
price: string;
|
|
70
|
+
tradeId: number;
|
|
71
|
+
commission: string;
|
|
72
|
+
commissionAsset: string;
|
|
73
|
+
}[];
|
|
74
|
+
selfTradePreventionMode?: string;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export interface BinanceAccountResponse {
|
|
78
|
+
makerCommission: number;
|
|
79
|
+
takerCommission: number;
|
|
80
|
+
buyerCommission: number;
|
|
81
|
+
sellerCommission: number;
|
|
82
|
+
commissionRates: {
|
|
83
|
+
maker: number;
|
|
84
|
+
taker: number;
|
|
85
|
+
buyer: number;
|
|
86
|
+
seller: number;
|
|
87
|
+
};
|
|
88
|
+
canTrade: boolean;
|
|
89
|
+
canWithdraw: boolean;
|
|
90
|
+
canDeposit: boolean;
|
|
91
|
+
brokered: boolean;
|
|
92
|
+
requireSelfTradePrevention: boolean;
|
|
93
|
+
preventSor: boolean;
|
|
94
|
+
updateTime: number;
|
|
95
|
+
accountType: string;
|
|
96
|
+
balances: {
|
|
97
|
+
asset: string;
|
|
98
|
+
free: number;
|
|
99
|
+
locked: number;
|
|
100
|
+
}[];
|
|
101
|
+
permissions: string[];
|
|
102
|
+
uid: number;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export interface BinanceOrder {
|
|
106
|
+
symbol: string;
|
|
107
|
+
orderId: number;
|
|
108
|
+
orderListId: number;
|
|
109
|
+
clientOrderId: string;
|
|
110
|
+
transactTime: number;
|
|
111
|
+
price: number;
|
|
112
|
+
origQty: number;
|
|
113
|
+
executedQty: number;
|
|
114
|
+
origQuoteOrderQty: number;
|
|
115
|
+
cummulativeQuoteQty: number;
|
|
116
|
+
status: BinanceOrderStatus;
|
|
117
|
+
timeInForce: BinanceTimeInForce;
|
|
118
|
+
type: BinanceOrderType;
|
|
119
|
+
side: BinanceOrderSide;
|
|
120
|
+
workingTime: number;
|
|
121
|
+
fills: {
|
|
122
|
+
qty: number;
|
|
123
|
+
price: number;
|
|
124
|
+
tradeId: number;
|
|
125
|
+
commission: number;
|
|
126
|
+
commissionAsset: string;
|
|
127
|
+
}[];
|
|
128
|
+
selfTradePreventionMode: string;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export enum BinanceSymbolStatus {
|
|
132
|
+
TRADING = 'TRADING',
|
|
133
|
+
BREAK = 'BREAK',
|
|
134
|
+
HALT = 'HALT'
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export enum BinanceOrderSide {
|
|
138
|
+
BUY = 'BUY',
|
|
139
|
+
SELL = 'SELL'
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export enum BinanceOrderType {
|
|
143
|
+
LIMIT = 'LIMIT',
|
|
144
|
+
MARKET = 'MARKET',
|
|
145
|
+
STOP_LOSS = 'STOP_LOSS',
|
|
146
|
+
STOP_LOSS_LIMIT = 'STOP_LOSS_LIMIT',
|
|
147
|
+
TAKE_PROFIT = 'TAKE_PROFIT',
|
|
148
|
+
TAKE_PROFIT_LIMIT = 'TAKE_PROFIT_LIMIT',
|
|
149
|
+
LIMIT_MAKER = 'LIMIT_MAKER'
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export enum BinanceTimeInForce {
|
|
153
|
+
GOOD_TILL_CANCELED = 'GTC',
|
|
154
|
+
INMEDIATE_OR_CANCEL = 'IOC',
|
|
155
|
+
FILL_OR_KILL = 'FOK'
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export enum BinanceOrderStatus {
|
|
159
|
+
NEW = 'NEW',
|
|
160
|
+
FILLED = 'FILLED',
|
|
161
|
+
CANCELED = 'CANCELED',
|
|
162
|
+
PARTIALLY_FILLED = 'PARTIALLY_FILLED'
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export enum BinanceCancelRestrictions {
|
|
166
|
+
ONLY_NEW = 'ONLY_NEW',
|
|
167
|
+
ONLY_PARTIALLY_FILLED = 'ONLY_PARTIALLY_FILLED'
|
|
168
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { RequestAxiosCall } from "itrm-tools";
|
|
2
|
+
import { BinanceConfig, BinanceConnection } from "../BinanceConnection";
|
|
3
|
+
import { BinanceCancelRestrictions, BinanceOrder, BinanceOrderSide, BinanceOrderType, BinanceParams, BinanceResponse, BinanceSymbolStatus, BinanceTimeInForce } from "../BinanceTypes";
|
|
4
|
+
|
|
5
|
+
export interface BinanceBookTicker {
|
|
6
|
+
symbol: string;
|
|
7
|
+
bidPrice: number;
|
|
8
|
+
bidQty: number;
|
|
9
|
+
askPrice: number;
|
|
10
|
+
askQty: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export class BinanceOperationConnection extends BinanceConnection {
|
|
14
|
+
constructor(config: BinanceConfig) {
|
|
15
|
+
super(config);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
public async getBookTicker(symbols: string[]) {
|
|
19
|
+
const url = `${this.config.baseUrl}/api/v3/ticker/bookTicker?symbols=${JSON.stringify(symbols)}`;
|
|
20
|
+
let response = await RequestAxiosCall.get<{ symbol: string; bidPrice: string; bidQty: string; askPrice: string; askQty: string; }[]>(url);
|
|
21
|
+
let result: BinanceBookTicker[] = [];
|
|
22
|
+
for (let value of response)
|
|
23
|
+
result.push({
|
|
24
|
+
symbol: value.symbol,
|
|
25
|
+
askPrice: parseFloat(value.askPrice),
|
|
26
|
+
askQty: parseFloat(value.askQty),
|
|
27
|
+
bidPrice: parseFloat(value.bidPrice),
|
|
28
|
+
bidQty: parseFloat(value.bidQty)
|
|
29
|
+
});
|
|
30
|
+
return result;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
public async createOrder(params: {
|
|
34
|
+
symbol: string;
|
|
35
|
+
side: BinanceOrderSide;
|
|
36
|
+
type: BinanceOrderType;
|
|
37
|
+
timeInForce: BinanceTimeInForce;
|
|
38
|
+
quantity?: number;
|
|
39
|
+
quoteOrderQty?: number;
|
|
40
|
+
price?: number;
|
|
41
|
+
recvWindow?: number;
|
|
42
|
+
}): Promise<BinanceOrder> {
|
|
43
|
+
let response = await this.sendPostRequest('/api/v3/order', params);
|
|
44
|
+
return this.parseOrder(response);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
private parseOrder(response: BinanceResponse) {
|
|
48
|
+
return {
|
|
49
|
+
symbol: response.symbol!,
|
|
50
|
+
orderId: response.orderId!,
|
|
51
|
+
orderListId: response.orderListId!,
|
|
52
|
+
clientOrderId: response.clientOrderId!,
|
|
53
|
+
transactTime: response.transactTime!,
|
|
54
|
+
price: parseFloat(response.price!),
|
|
55
|
+
origQty: parseFloat(response.origQty!),
|
|
56
|
+
executedQty: parseFloat(response.executedQty!),
|
|
57
|
+
origQuoteOrderQty: parseFloat(response.origQuoteOrderQty!),
|
|
58
|
+
cummulativeQuoteQty: parseFloat(response.cummulativeQuoteQty!),
|
|
59
|
+
status: response.status!,
|
|
60
|
+
timeInForce: response.timeInForce!,
|
|
61
|
+
type: response.type!,
|
|
62
|
+
side: response.side!,
|
|
63
|
+
workingTime: response.workingTime!,
|
|
64
|
+
fills: response.fills ? this.parseOrderFills(response.fills!) : [],
|
|
65
|
+
selfTradePreventionMode: response.selfTradePreventionMode!
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
private parseOrderFills(fills: { qty: string; price: string; tradeId: number; commission: string; commissionAsset: string; }[]): { qty: number; price: number; tradeId: number; commission: number; commissionAsset: string; }[] {
|
|
70
|
+
let result = [];
|
|
71
|
+
for (let fill of fills)
|
|
72
|
+
result.push({
|
|
73
|
+
qty: parseFloat(fill.qty),
|
|
74
|
+
price: parseFloat(fill.price),
|
|
75
|
+
tradeId: fill.tradeId,
|
|
76
|
+
commission: parseFloat(fill.commission),
|
|
77
|
+
commissionAsset: fill.commissionAsset
|
|
78
|
+
});
|
|
79
|
+
return result;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
public async cancelOrder(params: {
|
|
83
|
+
symbol: string;
|
|
84
|
+
orderId?: number;
|
|
85
|
+
origClientOrderId?: string;
|
|
86
|
+
newClientOrderId?: string;
|
|
87
|
+
cancelRestrictions?: BinanceCancelRestrictions;
|
|
88
|
+
recvWindow?: number
|
|
89
|
+
}) {
|
|
90
|
+
let response = await this.sendDeleteRequest('/api/v3/order', params);
|
|
91
|
+
return this.parseOrder(response);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
public async getOrder(params: {
|
|
95
|
+
symbol: string;
|
|
96
|
+
orderId?: number;
|
|
97
|
+
origClientOrderId?: string;
|
|
98
|
+
recvWindow?: number;
|
|
99
|
+
}) {
|
|
100
|
+
let response = await this.sendGetRequest('/api/v3/order', params);
|
|
101
|
+
return this.parseOrder(response);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
public async getOpenOrders(params: { symbol?: string; recvWindow?: number }) {
|
|
105
|
+
let response = (await this.sendGetRequest('/api/v3/openOrders', params) as BinanceResponse[]);
|
|
106
|
+
let result = [];
|
|
107
|
+
for (let order of response)
|
|
108
|
+
result.push(this.parseOrder(order));
|
|
109
|
+
return result;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
public async transferToSubaccountOfSameMaster(params: {
|
|
113
|
+
toEmail: string;
|
|
114
|
+
asset: string;
|
|
115
|
+
amount: number;
|
|
116
|
+
recvWindow?: number;
|
|
117
|
+
}) {
|
|
118
|
+
//const signature = CryptoJS.HmacSHA256(queryString, SUB_ACCOUNT_SECRET).toString();
|
|
119
|
+
let response = (await this.sendSapiPostRequest('/sapi/v1/sub-account/transfer/subToSub', params));
|
|
120
|
+
return response;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export class BinanceRfqConnection {}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { ExternalApiConnection, RequestMethod, StandardKeyGroup } from "itrm-tools";
|
|
2
|
+
|
|
3
|
+
export interface CetusStrategy {
|
|
4
|
+
id: number;
|
|
5
|
+
name: string;
|
|
6
|
+
settings: any;
|
|
7
|
+
memory: any;
|
|
8
|
+
account: string;
|
|
9
|
+
pair: string;
|
|
10
|
+
intermediary: string;
|
|
11
|
+
status: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export enum StrategyStatus {
|
|
15
|
+
RUNNING = "RUNNING",
|
|
16
|
+
BUY_ONLY = "BUY_ONLY",
|
|
17
|
+
SELL_ONLY = "SELL_ONLY",
|
|
18
|
+
STOPPED = "STOPPED"
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export class CetusStrategyApiConnection extends ExternalApiConnection {
|
|
22
|
+
constructor(apiUrl: string, keys: StandardKeyGroup) {
|
|
23
|
+
super("cetus", apiUrl, keys);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public async getStrategies(params: { name: string; pair: string; account: string }) {
|
|
27
|
+
const queryString = new URLSearchParams(params as any).toString();
|
|
28
|
+
return this.request<CetusStrategy[]>(RequestMethod.GET, `/v1/strategies?${queryString}`);
|
|
29
|
+
}
|
|
30
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { BinanceApiSigner as BinanceApiSigner } from './binance/BinanceApiSigner';
|
|
2
|
+
export { BinanceConnection as BinanceConnection } from './binance/BinanceConnection';
|
|
3
|
+
export { BinanceOperationConnection as BinanceOperationConnection } from './binance/connection/BinanceOperationConnection';
|
|
4
|
+
export { BinanceParams as BinanceParams, BinanceResponse as BinanceResponse, BinanceAccountResponse as BinanceAccountResponse, BinanceSymbolStatus as BinanceSymbolStatus, BinanceOrderSide as BinanceOrderSide, BinanceOrderType as BinanceOrderType, BinanceTimeInForce as BinanceTimeInForce, BinanceOrder as BinanceOrder, BinanceOrderStatus as BinanceOrderStatus, BinanceCancelRestrictions as BinanceCancelRestrictions } from './binance/BinanceTypes';
|
|
5
|
+
|
|
6
|
+
export { CetusStrategyApiConnection as CetusStrategyApiConnection, CetusStrategy as CetusStrategy, StrategyStatus as StrategyStatus } from './cetus/CetusStrategyApiConnection';
|