@imtbl/sdk 0.45.3-alpha → 0.45.4

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/dist/x.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { ImmutableX, Config, TransfersApi, OrdersApi, UsersApi, Contracts, TradesApi, ExchangesApi, WithdrawalsApi, MintsApi, DepositsApi, EncodingApi, TokensApi } from '@imtbl/core-sdk';
2
2
  export { IMXError, createStarkSigner as imxClientCreateStarkSigner, generateLegacyStarkPrivateKey as imxClientGenerateLegacyStarkPrivateKey } from '@imtbl/core-sdk';
3
- import axios, { AxiosError } from 'axios';
3
+ import { AxiosError } from 'axios';
4
4
  import BN from 'bn.js';
5
5
  import * as encUtils from 'enc-utils';
6
6
  import { parseUnits } from '@ethersproject/units';
@@ -8,244 +8,6 @@ import { BigNumber } from '@ethersproject/bignumber';
8
8
  import { ethers } from 'ethers';
9
9
  import detectEthereumProvider from '@metamask/detect-provider';
10
10
 
11
- const isNode = () => typeof window === 'undefined';
12
- const isBrowser = () => !isNode();
13
-
14
- var Detail;
15
- (function (Detail) {
16
- Detail["RUNTIME_ID"] = "rid";
17
- Detail["PASSPORT_CLIENT_ID"] = "passportClientId";
18
- Detail["ENVIRONMENT"] = "env";
19
- Detail["PUBLISHABLE_API_KEY"] = "pak";
20
- Detail["IDENTITY"] = "uid";
21
- Detail["DOMAIN"] = "domain";
22
- })(Detail || (Detail = {}));
23
- var Store;
24
- (function (Store) {
25
- Store["EVENTS"] = "events";
26
- Store["RUNTIME"] = "runtime";
27
- })(Store || (Store = {}));
28
- const POLLING_FREQUENCY = 5000;
29
-
30
- const IMTBL_API = 'https://api.x.immutable.com';
31
- async function post(path, data) {
32
- const client = axios.create({
33
- baseURL: IMTBL_API,
34
- });
35
- const payload = JSON.stringify(data);
36
- const body = {
37
- payload: btoa(payload),
38
- };
39
- const response = await client.post(path, body);
40
- return response.data;
41
- }
42
-
43
- /**
44
- * Abstraction on localstorage
45
- */
46
- const localStoragePrefix = '__IMX-';
47
- const hasLocalstorage = () => isBrowser() && window.localStorage;
48
- const parseItem = (payload) => {
49
- // Try to parse, if can't be parsed assume string
50
- // and return string
51
- if (payload === null)
52
- return undefined;
53
- try {
54
- return JSON.parse(payload);
55
- }
56
- catch (error) {
57
- // Assumes it's a string.
58
- return payload;
59
- }
60
- };
61
- const serialiseItem = (payload) => {
62
- if (typeof payload === 'string') {
63
- return payload;
64
- }
65
- return JSON.stringify(payload);
66
- };
67
- /**
68
- * GenKey will take into account the namespace
69
- * as well as if being run in the Link, it will tap into the link
70
- * @param {string} key
71
- * @returns key
72
- */
73
- const genKey = (key) => `${localStoragePrefix}${key}`;
74
- function getItem(key) {
75
- if (hasLocalstorage()) {
76
- return parseItem(window.localStorage.getItem(genKey(key)));
77
- }
78
- return undefined;
79
- }
80
- const setItem = (key, payload) => {
81
- if (hasLocalstorage()) {
82
- window.localStorage.setItem(genKey(key), serialiseItem(payload));
83
- return true;
84
- }
85
- return false;
86
- };
87
-
88
- // In memory storage for events and other data
89
- let EVENT_STORE;
90
- let RUNTIME_DETAILS;
91
- // Initialise store and runtime
92
- const initialise$1 = () => {
93
- EVENT_STORE = getItem(Store.EVENTS) || [];
94
- RUNTIME_DETAILS = getItem(Store.RUNTIME) || {};
95
- };
96
- initialise$1();
97
- // Runtime Details
98
- const storeDetail = (key, value) => {
99
- RUNTIME_DETAILS = {
100
- ...RUNTIME_DETAILS,
101
- [key]: value,
102
- };
103
- setItem(Store.RUNTIME, RUNTIME_DETAILS);
104
- };
105
- const getDetail = (key) => {
106
- // Handle the scenario where detail is a falsy value
107
- if (RUNTIME_DETAILS[key] === undefined) {
108
- return undefined;
109
- }
110
- return RUNTIME_DETAILS[key];
111
- };
112
- const getAllDetails = () => RUNTIME_DETAILS;
113
- // Events
114
- const getEvents = () => EVENT_STORE;
115
- const removeSentEvents = (numberOfEvents) => {
116
- EVENT_STORE = EVENT_STORE.slice(numberOfEvents);
117
- setItem(Store.EVENTS, EVENT_STORE);
118
- };
119
- const flattenProperties = (properties) => {
120
- const propertyMap = [];
121
- Object.entries(properties).forEach(([key, value]) => {
122
- if (typeof value === 'string' || typeof value === 'number') {
123
- propertyMap.push([key, value.toString()]);
124
- }
125
- });
126
- return propertyMap;
127
- };
128
-
129
- let initialised = false;
130
- const isInitialised = () => initialised;
131
- const getReferrer = () => {
132
- if (isNode()) {
133
- return '';
134
- }
135
- if (window.location.ancestorOrigins
136
- && window.location.ancestorOrigins.length > 0) {
137
- return new URL(window.location.ancestorOrigins[0]).hostname;
138
- }
139
- return document.referrer ? new URL(window.document.referrer).hostname : '';
140
- };
141
- const runtimeHost = () => {
142
- if (isNode()) {
143
- return '';
144
- }
145
- if (window.opener || window.parent !== window) {
146
- return getReferrer();
147
- }
148
- return new URL(window.location.href).hostname;
149
- };
150
- const getRuntimeDetails = () => {
151
- if (isNode()) {
152
- return { browser: 'nodejs' };
153
- }
154
- const domain = runtimeHost();
155
- if (domain) {
156
- storeDetail(Detail.DOMAIN, domain);
157
- }
158
- return {
159
- browser: window.navigator.userAgent,
160
- domain,
161
- tz: Intl.DateTimeFormat().resolvedOptions().timeZone,
162
- screen: `${window.screen.width}x${window.screen.height}`,
163
- };
164
- };
165
- const initialise = async () => {
166
- const runtimeDetails = flattenProperties(getRuntimeDetails());
167
- // Get any existing runtimeId and send it along with the initialise
168
- const existingRuntimeId = getDetail(Detail.RUNTIME_ID);
169
- const body = {
170
- version: 1,
171
- data: {
172
- runtimeDetails,
173
- runtimeId: existingRuntimeId,
174
- },
175
- };
176
- const response = await post('/v1/sdk/initialise', body);
177
- // Get runtimeId and store it
178
- const { runtimeId } = response;
179
- storeDetail(Detail.RUNTIME_ID, runtimeId);
180
- initialised = true;
181
- };
182
-
183
- function errorBoundary(fn) {
184
- const wrappedFunction = ((...args) => {
185
- try {
186
- // Execute the original function
187
- const result = fn(...args);
188
- if (result instanceof Promise) {
189
- // Silent fail for now, in future
190
- // we can send errors to a logging service
191
- return result.catch(() => undefined);
192
- }
193
- return result;
194
- }
195
- catch (error) {
196
- // As above, fail silently for now
197
- return undefined;
198
- }
199
- });
200
- return wrappedFunction;
201
- }
202
-
203
- function isTestEnvironment() {
204
- if (typeof process === 'undefined') {
205
- return false;
206
- }
207
- return process.env.JEST_WORKER_ID !== undefined;
208
- }
209
- // Sending events to the server
210
- const flushFn = async () => {
211
- // Don't flush if not initialised
212
- if (isInitialised() === false) {
213
- await initialise();
214
- return;
215
- }
216
- const events = getEvents();
217
- if (events.length === 0) {
218
- return;
219
- }
220
- // Track events length here, incase
221
- const numEvents = events.length;
222
- // Get details and send it with the track request
223
- const details = getAllDetails();
224
- const metricsPayload = {
225
- version: 1,
226
- data: {
227
- events,
228
- details,
229
- },
230
- };
231
- const response = await post('/v1/sdk/metrics', metricsPayload);
232
- if (response instanceof Error) {
233
- return;
234
- }
235
- // Clear events if successfully posted
236
- removeSentEvents(numEvents);
237
- };
238
- const flush = errorBoundary(flushFn);
239
- // Flush events every 5 seconds
240
- const flushPoll = async () => {
241
- await flush();
242
- setTimeout(flushPoll, POLLING_FREQUENCY);
243
- };
244
- // This will get initialised when module is imported.
245
- if (!isTestEnvironment()) {
246
- errorBoundary(flushPoll)();
247
- }
248
-
249
11
  var Environment;
250
12
  (function (Environment) {
251
13
  Environment["PRODUCTION"] = "production";
@@ -261,7 +23,7 @@ const createImmutableXConfiguration = ({ basePath, chainID, coreContractAddress,
261
23
  chainID,
262
24
  coreContractAddress,
263
25
  registrationContractAddress,
264
- sdkVersion: 'ts-immutable-sdk-0.45.3-alpha',
26
+ sdkVersion: 'ts-immutable-sdk-0.45.4',
265
27
  });
266
28
  /**
267
29
  * Sets `sdkVersion` at the time of build
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@imtbl/sdk",
3
3
  "description": "Immutable Unified SDK",
4
- "version": "0.45.3-alpha",
4
+ "version": "0.45.4",
5
5
  "author": "Immutable",
6
6
  "browser": "./dist/index.browser.js",
7
7
  "bugs": "https://github.com/immutable/ts-immutable-sdk/issues",