@cowprotocol/widget-react 0.5.0-RC.1 → 0.5.0-RC.2
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/index.js +1 -1
- package/index.mjs +159 -163
- package/package.json +2 -2
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var N=Object.defineProperty;var H=(n,e,t)=>e in n?N(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t;var h=(n,e,t)=>(H(n,typeof e!="symbol"?e+"":e,t),t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const g=require("react/jsx-runtime"),u=require("react"),D="cowSwapWidget";function R(n,e,t){const s={key:D,method:e,...typeof t=="object"?t:{}};n.postMessage(s,"*")}function P(n,e,t){const r=s=>{!b(s.data)||s.data.key!==D||s.data.method!==e||t(s.data)};return n.addEventListener("message",r),r}function b(n){return typeof n=="object"&&n!==null&&"key"in n&&"method"in n&&typeof n.key=="string"&&typeof n.method=="string"}function O(n,e,t){n.removeEventListener("message",t)}function _(n,e){n.removeEventListener("message",e)}var l=(n=>(n.ACTIVATE="ACTIVATE",n.UPDATE_HEIGHT="UPDATE_HEIGHT",n.EMIT_COW_EVENT="EMIT_COW_EVENT",n.PROVIDER_RPC_REQUEST="PROVIDER_RPC_REQUEST",n))(l||{}),m=(n=>(n.UPDATE_PARAMS="UPDATE_PARAMS",n.UPDATE_APP_DATA="UPDATE_APP_DATA",n.PROVIDER_RPC_RESPONSE="PROVIDER_RPC_RESPONSE",n.PROVIDER_ON_EVENT="PROVIDER_ON_EVENT",n))(m||{}),v=(n=>(n.SWAP="swap",n.LIMIT="limit",n.ADVANCED="advanced",n))(v||{});const F=["connect","disconnect","close","chainChanged","accountsChanged"];class M{constructor(e){h(this,"ethereumProvider",null);h(this,"requestWaitingForConnection",{});h(this,"processRpcCallFromWindow",({rpcRequest:e})=>{if(!this.ethereumProvider){e.id&&(this.requestWaitingForConnection[e.id]=e);return}this.processRpcRequest(e)});this.iframeWidow=e}disconnect(){this.ethereumProvider=null,O(window,l.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow)}onConnect(e){this.ethereumProvider?this.disconnect():P(window,l.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),this.ethereumProvider=e,this.processPendingRequests(),F.forEach(t=>{e.on(t,r=>this.onProviderEvent(t,r))})}processPendingRequests(){Object.keys(this.requestWaitingForConnection).forEach(e=>{this.processRpcRequest(this.requestWaitingForConnection[e])}),this.requestWaitingForConnection={}}processRpcRequest(e){const{id:t,jsonrpc:r,method:s}=e;if(!this.ethereumProvider||!t)return;(s==="enable"?this.ethereumProvider.enable():this.ethereumProvider.request({...e,id:t})).then(o=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:r,id:t,result:o}})).catch(o=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:r,id:t,error:o}}))}onProviderEvent(e,t){R(this.iframeWidow,m.PROVIDER_ON_EVENT,{event:e,params:t})}forwardRpcResponseToIframe(e){R(this.iframeWidow,m.PROVIDER_RPC_RESPONSE,e)}}function q(){return typeof window>"u"||!window?"":`https://swap-dev-git-${window.location.hostname.replace("widget-configurator-git-","").replace("-cowswap.vercel.app","")}-cowswap.vercel.app`}const L={local:"http://localhost:3000",prod:"https://swap.cow.fi",dev:"https://dev.swap.cow.fi",pr:q()},W="_";function x(n){const e=L[n.env||"prod"],t=y(n),r=U(n);return e+"/#"+t+"?"+r}function y(n){const{chainId:e=1,sell:t,buy:r,tradeType:s=v.SWAP}=n,c=[(t==null?void 0:t.asset)||W,(r==null?void 0:r.asset)||W].map(encodeURIComponent).join("/");return`/${e}/widget/${s}/${c}`}function U(n){const{sell:e,buy:t,theme:r}=n,s=new URLSearchParams;return e!=null&&e.amount&&s.append("sellAmount",e.amount),t!=null&&t.amount&&s.append("buyAmount",t.amount),r&&s.append("theme",typeof r=="string"?r:r.baseTheme),s}class G{constructor(){h(this,"subscriptions",{})}on(e){const{event:t,handler:r}=e;this.subscriptions[t]||(this.subscriptions[t]=[]),this.subscriptions[t].push(r)}off(e){const{event:t,handler:r}=e;this.subscriptions[t]&&(this.subscriptions[t]=this.subscriptions[t].filter(s=>s!==r))}emit(e,t){this.subscriptions[e]&&this.subscriptions[e].forEach(r=>r(t))}}let $=1;class k{constructor(e,t=[]){h(this,"eventEmitter",new G);h(this,"listeners",[]);h(this,"widgetListener");h(this,"instanceNumber",$++);this.contentWindow=e,this.updateListeners(t),this.widgetListener=P(this.contentWindow,l.EMIT_COW_EVENT,r=>this.eventEmitter.emit(r.event,r.payload))}stopListeningIframe(){_(this.contentWindow,this.widgetListener)}updateListeners(e){for(const t of this.listeners)this.eventEmitter.off(t);this.listeners=e||[];for(const t of this.listeners)this.eventEmitter.on(t)}}const V="640px",j="450px",Q=20;function T(n,e){const{params:t,provider:r,listeners:s}=e;let c=r;const o=K(t);n.innerHTML="",n.appendChild(o);const{contentWindow:a}=o;if(!a)throw console.error("Iframe does not contain a window",o),new Error("Iframe does not contain a window!");const w=[];w.push(J(a,t.appCode)),w.push(Y(o,t.height));const f=new k(window,s);let d=I(a,null,c);return o.addEventListener("load",()=>S(a,t,c)),{updateParams:i=>S(a,i,c),updateListeners:i=>f.updateListeners(i),updateProvider:i=>{c=i,d=I(a,d,i)},destroy:()=>{f.stopListeningIframe(),w.forEach(i=>window.removeEventListener("message",i)),n.removeChild(o)}}}function I(n,e,t){e&&e.disconnect();const r=e||new M(n);return t&&r.onConnect(t),r}function K(n){const{width:e=j,height:t=V}=n,r=document.createElement("iframe");return r.src=x(n),r.width=e,r.height=t,r.style.border="0",r}function S(n,e,t){const r=!!t,s=y(e),c=U(e).toString();R(n,m.UPDATE_PARAMS,{urlParams:{pathname:s,search:c},appParams:{...e},hasProvider:r})}function J(n,e){const t=P(window,l.ACTIVATE,()=>{_(window,t),R(n,m.UPDATE_APP_DATA,{metaData:e?{appCode:e}:void 0})});return t}function Y(n,e=V){return P(window,l.UPDATE_HEIGHT,t=>{n.style.height=t.height?`${t.height+Q}px`:e})}function z(n){const{params:e,provider:t,listeners:r}=n,[s,c]=u.useState(null),o=u.useRef(null),a=u.useRef(t),w=u.useRef(r),f=u.useRef(null),d=u.useRef(null),i=u.useCallback((p,E)=>{try{console.log(`[WIDGET] ${p}`),E()}catch(A){const C=`Error ${p.toLowerCase()}`;console.error(`[WIDGET] ${C}`,A),c({message:C,error:A})}},[c]);return u.useEffect(()=>{if(!f.current||JSON.stringify(o.current)===JSON.stringify(e))return;const p=f.current,E=d.current;o.current=e,E===null?i("Creating a new widget",()=>{d.current=T(p,{params:e,provider:a.current,listeners:r}),w.current=r}):i("Updating the widget",()=>E.updateParams(e))},[e]),u.useEffect(()=>{if(!d.current||a.current===t)return;a.current=t;const p=f.current;p&&i("Updating the provider",()=>{var E;(E=d.current)==null||E.destroy(),d.current=T(p,{params:e,provider:a.current,listeners:r})})},[t]),u.useEffect(()=>{if(!d.current||w.current===r)return;const p=d.current;i("Updating the listeners",()=>p.updateListeners(r))},[r]),s?g.jsxs("div",{style:{color:"#ff3a3a"},children:[s.message,s.error.message&&g.jsx("pre",{style:{whiteSpace:"pre-wrap",fontSize:"0.75em"},children:s.error.message})]}):g.jsx("div",{ref:f})}exports.COWSWAP_URLS=L;exports.CowSwapWidget=z;exports.TradeType=v;exports.WidgetMethodsEmit=l;exports.WidgetMethodsListen=m;exports.createCowSwapWidget=T;exports.listenToMessageFromWindow=P;exports.postMessageToWindow=R;exports.stopListeningToMessageFromWindow=O;exports.stopListeningWindowListener=_;
|
package/index.mjs
CHANGED
|
@@ -1,283 +1,279 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
import { jsxs as
|
|
5
|
-
import { useState as
|
|
6
|
-
const
|
|
7
|
-
function
|
|
1
|
+
var V = Object.defineProperty;
|
|
2
|
+
var N = (n, e, t) => e in n ? V(n, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[e] = t;
|
|
3
|
+
var p = (n, e, t) => (N(n, typeof e != "symbol" ? e + "" : e, t), t);
|
|
4
|
+
import { jsxs as H, jsx as v } from "react/jsx-runtime";
|
|
5
|
+
import { useState as b, useRef as m, useCallback as F, useEffect as g } from "react";
|
|
6
|
+
const W = "cowSwapWidget";
|
|
7
|
+
function P(n, e, t) {
|
|
8
8
|
const s = {
|
|
9
|
-
key:
|
|
10
|
-
method:
|
|
11
|
-
...typeof
|
|
9
|
+
key: W,
|
|
10
|
+
method: e,
|
|
11
|
+
...typeof t == "object" ? t : {}
|
|
12
12
|
};
|
|
13
|
-
|
|
13
|
+
n.postMessage(
|
|
14
14
|
s,
|
|
15
15
|
"*"
|
|
16
16
|
// TODO: Change to CoW specific origin in production. https://github.com/cowprotocol/cowswap/issues/3828
|
|
17
17
|
);
|
|
18
18
|
}
|
|
19
|
-
function
|
|
19
|
+
function R(n, e, t) {
|
|
20
20
|
const r = (s) => {
|
|
21
|
-
!
|
|
21
|
+
!q(s.data) || s.data.key !== W || s.data.method !== e || t(s.data);
|
|
22
22
|
};
|
|
23
|
-
return
|
|
23
|
+
return n.addEventListener("message", r), r;
|
|
24
24
|
}
|
|
25
|
-
function
|
|
26
|
-
return typeof
|
|
25
|
+
function q(n) {
|
|
26
|
+
return typeof n == "object" && n !== null && "key" in n && "method" in n && typeof n.key == "string" && typeof n.method == "string";
|
|
27
27
|
}
|
|
28
|
-
function
|
|
29
|
-
|
|
28
|
+
function M(n, e, t) {
|
|
29
|
+
n.removeEventListener("message", t);
|
|
30
30
|
}
|
|
31
|
-
function
|
|
32
|
-
|
|
31
|
+
function O(n, e) {
|
|
32
|
+
n.removeEventListener("message", e);
|
|
33
33
|
}
|
|
34
|
-
var
|
|
35
|
-
const
|
|
36
|
-
class
|
|
34
|
+
var l = /* @__PURE__ */ ((n) => (n.ACTIVATE = "ACTIVATE", n.UPDATE_HEIGHT = "UPDATE_HEIGHT", n.EMIT_COW_EVENT = "EMIT_COW_EVENT", n.PROVIDER_RPC_REQUEST = "PROVIDER_RPC_REQUEST", n))(l || {}), w = /* @__PURE__ */ ((n) => (n.UPDATE_PARAMS = "UPDATE_PARAMS", n.UPDATE_APP_DATA = "UPDATE_APP_DATA", n.PROVIDER_RPC_RESPONSE = "PROVIDER_RPC_RESPONSE", n.PROVIDER_ON_EVENT = "PROVIDER_ON_EVENT", n))(w || {}), S = /* @__PURE__ */ ((n) => (n.SWAP = "swap", n.LIMIT = "limit", n.ADVANCED = "advanced", n))(S || {});
|
|
35
|
+
const G = ["connect", "disconnect", "close", "chainChanged", "accountsChanged"];
|
|
36
|
+
class $ {
|
|
37
37
|
/**
|
|
38
38
|
* Creates an instance of IframeRpcProviderBridge.
|
|
39
39
|
* @param iframeWidow - The iFrame window that will post up general RPC messages and to which the IframeRpcProviderBridge will forward the RPC result.
|
|
40
40
|
* Also it will receive some special RPC events coming from the wallet, like connect/chainChanged,accountChanged
|
|
41
41
|
*/
|
|
42
|
-
constructor(
|
|
42
|
+
constructor(e) {
|
|
43
43
|
/**
|
|
44
44
|
* The Ethereum provider instance.
|
|
45
45
|
* When is null the JSON-RPC bridge is disconnected from the Ethereum provider.
|
|
46
46
|
* */
|
|
47
|
-
|
|
47
|
+
p(this, "ethereumProvider", null);
|
|
48
48
|
/** Stored JSON-RPC requests, to queue them when disconnected. */
|
|
49
|
-
|
|
50
|
-
|
|
49
|
+
p(this, "requestWaitingForConnection", {});
|
|
50
|
+
p(this, "processRpcCallFromWindow", ({ rpcRequest: e }) => {
|
|
51
51
|
if (!this.ethereumProvider) {
|
|
52
|
-
|
|
52
|
+
e.id && (this.requestWaitingForConnection[e.id] = e);
|
|
53
53
|
return;
|
|
54
54
|
}
|
|
55
|
-
this.processRpcRequest(
|
|
55
|
+
this.processRpcRequest(e);
|
|
56
56
|
});
|
|
57
|
-
this.iframeWidow =
|
|
57
|
+
this.iframeWidow = e;
|
|
58
58
|
}
|
|
59
59
|
/**
|
|
60
60
|
* Disconnects the JSON-RPC bridge from the Ethereum provider.
|
|
61
61
|
*/
|
|
62
62
|
disconnect() {
|
|
63
|
-
this.ethereumProvider = null,
|
|
63
|
+
this.ethereumProvider = null, M(window, l.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow);
|
|
64
64
|
}
|
|
65
65
|
/**
|
|
66
66
|
* Handles the 'connect' event and sets up event listeners for Ethereum provider events.
|
|
67
67
|
* @param newProvider - The Ethereum provider to connect.
|
|
68
68
|
*/
|
|
69
|
-
onConnect(
|
|
70
|
-
this.ethereumProvider ? this.disconnect() :
|
|
71
|
-
|
|
69
|
+
onConnect(e) {
|
|
70
|
+
this.ethereumProvider ? this.disconnect() : R(window, l.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow), this.ethereumProvider = e, this.processPendingRequests(), G.forEach((t) => {
|
|
71
|
+
e.on(t, (r) => this.onProviderEvent(t, r));
|
|
72
72
|
});
|
|
73
73
|
}
|
|
74
74
|
processPendingRequests() {
|
|
75
|
-
Object.keys(this.requestWaitingForConnection).forEach((
|
|
76
|
-
this.processRpcRequest(this.requestWaitingForConnection[
|
|
75
|
+
Object.keys(this.requestWaitingForConnection).forEach((e) => {
|
|
76
|
+
this.processRpcRequest(this.requestWaitingForConnection[e]);
|
|
77
77
|
}), this.requestWaitingForConnection = {};
|
|
78
78
|
}
|
|
79
79
|
/**
|
|
80
80
|
* Processes a JSON-RPC request and sends appropriate response or error via the content window.
|
|
81
81
|
* @param request - The JSON-RPC request to be processed.
|
|
82
82
|
*/
|
|
83
|
-
processRpcRequest(
|
|
84
|
-
const { id:
|
|
85
|
-
if (!this.ethereumProvider || !
|
|
83
|
+
processRpcRequest(e) {
|
|
84
|
+
const { id: t, jsonrpc: r, method: s } = e;
|
|
85
|
+
if (!this.ethereumProvider || !t)
|
|
86
86
|
return;
|
|
87
|
-
(s === "enable" ? this.ethereumProvider.enable() : this.ethereumProvider.request({ ...
|
|
88
|
-
(
|
|
89
|
-
rpcResponse: { jsonrpc: r, id:
|
|
87
|
+
(s === "enable" ? this.ethereumProvider.enable() : this.ethereumProvider.request({ ...e, id: t })).then(
|
|
88
|
+
(o) => this.forwardRpcResponseToIframe({
|
|
89
|
+
rpcResponse: { jsonrpc: r, id: t, result: o }
|
|
90
90
|
})
|
|
91
91
|
).catch(
|
|
92
|
-
(
|
|
93
|
-
rpcResponse: { jsonrpc: r, id:
|
|
92
|
+
(o) => this.forwardRpcResponseToIframe({
|
|
93
|
+
rpcResponse: { jsonrpc: r, id: t, error: o }
|
|
94
94
|
})
|
|
95
95
|
);
|
|
96
96
|
}
|
|
97
|
-
onProviderEvent(
|
|
98
|
-
|
|
99
|
-
event:
|
|
100
|
-
params:
|
|
97
|
+
onProviderEvent(e, t) {
|
|
98
|
+
P(this.iframeWidow, w.PROVIDER_ON_EVENT, {
|
|
99
|
+
event: e,
|
|
100
|
+
params: t
|
|
101
101
|
});
|
|
102
102
|
}
|
|
103
103
|
/**
|
|
104
104
|
* Forward a JSON-RPC message to the content window.
|
|
105
105
|
*/
|
|
106
|
-
forwardRpcResponseToIframe(
|
|
107
|
-
|
|
106
|
+
forwardRpcResponseToIframe(e) {
|
|
107
|
+
P(this.iframeWidow, w.PROVIDER_RPC_RESPONSE, e);
|
|
108
108
|
}
|
|
109
109
|
}
|
|
110
|
-
function
|
|
110
|
+
function x() {
|
|
111
111
|
return typeof window > "u" || !window ? "" : `https://swap-dev-git-${window.location.hostname.replace("widget-configurator-git-", "").replace("-cowswap.vercel.app", "")}-cowswap.vercel.app`;
|
|
112
112
|
}
|
|
113
113
|
const k = {
|
|
114
114
|
local: "http://localhost:3000",
|
|
115
115
|
prod: "https://swap.cow.fi",
|
|
116
116
|
dev: "https://dev.swap.cow.fi",
|
|
117
|
-
pr:
|
|
118
|
-
},
|
|
119
|
-
function
|
|
120
|
-
const
|
|
121
|
-
return
|
|
117
|
+
pr: x()
|
|
118
|
+
}, A = "_";
|
|
119
|
+
function Q(n) {
|
|
120
|
+
const e = k[n.env || "prod"], t = y(n), r = L(n);
|
|
121
|
+
return e + "/#" + t + "?" + r;
|
|
122
122
|
}
|
|
123
|
-
function
|
|
124
|
-
const { chainId:
|
|
125
|
-
return `/${
|
|
123
|
+
function y(n) {
|
|
124
|
+
const { chainId: e = 1, sell: t, buy: r, tradeType: s = S.SWAP } = n, c = [(t == null ? void 0 : t.asset) || A, (r == null ? void 0 : r.asset) || A].map(encodeURIComponent).join("/");
|
|
125
|
+
return `/${e}/widget/${s}/${c}`;
|
|
126
126
|
}
|
|
127
|
-
function L(
|
|
128
|
-
const { sell:
|
|
129
|
-
return
|
|
127
|
+
function L(n) {
|
|
128
|
+
const { sell: e, buy: t, theme: r } = n, s = new URLSearchParams();
|
|
129
|
+
return e != null && e.amount && s.append("sellAmount", e.amount), t != null && t.amount && s.append("buyAmount", t.amount), r && s.append("theme", typeof r == "string" ? r : r.baseTheme), s;
|
|
130
130
|
}
|
|
131
|
-
|
|
132
|
-
class j {
|
|
131
|
+
class K {
|
|
133
132
|
constructor() {
|
|
134
|
-
|
|
133
|
+
p(this, "subscriptions", {});
|
|
135
134
|
}
|
|
136
|
-
on(
|
|
137
|
-
const { event:
|
|
138
|
-
this.subscriptions[
|
|
135
|
+
on(e) {
|
|
136
|
+
const { event: t, handler: r } = e;
|
|
137
|
+
this.subscriptions[t] || (this.subscriptions[t] = []), this.subscriptions[t].push(r);
|
|
139
138
|
}
|
|
140
|
-
off(
|
|
141
|
-
const { event:
|
|
142
|
-
this.subscriptions[
|
|
139
|
+
off(e) {
|
|
140
|
+
const { event: t, handler: r } = e;
|
|
141
|
+
this.subscriptions[t] && (this.subscriptions[t] = this.subscriptions[t].filter((s) => s !== r));
|
|
143
142
|
}
|
|
144
|
-
emit(
|
|
145
|
-
this.subscriptions[
|
|
143
|
+
emit(e, t) {
|
|
144
|
+
this.subscriptions[e] && this.subscriptions[e].forEach((r) => r(t));
|
|
146
145
|
}
|
|
147
146
|
}
|
|
148
|
-
let
|
|
149
|
-
class
|
|
150
|
-
constructor(
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
this.contentWindow =
|
|
147
|
+
let j = 1;
|
|
148
|
+
class J {
|
|
149
|
+
constructor(e, t = []) {
|
|
150
|
+
p(this, "eventEmitter", new K());
|
|
151
|
+
p(this, "listeners", []);
|
|
152
|
+
p(this, "widgetListener");
|
|
153
|
+
p(this, "instanceNumber", j++);
|
|
154
|
+
this.contentWindow = e, this.updateListeners(t), this.widgetListener = R(
|
|
156
155
|
this.contentWindow,
|
|
157
|
-
|
|
156
|
+
l.EMIT_COW_EVENT,
|
|
158
157
|
(r) => this.eventEmitter.emit(r.event, r.payload)
|
|
159
158
|
);
|
|
160
159
|
}
|
|
161
160
|
stopListeningIframe() {
|
|
162
|
-
|
|
161
|
+
O(this.contentWindow, this.widgetListener);
|
|
163
162
|
}
|
|
164
|
-
updateListeners(
|
|
165
|
-
for (const
|
|
166
|
-
this.eventEmitter.off(
|
|
167
|
-
this.listeners =
|
|
168
|
-
for (const
|
|
169
|
-
this.eventEmitter.on(
|
|
163
|
+
updateListeners(e) {
|
|
164
|
+
for (const t of this.listeners)
|
|
165
|
+
this.eventEmitter.off(t);
|
|
166
|
+
this.listeners = e || [];
|
|
167
|
+
for (const t of this.listeners)
|
|
168
|
+
this.eventEmitter.on(t);
|
|
170
169
|
}
|
|
171
170
|
}
|
|
172
|
-
const U = "640px",
|
|
173
|
-
function
|
|
174
|
-
const { params:
|
|
171
|
+
const U = "640px", Y = "450px", z = 20;
|
|
172
|
+
function C(n, e) {
|
|
173
|
+
const { params: t, provider: r, listeners: s } = e;
|
|
175
174
|
let c = r;
|
|
176
|
-
const
|
|
177
|
-
|
|
178
|
-
const { contentWindow: a } =
|
|
175
|
+
const o = B(t);
|
|
176
|
+
n.innerHTML = "", n.appendChild(o);
|
|
177
|
+
const { contentWindow: a } = o;
|
|
179
178
|
if (!a)
|
|
180
|
-
throw console.error("Iframe does not contain a window",
|
|
181
|
-
const
|
|
182
|
-
|
|
183
|
-
const
|
|
184
|
-
let
|
|
185
|
-
return
|
|
186
|
-
updateParams: (
|
|
187
|
-
updateListeners: (
|
|
188
|
-
updateProvider: (
|
|
189
|
-
c =
|
|
179
|
+
throw console.error("Iframe does not contain a window", o), new Error("Iframe does not contain a window!");
|
|
180
|
+
const E = [];
|
|
181
|
+
E.push(X(a, t.appCode)), E.push(Z(o, t.height));
|
|
182
|
+
const h = new J(window, s);
|
|
183
|
+
let d = I(a, null, c);
|
|
184
|
+
return o.addEventListener("load", () => D(a, t, c)), {
|
|
185
|
+
updateParams: (i) => D(a, i, c),
|
|
186
|
+
updateListeners: (i) => h.updateListeners(i),
|
|
187
|
+
updateProvider: (i) => {
|
|
188
|
+
c = i, d = I(a, d, i);
|
|
190
189
|
},
|
|
191
190
|
destroy: () => {
|
|
192
|
-
|
|
191
|
+
h.stopListeningIframe(), E.forEach((i) => window.removeEventListener("message", i)), n.removeChild(o);
|
|
193
192
|
}
|
|
194
193
|
};
|
|
195
194
|
}
|
|
196
|
-
function
|
|
197
|
-
|
|
198
|
-
const r =
|
|
199
|
-
return
|
|
195
|
+
function I(n, e, t) {
|
|
196
|
+
e && e.disconnect();
|
|
197
|
+
const r = e || new $(n);
|
|
198
|
+
return t && r.onConnect(t), r;
|
|
200
199
|
}
|
|
201
|
-
function
|
|
202
|
-
const { width:
|
|
203
|
-
return r.src =
|
|
200
|
+
function B(n) {
|
|
201
|
+
const { width: e = Y, height: t = U } = n, r = document.createElement("iframe");
|
|
202
|
+
return r.src = Q(n), r.width = e, r.height = t, r.style.border = "0", r;
|
|
204
203
|
}
|
|
205
|
-
function
|
|
206
|
-
const r = !!
|
|
207
|
-
|
|
204
|
+
function D(n, e, t) {
|
|
205
|
+
const r = !!t, s = y(e), c = L(e).toString();
|
|
206
|
+
P(n, w.UPDATE_PARAMS, {
|
|
208
207
|
urlParams: {
|
|
209
208
|
pathname: s,
|
|
210
209
|
search: c
|
|
211
210
|
},
|
|
212
211
|
appParams: {
|
|
213
|
-
...
|
|
212
|
+
...e
|
|
214
213
|
},
|
|
215
214
|
hasProvider: r
|
|
216
215
|
});
|
|
217
216
|
}
|
|
218
|
-
function
|
|
219
|
-
const
|
|
220
|
-
|
|
221
|
-
metaData:
|
|
217
|
+
function X(n, e) {
|
|
218
|
+
const t = R(window, l.ACTIVATE, () => {
|
|
219
|
+
O(window, t), P(n, w.UPDATE_APP_DATA, {
|
|
220
|
+
metaData: e ? { appCode: e } : void 0
|
|
222
221
|
});
|
|
223
222
|
});
|
|
224
|
-
return
|
|
223
|
+
return t;
|
|
225
224
|
}
|
|
226
|
-
function
|
|
227
|
-
return
|
|
228
|
-
|
|
225
|
+
function Z(n, e = U) {
|
|
226
|
+
return R(window, l.UPDATE_HEIGHT, (t) => {
|
|
227
|
+
n.style.height = t.height ? `${t.height + z}px` : e;
|
|
229
228
|
});
|
|
230
229
|
}
|
|
231
|
-
function
|
|
232
|
-
const { params:
|
|
233
|
-
(
|
|
230
|
+
function re(n) {
|
|
231
|
+
const { params: e, provider: t, listeners: r } = n, [s, c] = b(null), o = m(null), a = m(t), E = m(r), h = m(null), d = m(null), i = F(
|
|
232
|
+
(u, f) => {
|
|
234
233
|
try {
|
|
235
|
-
console.log(`[WIDGET] ${
|
|
234
|
+
console.log(`[WIDGET] ${u}`), f();
|
|
236
235
|
} catch (T) {
|
|
237
|
-
const
|
|
238
|
-
console.error(`[WIDGET] ${
|
|
236
|
+
const _ = `Error ${u.toLowerCase()}`;
|
|
237
|
+
console.error(`[WIDGET] ${_}`, T), c({ message: _, error: T });
|
|
239
238
|
}
|
|
240
239
|
},
|
|
241
240
|
[c]
|
|
242
241
|
);
|
|
243
|
-
return
|
|
244
|
-
if (!
|
|
242
|
+
return g(() => {
|
|
243
|
+
if (!h.current || JSON.stringify(o.current) === JSON.stringify(e))
|
|
245
244
|
return;
|
|
246
|
-
const
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
}) :
|
|
250
|
-
}, [
|
|
251
|
-
if (!
|
|
245
|
+
const u = h.current, f = d.current;
|
|
246
|
+
o.current = e, f === null ? i("Creating a new widget", () => {
|
|
247
|
+
d.current = C(u, { params: e, provider: a.current, listeners: r }), E.current = r;
|
|
248
|
+
}) : i("Updating the widget", () => f.updateParams(e));
|
|
249
|
+
}, [e]), g(() => {
|
|
250
|
+
if (!d.current || a.current === t)
|
|
252
251
|
return;
|
|
253
|
-
a.current =
|
|
254
|
-
const
|
|
255
|
-
|
|
256
|
-
var
|
|
257
|
-
(
|
|
252
|
+
a.current = t;
|
|
253
|
+
const u = h.current;
|
|
254
|
+
u && i("Updating the provider", () => {
|
|
255
|
+
var f;
|
|
256
|
+
(f = d.current) == null || f.destroy(), d.current = C(u, { params: e, provider: a.current, listeners: r });
|
|
258
257
|
});
|
|
259
|
-
}, [
|
|
260
|
-
if (!
|
|
258
|
+
}, [t]), g(() => {
|
|
259
|
+
if (!d.current || E.current === r)
|
|
261
260
|
return;
|
|
262
|
-
const
|
|
263
|
-
|
|
264
|
-
}, [r]), s ? /* @__PURE__ */
|
|
261
|
+
const u = d.current;
|
|
262
|
+
i("Updating the listeners", () => u.updateListeners(r));
|
|
263
|
+
}, [r]), s ? /* @__PURE__ */ H("div", { style: { color: "#ff3a3a" }, children: [
|
|
265
264
|
s.message,
|
|
266
|
-
s.error.message && /* @__PURE__ */
|
|
267
|
-
] }) : /* @__PURE__ */
|
|
265
|
+
s.error.message && /* @__PURE__ */ v("pre", { style: { whiteSpace: "pre-wrap", fontSize: "0.75em" }, children: s.error.message })
|
|
266
|
+
] }) : /* @__PURE__ */ v("div", { ref: h });
|
|
268
267
|
}
|
|
269
268
|
export {
|
|
270
269
|
k as COWSWAP_URLS,
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
m as postMessageToWindow,
|
|
281
|
-
q as stopListeningToMessageFromWindow,
|
|
282
|
-
W as stopListeningWindowListener
|
|
270
|
+
re as CowSwapWidget,
|
|
271
|
+
S as TradeType,
|
|
272
|
+
l as WidgetMethodsEmit,
|
|
273
|
+
w as WidgetMethodsListen,
|
|
274
|
+
C as createCowSwapWidget,
|
|
275
|
+
R as listenToMessageFromWindow,
|
|
276
|
+
P as postMessageToWindow,
|
|
277
|
+
M as stopListeningToMessageFromWindow,
|
|
278
|
+
O as stopListeningWindowListener
|
|
283
279
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cowprotocol/widget-react",
|
|
3
|
-
"version": "0.5.0-RC.
|
|
3
|
+
"version": "0.5.0-RC.2",
|
|
4
4
|
"type": "commonjs",
|
|
5
5
|
"description": "CoW Swap Widget Library. Allows you to easily embed a CoW Swap widget on your React application.",
|
|
6
6
|
"main": "index.js",
|
|
@@ -22,6 +22,6 @@
|
|
|
22
22
|
"react"
|
|
23
23
|
],
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@cowprotocol/widget-lib": "^0.6.0-RC.
|
|
25
|
+
"@cowprotocol/widget-lib": "^0.6.0-RC.2"
|
|
26
26
|
}
|
|
27
27
|
}
|