@steerprotocol/app-loader 0.2.2 → 0.2.3

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.
Files changed (2) hide show
  1. package/lib/index.js +123 -65
  2. package/package.json +2 -2
package/lib/index.js CHANGED
@@ -48,6 +48,7 @@ let _fetchPOSTSyncPtr = 0;
48
48
  let _ohlcvPtr = 0;
49
49
  let ASYNCIFY_PTR = 0;
50
50
  let ASYNCIFY_MEM;
51
+ let ASYNCIFY_INITIALIZED = false;
51
52
  let _exports;
52
53
  function instantiate(module, imports = {}) {
53
54
  return __awaiter(this, void 0, void 0, function* () {
@@ -55,10 +56,13 @@ function instantiate(module, imports = {}) {
55
56
  // @ts-ignore
56
57
  env: Object.assign(Object.create(globalThis), imports.env || {}, {
57
58
  _initAsyncify(frame_ptr, stack_ptr) {
59
+ if (ASYNCIFY_INITIALIZED)
60
+ return;
58
61
  ASYNCIFY_PTR = frame_ptr;
59
62
  ASYNCIFY_MEM[ASYNCIFY_PTR >> 2] = ASYNCIFY_PTR + 8;
60
63
  // I don't know if I need to reserve all this memory...
61
64
  ASYNCIFY_MEM[ASYNCIFY_PTR + 4 >> 2] = stack_ptr;
65
+ ASYNCIFY_INITIALIZED = true;
62
66
  },
63
67
  abort(message, fileName, lineNumber, columnNumber) {
64
68
  (() => {
@@ -69,89 +73,64 @@ function instantiate(module, imports = {}) {
69
73
  const _data = __liftString(data) || '[]';
70
74
  const _candleSize = __liftString(candleSize) || '69m';
71
75
  const candles = generateCandles(JSON.parse(_data), _candleSize);
72
- // We could pack this data into binary before sending...
73
- // Much faster
74
- // Which are 32 bit and which are 64?
75
- // I'll assume 32 bits for now though I believe timestamp should be 64
76
- /*
77
- const decodedCandles: Candle[] = [];
78
-
79
- for (let i = 0; i < __liftBuffer(data).byteLength >> 2; i++) {
80
- decodedCandles.push(decodeCandle(new Uint32Array(data, i * 6, 6)));
81
- }
82
- const candles =
83
-
84
- let buffer = new Uint32Array(candles.length * 6);
85
- for (let i = 0; i < candles.length; i++) {
86
- const candle = candles[i];
87
- // Pack it into
88
-
89
- // Structure: [timestamp, high, low, open, close, volume]
90
- // Stride: 24 bytes
91
- // This is essentially what FASS does
92
-
93
- buffer.set(
94
- encodeCandle(candle),
95
- i
96
- );
97
- }
98
- return __lowerBuffer(buffer);*/
99
76
  return __lowerString(JSON.stringify(candles));
100
77
  },
101
78
  'console.log': (text) => {
102
79
  console.log(__liftString(text));
103
80
  },
104
81
  ccxt_fetchOHLCV: (exchangeId, symbol, timeframe, limit, since) => {
82
+ // Since may need to be passed as 64-bit?
83
+ // Timestamps are large
105
84
  // @ts-ignore
106
85
  const currentState = _exports.asyncify_get_state();
107
86
  if (currentState === 2) {
87
+ //console.log("asyncify_stop_rewind() [resume wasm]");
108
88
  // @ts-ignore
109
89
  _exports.asyncify_stop_rewind();
110
90
  return _ohlcvPtr;
111
91
  }
112
92
  else if (currentState === 0) {
93
+ //console.log("asyncify_start_unwind() [pause wasm]");
113
94
  // @ts-ignore
114
95
  _exports.asyncify_start_unwind(ASYNCIFY_PTR);
115
- const _exchangeId = __liftString(exchangeId);
96
+ // How about we pass an Enum back and forth?
97
+ const _exchange = __liftString(exchangeId);
116
98
  const _symbol = __liftString(symbol);
117
99
  const _timeframe = __liftString(timeframe);
118
- const _since = since;
119
- const _limit = limit;
120
- if (_exchangeId &&
121
- _symbol &&
122
- _timeframe &&
123
- _since &&
124
- _limit) {
125
- try {
126
- // @ts-ignore
127
- _exports.asyncify_stop_unwind();
128
- // @ts-ignore
129
- new ccxt_1.default[exchangeId]({
130
- apiKey: "",
131
- secret: ""
132
- }).fetchOHLCV(_symbol, _timeframe, _limit, _since).then((data) => {
133
- // @ts-ignore
134
- _exports.asyncify_start_rewind(ASYNCIFY_PTR);
135
- _ohlcvPtr = __lowerString(JSON.stringify(data));
136
- _exports.execute();
137
- });
138
- }
139
- catch (e) {
140
- // @ts-ignore
141
- _exports.asyncify_start_rewind(ASYNCIFY_PTR);
142
- process.env.DEBUG && console.error(e);
143
- _exports.execute();
144
- }
145
- }
100
+ if (!_exchange || !_symbol || !_timeframe)
101
+ throw new Error("Exchange, Symbol, or Timeframe not provided when fetching OHCLV data.");
102
+ // @ts-ignore
103
+ new ccxt_1.default[_exchange]({
104
+ apiKey: "",
105
+ secret: ""
106
+ }).fetchOHLCV(_symbol, _timeframe, since, limit).then((data) => {
107
+ //console.log("asyncify_stop_unwind() [unpause wasm]");
108
+ // @ts-ignore
109
+ _exports.asyncify_stop_unwind();
110
+ // @ts-ignore: __new is defined
111
+ _ohlcvPtr = __lowerStaticArray((pointer, value) => { __setU32(pointer, __lowerStaticArray(__setF64, 7, 3, value, Float64Array)); }, 8, 2, data);
112
+ //console.log("asyncify_start_rewind() [resuming wasm]");
113
+ // @ts-ignore
114
+ _exports.asyncify_start_rewind(ASYNCIFY_PTR);
115
+ _exports.execute();
116
+ });
146
117
  }
147
118
  }
148
119
  }),
120
+ console: {
121
+ log: (msg) => {
122
+ console.log(__liftString(msg >>> 0));
123
+ }
124
+ },
149
125
  'as-fetch': {
150
126
  _initAsyncify(frame_ptr, stack_ptr) {
127
+ if (ASYNCIFY_INITIALIZED)
128
+ return;
151
129
  ASYNCIFY_PTR = frame_ptr;
152
130
  ASYNCIFY_MEM[ASYNCIFY_PTR >> 2] = ASYNCIFY_PTR + 8;
153
131
  // I don't know if I need to reserve all this memory...
154
132
  ASYNCIFY_MEM[ASYNCIFY_PTR + 4 >> 2] = stack_ptr;
133
+ ASYNCIFY_INITIALIZED = true;
155
134
  },
156
135
  _fetchPOSTSync(url, mode, headers, body) {
157
136
  var _a;
@@ -280,6 +259,61 @@ function instantiate(module, imports = {}) {
280
259
  }
281
260
  },
282
261
  }, exports);
262
+ let __dataview = new DataView(memory.buffer);
263
+ function __liftArray(liftElement, align, pointer) {
264
+ if (!pointer)
265
+ return null;
266
+ const dataStart = __getU32(pointer + 4), length = __dataview.getUint32(pointer + 12, true), values = new Array(length);
267
+ for (let i = 0; i < length; ++i)
268
+ values[i] = liftElement(dataStart + (i << align));
269
+ return values;
270
+ }
271
+ function __lowerArray(lowerElement, id, align, values) {
272
+ if (values == null)
273
+ return 0;
274
+ const length = values.length,
275
+ // @ts-ignore
276
+ buffer = __retain(exports.__new(length << align, 1)),
277
+ // @ts-ignore
278
+ header = __retain(exports.__new(16, id));
279
+ __setU32(header + 0, buffer);
280
+ __dataview.setUint32(header + 4, buffer, true);
281
+ __dataview.setUint32(header + 8, length << align, true);
282
+ __dataview.setUint32(header + 12, length, true);
283
+ for (let i = 0; i < length; ++i)
284
+ lowerElement(buffer + (i << align), values[i]);
285
+ // @ts-ignore
286
+ //exports.__unpin(buffer);
287
+ // @ts-ignore
288
+ //exports.__unpin(header);
289
+ return header;
290
+ }
291
+ function __lowerStaticArray(lowerElement, id, align, values, typedConstructor) {
292
+ if (values == null)
293
+ return 0;
294
+ const length = values.length,
295
+ // @ts-ignore: __pin and __new are defined
296
+ buffer = _exports.__pin(_exports.__new(length << align, id)) >>> 0;
297
+ if (typedConstructor) {
298
+ new typedConstructor(_exports.memory.buffer, buffer, length).set(values);
299
+ }
300
+ else {
301
+ for (let i = 0; i < length; i++)
302
+ lowerElement(buffer + (i << align >>> 0), values[i]);
303
+ }
304
+ // @ts-ignore: __unpin is defined
305
+ _exports.__unpin(buffer);
306
+ return buffer;
307
+ }
308
+ function __setF64(pointer, value) {
309
+ try {
310
+ __dataview.setFloat64(pointer, value, true);
311
+ }
312
+ catch (_a) {
313
+ __dataview = new DataView(memory.buffer);
314
+ __dataview.setFloat64(pointer, value, true);
315
+ }
316
+ }
283
317
  function __liftBuffer(pointer) {
284
318
  if (!pointer)
285
319
  return null;
@@ -313,15 +347,6 @@ function instantiate(module, imports = {}) {
313
347
  memoryU16[(pointer >>> 1) + i] = value.charCodeAt(i);
314
348
  return pointer;
315
349
  }
316
- function __liftArray(liftElement, align, pointer) {
317
- if (!pointer)
318
- return null;
319
- const dataStart = __getU32(pointer + 4), length = __dataview.getUint32(pointer + 12, true), values = new Array(length);
320
- for (let i = 0; i < length; ++i)
321
- values[i] = liftElement(dataStart + (i << align));
322
- return values;
323
- }
324
- let __dataview = new DataView(memory.buffer);
325
350
  function __getU32(pointer) {
326
351
  try {
327
352
  return __dataview.getUint32(pointer, true);
@@ -331,6 +356,39 @@ function instantiate(module, imports = {}) {
331
356
  return __dataview.getUint32(pointer, true);
332
357
  }
333
358
  }
359
+ function __setU32(pointer, value) {
360
+ try {
361
+ __dataview.setUint32(pointer, value, true);
362
+ }
363
+ catch (_a) {
364
+ __dataview = new DataView(memory.buffer);
365
+ __dataview.setUint32(pointer, value, true);
366
+ }
367
+ }
368
+ const refcounts = new Map();
369
+ function __retain(pointer) {
370
+ if (pointer) {
371
+ const refcount = refcounts.get(pointer);
372
+ if (refcount)
373
+ refcounts.set(pointer, refcount + 1);
374
+ // @ts-ignore: __pin is defined
375
+ else
376
+ refcounts.set(exports.__pin(pointer), 1);
377
+ }
378
+ return pointer;
379
+ }
380
+ function __release(pointer) {
381
+ if (pointer) {
382
+ const refcount = refcounts.get(pointer);
383
+ // @ts-ignore: __unpin is defined
384
+ if (refcount === 1)
385
+ exports.__unpin(pointer), refcounts.delete(pointer);
386
+ else if (refcount)
387
+ refcounts.set(pointer, refcount - 1);
388
+ else
389
+ throw Error(`invalid refcount '${refcount}' for reference '${pointer}'`);
390
+ }
391
+ }
334
392
  function generateCandles(data, candleSize) {
335
393
  const candleWidth = (0, timestring_1.default)(candleSize, 'ms', {});
336
394
  if (data.length === 0) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@steerprotocol/app-loader",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "description": "App Loader for Steer Protocol",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -46,7 +46,7 @@
46
46
  "lib/**/*"
47
47
  ],
48
48
  "dependencies": {
49
- "ccxt": "^3.0.30",
49
+ "ccxt": "^4.0.52",
50
50
  "timestring": "^7.0.0"
51
51
  }
52
52
  }