@steerprotocol/app-loader 1.0.3 → 1.1.1

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/lib/Candle.js CHANGED
@@ -20,14 +20,7 @@ class Candle {
20
20
  }
21
21
  exports.Candle = Candle;
22
22
  function encodeCandle(candle) {
23
- return new Uint32Array([
24
- candle.timestamp,
25
- candle.high,
26
- candle.low,
27
- candle.open,
28
- candle.close,
29
- candle.volume
30
- ]);
23
+ return new Uint32Array([candle.timestamp, candle.high, candle.low, candle.open, candle.close, candle.volume]);
31
24
  }
32
25
  exports.encodeCandle = encodeCandle;
33
26
  function decodeCandle(buffer) {
@@ -0,0 +1,14 @@
1
+ import { RawTradeData } from './RawTradeData';
2
+ export declare class Candle {
3
+ timestamp: number;
4
+ high: number;
5
+ low: number;
6
+ open: number;
7
+ close: number;
8
+ volume: number;
9
+ constructor(timestamp: number, high: number, low: number, open: number, close: number, volume: number);
10
+ toString(): string;
11
+ }
12
+ export declare function encodeCandle(candle: Candle): Uint32Array;
13
+ export declare function decodeCandle(buffer: Uint32Array): Candle;
14
+ export declare function generateCandles(data: RawTradeData[], candleSize: string): Candle[];
@@ -0,0 +1,75 @@
1
+ import timestring from 'timestring';
2
+ export class Candle {
3
+ timestamp;
4
+ high;
5
+ low;
6
+ open;
7
+ close;
8
+ volume;
9
+ constructor(timestamp, high, low, open, close, volume) {
10
+ this.timestamp = timestamp;
11
+ this.high = high;
12
+ this.low = low;
13
+ this.open = open;
14
+ this.close = close;
15
+ this.volume = volume;
16
+ }
17
+ toString() {
18
+ return JSON.stringify(this);
19
+ }
20
+ }
21
+ export function encodeCandle(candle) {
22
+ return new Uint32Array([candle.timestamp, candle.high, candle.low, candle.open, candle.close, candle.volume]);
23
+ }
24
+ export function decodeCandle(buffer) {
25
+ return new Candle(buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], buffer[5]);
26
+ }
27
+ export function generateCandles(data, candleSize) {
28
+ const candleWidth = timestring(candleSize, 'ms', {});
29
+ if (data.length === 0) {
30
+ throw new Error('Input data is empty');
31
+ }
32
+ const _data = data.map((point) => {
33
+ return Object.assign(Object.assign({}, point), {
34
+ // This will convert the timestamp to milliseconds if it's in seconds
35
+ timestamp: point.timestamp && String(point.timestamp).length == 10 ? point.timestamp * 1000 : point.timestamp,
36
+ });
37
+ });
38
+ if (_data[0] === undefined || Object.keys(_data[0]).length === 0) {
39
+ return [];
40
+ }
41
+ const ohlcvData = [];
42
+ let i = 0;
43
+ let currentTimestamp = Math.floor(_data[0].timestamp / candleWidth) * candleWidth;
44
+ let open = _data[0].price;
45
+ let high = _data[0].price;
46
+ let low = _data[0].price;
47
+ let close = _data[0].price;
48
+ let volume = 0.0;
49
+ while (i < _data.length) {
50
+ open = close;
51
+ high = close;
52
+ low = close;
53
+ volume = 0.0;
54
+ let innerI = i;
55
+ while (innerI < _data.length && _data[innerI].timestamp < currentTimestamp + candleWidth) {
56
+ open = innerI === 0 ? _data[innerI].price : open;
57
+ high = Math.max(high, _data[innerI].price);
58
+ low = Math.min(low, _data[innerI].price);
59
+ close = _data[innerI].price;
60
+ volume += _data[innerI].volume;
61
+ innerI++;
62
+ }
63
+ ohlcvData.push({
64
+ timestamp: currentTimestamp,
65
+ high,
66
+ low,
67
+ open,
68
+ close,
69
+ volume,
70
+ });
71
+ currentTimestamp += candleWidth;
72
+ i = innerI;
73
+ }
74
+ return ohlcvData;
75
+ }
@@ -0,0 +1,6 @@
1
+ export declare class RawTradeData {
2
+ timestamp: number;
3
+ price: number;
4
+ volume: number;
5
+ constructor(timestamp: number, price: number, volume: number);
6
+ }
@@ -0,0 +1,10 @@
1
+ export class RawTradeData {
2
+ timestamp;
3
+ price;
4
+ volume;
5
+ constructor(timestamp, price, volume) {
6
+ this.timestamp = timestamp;
7
+ this.price = price;
8
+ this.volume = volume;
9
+ }
10
+ }
@@ -0,0 +1,24 @@
1
+ declare function responseHandler(body: ArrayBuffer, statusCode: number, redirected: number, callbackID: number): void;
2
+ declare function initialize(config: string): void;
3
+ declare function config(): string;
4
+ declare function version(): number;
5
+ declare function transform(): string;
6
+ declare function execute(...params: number[] | string[] | ArrayBuffer[] | null[]): string;
7
+ declare function __new(size: number, id: number): number;
8
+ declare function __pin(ptr: number): number;
9
+ declare function __unpin(ptr: number): number;
10
+ declare function reset(): void;
11
+ export type WasmModule = {
12
+ responseHandler: typeof responseHandler;
13
+ initialize: typeof initialize;
14
+ execute: typeof execute;
15
+ config: typeof config;
16
+ version: typeof version;
17
+ transform: typeof transform;
18
+ memory: WebAssembly.Memory;
19
+ __new: typeof __new;
20
+ __pin: typeof __pin;
21
+ __unpin: typeof __unpin;
22
+ reset: typeof reset;
23
+ };
24
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,18 @@
1
+ import { Candle } from './Candle';
2
+ import { RawTradeData } from './RawTradeData';
3
+ import { WasmModule } from './WasmModule';
4
+ export { Candle, RawTradeData, WasmModule };
5
+ /**
6
+ * Load a wasm bundle synchronously. Only accepts the actual binary data.
7
+ * @param input - Wasm bundle data
8
+ * @param imports - Imports
9
+ * @returns
10
+ */
11
+ export declare function loadWasmSync(input: ArrayBuffer, imports?: {}): WasmModule;
12
+ /**
13
+ * Load a wasm bundle asynchronously. Accepts the actual binary data or file path.
14
+ * @param input - Wasm bundle data or path
15
+ * @param imports - Imports
16
+ * @returns
17
+ */
18
+ export declare function loadWasm(input: string | ArrayBuffer, imports?: {}): Promise<WasmModule>;
@@ -0,0 +1,397 @@
1
+ import { ethers } from 'ethers';
2
+ import { Candle, generateCandles } from './Candle';
3
+ import { RawTradeData } from './RawTradeData';
4
+ let isNode = process || null;
5
+ let fetchImpl = null;
6
+ let fsImpl = null;
7
+ let ccxt = null;
8
+ if (globalThis['fetch'])
9
+ fetchImpl = fetch;
10
+ export { Candle, RawTradeData };
11
+ var State;
12
+ (function (State) {
13
+ State[State["None"] = 0] = "None";
14
+ State[State["Unwinding"] = 1] = "Unwinding";
15
+ State[State["Rewinding"] = 2] = "Rewinding";
16
+ })(State || (State = {}));
17
+ /**
18
+ * Load a wasm bundle synchronously. Only accepts the actual binary data.
19
+ * @param input - Wasm bundle data
20
+ * @param imports - Imports
21
+ * @returns
22
+ */
23
+ export function loadWasmSync(input, imports = {}) {
24
+ return instantiate(new WebAssembly.Module(input), imports);
25
+ }
26
+ /**
27
+ * Load a wasm bundle asynchronously. Accepts the actual binary data or file path.
28
+ * @param input - Wasm bundle data or path
29
+ * @param imports - Imports
30
+ * @returns
31
+ */
32
+ export async function loadWasm(input, imports = {}) {
33
+ const mod = instantiate(await (async () => {
34
+ if (typeof input === 'string') {
35
+ if (isNode) {
36
+ if (!fetchImpl)
37
+ fetchImpl = (await import('undici')).fetch;
38
+ if (!fsImpl)
39
+ fsImpl = await import('fs/promises');
40
+ if (!ccxt)
41
+ ccxt = (await import('ccxt')).default;
42
+ return WebAssembly.compile(await fsImpl.readFile(input));
43
+ }
44
+ return await WebAssembly.compileStreaming(globalThis.fetch(input));
45
+ }
46
+ else {
47
+ return WebAssembly.compile(input);
48
+ }
49
+ })(), imports);
50
+ return mod;
51
+ }
52
+ function instantiate(module, imports = {}) {
53
+ let WASM_MEMORY;
54
+ let WASM_EXPORTS;
55
+ let WASM_DV;
56
+ let ASYNCIFY_PTR = 0;
57
+ let ASYNCIFY_MEM;
58
+ let ASYNCIFY_INITIALIZED = false;
59
+ let fetchFn = null;
60
+ let ccxtFn = null;
61
+ let detachedValue = null;
62
+ const adaptedImports = Object.assign({
63
+ console: {
64
+ log(text) {
65
+ console.log(__liftString(text));
66
+ },
67
+ },
68
+ ethers: {
69
+ keccak256_str(str_ptr) {
70
+ const str = __liftString(str_ptr);
71
+ const hash = ethers.keccak256(str);
72
+ return __lowerString(hash);
73
+ },
74
+ keccak256_buf(buf_ptr) {
75
+ const buf = __liftBuffer(buf_ptr);
76
+ const hash = ethers.keccak256(new Uint8Array(buf));
77
+ return __lowerString(hash);
78
+ },
79
+ },
80
+ // @ts-ignore
81
+ env: {
82
+ abort(message, fileName, lineNumber, columnNumber) {
83
+ (() => {
84
+ throw Error(`${__liftString(message)} in ${__liftString(fileName)}:${lineNumber}:${columnNumber}`);
85
+ })();
86
+ },
87
+ _initAsyncify(frame_ptr, stack_ptr) {
88
+ // @ts-ignore
89
+ if (!WASM_EXPORTS['asyncify_get_state'])
90
+ throw new Error('Asyncify initialized, but not enabled when run! Compile with the --runPasses asyncify flag or asyncify-shim transform!');
91
+ if (ASYNCIFY_INITIALIZED)
92
+ return;
93
+ ASYNCIFY_PTR = frame_ptr;
94
+ ASYNCIFY_MEM[ASYNCIFY_PTR >> 2] = ASYNCIFY_PTR + 8;
95
+ ASYNCIFY_MEM[(ASYNCIFY_PTR + 4) >> 2] = stack_ptr;
96
+ ASYNCIFY_INITIALIZED = true;
97
+ },
98
+ generateCandles(data, candleSize) {
99
+ const _data = __liftString(data) || '[]';
100
+ const _candleSize = __liftString(candleSize) || '69m';
101
+ const candles = generateCandles(JSON.parse(_data), _candleSize);
102
+ return __lowerString(JSON.stringify(candles));
103
+ },
104
+ 'console.log': (text) => {
105
+ console.log(__liftString(text));
106
+ },
107
+ ccxt_fetchOHLCV: (exchangeId, symbol, timeframe, limit, since) => {
108
+ // @ts-ignore
109
+ const currentState = WASM_EXPORTS.asyncify_get_state();
110
+ if (currentState === State.Rewinding) {
111
+ // @ts-ignore
112
+ WASM_EXPORTS.asyncify_stop_rewind();
113
+ // @ts-ignore
114
+ const ptr = __lowerStaticArray((pointer, value) => {
115
+ __setU32(pointer, __lowerStaticArray(__setF64, 7, 3, value, Float64Array));
116
+ }, 8, 2, detachedValue);
117
+ return ptr;
118
+ }
119
+ const _exchange = __liftString(exchangeId);
120
+ const _symbol = __liftString(symbol);
121
+ const _timeframe = __liftString(timeframe);
122
+ ccxtFn = async () => {
123
+ if (!_exchange || !_symbol || !_timeframe)
124
+ throw new Error('Exchange, Symbol, or Timeframe not provided when fetching OHCLV data.');
125
+ // @ts-ignore
126
+ const data = await new ccxt[_exchange]({
127
+ apiKey: '',
128
+ secret: '',
129
+ }).fetchOHLCV(_symbol, _timeframe, since, limit);
130
+ return data;
131
+ };
132
+ // @ts-ignore
133
+ WASM_EXPORTS.asyncify_start_unwind(ASYNCIFY_PTR);
134
+ },
135
+ },
136
+ 'as-fetch': {
137
+ _initAsyncify(frame_ptr, stack_ptr) {
138
+ // @ts-ignore
139
+ if (!WASM_EXPORTS['asyncify_get_state'])
140
+ throw new Error('Asyncify initialized, but not enabled when run! Compile with the --runPasses asyncify flag or asyncify-shim transform!');
141
+ if (ASYNCIFY_INITIALIZED)
142
+ return;
143
+ ASYNCIFY_PTR = frame_ptr;
144
+ ASYNCIFY_MEM[ASYNCIFY_PTR >> 2] = ASYNCIFY_PTR + 8;
145
+ // I don"t know if I need to reserve all this memory...
146
+ ASYNCIFY_MEM[(ASYNCIFY_PTR + 4) >> 2] = stack_ptr;
147
+ ASYNCIFY_INITIALIZED = true;
148
+ },
149
+ _fetchPOSTSync(url, mode, headers, body) {
150
+ // @ts-ignore
151
+ const currentState = WASM_EXPORTS.asyncify_get_state();
152
+ if (currentState === State.Rewinding) {
153
+ // @ts-ignore
154
+ WASM_EXPORTS.asyncify_stop_rewind();
155
+ const ptr = __lowerBuffer(detachedValue);
156
+ detachedValue = null;
157
+ return ptr;
158
+ }
159
+ fetchFn = async () => {
160
+ const res = await fetchImpl(__liftString(url) || '', {
161
+ method: 'POST',
162
+ mode: modeToString(mode) || 'cors',
163
+ headers: __liftArray((pointer) => __liftArray((pointer) => __liftString(__getU32(pointer)), 2, __getU32(pointer)), 2, headers) || [],
164
+ body: __liftBuffer(body),
165
+ });
166
+ const value = await res.arrayBuffer();
167
+ return value;
168
+ };
169
+ // @ts-ignore
170
+ WASM_EXPORTS.asyncify_start_unwind(ASYNCIFY_PTR);
171
+ },
172
+ _fetchGETSync(url, mode, headers) {
173
+ // @ts-ignore
174
+ const currentState = WASM_EXPORTS.asyncify_get_state();
175
+ if (currentState === State.Rewinding) {
176
+ // @ts-ignore
177
+ WASM_EXPORTS.asyncify_stop_rewind();
178
+ const ptr = __lowerBuffer(detachedValue);
179
+ detachedValue = null;
180
+ return ptr;
181
+ }
182
+ fetchFn = async () => {
183
+ const res = await fetchImpl(__liftString(url) || '', {
184
+ method: 'GET',
185
+ mode: modeToString(mode) || 'cors',
186
+ headers: __liftArray((pointer) => __liftArray((pointer) => __liftString(__getU32(pointer)), 2, __getU32(pointer)), 2, headers) || [],
187
+ });
188
+ const value = await res.arrayBuffer();
189
+ return value;
190
+ };
191
+ // @ts-ignore
192
+ WASM_EXPORTS.asyncify_start_unwind(ASYNCIFY_PTR);
193
+ },
194
+ _fetchGET(url, mode, headers, callbackID) {
195
+ fetchImpl(__liftString(url) || '', {
196
+ method: 'GET',
197
+ mode: modeToString(mode) || 'cors',
198
+ headers: __liftArray((pointer) => __liftArray((pointer) => __liftString(__getU32(pointer)), 2, __getU32(pointer)), 2, headers) || [],
199
+ }).then(async (res) => {
200
+ const body = await res.arrayBuffer();
201
+ WASM_EXPORTS.responseHandler(body, res.status, res.redirected ? 1 : 0, callbackID);
202
+ });
203
+ },
204
+ _fetchPOST(url, mode, headers, body, callbackID) {
205
+ fetchImpl(__liftString(url) || '', {
206
+ method: 'POST',
207
+ mode: modeToString(mode) || 'cors',
208
+ body: body,
209
+ headers: __liftArray((pointer) => __liftArray((pointer) => __liftString(__getU32(pointer)), 2, __getU32(pointer)), 2, headers) || [],
210
+ }).then(async (res) => {
211
+ const body = await res.arrayBuffer();
212
+ WASM_EXPORTS.responseHandler(body, res.status, res.redirected ? 1 : 0, callbackID);
213
+ });
214
+ },
215
+ },
216
+ }, imports);
217
+ const mod = new WebAssembly.Instance(module, adaptedImports);
218
+ WASM_EXPORTS = mod.exports;
219
+ // @ts-ignore
220
+ WASM_MEMORY = WASM_EXPORTS.memory || adaptedImports.env.memory;
221
+ ASYNCIFY_MEM = new Uint32Array(WASM_MEMORY.buffer);
222
+ const handledExports = Object.setPrototypeOf({
223
+ responseHandler(body, statusCode, redirected, callbackID) {
224
+ if (!WASM_EXPORTS['responseHandler'])
225
+ throw new Error('Unable to call .responseHandler on wasm module. Add the line export { responseHandler } from "as-fetch/assembly" to your entry file.');
226
+ // @ts-ignore
227
+ WASM_EXPORTS.responseHandler(__lowerBuffer(body), statusCode, redirected ? 1 : 0, callbackID);
228
+ },
229
+ initialize(config) {
230
+ if (!WASM_EXPORTS['initialize'])
231
+ throw new Error('Unable to call .initialize on wasm module. Are you sure this is a data connector?');
232
+ try {
233
+ // @ts-ignore
234
+ WASM_EXPORTS.initialize(__lowerString(config));
235
+ return true;
236
+ }
237
+ catch (e) {
238
+ console.error(e);
239
+ return false;
240
+ }
241
+ },
242
+ async execute(...params) {
243
+ if (!WASM_EXPORTS['execute'])
244
+ throw new Error('Unable to call .execute on wasm module. Are you sure this is a data connector?');
245
+ const loweredArgs = new Array(params.length);
246
+ for (let i = 0; i < params.length; i++) {
247
+ loweredArgs[i] = __lower(params[i]);
248
+ }
249
+ let result = WASM_EXPORTS.execute(...loweredArgs);
250
+ if (ASYNCIFY_INITIALIZED) {
251
+ // @ts-ignore
252
+ while (WASM_EXPORTS.asyncify_get_state() === State.Unwinding) {
253
+ // @ts-ignore
254
+ WASM_EXPORTS.asyncify_stop_unwind();
255
+ if (fetchFn)
256
+ detachedValue = await fetchFn();
257
+ if (ccxtFn)
258
+ detachedValue = await ccxtFn();
259
+ // @ts-ignore
260
+ WASM_EXPORTS.asyncify_start_rewind(ASYNCIFY_PTR);
261
+ result = WASM_EXPORTS.execute();
262
+ }
263
+ }
264
+ if (fetchFn)
265
+ fetchFn = null;
266
+ if (ccxtFn)
267
+ ccxtFn = null;
268
+ // @ts-ignore
269
+ return __liftString(result);
270
+ },
271
+ config() {
272
+ if (!WASM_EXPORTS['config'])
273
+ throw new Error('Unable to call .config on wasm module. Are you sure this is a data connector?');
274
+ return __liftString(WASM_EXPORTS.config());
275
+ },
276
+ transform() {
277
+ if (!WASM_EXPORTS['transform'])
278
+ throw new Error('Unable to call .transform on wasm module. Are you sure this is a data connector?');
279
+ return __liftString(WASM_EXPORTS.transform());
280
+ },
281
+ reset() {
282
+ if (WASM_EXPORTS['reset'])
283
+ WASM_EXPORTS.reset();
284
+ },
285
+ }, WASM_EXPORTS);
286
+ WASM_DV = new DataView(WASM_MEMORY.buffer);
287
+ function __lower(val) {
288
+ if (typeof val === 'number') {
289
+ return val;
290
+ }
291
+ else if (typeof val === 'string') {
292
+ return __lowerString(val);
293
+ }
294
+ else if (val instanceof ArrayBuffer) {
295
+ return __lowerBuffer(val);
296
+ }
297
+ else {
298
+ return 0;
299
+ }
300
+ }
301
+ function __liftString(ptr) {
302
+ if (!ptr)
303
+ return null;
304
+ const end = (ptr + new Uint32Array(WASM_MEMORY.buffer)[(ptr - 4) >>> 2]) >>> 1;
305
+ const memoryU16 = new Uint16Array(WASM_MEMORY.buffer);
306
+ let start = ptr >>> 1;
307
+ let string = '';
308
+ while (end - start > 1024)
309
+ string += String.fromCharCode(...memoryU16.subarray(start, (start += 1024)));
310
+ return string + String.fromCharCode(...memoryU16.subarray(start, end));
311
+ }
312
+ function __lowerString(value) {
313
+ if (value == null)
314
+ return 0;
315
+ const length = value.length;
316
+ const ptr = WASM_EXPORTS.__pin(WASM_EXPORTS.__new(length << 1, 2) >>> 0);
317
+ const memoryU16 = new Uint16Array(WASM_MEMORY.buffer);
318
+ for (let i = 0; i < length; ++i)
319
+ memoryU16[(ptr >>> 1) + i] = value.charCodeAt(i);
320
+ return ptr;
321
+ }
322
+ function __liftBuffer(ptr) {
323
+ if (!ptr)
324
+ return null;
325
+ return WASM_MEMORY.buffer.slice(ptr, ptr + new Uint32Array(WASM_MEMORY.buffer)[(ptr - 4) >>> 2]);
326
+ }
327
+ function __lowerBuffer(value) {
328
+ if (value == null)
329
+ return 0;
330
+ const ptr = WASM_EXPORTS.__pin(WASM_EXPORTS.__new(value.byteLength, 1) >>> 0);
331
+ new Uint8Array(WASM_MEMORY.buffer).set(new Uint8Array(value), ptr);
332
+ return ptr;
333
+ }
334
+ function __liftArray(liftElement, align, ptr) {
335
+ if (!ptr)
336
+ return null;
337
+ const dataStart = __getU32(ptr + 4);
338
+ const length = WASM_DV.getUint32(ptr + 12, true);
339
+ const values = new Array(length);
340
+ for (let i = 0; i < length; ++i)
341
+ values[i] = liftElement(dataStart + ((i << align) >>> 0));
342
+ return values;
343
+ }
344
+ function __lowerStaticArray(lowerElement, id, align, values, typedConstructor = null) {
345
+ if (values == null)
346
+ return 0;
347
+ const length = values.length;
348
+ const buffer = WASM_EXPORTS.__pin(WASM_EXPORTS.__new(length << align, id)) >>> 0;
349
+ if (typedConstructor) {
350
+ new typedConstructor(WASM_MEMORY.buffer, buffer, length).set(values);
351
+ }
352
+ else {
353
+ for (let i = 0; i < length; i++)
354
+ lowerElement(buffer + ((i << align) >>> 0), values[i]);
355
+ }
356
+ return buffer;
357
+ }
358
+ function __setU32(ptr, value) {
359
+ try {
360
+ WASM_DV.setUint32(ptr, value, true);
361
+ }
362
+ catch {
363
+ WASM_DV = new DataView(WASM_MEMORY.buffer);
364
+ WASM_DV.setUint32(ptr, value, true);
365
+ }
366
+ }
367
+ function __setF64(ptr, value) {
368
+ try {
369
+ WASM_DV.setFloat64(ptr, value, true);
370
+ }
371
+ catch {
372
+ WASM_DV = new DataView(WASM_MEMORY.buffer);
373
+ WASM_DV.setFloat64(ptr, value, true);
374
+ }
375
+ }
376
+ function __getU32(ptr) {
377
+ try {
378
+ return WASM_DV.getUint32(ptr, true);
379
+ }
380
+ catch {
381
+ WASM_DV = new DataView(WASM_MEMORY.buffer);
382
+ return WASM_DV.getUint32(ptr, true);
383
+ }
384
+ }
385
+ return handledExports;
386
+ }
387
+ function modeToString(mode) {
388
+ if (mode == 1)
389
+ return 'cors';
390
+ if (mode == 2)
391
+ return 'no-cors';
392
+ if (mode == 3)
393
+ return 'same-origin';
394
+ if (mode == 4)
395
+ return 'navigate';
396
+ return null;
397
+ }
@@ -0,0 +1 @@
1
+ {"type": "module"}
package/lib/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { Candle } from "./Candle";
2
- import { RawTradeData } from "./RawTradeData";
3
- import { WasmModule } from "./WasmModule";
1
+ import { Candle } from './Candle';
2
+ import { RawTradeData } from './RawTradeData';
3
+ import { WasmModule } from './WasmModule';
4
4
  export { Candle, RawTradeData, WasmModule };
5
5
  /**
6
6
  * Load a wasm bundle synchronously. Only accepts the actual binary data.
package/lib/index.js CHANGED
@@ -33,6 +33,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
33
33
  };
34
34
  Object.defineProperty(exports, "__esModule", { value: true });
35
35
  exports.loadWasm = exports.loadWasmSync = exports.RawTradeData = exports.Candle = void 0;
36
+ const ethers_1 = require("ethers");
36
37
  const Candle_1 = require("./Candle");
37
38
  Object.defineProperty(exports, "Candle", { enumerable: true, get: function () { return Candle_1.Candle; } });
38
39
  const RawTradeData_1 = require("./RawTradeData");
@@ -41,7 +42,7 @@ let isNode = process || null;
41
42
  let fetchImpl = null;
42
43
  let fsImpl = null;
43
44
  let ccxt = null;
44
- if (globalThis["fetch"])
45
+ if (globalThis['fetch'])
45
46
  fetchImpl = fetch;
46
47
  var State;
47
48
  (function (State) {
@@ -68,14 +69,14 @@ exports.loadWasmSync = loadWasmSync;
68
69
  function loadWasm(input, imports = {}) {
69
70
  return __awaiter(this, void 0, void 0, function* () {
70
71
  const mod = instantiate(yield (() => __awaiter(this, void 0, void 0, function* () {
71
- if (typeof input === "string") {
72
+ if (typeof input === 'string') {
72
73
  if (isNode) {
73
74
  if (!fetchImpl)
74
- fetchImpl = (yield Promise.resolve().then(() => __importStar(require("undici")))).fetch;
75
+ fetchImpl = (yield Promise.resolve().then(() => __importStar(require('undici')))).fetch;
75
76
  if (!fsImpl)
76
- fsImpl = yield Promise.resolve().then(() => __importStar(require("fs/promises")));
77
+ fsImpl = yield Promise.resolve().then(() => __importStar(require('fs/promises')));
77
78
  if (!ccxt)
78
- ccxt = (yield Promise.resolve().then(() => __importStar(require("ccxt")))).default;
79
+ ccxt = (yield Promise.resolve().then(() => __importStar(require('ccxt')))).default;
79
80
  return WebAssembly.compile(yield fsImpl.readFile(input));
80
81
  }
81
82
  return yield WebAssembly.compileStreaming(globalThis.fetch(input));
@@ -104,6 +105,18 @@ function instantiate(module, imports = {}) {
104
105
  console.log(__liftString(text));
105
106
  },
106
107
  },
108
+ ethers: {
109
+ keccak256_str(str_ptr) {
110
+ const str = __liftString(str_ptr);
111
+ const hash = ethers_1.ethers.keccak256(str);
112
+ return __lowerString(hash);
113
+ },
114
+ keccak256_buf(buf_ptr) {
115
+ const buf = __liftBuffer(buf_ptr);
116
+ const hash = ethers_1.ethers.keccak256(new Uint8Array(buf));
117
+ return __lowerString(hash);
118
+ },
119
+ },
107
120
  // @ts-ignore
108
121
  env: {
109
122
  abort(message, fileName, lineNumber, columnNumber) {
@@ -113,8 +126,8 @@ function instantiate(module, imports = {}) {
113
126
  },
114
127
  _initAsyncify(frame_ptr, stack_ptr) {
115
128
  // @ts-ignore
116
- if (!WASM_EXPORTS["asyncify_get_state"])
117
- throw new Error("Asyncify initialized, but not enabled when run! Compile with the --runPasses asyncify flag or asyncify-shim transform!");
129
+ if (!WASM_EXPORTS['asyncify_get_state'])
130
+ throw new Error('Asyncify initialized, but not enabled when run! Compile with the --runPasses asyncify flag or asyncify-shim transform!');
118
131
  if (ASYNCIFY_INITIALIZED)
119
132
  return;
120
133
  ASYNCIFY_PTR = frame_ptr;
@@ -123,12 +136,12 @@ function instantiate(module, imports = {}) {
123
136
  ASYNCIFY_INITIALIZED = true;
124
137
  },
125
138
  generateCandles(data, candleSize) {
126
- const _data = __liftString(data) || "[]";
127
- const _candleSize = __liftString(candleSize) || "69m";
139
+ const _data = __liftString(data) || '[]';
140
+ const _candleSize = __liftString(candleSize) || '69m';
128
141
  const candles = (0, Candle_1.generateCandles)(JSON.parse(_data), _candleSize);
129
142
  return __lowerString(JSON.stringify(candles));
130
143
  },
131
- "console.log": (text) => {
144
+ 'console.log': (text) => {
132
145
  console.log(__liftString(text));
133
146
  },
134
147
  ccxt_fetchOHLCV: (exchangeId, symbol, timeframe, limit, since) => {
@@ -146,25 +159,25 @@ function instantiate(module, imports = {}) {
146
159
  const _exchange = __liftString(exchangeId);
147
160
  const _symbol = __liftString(symbol);
148
161
  const _timeframe = __liftString(timeframe);
149
- ccxtFn = (() => __awaiter(this, void 0, void 0, function* () {
162
+ ccxtFn = () => __awaiter(this, void 0, void 0, function* () {
150
163
  if (!_exchange || !_symbol || !_timeframe)
151
- throw new Error("Exchange, Symbol, or Timeframe not provided when fetching OHCLV data.");
164
+ throw new Error('Exchange, Symbol, or Timeframe not provided when fetching OHCLV data.');
152
165
  // @ts-ignore
153
166
  const data = yield new ccxt[_exchange]({
154
- apiKey: "",
155
- secret: "",
167
+ apiKey: '',
168
+ secret: '',
156
169
  }).fetchOHLCV(_symbol, _timeframe, since, limit);
157
170
  return data;
158
- }));
171
+ });
159
172
  // @ts-ignore
160
173
  WASM_EXPORTS.asyncify_start_unwind(ASYNCIFY_PTR);
161
- }
174
+ },
162
175
  },
163
- "as-fetch": {
176
+ 'as-fetch': {
164
177
  _initAsyncify(frame_ptr, stack_ptr) {
165
178
  // @ts-ignore
166
- if (!WASM_EXPORTS["asyncify_get_state"])
167
- throw new Error("Asyncify initialized, but not enabled when run! Compile with the --runPasses asyncify flag or asyncify-shim transform!");
179
+ if (!WASM_EXPORTS['asyncify_get_state'])
180
+ throw new Error('Asyncify initialized, but not enabled when run! Compile with the --runPasses asyncify flag or asyncify-shim transform!');
168
181
  if (ASYNCIFY_INITIALIZED)
169
182
  return;
170
183
  ASYNCIFY_PTR = frame_ptr;
@@ -183,16 +196,16 @@ function instantiate(module, imports = {}) {
183
196
  detachedValue = null;
184
197
  return ptr;
185
198
  }
186
- fetchFn = (() => __awaiter(this, void 0, void 0, function* () {
187
- const res = yield fetchImpl(__liftString(url) || "", {
188
- method: "POST",
189
- mode: modeToString(mode) || "cors",
199
+ fetchFn = () => __awaiter(this, void 0, void 0, function* () {
200
+ const res = yield fetchImpl(__liftString(url) || '', {
201
+ method: 'POST',
202
+ mode: modeToString(mode) || 'cors',
190
203
  headers: __liftArray((pointer) => __liftArray((pointer) => __liftString(__getU32(pointer)), 2, __getU32(pointer)), 2, headers) || [],
191
- body: __liftBuffer(body)
204
+ body: __liftBuffer(body),
192
205
  });
193
206
  const value = yield res.arrayBuffer();
194
207
  return value;
195
- }));
208
+ });
196
209
  // @ts-ignore
197
210
  WASM_EXPORTS.asyncify_start_unwind(ASYNCIFY_PTR);
198
211
  },
@@ -206,22 +219,22 @@ function instantiate(module, imports = {}) {
206
219
  detachedValue = null;
207
220
  return ptr;
208
221
  }
209
- fetchFn = (() => __awaiter(this, void 0, void 0, function* () {
210
- const res = yield fetchImpl(__liftString(url) || "", {
211
- method: "GET",
212
- mode: modeToString(mode) || "cors",
222
+ fetchFn = () => __awaiter(this, void 0, void 0, function* () {
223
+ const res = yield fetchImpl(__liftString(url) || '', {
224
+ method: 'GET',
225
+ mode: modeToString(mode) || 'cors',
213
226
  headers: __liftArray((pointer) => __liftArray((pointer) => __liftString(__getU32(pointer)), 2, __getU32(pointer)), 2, headers) || [],
214
227
  });
215
228
  const value = yield res.arrayBuffer();
216
229
  return value;
217
- }));
230
+ });
218
231
  // @ts-ignore
219
232
  WASM_EXPORTS.asyncify_start_unwind(ASYNCIFY_PTR);
220
233
  },
221
234
  _fetchGET(url, mode, headers, callbackID) {
222
- fetchImpl(__liftString(url) || "", {
223
- method: "GET",
224
- mode: modeToString(mode) || "cors",
235
+ fetchImpl(__liftString(url) || '', {
236
+ method: 'GET',
237
+ mode: modeToString(mode) || 'cors',
225
238
  headers: __liftArray((pointer) => __liftArray((pointer) => __liftString(__getU32(pointer)), 2, __getU32(pointer)), 2, headers) || [],
226
239
  }).then((res) => __awaiter(this, void 0, void 0, function* () {
227
240
  const body = yield res.arrayBuffer();
@@ -229,9 +242,9 @@ function instantiate(module, imports = {}) {
229
242
  }));
230
243
  },
231
244
  _fetchPOST(url, mode, headers, body, callbackID) {
232
- fetchImpl(__liftString(url) || "", {
233
- method: "POST",
234
- mode: modeToString(mode) || "cors",
245
+ fetchImpl(__liftString(url) || '', {
246
+ method: 'POST',
247
+ mode: modeToString(mode) || 'cors',
235
248
  body: body,
236
249
  headers: __liftArray((pointer) => __liftArray((pointer) => __liftString(__getU32(pointer)), 2, __getU32(pointer)), 2, headers) || [],
237
250
  }).then((res) => __awaiter(this, void 0, void 0, function* () {
@@ -248,14 +261,14 @@ function instantiate(module, imports = {}) {
248
261
  ASYNCIFY_MEM = new Uint32Array(WASM_MEMORY.buffer);
249
262
  const handledExports = Object.setPrototypeOf({
250
263
  responseHandler(body, statusCode, redirected, callbackID) {
251
- if (!WASM_EXPORTS["responseHandler"])
252
- throw new Error("Unable to call .responseHandler on wasm module. Add the line export { responseHandler } from \"as-fetch/assembly\" to your entry file.");
264
+ if (!WASM_EXPORTS['responseHandler'])
265
+ throw new Error('Unable to call .responseHandler on wasm module. Add the line export { responseHandler } from "as-fetch/assembly" to your entry file.');
253
266
  // @ts-ignore
254
267
  WASM_EXPORTS.responseHandler(__lowerBuffer(body), statusCode, redirected ? 1 : 0, callbackID);
255
268
  },
256
269
  initialize(config) {
257
- if (!WASM_EXPORTS["initialize"])
258
- throw new Error("Unable to call .initialize on wasm module. Are you sure this is a data connector?");
270
+ if (!WASM_EXPORTS['initialize'])
271
+ throw new Error('Unable to call .initialize on wasm module. Are you sure this is a data connector?');
259
272
  try {
260
273
  // @ts-ignore
261
274
  WASM_EXPORTS.initialize(__lowerString(config));
@@ -268,8 +281,8 @@ function instantiate(module, imports = {}) {
268
281
  },
269
282
  execute(...params) {
270
283
  return __awaiter(this, void 0, void 0, function* () {
271
- if (!WASM_EXPORTS["execute"])
272
- throw new Error("Unable to call .execute on wasm module. Are you sure this is a data connector?");
284
+ if (!WASM_EXPORTS['execute'])
285
+ throw new Error('Unable to call .execute on wasm module. Are you sure this is a data connector?');
273
286
  const loweredArgs = new Array(params.length);
274
287
  for (let i = 0; i < params.length; i++) {
275
288
  loweredArgs[i] = __lower(params[i]);
@@ -298,26 +311,26 @@ function instantiate(module, imports = {}) {
298
311
  });
299
312
  },
300
313
  config() {
301
- if (!WASM_EXPORTS["config"])
302
- throw new Error("Unable to call .config on wasm module. Are you sure this is a data connector?");
314
+ if (!WASM_EXPORTS['config'])
315
+ throw new Error('Unable to call .config on wasm module. Are you sure this is a data connector?');
303
316
  return __liftString(WASM_EXPORTS.config());
304
317
  },
305
318
  transform() {
306
- if (!WASM_EXPORTS["transform"])
307
- throw new Error("Unable to call .transform on wasm module. Are you sure this is a data connector?");
319
+ if (!WASM_EXPORTS['transform'])
320
+ throw new Error('Unable to call .transform on wasm module. Are you sure this is a data connector?');
308
321
  return __liftString(WASM_EXPORTS.transform());
309
322
  },
310
323
  reset() {
311
- if (WASM_EXPORTS["reset"])
324
+ if (WASM_EXPORTS['reset'])
312
325
  WASM_EXPORTS.reset();
313
- }
326
+ },
314
327
  }, WASM_EXPORTS);
315
328
  WASM_DV = new DataView(WASM_MEMORY.buffer);
316
329
  function __lower(val) {
317
- if (typeof val === "number") {
330
+ if (typeof val === 'number') {
318
331
  return val;
319
332
  }
320
- else if (typeof val === "string") {
333
+ else if (typeof val === 'string') {
321
334
  return __lowerString(val);
322
335
  }
323
336
  else if (val instanceof ArrayBuffer) {
@@ -330,12 +343,12 @@ function instantiate(module, imports = {}) {
330
343
  function __liftString(ptr) {
331
344
  if (!ptr)
332
345
  return null;
333
- const end = ptr + new Uint32Array(WASM_MEMORY.buffer)[ptr - 4 >>> 2] >>> 1;
346
+ const end = (ptr + new Uint32Array(WASM_MEMORY.buffer)[(ptr - 4) >>> 2]) >>> 1;
334
347
  const memoryU16 = new Uint16Array(WASM_MEMORY.buffer);
335
348
  let start = ptr >>> 1;
336
- let string = "";
349
+ let string = '';
337
350
  while (end - start > 1024)
338
- string += String.fromCharCode(...memoryU16.subarray(start, start += 1024));
351
+ string += String.fromCharCode(...memoryU16.subarray(start, (start += 1024)));
339
352
  return string + String.fromCharCode(...memoryU16.subarray(start, end));
340
353
  }
341
354
  function __lowerString(value) {
@@ -351,7 +364,7 @@ function instantiate(module, imports = {}) {
351
364
  function __liftBuffer(ptr) {
352
365
  if (!ptr)
353
366
  return null;
354
- return WASM_MEMORY.buffer.slice(ptr, ptr + new Uint32Array(WASM_MEMORY.buffer)[ptr - 4 >>> 2]);
367
+ return WASM_MEMORY.buffer.slice(ptr, ptr + new Uint32Array(WASM_MEMORY.buffer)[(ptr - 4) >>> 2]);
355
368
  }
356
369
  function __lowerBuffer(value) {
357
370
  if (value == null)
@@ -367,7 +380,7 @@ function instantiate(module, imports = {}) {
367
380
  const length = WASM_DV.getUint32(ptr + 12, true);
368
381
  const values = new Array(length);
369
382
  for (let i = 0; i < length; ++i)
370
- values[i] = liftElement(dataStart + (i << align >>> 0));
383
+ values[i] = liftElement(dataStart + ((i << align) >>> 0));
371
384
  return values;
372
385
  }
373
386
  function __lowerStaticArray(lowerElement, id, align, values, typedConstructor = null) {
@@ -380,7 +393,7 @@ function instantiate(module, imports = {}) {
380
393
  }
381
394
  else {
382
395
  for (let i = 0; i < length; i++)
383
- lowerElement(buffer + (i << align >>> 0), values[i]);
396
+ lowerElement(buffer + ((i << align) >>> 0), values[i]);
384
397
  }
385
398
  return buffer;
386
399
  }
@@ -415,12 +428,12 @@ function instantiate(module, imports = {}) {
415
428
  }
416
429
  function modeToString(mode) {
417
430
  if (mode == 1)
418
- return "cors";
431
+ return 'cors';
419
432
  if (mode == 2)
420
- return "no-cors";
433
+ return 'no-cors';
421
434
  if (mode == 3)
422
- return "same-origin";
435
+ return 'same-origin';
423
436
  if (mode == 4)
424
- return "navigate";
437
+ return 'navigate';
425
438
  return null;
426
439
  }
package/package.json CHANGED
@@ -1,11 +1,26 @@
1
1
  {
2
2
  "name": "@steerprotocol/app-loader",
3
- "version": "1.0.3",
3
+ "version": "1.1.1",
4
4
  "description": "App Loader for Steer Protocol",
5
5
  "main": "lib/index.js",
6
+ "module": "lib/esm/index.js",
6
7
  "types": "lib/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./lib/index.d.ts",
11
+ "import": "./lib/esm/index.js",
12
+ "require": "./lib/index.js"
13
+ },
14
+ "./*": "./lib/*"
15
+ },
16
+ "publishConfig": {
17
+ "access": "public"
18
+ },
7
19
  "scripts": {
8
- "build": "tsc",
20
+ "build": "rm -rf lib && npm run build:cjs && npm run build:esm && npm run build:post",
21
+ "build:cjs": "tsc",
22
+ "build:esm": "tsc -p tsconfig.esm.json",
23
+ "build:post": "echo '{\"type\": \"module\"}' > lib/esm/package.json",
9
24
  "format": "prettier --write \"src/**/*.(js|ts)\"",
10
25
  "lint": "eslint src --ext .js,.ts",
11
26
  "lint:fix": "eslint src --fix --ext .js,.ts",
@@ -18,7 +33,7 @@
18
33
  },
19
34
  "repository": {
20
35
  "type": "git",
21
- "url": "git+https://github.com/hebertcisco/ts-npm-package-boilerplate.git"
36
+ "url": "git+https://github.com/SteerProtocol/app-loader.git"
22
37
  },
23
38
  "keywords": [
24
39
  "boilerplate",
@@ -26,11 +41,20 @@
26
41
  ],
27
42
  "author": "Hebert Cisco",
28
43
  "license": "MIT",
44
+ "release": {
45
+ "branches": [
46
+ "main"
47
+ ]
48
+ },
29
49
  "bugs": {
30
- "url": "https://github.com/hebertcisco/ts-npm-package-boilerplate/issues"
50
+ "url": "https://github.com/SteerProtocol/app-loader/issues"
31
51
  },
32
- "homepage": "https://github.com/hebertcisco/ts-npm-package-boilerplate#readme",
52
+ "homepage": "https://github.com/SteerProtocol/app-loader#readme",
33
53
  "devDependencies": {
54
+ "@semantic-release/commit-analyzer": "^13.0.1",
55
+ "@semantic-release/github": "^12.0.2",
56
+ "@semantic-release/npm": "^13.1.3",
57
+ "@semantic-release/release-notes-generator": "^14.1.0",
34
58
  "@types/jest": "29.5.6",
35
59
  "@types/timestring": "^6.0.4",
36
60
  "@typescript-eslint/eslint-plugin": "6.8.0",
@@ -39,6 +63,7 @@
39
63
  "eslint-plugin-jest": "27.4.2",
40
64
  "jest": "29.7.0",
41
65
  "prettier": "3.0.3",
66
+ "semantic-release": "^24.2.9",
42
67
  "ts-jest": "29.1.1",
43
68
  "typescript": "5.2.2"
44
69
  },
@@ -47,6 +72,7 @@
47
72
  ],
48
73
  "dependencies": {
49
74
  "ccxt": "^4.1.19",
75
+ "ethers": "^6.12.1",
50
76
  "timestring": "^7.0.0",
51
77
  "undici": "^5.26.4"
52
78
  }