@metaflux/fluxaction 0.1.3 → 0.1.5

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.
@@ -1,4 +1,7 @@
1
- export class RiskManager {
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RiskManager = void 0;
4
+ class RiskManager {
2
5
  constructor(cfg) {
3
6
  this.orders = new Map();
4
7
  this.started = false;
@@ -50,10 +53,11 @@ export class RiskManager {
50
53
  return [...this.orders.values()];
51
54
  }
52
55
  startPolling() {
56
+ var _a;
53
57
  if (this.started)
54
58
  return;
55
59
  this.started = true;
56
- const interval = this.cfg.pollIntervalMs ?? 3000;
60
+ const interval = (_a = this.cfg.pollIntervalMs) !== null && _a !== void 0 ? _a : 3000;
57
61
  setInterval(() => {
58
62
  this.tick().catch((e) => {
59
63
  console.error('[RiskManager] tick error', e);
@@ -127,6 +131,7 @@ export class RiskManager {
127
131
  }
128
132
  }
129
133
  async executeAndClear(ex, o, price) {
134
+ var _a, _b;
130
135
  console.log('[RiskManager] EXIT', {
131
136
  exchangeId: ex.id,
132
137
  symbol: o.symbol,
@@ -145,7 +150,7 @@ export class RiskManager {
145
150
  }
146
151
  const bal = await ex.fetchBalance();
147
152
  const base = o.symbol.split('/')[0];
148
- const free = bal[base]?.free ?? 0;
153
+ const free = (_b = (_a = bal[base]) === null || _a === void 0 ? void 0 : _a.free) !== null && _b !== void 0 ? _b : 0;
149
154
  const rawQty = Math.min(o.qty, free * 0.995);
150
155
  if (rawQty <= 0) {
151
156
  console.warn('[RiskManager] skip exit: qty <= 0', { symbol: o.symbol, base, free, requested: o.qty });
@@ -177,3 +182,4 @@ export class RiskManager {
177
182
  }
178
183
  }
179
184
  }
185
+ exports.RiskManager = RiskManager;
@@ -1 +1,2 @@
1
- export {};
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1 +1,2 @@
1
- export {};
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1 +1,2 @@
1
- export {};
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,9 +1,16 @@
1
- import ccxt from 'ccxt';
2
- export class GateSpotAdapter {
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.GateSpotAdapter = void 0;
7
+ const ccxt_1 = __importDefault(require("ccxt"));
8
+ class GateSpotAdapter {
3
9
  constructor(cfg) {
10
+ var _a;
4
11
  this.id = cfg.id;
5
- this.label = cfg.label ?? 'gate-spot';
6
- this.ex = new ccxt.gateio({
12
+ this.label = (_a = cfg.label) !== null && _a !== void 0 ? _a : 'gate-spot';
13
+ this.ex = new ccxt_1.default.gateio({
7
14
  apiKey: cfg.apiKey,
8
15
  secret: cfg.secret,
9
16
  enableRateLimit: true,
@@ -29,14 +36,16 @@ export class GateSpotAdapter {
29
36
  await this.ex.loadMarkets();
30
37
  }
31
38
  hasSpotMarket(symbol) {
32
- const m = this.ex.markets?.[symbol];
39
+ var _a;
40
+ const m = (_a = this.ex.markets) === null || _a === void 0 ? void 0 : _a[symbol];
33
41
  return !!m && m.type === 'spot';
34
42
  }
35
43
  hasFuturesMarket() {
36
44
  return false;
37
45
  }
38
46
  getMarket(symbol) {
39
- const m = this.ex.markets?.[symbol];
47
+ var _a, _b, _c, _d, _e, _f, _g;
48
+ const m = (_a = this.ex.markets) === null || _a === void 0 ? void 0 : _a[symbol];
40
49
  if (!m)
41
50
  return undefined;
42
51
  return {
@@ -44,26 +53,29 @@ export class GateSpotAdapter {
44
53
  base: m.base,
45
54
  quote: m.quote,
46
55
  type: m.type,
47
- precisionAmount: m.precision?.amount,
48
- precisionPrice: m.precision?.price,
49
- minCost: m.limits?.cost?.min,
50
- minAmount: m.limits?.amount?.min,
56
+ precisionAmount: (_b = m.precision) === null || _b === void 0 ? void 0 : _b.amount,
57
+ precisionPrice: (_c = m.precision) === null || _c === void 0 ? void 0 : _c.price,
58
+ minCost: (_e = (_d = m.limits) === null || _d === void 0 ? void 0 : _d.cost) === null || _e === void 0 ? void 0 : _e.min,
59
+ minAmount: (_g = (_f = m.limits) === null || _f === void 0 ? void 0 : _f.amount) === null || _g === void 0 ? void 0 : _g.min,
51
60
  };
52
61
  }
53
62
  roundAmount(symbol, amount) {
54
- const m = this.ex.markets?.[symbol];
63
+ var _a;
64
+ const m = (_a = this.ex.markets) === null || _a === void 0 ? void 0 : _a[symbol];
55
65
  if (!m)
56
66
  return amount;
57
67
  return Number(this.ex.amountToPrecision(symbol, amount));
58
68
  }
59
69
  roundPrice(symbol, price) {
60
- const m = this.ex.markets?.[symbol];
70
+ var _a;
71
+ const m = (_a = this.ex.markets) === null || _a === void 0 ? void 0 : _a[symbol];
61
72
  if (!m)
62
73
  return price;
63
74
  return Number(this.ex.priceToPrecision(symbol, price));
64
75
  }
65
76
  mapOrder(symbol, o) {
66
- const price = o.price ?? o.average ?? o.avgPrice;
77
+ var _a, _b;
78
+ const price = (_b = (_a = o.price) !== null && _a !== void 0 ? _a : o.average) !== null && _b !== void 0 ? _b : o.avgPrice;
67
79
  let baseAmount;
68
80
  if (price && o.cost) {
69
81
  baseAmount = o.cost / price;
@@ -89,8 +101,9 @@ export class GateSpotAdapter {
89
101
  };
90
102
  }
91
103
  async fetchTicker(symbol) {
104
+ var _a;
92
105
  const t = await this.ex.fetchTicker(symbol);
93
- return { last: t.last ?? 0 };
106
+ return { last: (_a = t.last) !== null && _a !== void 0 ? _a : 0 };
94
107
  }
95
108
  async fetchBalance() {
96
109
  const bal = await this.ex.fetchBalance();
@@ -109,7 +122,7 @@ export class GateSpotAdapter {
109
122
  throw new Error(`gate-spot invalid price for ${symbol}: ${ticker.last}`);
110
123
  }
111
124
  const m = this.getMarket(symbol);
112
- if (m?.minCost && quoteCost < m.minCost) {
125
+ if ((m === null || m === void 0 ? void 0 : m.minCost) && quoteCost < m.minCost) {
113
126
  throw new Error(`gate-spot buy violates minCost: ${quoteCost} < ${m.minCost} ${m.quote}`);
114
127
  }
115
128
  const order = await this.ex.createMarketBuyOrderWithCost(symbol, quoteCost, {
@@ -144,3 +157,4 @@ export class GateSpotAdapter {
144
157
  return [];
145
158
  }
146
159
  }
160
+ exports.GateSpotAdapter = GateSpotAdapter;
package/dist/index.js CHANGED
@@ -1,19 +1,23 @@
1
- import { RiskManager } from './core/RiskManager';
2
- import { GateSpotAdapter } from './exchanges/GateSpotAdapter';
3
- export class FluxAction {
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GateSpotAdapter = exports.RiskManager = exports.FluxAction = void 0;
4
+ const RiskManager_1 = require("./core/RiskManager");
5
+ const GateSpotAdapter_1 = require("./exchanges/GateSpotAdapter");
6
+ class FluxAction {
4
7
  constructor(cfg) {
8
+ var _a;
5
9
  this.adapters = {};
6
10
  if (cfg.gate) {
7
- const gate = new GateSpotAdapter({
11
+ const gate = new GateSpotAdapter_1.GateSpotAdapter({
8
12
  id: 'gate-spot',
9
13
  apiKey: cfg.gate.apiKey,
10
14
  secret: cfg.gate.secret,
11
15
  });
12
16
  this.adapters[gate.id] = gate;
13
17
  }
14
- this.risk = new RiskManager({
18
+ this.risk = new RiskManager_1.RiskManager({
15
19
  exchanges: this.adapters,
16
- pollIntervalMs: cfg.risk?.pollIntervalMs,
20
+ pollIntervalMs: (_a = cfg.risk) === null || _a === void 0 ? void 0 : _a.pollIntervalMs,
17
21
  });
18
22
  }
19
23
  async init() {
@@ -23,12 +27,13 @@ export class FluxAction {
23
27
  return this.adapters[id];
24
28
  }
25
29
  async openSpotPosition(params) {
30
+ var _a;
26
31
  const ex = this.requireExchange(params.exchangeId);
27
32
  const order = await ex.marketBuySpot(params.symbol, params.quoteCost);
28
33
  return {
29
34
  orderId: order.id,
30
35
  filledBase: order.amount,
31
- avgPrice: order.price ?? 0,
36
+ avgPrice: (_a = order.price) !== null && _a !== void 0 ? _a : 0,
32
37
  };
33
38
  }
34
39
  async closeSpotMarket(params) {
@@ -66,5 +71,8 @@ export class FluxAction {
66
71
  return ex;
67
72
  }
68
73
  }
69
- export { RiskManager } from './core/RiskManager';
70
- export { GateSpotAdapter, } from './exchanges/GateSpotAdapter';
74
+ exports.FluxAction = FluxAction;
75
+ var RiskManager_2 = require("./core/RiskManager");
76
+ Object.defineProperty(exports, "RiskManager", { enumerable: true, get: function () { return RiskManager_2.RiskManager; } });
77
+ var GateSpotAdapter_2 = require("./exchanges/GateSpotAdapter");
78
+ Object.defineProperty(exports, "GateSpotAdapter", { enumerable: true, get: function () { return GateSpotAdapter_2.GateSpotAdapter; } });
package/package.json CHANGED
@@ -1,9 +1,8 @@
1
1
  {
2
2
  "name": "@metaflux/fluxaction",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "Programmatic spot trading actions (TP/SL/trailing) for crypto exchanges",
5
5
  "main": "dist/index.js",
6
- "module": "dist/index.js",
7
6
  "types": "dist/index.d.ts",
8
7
  "publishConfig": {
9
8
  "access": "public"
package/tsconfig.json CHANGED
@@ -1,14 +1,13 @@
1
1
  {
2
2
  "compilerOptions": {
3
- "target": "ES2020",
4
- "module": "ES2020",
3
+ "target": "ES2019",
4
+ "module": "CommonJS",
5
5
  "moduleResolution": "Node",
6
6
  "declaration": true,
7
7
  "outDir": "dist",
8
8
  "rootDir": "src",
9
- "strict": true,
10
9
  "esModuleInterop": true,
11
- "resolveJsonModule": true,
10
+ "strict": true,
12
11
  "skipLibCheck": true
13
12
  },
14
13
  "include": ["src"]