@cowprotocol/widget-react 0.14.3 → 1.0.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/CHANGELOG.md +232 -0
- package/LICENSE +674 -0
- package/README.md +0 -4
- package/jest.config.ts +11 -0
- package/package.json +14 -20
- package/project.json +42 -0
- package/src/index.ts +2 -0
- package/src/lib/CowSwapWidget.spec.tsx +7 -0
- package/src/lib/CowSwapWidget.tsx +125 -0
- package/tsconfig.json +21 -0
- package/tsconfig.lib.json +10 -0
- package/tsconfig.spec.json +20 -0
- package/index.d.ts +0 -2
- package/index.js +0 -1
- package/index.mjs +0 -533
- package/lib/CowSwapWidget.d.ts +0 -2
package/index.mjs
DELETED
|
@@ -1,533 +0,0 @@
|
|
|
1
|
-
import { jsxs as G, jsx as L } from "react/jsx-runtime";
|
|
2
|
-
import { useState as q, useRef as O, useCallback as $, useEffect as y } from "react";
|
|
3
|
-
class U {
|
|
4
|
-
constructor(t) {
|
|
5
|
-
this.key = t;
|
|
6
|
-
}
|
|
7
|
-
// TODO: Add proper return type annotation
|
|
8
|
-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
9
|
-
postMessageToWindow(t, n, r) {
|
|
10
|
-
const p = typeof r == "object" ? r : {}, h = {
|
|
11
|
-
key: this.key,
|
|
12
|
-
method: n,
|
|
13
|
-
...p
|
|
14
|
-
};
|
|
15
|
-
t.postMessage(
|
|
16
|
-
h,
|
|
17
|
-
"*"
|
|
18
|
-
// TODO: Change to CoW specific origin in production. https://github.com/cowprotocol/cowswap/issues/3828
|
|
19
|
-
);
|
|
20
|
-
}
|
|
21
|
-
listenToMessageFromWindow(t, n, r) {
|
|
22
|
-
const p = (h) => {
|
|
23
|
-
!B(h.data) || h.data.key !== this.key || h.data.method !== n || r(h.data);
|
|
24
|
-
};
|
|
25
|
-
return t.addEventListener("message", p), p;
|
|
26
|
-
}
|
|
27
|
-
// TODO: Add proper return type annotation
|
|
28
|
-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
29
|
-
stopListeningToMessageFromWindow(t, n, r) {
|
|
30
|
-
t.removeEventListener("message", r);
|
|
31
|
-
}
|
|
32
|
-
// TODO: Add proper return type annotation
|
|
33
|
-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
34
|
-
stopListeningWindowListener(t, n) {
|
|
35
|
-
t.removeEventListener("message", n);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
function B(e) {
|
|
39
|
-
return typeof e == "object" && e !== null && "key" in e && "method" in e && typeof e.key == "string" && typeof e.method == "string";
|
|
40
|
-
}
|
|
41
|
-
var m = /* @__PURE__ */ ((e) => (e.PROVIDER_RPC_REQUEST = "PROVIDER_RPC_REQUEST", e.PROVIDER_RPC_RESPONSE = "PROVIDER_RPC_RESPONSE", e.PROVIDER_ON_EVENT = "PROVIDER_ON_EVENT", e.SEND_PROVIDER_META_INFO = "SEND_PROVIDER_META_INFO", e.REQUEST_PROVIDER_META_INFO = "REQUEST_PROVIDER_META_INFO", e))(m || {});
|
|
42
|
-
const P = new U(
|
|
43
|
-
"cowSwapIframeRpcProviderTransport"
|
|
44
|
-
);
|
|
45
|
-
function J(e) {
|
|
46
|
-
if (e.isWalletConnect)
|
|
47
|
-
return e?.session?.peer?.metadata;
|
|
48
|
-
}
|
|
49
|
-
function z(e, t) {
|
|
50
|
-
return t.find((n) => n.provider === e)?.info;
|
|
51
|
-
}
|
|
52
|
-
const Y = ["connect", "disconnect", "close", "chainChanged", "accountsChanged"], x = [];
|
|
53
|
-
window.addEventListener("eip6963:announceProvider", (e) => {
|
|
54
|
-
const t = e;
|
|
55
|
-
x.push(t.detail);
|
|
56
|
-
});
|
|
57
|
-
class K {
|
|
58
|
-
/**
|
|
59
|
-
* Creates an instance of IframeRpcProviderBridge.
|
|
60
|
-
* @param iframeWidow - The iFrame window that will post up general RPC messages and to which the IframeRpcProviderBridge will forward the RPC result.
|
|
61
|
-
* Also it will receive some special RPC events coming from the wallet, like connect/chainChanged,accountChanged
|
|
62
|
-
*/
|
|
63
|
-
constructor(t) {
|
|
64
|
-
this.iframeWidow = t;
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* The Ethereum provider instance.
|
|
68
|
-
* When is null the JSON-RPC bridge is disconnected from the Ethereum provider.
|
|
69
|
-
* */
|
|
70
|
-
ethereumProvider = null;
|
|
71
|
-
/** Stored JSON-RPC requests, to queue them when disconnected. */
|
|
72
|
-
requestWaitingForConnection = {};
|
|
73
|
-
/**
|
|
74
|
-
* Disconnects the JSON-RPC bridge from the Ethereum provider.
|
|
75
|
-
*/
|
|
76
|
-
// TODO: Add proper return type annotation
|
|
77
|
-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
78
|
-
disconnect() {
|
|
79
|
-
this.ethereumProvider = null, P.stopListeningToMessageFromWindow(
|
|
80
|
-
window,
|
|
81
|
-
m.PROVIDER_RPC_REQUEST,
|
|
82
|
-
this.processRpcCallFromWindow
|
|
83
|
-
), P.stopListeningToMessageFromWindow(
|
|
84
|
-
window,
|
|
85
|
-
m.REQUEST_PROVIDER_META_INFO,
|
|
86
|
-
this.processProviderMetaInfoRequest
|
|
87
|
-
);
|
|
88
|
-
}
|
|
89
|
-
/**
|
|
90
|
-
* Handles the 'connect' event and sets up event listeners for Ethereum provider events.
|
|
91
|
-
* @param newProvider - The Ethereum provider to connect.
|
|
92
|
-
*/
|
|
93
|
-
// TODO: Add proper return type annotation
|
|
94
|
-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
95
|
-
onConnect(t) {
|
|
96
|
-
this.ethereumProvider ? this.disconnect() : P.listenToMessageFromWindow(
|
|
97
|
-
window,
|
|
98
|
-
m.PROVIDER_RPC_REQUEST,
|
|
99
|
-
this.processRpcCallFromWindow
|
|
100
|
-
), this.ethereumProvider = t, this.processPendingRequests(), Y.forEach((n) => {
|
|
101
|
-
t.on(n, (r) => this.onProviderEvent(n, r));
|
|
102
|
-
}), P.listenToMessageFromWindow(
|
|
103
|
-
window,
|
|
104
|
-
m.REQUEST_PROVIDER_META_INFO,
|
|
105
|
-
this.processProviderMetaInfoRequest
|
|
106
|
-
);
|
|
107
|
-
}
|
|
108
|
-
// TODO: Add proper return type annotation
|
|
109
|
-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
110
|
-
processPendingRequests() {
|
|
111
|
-
Object.keys(this.requestWaitingForConnection).forEach((t) => {
|
|
112
|
-
this.processRpcRequest(this.requestWaitingForConnection[t]);
|
|
113
|
-
}), this.requestWaitingForConnection = {};
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* Processes a JSON-RPC request and sends appropriate response or error via the content window.
|
|
117
|
-
* @param request - The JSON-RPC request to be processed.
|
|
118
|
-
*/
|
|
119
|
-
// TODO: Add proper return type annotation
|
|
120
|
-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
121
|
-
processRpcRequest(t) {
|
|
122
|
-
const { id: n, jsonrpc: r, method: p } = t;
|
|
123
|
-
if (!this.ethereumProvider || !n)
|
|
124
|
-
return;
|
|
125
|
-
// Keep the legacy "enable" method for backward compatibility
|
|
126
|
-
(p === "enable" ? this.ethereumProvider.request({ method: "eth_requestAccounts", id: n }) : this.ethereumProvider.request({ ...t, id: n })).then(
|
|
127
|
-
(w) => this.forwardRpcResponseToIframe({
|
|
128
|
-
rpcResponse: { jsonrpc: r, id: n, result: w }
|
|
129
|
-
})
|
|
130
|
-
).catch(
|
|
131
|
-
(w) => this.forwardRpcResponseToIframe({
|
|
132
|
-
rpcResponse: { jsonrpc: r, id: n, error: w }
|
|
133
|
-
})
|
|
134
|
-
);
|
|
135
|
-
}
|
|
136
|
-
processRpcCallFromWindow = ({ rpcRequest: t }) => {
|
|
137
|
-
if (!this.ethereumProvider) {
|
|
138
|
-
t.id && (this.requestWaitingForConnection[t.id] = t);
|
|
139
|
-
return;
|
|
140
|
-
}
|
|
141
|
-
this.processRpcRequest(t);
|
|
142
|
-
};
|
|
143
|
-
processProviderMetaInfoRequest = () => {
|
|
144
|
-
if (!this.ethereumProvider) return;
|
|
145
|
-
const t = z(this.ethereumProvider, x), n = J(this.ethereumProvider);
|
|
146
|
-
P.postMessageToWindow(this.iframeWidow, m.SEND_PROVIDER_META_INFO, {
|
|
147
|
-
providerEip6963Info: t,
|
|
148
|
-
providerWcMetadata: n
|
|
149
|
-
});
|
|
150
|
-
};
|
|
151
|
-
onProviderEvent(t, n) {
|
|
152
|
-
P.postMessageToWindow(this.iframeWidow, m.PROVIDER_ON_EVENT, {
|
|
153
|
-
event: t,
|
|
154
|
-
params: n
|
|
155
|
-
});
|
|
156
|
-
}
|
|
157
|
-
/**
|
|
158
|
-
* Forward a JSON-RPC message to the content window.
|
|
159
|
-
*/
|
|
160
|
-
// TODO: Add proper return type annotation
|
|
161
|
-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
162
|
-
forwardRpcResponseToIframe(t) {
|
|
163
|
-
P.postMessageToWindow(
|
|
164
|
-
this.iframeWidow,
|
|
165
|
-
m.PROVIDER_RPC_RESPONSE,
|
|
166
|
-
t
|
|
167
|
-
);
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
var W = { exports: {} }, b;
|
|
171
|
-
function X() {
|
|
172
|
-
return b || (b = 1, function(e) {
|
|
173
|
-
var t = Object.prototype.hasOwnProperty, n = "~";
|
|
174
|
-
function r() {
|
|
175
|
-
}
|
|
176
|
-
Object.create && (r.prototype = /* @__PURE__ */ Object.create(null), new r().__proto__ || (n = !1));
|
|
177
|
-
function p(c, i, s) {
|
|
178
|
-
this.fn = c, this.context = i, this.once = s || !1;
|
|
179
|
-
}
|
|
180
|
-
function h(c, i, s, o, f) {
|
|
181
|
-
if (typeof s != "function")
|
|
182
|
-
throw new TypeError("The listener must be a function");
|
|
183
|
-
var d = new p(s, o || c, f), l = n ? n + i : i;
|
|
184
|
-
return c._events[l] ? c._events[l].fn ? c._events[l] = [c._events[l], d] : c._events[l].push(d) : (c._events[l] = d, c._eventsCount++), c;
|
|
185
|
-
}
|
|
186
|
-
function w(c, i) {
|
|
187
|
-
--c._eventsCount === 0 ? c._events = new r() : delete c._events[i];
|
|
188
|
-
}
|
|
189
|
-
function u() {
|
|
190
|
-
this._events = new r(), this._eventsCount = 0;
|
|
191
|
-
}
|
|
192
|
-
u.prototype.eventNames = function() {
|
|
193
|
-
var i = [], s, o;
|
|
194
|
-
if (this._eventsCount === 0) return i;
|
|
195
|
-
for (o in s = this._events)
|
|
196
|
-
t.call(s, o) && i.push(n ? o.slice(1) : o);
|
|
197
|
-
return Object.getOwnPropertySymbols ? i.concat(Object.getOwnPropertySymbols(s)) : i;
|
|
198
|
-
}, u.prototype.listeners = function(i) {
|
|
199
|
-
var s = n ? n + i : i, o = this._events[s];
|
|
200
|
-
if (!o) return [];
|
|
201
|
-
if (o.fn) return [o.fn];
|
|
202
|
-
for (var f = 0, d = o.length, l = new Array(d); f < d; f++)
|
|
203
|
-
l[f] = o[f].fn;
|
|
204
|
-
return l;
|
|
205
|
-
}, u.prototype.listenerCount = function(i) {
|
|
206
|
-
var s = n ? n + i : i, o = this._events[s];
|
|
207
|
-
return o ? o.fn ? 1 : o.length : 0;
|
|
208
|
-
}, u.prototype.emit = function(i, s, o, f, d, l) {
|
|
209
|
-
var _ = n ? n + i : i;
|
|
210
|
-
if (!this._events[_]) return !1;
|
|
211
|
-
var a = this._events[_], v = arguments.length, T, E;
|
|
212
|
-
if (a.fn) {
|
|
213
|
-
switch (a.once && this.removeListener(i, a.fn, void 0, !0), v) {
|
|
214
|
-
case 1:
|
|
215
|
-
return a.fn.call(a.context), !0;
|
|
216
|
-
case 2:
|
|
217
|
-
return a.fn.call(a.context, s), !0;
|
|
218
|
-
case 3:
|
|
219
|
-
return a.fn.call(a.context, s, o), !0;
|
|
220
|
-
case 4:
|
|
221
|
-
return a.fn.call(a.context, s, o, f), !0;
|
|
222
|
-
case 5:
|
|
223
|
-
return a.fn.call(a.context, s, o, f, d), !0;
|
|
224
|
-
case 6:
|
|
225
|
-
return a.fn.call(a.context, s, o, f, d, l), !0;
|
|
226
|
-
}
|
|
227
|
-
for (E = 1, T = new Array(v - 1); E < v; E++)
|
|
228
|
-
T[E - 1] = arguments[E];
|
|
229
|
-
a.fn.apply(a.context, T);
|
|
230
|
-
} else {
|
|
231
|
-
var j = a.length, I;
|
|
232
|
-
for (E = 0; E < j; E++)
|
|
233
|
-
switch (a[E].once && this.removeListener(i, a[E].fn, void 0, !0), v) {
|
|
234
|
-
case 1:
|
|
235
|
-
a[E].fn.call(a[E].context);
|
|
236
|
-
break;
|
|
237
|
-
case 2:
|
|
238
|
-
a[E].fn.call(a[E].context, s);
|
|
239
|
-
break;
|
|
240
|
-
case 3:
|
|
241
|
-
a[E].fn.call(a[E].context, s, o);
|
|
242
|
-
break;
|
|
243
|
-
case 4:
|
|
244
|
-
a[E].fn.call(a[E].context, s, o, f);
|
|
245
|
-
break;
|
|
246
|
-
default:
|
|
247
|
-
if (!T) for (I = 1, T = new Array(v - 1); I < v; I++)
|
|
248
|
-
T[I - 1] = arguments[I];
|
|
249
|
-
a[E].fn.apply(a[E].context, T);
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
return !0;
|
|
253
|
-
}, u.prototype.on = function(i, s, o) {
|
|
254
|
-
return h(this, i, s, o, !1);
|
|
255
|
-
}, u.prototype.once = function(i, s, o) {
|
|
256
|
-
return h(this, i, s, o, !0);
|
|
257
|
-
}, u.prototype.removeListener = function(i, s, o, f) {
|
|
258
|
-
var d = n ? n + i : i;
|
|
259
|
-
if (!this._events[d]) return this;
|
|
260
|
-
if (!s)
|
|
261
|
-
return w(this, d), this;
|
|
262
|
-
var l = this._events[d];
|
|
263
|
-
if (l.fn)
|
|
264
|
-
l.fn === s && (!f || l.once) && (!o || l.context === o) && w(this, d);
|
|
265
|
-
else {
|
|
266
|
-
for (var _ = 0, a = [], v = l.length; _ < v; _++)
|
|
267
|
-
(l[_].fn !== s || f && !l[_].once || o && l[_].context !== o) && a.push(l[_]);
|
|
268
|
-
a.length ? this._events[d] = a.length === 1 ? a[0] : a : w(this, d);
|
|
269
|
-
}
|
|
270
|
-
return this;
|
|
271
|
-
}, u.prototype.removeAllListeners = function(i) {
|
|
272
|
-
var s;
|
|
273
|
-
return i ? (s = n ? n + i : i, this._events[s] && w(this, s)) : (this._events = new r(), this._eventsCount = 0), this;
|
|
274
|
-
}, u.prototype.off = u.prototype.removeListener, u.prototype.addListener = u.prototype.on, u.prefixed = n, u.EventEmitter = u, e.exports = u;
|
|
275
|
-
}(W)), W.exports;
|
|
276
|
-
}
|
|
277
|
-
X();
|
|
278
|
-
class Z {
|
|
279
|
-
subscriptions = {};
|
|
280
|
-
on(t) {
|
|
281
|
-
const { event: n, handler: r } = t;
|
|
282
|
-
return this.subscriptions[n] || (this.subscriptions[n] = []), this.subscriptions[n].push(r), t;
|
|
283
|
-
}
|
|
284
|
-
off(t) {
|
|
285
|
-
const { event: n, handler: r } = t;
|
|
286
|
-
return this.subscriptions[n] && (this.subscriptions[n] = this.subscriptions[n].filter((p) => p !== r)), t;
|
|
287
|
-
}
|
|
288
|
-
emit(t, n) {
|
|
289
|
-
this.subscriptions[t] && this.subscriptions[t].forEach((r) => r(n));
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
var g = /* @__PURE__ */ ((e) => (e.ACTIVATE = "ACTIVATE", e.UPDATE_HEIGHT = "UPDATE_HEIGHT", e.SET_FULL_HEIGHT = "SET_FULL_HEIGHT", e.EMIT_COW_EVENT = "EMIT_COW_EVENT", e.PROVIDER_RPC_REQUEST = "PROVIDER_RPC_REQUEST", e.INTERCEPT_WINDOW_OPEN = "INTERCEPT_WINDOW_OPEN", e))(g || {}), A = /* @__PURE__ */ ((e) => (e.UPDATE_PARAMS = "UPDATE_PARAMS", e.UPDATE_APP_DATA = "UPDATE_APP_DATA", e.PROVIDER_RPC_RESPONSE = "PROVIDER_RPC_RESPONSE", e.PROVIDER_ON_EVENT = "PROVIDER_ON_EVENT", e))(A || {}), D = /* @__PURE__ */ ((e) => (e.SWAP = "swap", e.LIMIT = "limit", e.ADVANCED = "advanced", e.YIELD = "yield", e))(D || {});
|
|
293
|
-
const Re = [
|
|
294
|
-
"primary",
|
|
295
|
-
"background",
|
|
296
|
-
"paper",
|
|
297
|
-
"text",
|
|
298
|
-
"danger",
|
|
299
|
-
"warning",
|
|
300
|
-
"alert",
|
|
301
|
-
"info",
|
|
302
|
-
"success"
|
|
303
|
-
], R = new U("cowSwapWidget");
|
|
304
|
-
class ee {
|
|
305
|
-
constructor(t, n = []) {
|
|
306
|
-
this.contentWindow = t, this.updateListeners(n), this.widgetListener = R.listenToMessageFromWindow(
|
|
307
|
-
this.contentWindow,
|
|
308
|
-
g.EMIT_COW_EVENT,
|
|
309
|
-
(r) => this.eventEmitter.emit(r.event, r.payload)
|
|
310
|
-
);
|
|
311
|
-
}
|
|
312
|
-
eventEmitter = new Z();
|
|
313
|
-
listeners = [];
|
|
314
|
-
widgetListener;
|
|
315
|
-
// TODO: Add proper return type annotation
|
|
316
|
-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
317
|
-
stopListeningIframe() {
|
|
318
|
-
R.stopListeningWindowListener(this.contentWindow, this.widgetListener);
|
|
319
|
-
}
|
|
320
|
-
updateListeners(t) {
|
|
321
|
-
for (const n of this.listeners)
|
|
322
|
-
this.eventEmitter.off(n);
|
|
323
|
-
this.listeners = t || [];
|
|
324
|
-
for (const n of this.listeners)
|
|
325
|
-
this.eventEmitter.on(n);
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
class te {
|
|
329
|
-
constructor(t, n) {
|
|
330
|
-
this.appWindow = t, this.iframeWidow = n, this.forwardSdkMessage = (r) => {
|
|
331
|
-
ne(r.data) && r.origin !== window.location.origin && (re(r.data) ? this.appWindow.parent.postMessage(r.data, "*") : se(r.data) && this.iframeWidow.postMessage(r.data, "*"));
|
|
332
|
-
}, this.startListening();
|
|
333
|
-
}
|
|
334
|
-
forwardSdkMessage;
|
|
335
|
-
// TODO: Add proper return type annotation
|
|
336
|
-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
337
|
-
startListening() {
|
|
338
|
-
this.appWindow.addEventListener("message", this.forwardSdkMessage);
|
|
339
|
-
}
|
|
340
|
-
// TODO: Add proper return type annotation
|
|
341
|
-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
342
|
-
stopListening() {
|
|
343
|
-
this.appWindow.removeEventListener("message", this.forwardSdkMessage);
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
function ne(e) {
|
|
347
|
-
return typeof e == "object" && e !== null && "id" in e && typeof e.id == "string";
|
|
348
|
-
}
|
|
349
|
-
function re(e) {
|
|
350
|
-
return "method" in e && typeof e.method == "string" && "params" in e && "env" in e && typeof e.env == "object" && e.env !== null && "sdkVersion" in e.env;
|
|
351
|
-
}
|
|
352
|
-
function se(e) {
|
|
353
|
-
return "success" in e && typeof e.success == "boolean" && "version" in e && typeof e.version == "string";
|
|
354
|
-
}
|
|
355
|
-
function oe(e) {
|
|
356
|
-
return !!(e && typeof e == "object");
|
|
357
|
-
}
|
|
358
|
-
const N = "_";
|
|
359
|
-
function ie(e) {
|
|
360
|
-
const t = typeof e.baseUrl == "string" ? e.baseUrl : "https://swap.cow.fi", n = k(e);
|
|
361
|
-
return t + "/#" + n + "?" + H(e);
|
|
362
|
-
}
|
|
363
|
-
function k(e) {
|
|
364
|
-
const { chainId: t = 1, sell: n, buy: r, tradeType: p = D.SWAP } = e, h = [n?.asset || N, r?.asset || N].map(encodeURIComponent).join("/");
|
|
365
|
-
return `/${t}/widget/${p}/${h}`;
|
|
366
|
-
}
|
|
367
|
-
function H(e) {
|
|
368
|
-
const t = new URLSearchParams();
|
|
369
|
-
return ue(ce(ae(t, e), e), e);
|
|
370
|
-
}
|
|
371
|
-
function ae(e, t) {
|
|
372
|
-
const { sell: n, buy: r } = t;
|
|
373
|
-
return n?.amount && e.append("sellAmount", n.amount), r?.amount && e.append("buyAmount", r.amount), e;
|
|
374
|
-
}
|
|
375
|
-
function ce(e, t) {
|
|
376
|
-
const n = t.theme;
|
|
377
|
-
return n ? (oe(n) ? (e.append("palette", encodeURIComponent(JSON.stringify(n))), e.append("theme", n.baseTheme)) : (e.append("palette", "null"), e.append("theme", n)), e) : (e.append("palette", "null"), e);
|
|
378
|
-
}
|
|
379
|
-
function ue(e, t) {
|
|
380
|
-
return t.targetChainId && e.append("targetChainId", t.targetChainId.toString()), e;
|
|
381
|
-
}
|
|
382
|
-
const Q = "640px", de = "450px", fe = 20;
|
|
383
|
-
function M(e, t) {
|
|
384
|
-
const { params: n, provider: r, listeners: p } = t;
|
|
385
|
-
let h = r, w = n;
|
|
386
|
-
const u = pe(n);
|
|
387
|
-
e.innerHTML = "", e.appendChild(u);
|
|
388
|
-
const { contentWindow: c } = u;
|
|
389
|
-
if (!c)
|
|
390
|
-
throw console.error("Iframe does not contain a window", u), new Error("Iframe does not contain a window!");
|
|
391
|
-
const i = [];
|
|
392
|
-
i.push(le(c, n.appCode)), i.push(...Ee(u, n.height, n.maxHeight)), i.push(he());
|
|
393
|
-
const s = new ee(window, p);
|
|
394
|
-
let o = V(c, null, h);
|
|
395
|
-
u.addEventListener("load", () => F(c, w, h));
|
|
396
|
-
const f = new te(window, c);
|
|
397
|
-
return {
|
|
398
|
-
updateParams: (d) => {
|
|
399
|
-
w = d, F(c, w, h);
|
|
400
|
-
},
|
|
401
|
-
updateListeners: (d) => s.updateListeners(d),
|
|
402
|
-
updateProvider: (d) => {
|
|
403
|
-
h = d, o = V(c, o, d);
|
|
404
|
-
},
|
|
405
|
-
destroy: () => {
|
|
406
|
-
o.disconnect(), s.stopListeningIframe(), i.forEach((d) => window.removeEventListener("message", d)), f.stopListening(), e.removeChild(u);
|
|
407
|
-
}
|
|
408
|
-
};
|
|
409
|
-
}
|
|
410
|
-
function V(e, t, n) {
|
|
411
|
-
t && t.disconnect();
|
|
412
|
-
const r = t || new K(e);
|
|
413
|
-
return n && r.onConnect(n), r;
|
|
414
|
-
}
|
|
415
|
-
function pe(e) {
|
|
416
|
-
const { width: t = de, height: n = Q } = e, r = document.createElement("iframe");
|
|
417
|
-
return r.src = ie(e), r.width = t, r.height = n, r.style.border = "0", r.allow = "clipboard-read; clipboard-write", r;
|
|
418
|
-
}
|
|
419
|
-
function F(e, t, n) {
|
|
420
|
-
const r = !!n, p = k(t), h = H(t).toString(), { theme: w, ...u } = t;
|
|
421
|
-
R.postMessageToWindow(e, A.UPDATE_PARAMS, {
|
|
422
|
-
urlParams: {
|
|
423
|
-
pathname: p,
|
|
424
|
-
search: h
|
|
425
|
-
},
|
|
426
|
-
appParams: u,
|
|
427
|
-
hasProvider: r
|
|
428
|
-
});
|
|
429
|
-
}
|
|
430
|
-
function le(e, t) {
|
|
431
|
-
return R.listenToMessageFromWindow(window, g.ACTIVATE, () => {
|
|
432
|
-
R.postMessageToWindow(e, A.UPDATE_APP_DATA, {
|
|
433
|
-
metaData: t ? { appCode: t } : void 0
|
|
434
|
-
});
|
|
435
|
-
});
|
|
436
|
-
}
|
|
437
|
-
function he() {
|
|
438
|
-
return R.listenToMessageFromWindow(
|
|
439
|
-
window,
|
|
440
|
-
g.INTERCEPT_WINDOW_OPEN,
|
|
441
|
-
({ href: e, rel: t, target: n }) => {
|
|
442
|
-
const r = e.toString();
|
|
443
|
-
if (!r.startsWith("http") && r.match(/^[a-zA-Z0-9]+:\/\//)) {
|
|
444
|
-
window.open(r, n, t);
|
|
445
|
-
return;
|
|
446
|
-
}
|
|
447
|
-
}
|
|
448
|
-
);
|
|
449
|
-
}
|
|
450
|
-
function Ee(e, t = Q, n) {
|
|
451
|
-
return [
|
|
452
|
-
R.listenToMessageFromWindow(window, g.UPDATE_HEIGHT, (r) => {
|
|
453
|
-
const p = r.height ? r.height + fe : void 0;
|
|
454
|
-
e.style.height = p ? `${n ? Math.min(p, n) : p}px` : t;
|
|
455
|
-
}),
|
|
456
|
-
R.listenToMessageFromWindow(window, g.SET_FULL_HEIGHT, ({ isUpToSmall: r }) => {
|
|
457
|
-
e.style.height = r ? t : `${n || document.body.offsetHeight}px`;
|
|
458
|
-
})
|
|
459
|
-
];
|
|
460
|
-
}
|
|
461
|
-
const we = Object.values(D);
|
|
462
|
-
function Te(e, t, n) {
|
|
463
|
-
if (C(e)) {
|
|
464
|
-
const r = e[n];
|
|
465
|
-
return S(r) ? r[t] : r;
|
|
466
|
-
}
|
|
467
|
-
if (S(e)) {
|
|
468
|
-
const r = e[t];
|
|
469
|
-
return C(r) ? r[n] : r;
|
|
470
|
-
}
|
|
471
|
-
return e;
|
|
472
|
-
}
|
|
473
|
-
function C(e) {
|
|
474
|
-
return typeof e != "object" ? !1 : Object.keys(e).every((t) => we.includes(t));
|
|
475
|
-
}
|
|
476
|
-
const _e = /^\d+$/;
|
|
477
|
-
function S(e) {
|
|
478
|
-
return typeof e != "object" ? !1 : Object.keys(e).every((t) => _e.test(t));
|
|
479
|
-
}
|
|
480
|
-
function Pe(e) {
|
|
481
|
-
return C(e) ? Object.values(e).flatMap((t) => S(t) ? Object.values(t) : [t]) : S(e) ? Object.values(e).flatMap((t) => C(t) ? Object.values(t) : [t]) : [e];
|
|
482
|
-
}
|
|
483
|
-
function ge(e) {
|
|
484
|
-
const { params: t, provider: n, listeners: r } = e, [p, h] = q(null), w = O(null), u = O(n), c = O(r), i = O(null), s = O(null), o = $((f, d) => {
|
|
485
|
-
try {
|
|
486
|
-
console.log(`[WIDGET] ${f}`), d();
|
|
487
|
-
} catch (l) {
|
|
488
|
-
const _ = `Error ${f.toLowerCase()}`;
|
|
489
|
-
console.error(`[WIDGET] ${_}`, l), h({ message: _, error: l });
|
|
490
|
-
}
|
|
491
|
-
}, []);
|
|
492
|
-
return y(() => () => {
|
|
493
|
-
w.current = null, u.current = void 0, c.current = void 0;
|
|
494
|
-
const f = s.current;
|
|
495
|
-
f && (o("💥 Destroy widget", () => f.destroy()), s.current = null);
|
|
496
|
-
}, []), y(() => {
|
|
497
|
-
if (!i.current || JSON.stringify(w.current) === JSON.stringify(t))
|
|
498
|
-
return;
|
|
499
|
-
const f = i.current, d = s.current;
|
|
500
|
-
w.current = t, d === null ? o("Creating a new widget", () => {
|
|
501
|
-
s.current = M(f, { params: t, provider: u.current, listeners: r }), c.current = r;
|
|
502
|
-
}) : o("Updating the widget", () => d.updateParams(t));
|
|
503
|
-
}, [t, o]), y(() => {
|
|
504
|
-
if (!s.current || u.current === n)
|
|
505
|
-
return;
|
|
506
|
-
u.current = n;
|
|
507
|
-
const f = i.current;
|
|
508
|
-
f && o("Updating the provider", () => {
|
|
509
|
-
s.current?.destroy(), s.current = M(f, { params: t, provider: u.current, listeners: r });
|
|
510
|
-
});
|
|
511
|
-
}, [n, o]), y(() => {
|
|
512
|
-
if (!s.current || c.current === r) return;
|
|
513
|
-
const f = s.current;
|
|
514
|
-
o("Updating the listeners", () => f.updateListeners(r));
|
|
515
|
-
}, [r, o]), p ? /* @__PURE__ */ G("div", { style: { color: "#ff3a3a" }, children: [
|
|
516
|
-
p.message,
|
|
517
|
-
p.error.message && /* @__PURE__ */ L("pre", { style: { whiteSpace: "pre-wrap", fontSize: "0.75em" }, children: p.error.message })
|
|
518
|
-
] }) : /* @__PURE__ */ L("div", { ref: i, style: { width: "100%" } });
|
|
519
|
-
}
|
|
520
|
-
export {
|
|
521
|
-
ge as CowSwapWidget,
|
|
522
|
-
D as TradeType,
|
|
523
|
-
Re as WIDGET_PALETTE_COLORS,
|
|
524
|
-
g as WidgetMethodsEmit,
|
|
525
|
-
A as WidgetMethodsListen,
|
|
526
|
-
M as createCowSwapWidget,
|
|
527
|
-
oe as isCowSwapWidgetPalette,
|
|
528
|
-
S as isPerNetworkConfig,
|
|
529
|
-
C as isPerTradeTypeConfig,
|
|
530
|
-
Te as resolveFlexibleConfig,
|
|
531
|
-
Pe as resolveFlexibleConfigValues,
|
|
532
|
-
R as widgetIframeTransport
|
|
533
|
-
};
|
package/lib/CowSwapWidget.d.ts
DELETED