@cowprotocol/widget-react 0.5.0-RC.0 → 0.5.0-RC.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/index.js +1 -1
- package/index.mjs +163 -159
- package/package.json +2 -2
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var b=Object.defineProperty;var G=(e,t,n)=>t in e?b(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n;var p=(e,t,n)=>(G(e,typeof t!="symbol"?t+"":t,n),n);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const w=require("react/jsx-runtime"),d=require("react"),v="cowSwapWidget";function m(e,t,n){const s={key:v,method:t,...typeof n=="object"?n:{}};e.postMessage(s,"*")}function P(e,t,n){const r=s=>{!M(s.data)||s.data.key!==v||s.data.method!==t||n(s.data)};return e.addEventListener("message",r),r}function M(e){return typeof e=="object"&&e!==null&&"key"in e&&"method"in e&&typeof e.key=="string"&&typeof e.method=="string"}function I(e,t,n){e.removeEventListener("message",n)}function A(e,t){e.removeEventListener("message",t)}var _=(e=>(e.ACTIVATE="ACTIVATE",e.UPDATE_HEIGHT="UPDATE_HEIGHT",e.EMIT_COW_EVENT="EMIT_COW_EVENT",e.PROVIDER_RPC_REQUEST="PROVIDER_RPC_REQUEST",e))(_||{}),l=(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))(l||{}),O=(e=>(e.SWAP="swap",e.LIMIT="limit",e.ADVANCED="advanced",e))(O||{});const q=["connect","disconnect","close","chainChanged","accountsChanged"];class x{constructor(t){p(this,"ethereumProvider",null);p(this,"requestWaitingForConnection",{});p(this,"processRpcCallFromWindow",({rpcRequest:t})=>{if(!this.ethereumProvider){t.id&&(this.requestWaitingForConnection[t.id]=t);return}this.processRpcRequest(t)});this.iframeWidow=t}disconnect(){this.ethereumProvider=null,I(window,_.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow)}onConnect(t){this.ethereumProvider?this.disconnect():P(window,_.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),this.ethereumProvider=t,this.processPendingRequests(),q.forEach(n=>{t.on(n,r=>this.onProviderEvent(n,r))})}processPendingRequests(){Object.keys(this.requestWaitingForConnection).forEach(t=>{this.processRpcRequest(this.requestWaitingForConnection[t])}),this.requestWaitingForConnection={}}processRpcRequest(t){const{id:n,jsonrpc:r,method:s}=t;if(!this.ethereumProvider||!n)return;(s==="enable"?this.ethereumProvider.enable():this.ethereumProvider.request({...t,id:n})).then(i=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:r,id:n,result:i}})).catch(i=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:r,id:n,error:i}}))}onProviderEvent(t,n){m(this.iframeWidow,l.PROVIDER_ON_EVENT,{event:t,params:n})}forwardRpcResponseToIframe(t){m(this.iframeWidow,l.PROVIDER_RPC_RESPONSE,t)}}function $(){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:$()},D="_";function k(e){const t=L[e.env||"prod"],n=N(e),r=U(e);return t+"/#"+n+"?"+r}function N(e){const{chainId:t=1,sell:n,buy:r,tradeType:s=O.SWAP}=e,c=[(n==null?void 0:n.asset)||D,(r==null?void 0:r.asset)||D].map(encodeURIComponent).join("/");return`/${t}/widget/${s}/${c}`}function U(e){const{sell:t,buy:n,theme:r}=e,s=new URLSearchParams;return t!=null&&t.amount&&s.append("sellAmount",t.amount),n!=null&&n.amount&&s.append("buyAmount",n.amount),r&&s.append("theme",typeof r=="string"?r:r.baseTheme),s}var y=(e=>(e.SWAP_ETH_FLOW_SENT_TX="SWAP_ETH_FLOW_SENT_TX",e.SWAP_POSTED_API="SWAP_POSTED_API",e.SWAP_SIGNING_ERROR="SWAP_SIGNING_ERROR",e.SWAP_TRADE_EXECUTED="SWAP_TRADE_EXECUTED",e.SWAP_ORDER_CANCELLED="SWAP_ORDER_CANCELLED",e))(y||{}),H=(e=>(e.ON_TOAST_MESSAGE="ON_TOAST_MESSAGE",e.ON_POSTED_ORDER="ON_POSTED_ORDER",e.ON_POSTED_ETH_FLOW_ORDER="ON_POSTED_ETH_FLOW_ORDER",e.ON_EXECUTED_ORDER="ON_EXECUTED_ORDER",e.ON_CANCELLED_ORDER="ON_CANCELLED_ORDER",e))(H||{});class V{constructor(){p(this,"subscriptions",{})}on(t){const{event:n,handler:r}=t;this.subscriptions[n]||(this.subscriptions[n]=[]),this.subscriptions[n].push(r)}off(t){const{event:n,handler:r}=t;this.subscriptions[n]&&(this.subscriptions[n]=this.subscriptions[n].filter(s=>s!==r))}emit(t,n){this.subscriptions[t]&&this.subscriptions[t].forEach(r=>r(n))}}let X=1;class j{constructor(t,n=[]){p(this,"eventEmitter",new V);p(this,"listeners",[]);p(this,"widgetListener");p(this,"instanceNumber",X++);this.contentWindow=t,this.updateListeners(n),this.widgetListener=P(this.contentWindow,_.EMIT_COW_EVENT,r=>this.eventEmitter.emit(r.event,r.payload))}stopListeningIframe(){A(this.contentWindow,this.widgetListener)}updateListeners(t){for(const n of this.listeners)this.eventEmitter.off(n);this.listeners=t||[];for(const n of this.listeners)this.eventEmitter.on(n)}}const F="640px",Q="450px",K=20;function T(e,t){const{params:n,provider:r,listeners:s}=t;let c=r;const i=J(n);e.innerHTML="",e.appendChild(i);const{contentWindow:a}=i;if(!a)throw console.error("Iframe does not contain a window",i),new Error("Iframe does not contain a window!");const R=[];R.push(Y(a,n.appCode)),R.push(z(i,n.height));const h=new j(window,s);let E=W(a,null,c);return i.addEventListener("load",()=>C(a,n,c)),{updateParams:o=>C(a,o,c),updateListeners:o=>h.updateListeners(o),updateProvider:o=>{c=o,E=W(a,E,o)},destroy:()=>{h.stopListeningIframe(),R.forEach(o=>window.removeEventListener("message",o)),e.removeChild(i)}}}function W(e,t,n){t&&t.disconnect();const r=t||new x(e);return n&&r.onConnect(n),r}function J(e){const{width:t=Q,height:n=F}=e,r=document.createElement("iframe");return r.src=k(e),r.width=t,r.height=n,r.style.border="0",r}function C(e,t,n){const r=!!n,s=N(t),c=U(t).toString();m(e,l.UPDATE_PARAMS,{urlParams:{pathname:s,search:c},appParams:{...t},hasProvider:r})}function Y(e,t){const n=P(window,_.ACTIVATE,()=>{A(window,n),m(e,l.UPDATE_APP_DATA,{metaData:t?{appCode:t}:void 0})});return n}function z(e,t=F){return P(window,_.UPDATE_HEIGHT,n=>{e.style.height=n.height?`${n.height+K}px`:t})}function B(e){const{params:t,provider:n,listeners:r}=e,[s,c]=d.useState(null),i=d.useRef(null),a=d.useRef(n),R=d.useRef(r),h=d.useRef(null),E=d.useRef(null),o=d.useCallback((u,f)=>{try{console.log(`[WIDGET] ${u}`),f()}catch(g){const S=`Error ${u.toLowerCase()}`;console.error(`[WIDGET] ${S}`,g),c({message:S,error:g})}},[c]);return d.useEffect(()=>{if(!h.current||JSON.stringify(i.current)===JSON.stringify(t))return;const u=h.current,f=E.current;i.current=t,f===null?o("Creating a new widget",()=>{E.current=T(u,{params:t,provider:a.current,listeners:r}),R.current=r}):o("Updating the widget",()=>f.updateParams(t))},[t]),d.useEffect(()=>{if(!E.current||a.current===n)return;a.current=n;const u=h.current;u&&o("Updating the provider",()=>{var f;(f=E.current)==null||f.destroy(),E.current=T(u,{params:t,provider:a.current,listeners:r})})},[n]),d.useEffect(()=>{if(!E.current||R.current===r)return;const u=E.current;o("Updating the listeners",()=>u.updateListeners(r))},[r]),s?w.jsxs("div",{style:{color:"#ff3a3a"},children:[s.message,s.error.message&&w.jsx("pre",{style:{whiteSpace:"pre-wrap",fontSize:"0.75em"},children:s.error.message})]}):w.jsx("div",{ref:h})}exports.COWSWAP_URLS=L;exports.CowEvents=H;exports.CowSwapWidget=B;exports.SimpleCowEventEmitter=V;exports.ToastMessageType=y;exports.TradeType=O;exports.WidgetMethodsEmit=_;exports.WidgetMethodsListen=l;exports.createCowSwapWidget=T;exports.listenToMessageFromWindow=P;exports.postMessageToWindow=m;exports.stopListeningToMessageFromWindow=I;exports.stopListeningWindowListener=A;
|
package/index.mjs
CHANGED
|
@@ -1,279 +1,283 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
import { jsxs as
|
|
5
|
-
import { useState as
|
|
6
|
-
const
|
|
7
|
-
function
|
|
1
|
+
var H = Object.defineProperty;
|
|
2
|
+
var V = (e, t, n) => t in e ? H(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n;
|
|
3
|
+
var u = (e, t, n) => (V(e, typeof t != "symbol" ? t + "" : t, n), n);
|
|
4
|
+
import { jsxs as y, jsx as O } from "react/jsx-runtime";
|
|
5
|
+
import { useState as F, useRef as R, useCallback as b, useEffect as w } from "react";
|
|
6
|
+
const v = "cowSwapWidget";
|
|
7
|
+
function m(e, t, n) {
|
|
8
8
|
const s = {
|
|
9
|
-
key:
|
|
10
|
-
method:
|
|
11
|
-
...typeof
|
|
9
|
+
key: v,
|
|
10
|
+
method: t,
|
|
11
|
+
...typeof n == "object" ? n : {}
|
|
12
12
|
};
|
|
13
|
-
|
|
13
|
+
e.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 P(e, t, n) {
|
|
20
20
|
const r = (s) => {
|
|
21
|
-
!
|
|
21
|
+
!G(s.data) || s.data.key !== v || s.data.method !== t || n(s.data);
|
|
22
22
|
};
|
|
23
|
-
return
|
|
23
|
+
return e.addEventListener("message", r), r;
|
|
24
24
|
}
|
|
25
|
-
function
|
|
26
|
-
return typeof
|
|
25
|
+
function G(e) {
|
|
26
|
+
return typeof e == "object" && e !== null && "key" in e && "method" in e && typeof e.key == "string" && typeof e.method == "string";
|
|
27
27
|
}
|
|
28
|
-
function
|
|
29
|
-
|
|
28
|
+
function q(e, t, n) {
|
|
29
|
+
e.removeEventListener("message", n);
|
|
30
30
|
}
|
|
31
|
-
function
|
|
32
|
-
|
|
31
|
+
function W(e, t) {
|
|
32
|
+
e.removeEventListener("message", t);
|
|
33
33
|
}
|
|
34
|
-
var
|
|
35
|
-
const
|
|
36
|
-
class
|
|
34
|
+
var f = /* @__PURE__ */ ((e) => (e.ACTIVATE = "ACTIVATE", e.UPDATE_HEIGHT = "UPDATE_HEIGHT", e.EMIT_COW_EVENT = "EMIT_COW_EVENT", e.PROVIDER_RPC_REQUEST = "PROVIDER_RPC_REQUEST", e))(f || {}), l = /* @__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))(l || {}), I = /* @__PURE__ */ ((e) => (e.SWAP = "swap", e.LIMIT = "limit", e.ADVANCED = "advanced", e))(I || {});
|
|
35
|
+
const $ = ["connect", "disconnect", "close", "chainChanged", "accountsChanged"];
|
|
36
|
+
class x {
|
|
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(t) {
|
|
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
|
+
u(this, "ethereumProvider", null);
|
|
48
48
|
/** Stored JSON-RPC requests, to queue them when disconnected. */
|
|
49
|
-
|
|
50
|
-
|
|
49
|
+
u(this, "requestWaitingForConnection", {});
|
|
50
|
+
u(this, "processRpcCallFromWindow", ({ rpcRequest: t }) => {
|
|
51
51
|
if (!this.ethereumProvider) {
|
|
52
|
-
|
|
52
|
+
t.id && (this.requestWaitingForConnection[t.id] = t);
|
|
53
53
|
return;
|
|
54
54
|
}
|
|
55
|
-
this.processRpcRequest(
|
|
55
|
+
this.processRpcRequest(t);
|
|
56
56
|
});
|
|
57
|
-
this.iframeWidow =
|
|
57
|
+
this.iframeWidow = t;
|
|
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, q(window, f.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(t) {
|
|
70
|
+
this.ethereumProvider ? this.disconnect() : P(window, f.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow), this.ethereumProvider = t, this.processPendingRequests(), $.forEach((n) => {
|
|
71
|
+
t.on(n, (r) => this.onProviderEvent(n, 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((t) => {
|
|
76
|
+
this.processRpcRequest(this.requestWaitingForConnection[t]);
|
|
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(t) {
|
|
84
|
+
const { id: n, jsonrpc: r, method: s } = t;
|
|
85
|
+
if (!this.ethereumProvider || !n)
|
|
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({ ...t, id: n })).then(
|
|
88
|
+
(i) => this.forwardRpcResponseToIframe({
|
|
89
|
+
rpcResponse: { jsonrpc: r, id: n, result: i }
|
|
90
90
|
})
|
|
91
91
|
).catch(
|
|
92
|
-
(
|
|
93
|
-
rpcResponse: { jsonrpc: r, id:
|
|
92
|
+
(i) => this.forwardRpcResponseToIframe({
|
|
93
|
+
rpcResponse: { jsonrpc: r, id: n, error: i }
|
|
94
94
|
})
|
|
95
95
|
);
|
|
96
96
|
}
|
|
97
|
-
onProviderEvent(
|
|
98
|
-
|
|
99
|
-
event:
|
|
100
|
-
params:
|
|
97
|
+
onProviderEvent(t, n) {
|
|
98
|
+
m(this.iframeWidow, l.PROVIDER_ON_EVENT, {
|
|
99
|
+
event: t,
|
|
100
|
+
params: n
|
|
101
101
|
});
|
|
102
102
|
}
|
|
103
103
|
/**
|
|
104
104
|
* Forward a JSON-RPC message to the content window.
|
|
105
105
|
*/
|
|
106
|
-
forwardRpcResponseToIframe(
|
|
107
|
-
|
|
106
|
+
forwardRpcResponseToIframe(t) {
|
|
107
|
+
m(this.iframeWidow, l.PROVIDER_RPC_RESPONSE, t);
|
|
108
108
|
}
|
|
109
109
|
}
|
|
110
|
-
function
|
|
110
|
+
function M() {
|
|
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: M()
|
|
118
|
+
}, D = "_";
|
|
119
|
+
function X(e) {
|
|
120
|
+
const t = k[e.env || "prod"], n = N(e), r = L(e);
|
|
121
|
+
return t + "/#" + n + "?" + r;
|
|
122
122
|
}
|
|
123
|
-
function
|
|
124
|
-
const { chainId:
|
|
125
|
-
return `/${
|
|
123
|
+
function N(e) {
|
|
124
|
+
const { chainId: t = 1, sell: n, buy: r, tradeType: s = I.SWAP } = e, c = [(n == null ? void 0 : n.asset) || D, (r == null ? void 0 : r.asset) || D].map(encodeURIComponent).join("/");
|
|
125
|
+
return `/${t}/widget/${s}/${c}`;
|
|
126
126
|
}
|
|
127
|
-
function L(
|
|
128
|
-
const { sell:
|
|
129
|
-
return
|
|
127
|
+
function L(e) {
|
|
128
|
+
const { sell: t, buy: n, theme: r } = e, s = new URLSearchParams();
|
|
129
|
+
return t != null && t.amount && s.append("sellAmount", t.amount), n != null && n.amount && s.append("buyAmount", n.amount), r && s.append("theme", typeof r == "string" ? r : r.baseTheme), s;
|
|
130
130
|
}
|
|
131
|
-
|
|
131
|
+
var Q = /* @__PURE__ */ ((e) => (e.SWAP_ETH_FLOW_SENT_TX = "SWAP_ETH_FLOW_SENT_TX", e.SWAP_POSTED_API = "SWAP_POSTED_API", e.SWAP_SIGNING_ERROR = "SWAP_SIGNING_ERROR", e.SWAP_TRADE_EXECUTED = "SWAP_TRADE_EXECUTED", e.SWAP_ORDER_CANCELLED = "SWAP_ORDER_CANCELLED", e))(Q || {}), K = /* @__PURE__ */ ((e) => (e.ON_TOAST_MESSAGE = "ON_TOAST_MESSAGE", e.ON_POSTED_ORDER = "ON_POSTED_ORDER", e.ON_POSTED_ETH_FLOW_ORDER = "ON_POSTED_ETH_FLOW_ORDER", e.ON_EXECUTED_ORDER = "ON_EXECUTED_ORDER", e.ON_CANCELLED_ORDER = "ON_CANCELLED_ORDER", e))(K || {});
|
|
132
|
+
class j {
|
|
132
133
|
constructor() {
|
|
133
|
-
|
|
134
|
+
u(this, "subscriptions", {});
|
|
134
135
|
}
|
|
135
|
-
on(
|
|
136
|
-
const { event:
|
|
137
|
-
this.subscriptions[
|
|
136
|
+
on(t) {
|
|
137
|
+
const { event: n, handler: r } = t;
|
|
138
|
+
this.subscriptions[n] || (this.subscriptions[n] = []), this.subscriptions[n].push(r);
|
|
138
139
|
}
|
|
139
|
-
off(
|
|
140
|
-
const { event:
|
|
141
|
-
this.subscriptions[
|
|
140
|
+
off(t) {
|
|
141
|
+
const { event: n, handler: r } = t;
|
|
142
|
+
this.subscriptions[n] && (this.subscriptions[n] = this.subscriptions[n].filter((s) => s !== r));
|
|
142
143
|
}
|
|
143
|
-
emit(
|
|
144
|
-
this.subscriptions[
|
|
144
|
+
emit(t, n) {
|
|
145
|
+
this.subscriptions[t] && this.subscriptions[t].forEach((r) => r(n));
|
|
145
146
|
}
|
|
146
147
|
}
|
|
147
|
-
let
|
|
148
|
-
class
|
|
149
|
-
constructor(
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
this.contentWindow =
|
|
148
|
+
let J = 1;
|
|
149
|
+
class Y {
|
|
150
|
+
constructor(t, n = []) {
|
|
151
|
+
u(this, "eventEmitter", new j());
|
|
152
|
+
u(this, "listeners", []);
|
|
153
|
+
u(this, "widgetListener");
|
|
154
|
+
u(this, "instanceNumber", J++);
|
|
155
|
+
this.contentWindow = t, this.updateListeners(n), this.widgetListener = P(
|
|
155
156
|
this.contentWindow,
|
|
156
|
-
|
|
157
|
+
f.EMIT_COW_EVENT,
|
|
157
158
|
(r) => this.eventEmitter.emit(r.event, r.payload)
|
|
158
159
|
);
|
|
159
160
|
}
|
|
160
161
|
stopListeningIframe() {
|
|
161
|
-
|
|
162
|
+
W(this.contentWindow, this.widgetListener);
|
|
162
163
|
}
|
|
163
|
-
updateListeners(
|
|
164
|
-
for (const
|
|
165
|
-
this.eventEmitter.off(
|
|
166
|
-
this.listeners =
|
|
167
|
-
for (const
|
|
168
|
-
this.eventEmitter.on(
|
|
164
|
+
updateListeners(t) {
|
|
165
|
+
for (const n of this.listeners)
|
|
166
|
+
this.eventEmitter.off(n);
|
|
167
|
+
this.listeners = t || [];
|
|
168
|
+
for (const n of this.listeners)
|
|
169
|
+
this.eventEmitter.on(n);
|
|
169
170
|
}
|
|
170
171
|
}
|
|
171
|
-
const U = "640px",
|
|
172
|
-
function
|
|
173
|
-
const { params:
|
|
172
|
+
const U = "640px", z = "450px", B = 20;
|
|
173
|
+
function S(e, t) {
|
|
174
|
+
const { params: n, provider: r, listeners: s } = t;
|
|
174
175
|
let c = r;
|
|
175
|
-
const
|
|
176
|
-
|
|
177
|
-
const { contentWindow: a } =
|
|
176
|
+
const i = Z(n);
|
|
177
|
+
e.innerHTML = "", e.appendChild(i);
|
|
178
|
+
const { contentWindow: a } = i;
|
|
178
179
|
if (!a)
|
|
179
|
-
throw console.error("Iframe does not contain a window",
|
|
180
|
-
const
|
|
181
|
-
|
|
182
|
-
const
|
|
183
|
-
let
|
|
184
|
-
return
|
|
185
|
-
updateParams: (
|
|
186
|
-
updateListeners: (
|
|
187
|
-
updateProvider: (
|
|
188
|
-
c =
|
|
180
|
+
throw console.error("Iframe does not contain a window", i), new Error("Iframe does not contain a window!");
|
|
181
|
+
const _ = [];
|
|
182
|
+
_.push(ee(a, n.appCode)), _.push(te(i, n.height));
|
|
183
|
+
const p = new Y(window, s);
|
|
184
|
+
let E = g(a, null, c);
|
|
185
|
+
return i.addEventListener("load", () => C(a, n, c)), {
|
|
186
|
+
updateParams: (o) => C(a, o, c),
|
|
187
|
+
updateListeners: (o) => p.updateListeners(o),
|
|
188
|
+
updateProvider: (o) => {
|
|
189
|
+
c = o, E = g(a, E, o);
|
|
189
190
|
},
|
|
190
191
|
destroy: () => {
|
|
191
|
-
|
|
192
|
+
p.stopListeningIframe(), _.forEach((o) => window.removeEventListener("message", o)), e.removeChild(i);
|
|
192
193
|
}
|
|
193
194
|
};
|
|
194
195
|
}
|
|
195
|
-
function
|
|
196
|
-
|
|
197
|
-
const r =
|
|
198
|
-
return
|
|
196
|
+
function g(e, t, n) {
|
|
197
|
+
t && t.disconnect();
|
|
198
|
+
const r = t || new x(e);
|
|
199
|
+
return n && r.onConnect(n), r;
|
|
199
200
|
}
|
|
200
|
-
function
|
|
201
|
-
const { width:
|
|
202
|
-
return r.src =
|
|
201
|
+
function Z(e) {
|
|
202
|
+
const { width: t = z, height: n = U } = e, r = document.createElement("iframe");
|
|
203
|
+
return r.src = X(e), r.width = t, r.height = n, r.style.border = "0", r;
|
|
203
204
|
}
|
|
204
|
-
function
|
|
205
|
-
const r = !!
|
|
206
|
-
|
|
205
|
+
function C(e, t, n) {
|
|
206
|
+
const r = !!n, s = N(t), c = L(t).toString();
|
|
207
|
+
m(e, l.UPDATE_PARAMS, {
|
|
207
208
|
urlParams: {
|
|
208
209
|
pathname: s,
|
|
209
210
|
search: c
|
|
210
211
|
},
|
|
211
212
|
appParams: {
|
|
212
|
-
...
|
|
213
|
+
...t
|
|
213
214
|
},
|
|
214
215
|
hasProvider: r
|
|
215
216
|
});
|
|
216
217
|
}
|
|
217
|
-
function
|
|
218
|
-
const
|
|
219
|
-
|
|
220
|
-
metaData:
|
|
218
|
+
function ee(e, t) {
|
|
219
|
+
const n = P(window, f.ACTIVATE, () => {
|
|
220
|
+
W(window, n), m(e, l.UPDATE_APP_DATA, {
|
|
221
|
+
metaData: t ? { appCode: t } : void 0
|
|
221
222
|
});
|
|
222
223
|
});
|
|
223
|
-
return
|
|
224
|
+
return n;
|
|
224
225
|
}
|
|
225
|
-
function
|
|
226
|
-
return
|
|
227
|
-
|
|
226
|
+
function te(e, t = U) {
|
|
227
|
+
return P(window, f.UPDATE_HEIGHT, (n) => {
|
|
228
|
+
e.style.height = n.height ? `${n.height + B}px` : t;
|
|
228
229
|
});
|
|
229
230
|
}
|
|
230
|
-
function
|
|
231
|
-
const { params:
|
|
232
|
-
(
|
|
231
|
+
function ie(e) {
|
|
232
|
+
const { params: t, provider: n, listeners: r } = e, [s, c] = F(null), i = R(null), a = R(n), _ = R(r), p = R(null), E = R(null), o = b(
|
|
233
|
+
(d, h) => {
|
|
233
234
|
try {
|
|
234
|
-
console.log(`[WIDGET] ${
|
|
235
|
+
console.log(`[WIDGET] ${d}`), h();
|
|
235
236
|
} catch (T) {
|
|
236
|
-
const
|
|
237
|
-
console.error(`[WIDGET] ${
|
|
237
|
+
const A = `Error ${d.toLowerCase()}`;
|
|
238
|
+
console.error(`[WIDGET] ${A}`, T), c({ message: A, error: T });
|
|
238
239
|
}
|
|
239
240
|
},
|
|
240
241
|
[c]
|
|
241
242
|
);
|
|
242
|
-
return
|
|
243
|
-
if (!
|
|
243
|
+
return w(() => {
|
|
244
|
+
if (!p.current || JSON.stringify(i.current) === JSON.stringify(t))
|
|
244
245
|
return;
|
|
245
|
-
const
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
}) :
|
|
249
|
-
}, [
|
|
250
|
-
if (!
|
|
246
|
+
const d = p.current, h = E.current;
|
|
247
|
+
i.current = t, h === null ? o("Creating a new widget", () => {
|
|
248
|
+
E.current = S(d, { params: t, provider: a.current, listeners: r }), _.current = r;
|
|
249
|
+
}) : o("Updating the widget", () => h.updateParams(t));
|
|
250
|
+
}, [t]), w(() => {
|
|
251
|
+
if (!E.current || a.current === n)
|
|
251
252
|
return;
|
|
252
|
-
a.current =
|
|
253
|
-
const
|
|
254
|
-
|
|
255
|
-
var
|
|
256
|
-
(
|
|
253
|
+
a.current = n;
|
|
254
|
+
const d = p.current;
|
|
255
|
+
d && o("Updating the provider", () => {
|
|
256
|
+
var h;
|
|
257
|
+
(h = E.current) == null || h.destroy(), E.current = S(d, { params: t, provider: a.current, listeners: r });
|
|
257
258
|
});
|
|
258
|
-
}, [
|
|
259
|
-
if (!
|
|
259
|
+
}, [n]), w(() => {
|
|
260
|
+
if (!E.current || _.current === r)
|
|
260
261
|
return;
|
|
261
|
-
const
|
|
262
|
-
|
|
263
|
-
}, [r]), s ? /* @__PURE__ */
|
|
262
|
+
const d = E.current;
|
|
263
|
+
o("Updating the listeners", () => d.updateListeners(r));
|
|
264
|
+
}, [r]), s ? /* @__PURE__ */ y("div", { style: { color: "#ff3a3a" }, children: [
|
|
264
265
|
s.message,
|
|
265
|
-
s.error.message && /* @__PURE__ */
|
|
266
|
-
] }) : /* @__PURE__ */
|
|
266
|
+
s.error.message && /* @__PURE__ */ O("pre", { style: { whiteSpace: "pre-wrap", fontSize: "0.75em" }, children: s.error.message })
|
|
267
|
+
] }) : /* @__PURE__ */ O("div", { ref: p });
|
|
267
268
|
}
|
|
268
269
|
export {
|
|
269
270
|
k as COWSWAP_URLS,
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
271
|
+
K as CowEvents,
|
|
272
|
+
ie as CowSwapWidget,
|
|
273
|
+
j as SimpleCowEventEmitter,
|
|
274
|
+
Q as ToastMessageType,
|
|
275
|
+
I as TradeType,
|
|
276
|
+
f as WidgetMethodsEmit,
|
|
277
|
+
l as WidgetMethodsListen,
|
|
278
|
+
S as createCowSwapWidget,
|
|
279
|
+
P as listenToMessageFromWindow,
|
|
280
|
+
m as postMessageToWindow,
|
|
281
|
+
q as stopListeningToMessageFromWindow,
|
|
282
|
+
W as stopListeningWindowListener
|
|
279
283
|
};
|
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.1",
|
|
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.1"
|
|
26
26
|
}
|
|
27
27
|
}
|