@cowprotocol/widget-lib 0.6.0-RC.1 → 0.6.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.d.ts +0 -1
- package/index.iife.js +1 -1
- package/index.js +1 -1
- package/index.mjs +91 -95
- package/package.json +4 -2
package/index.d.ts
CHANGED
package/index.iife.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var cowSwapWidget=function(o){"use strict";const
|
|
1
|
+
var cowSwapWidget=function(o){"use strict";const P="cowSwapWidget";function p(t,n,e){const s={key:P,method:n,...typeof e=="object"?e:{}};t.postMessage(s,"*")}function E(t,n,e){const i=s=>{!D(s.data)||s.data.key!==P||s.data.method!==n||e(s.data)};return t.addEventListener("message",i),i}function D(t){return typeof t=="object"&&t!==null&&"key"in t&&"method"in t&&typeof t.key=="string"&&typeof t.method=="string"}function l(t,n,e){t.removeEventListener("message",e)}function m(t,n){t.removeEventListener("message",n)}var d=(t=>(t.ACTIVATE="ACTIVATE",t.UPDATE_HEIGHT="UPDATE_HEIGHT",t.EMIT_COW_EVENT="EMIT_COW_EVENT",t.PROVIDER_RPC_REQUEST="PROVIDER_RPC_REQUEST",t))(d||{}),h=(t=>(t.UPDATE_PARAMS="UPDATE_PARAMS",t.UPDATE_APP_DATA="UPDATE_APP_DATA",t.PROVIDER_RPC_RESPONSE="PROVIDER_RPC_RESPONSE",t.PROVIDER_ON_EVENT="PROVIDER_ON_EVENT",t))(h||{}),w=(t=>(t.SWAP="swap",t.LIMIT="limit",t.ADVANCED="advanced",t))(w||{});const S=["connect","disconnect","close","chainChanged","accountsChanged"];class O{constructor(n){this.iframeWidow=n,this.ethereumProvider=null,this.requestWaitingForConnection={},this.processRpcCallFromWindow=({rpcRequest:e})=>{if(!this.ethereumProvider){e.id&&(this.requestWaitingForConnection[e.id]=e);return}this.processRpcRequest(e)}}disconnect(){this.ethereumProvider=null,l(window,d.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow)}onConnect(n){this.ethereumProvider?this.disconnect():E(window,d.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),this.ethereumProvider=n,this.processPendingRequests(),S.forEach(e=>{n.on(e,i=>this.onProviderEvent(e,i))})}processPendingRequests(){Object.keys(this.requestWaitingForConnection).forEach(n=>{this.processRpcRequest(this.requestWaitingForConnection[n])}),this.requestWaitingForConnection={}}processRpcRequest(n){const{id:e,jsonrpc:i,method:s}=n;if(!this.ethereumProvider||!e)return;(s==="enable"?this.ethereumProvider.enable():this.ethereumProvider.request({...n,id:e})).then(r=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:i,id:e,result:r}})).catch(r=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:i,id:e,error:r}}))}onProviderEvent(n,e){p(this.iframeWidow,h.PROVIDER_ON_EVENT,{event:n,params:e})}forwardRpcResponseToIframe(n){p(this.iframeWidow,h.PROVIDER_RPC_RESPONSE,n)}}function L(){return typeof window>"u"||!window?"":`https://swap-dev-git-${window.location.hostname.replace("widget-configurator-git-","").replace("-cowswap.vercel.app","")}-cowswap.vercel.app`}const R={local:"http://localhost:3000",prod:"https://swap.cow.fi",dev:"https://dev.swap.cow.fi",pr:L()},T="_";function V(t){const n=R[t.env||"prod"],e=_(t),i=g(t);return n+"/#"+e+"?"+i}function _(t){const{chainId:n=1,sell:e,buy:i,tradeType:s=w.SWAP}=t,c=[(e==null?void 0:e.asset)||T,(i==null?void 0:i.asset)||T].map(encodeURIComponent).join("/");return`/${n}/widget/${s}/${c}`}function g(t){const{sell:n,buy:e,theme:i}=t,s=new URLSearchParams;return n!=null&&n.amount&&s.append("sellAmount",n.amount),e!=null&&e.amount&&s.append("buyAmount",e.amount),i&&s.append("theme",typeof i=="string"?i:i.baseTheme),s}class y{constructor(){this.subscriptions={}}on(n){const{event:e,handler:i}=n;this.subscriptions[e]||(this.subscriptions[e]=[]),this.subscriptions[e].push(i)}off(n){const{event:e,handler:i}=n;this.subscriptions[e]&&(this.subscriptions[e]=this.subscriptions[e].filter(s=>s!==i))}emit(n,e){this.subscriptions[n]&&this.subscriptions[n].forEach(i=>i(e))}}let U=1;class N{constructor(n,e=[]){this.contentWindow=n,this.eventEmitter=new y,this.listeners=[],this.instanceNumber=U++,this.updateListeners(e),this.widgetListener=E(this.contentWindow,d.EMIT_COW_EVENT,i=>this.eventEmitter.emit(i.event,i.payload))}stopListeningIframe(){m(this.contentWindow,this.widgetListener)}updateListeners(n){for(const e of this.listeners)this.eventEmitter.off(e);this.listeners=n||[];for(const e of this.listeners)this.eventEmitter.on(e)}}const A="640px",F="450px",b=20;function H(t,n){const{params:e,provider:i,listeners:s}=n;let c=i;const r=M(e);t.innerHTML="",t.appendChild(r);const{contentWindow:u}=r;if(!u)throw console.error("Iframe does not contain a window",r),new Error("Iframe does not contain a window!");const f=[];f.push(q(u,e.appCode)),f.push(G(r,e.height));const C=new N(window,s);let I=v(u,null,c);return r.addEventListener("load",()=>W(u,e,c)),{updateParams:a=>W(u,a,c),updateListeners:a=>C.updateListeners(a),updateProvider:a=>{c=a,I=v(u,I,a)},destroy:()=>{C.stopListeningIframe(),f.forEach(a=>window.removeEventListener("message",a)),t.removeChild(r)}}}function v(t,n,e){n&&n.disconnect();const i=n||new O(t);return e&&i.onConnect(e),i}function M(t){const{width:n=F,height:e=A}=t,i=document.createElement("iframe");return i.src=V(t),i.width=n,i.height=e,i.style.border="0",i}function W(t,n,e){const i=!!e,s=_(n),c=g(n).toString();p(t,h.UPDATE_PARAMS,{urlParams:{pathname:s,search:c},appParams:{...n},hasProvider:i})}function q(t,n){const e=E(window,d.ACTIVATE,()=>{m(window,e),p(t,h.UPDATE_APP_DATA,{metaData:n?{appCode:n}:void 0})});return e}function G(t,n=A){return E(window,d.UPDATE_HEIGHT,e=>{t.style.height=e.height?`${e.height+b}px`:n})}return o.COWSWAP_URLS=R,o.TradeType=w,o.WidgetMethodsEmit=d,o.WidgetMethodsListen=h,o.createCowSwapWidget=H,o.listenToMessageFromWindow=E,o.postMessageToWindow=p,o.stopListeningToMessageFromWindow=l,o.stopListeningWindowListener=m,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"}),o}({});
|
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const _="cowSwapWidget";function p(t,n,e){const i={key:_,method:n,...typeof e=="object"?e:{}};t.postMessage(i,"*")}function u(t,n,e){const s=i=>{!I(i.data)||i.data.key!==_||i.data.method!==n||e(i.data)};return t.addEventListener("message",s),s}function I(t){return typeof t=="object"&&t!==null&&"key"in t&&"method"in t&&typeof t.key=="string"&&typeof t.method=="string"}function g(t,n,e){t.removeEventListener("message",e)}function m(t,n){t.removeEventListener("message",n)}var a=(t=>(t.ACTIVATE="ACTIVATE",t.UPDATE_HEIGHT="UPDATE_HEIGHT",t.EMIT_COW_EVENT="EMIT_COW_EVENT",t.PROVIDER_RPC_REQUEST="PROVIDER_RPC_REQUEST",t))(a||{}),h=(t=>(t.UPDATE_PARAMS="UPDATE_PARAMS",t.UPDATE_APP_DATA="UPDATE_APP_DATA",t.PROVIDER_RPC_RESPONSE="PROVIDER_RPC_RESPONSE",t.PROVIDER_ON_EVENT="PROVIDER_ON_EVENT",t))(h||{}),w=(t=>(t.SWAP="swap",t.LIMIT="limit",t.ADVANCED="advanced",t))(w||{});const D=["connect","disconnect","close","chainChanged","accountsChanged"];class O{constructor(n){this.iframeWidow=n,this.ethereumProvider=null,this.requestWaitingForConnection={},this.processRpcCallFromWindow=({rpcRequest:e})=>{if(!this.ethereumProvider){e.id&&(this.requestWaitingForConnection[e.id]=e);return}this.processRpcRequest(e)}}disconnect(){this.ethereumProvider=null,g(window,a.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow)}onConnect(n){this.ethereumProvider?this.disconnect():u(window,a.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),this.ethereumProvider=n,this.processPendingRequests(),D.forEach(e=>{n.on(e,s=>this.onProviderEvent(e,s))})}processPendingRequests(){Object.keys(this.requestWaitingForConnection).forEach(n=>{this.processRpcRequest(this.requestWaitingForConnection[n])}),this.requestWaitingForConnection={}}processRpcRequest(n){const{id:e,jsonrpc:s,method:i}=n;if(!this.ethereumProvider||!e)return;(i==="enable"?this.ethereumProvider.enable():this.ethereumProvider.request({...n,id:e})).then(o=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:s,id:e,result:o}})).catch(o=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:s,id:e,error:o}}))}onProviderEvent(n,e){p(this.iframeWidow,h.PROVIDER_ON_EVENT,{event:n,params:e})}forwardRpcResponseToIframe(n){p(this.iframeWidow,h.PROVIDER_RPC_RESPONSE,n)}}function S(){return typeof window>"u"||!window?"":`https://swap-dev-git-${window.location.hostname.replace("widget-configurator-git-","").replace("-cowswap.vercel.app","")}-cowswap.vercel.app`}const A={local:"http://localhost:3000",prod:"https://swap.cow.fi",dev:"https://dev.swap.cow.fi",pr:S()},l="_";function L(t){const n=A[t.env||"prod"],e=v(t),s=C(t);return n+"/#"+e+"?"+s}function v(t){const{chainId:n=1,sell:e,buy:s,tradeType:i=w.SWAP}=t,r=[(e==null?void 0:e.asset)||l,(s==null?void 0:s.asset)||l].map(encodeURIComponent).join("/");return`/${n}/widget/${i}/${r}`}function C(t){const{sell:n,buy:e,theme:s}=t,i=new URLSearchParams;return n!=null&&n.amount&&i.append("sellAmount",n.amount),e!=null&&e.amount&&i.append("buyAmount",e.amount),s&&i.append("theme",typeof s=="string"?s:s.baseTheme),i}class V{constructor(){this.subscriptions={}}on(n){const{event:e,handler:s}=n;this.subscriptions[e]||(this.subscriptions[e]=[]),this.subscriptions[e].push(s)}off(n){const{event:e,handler:s}=n;this.subscriptions[e]&&(this.subscriptions[e]=this.subscriptions[e].filter(i=>i!==s))}emit(n,e){this.subscriptions[n]&&this.subscriptions[n].forEach(s=>s(e))}}let y=1;class U{constructor(n,e=[]){this.contentWindow=n,this.eventEmitter=new V,this.listeners=[],this.instanceNumber=y++,this.updateListeners(e),this.widgetListener=u(this.contentWindow,a.EMIT_COW_EVENT,s=>this.eventEmitter.emit(s.event,s.payload))}stopListeningIframe(){m(this.contentWindow,this.widgetListener)}updateListeners(n){for(const e of this.listeners)this.eventEmitter.off(e);this.listeners=n||[];for(const e of this.listeners)this.eventEmitter.on(e)}}const W="640px",N="450px",F=20;function b(t,n){const{params:e,provider:s,listeners:i}=n;let r=s;const o=H(e);t.innerHTML="",t.appendChild(o);const{contentWindow:d}=o;if(!d)throw console.error("Iframe does not contain a window",o),new Error("Iframe does not contain a window!");const E=[];E.push(M(d,e.appCode)),E.push(q(o,e.height));const f=new U(window,i);let P=R(d,null,r);return o.addEventListener("load",()=>T(d,e,r)),{updateParams:c=>T(d,c,r),updateListeners:c=>f.updateListeners(c),updateProvider:c=>{r=c,P=R(d,P,c)},destroy:()=>{f.stopListeningIframe(),E.forEach(c=>window.removeEventListener("message",c)),t.removeChild(o)}}}function R(t,n,e){n&&n.disconnect();const s=n||new O(t);return e&&s.onConnect(e),s}function H(t){const{width:n=N,height:e=W}=t,s=document.createElement("iframe");return s.src=L(t),s.width=n,s.height=e,s.style.border="0",s}function T(t,n,e){const s=!!e,i=v(n),r=C(n).toString();p(t,h.UPDATE_PARAMS,{urlParams:{pathname:i,search:r},appParams:{...n},hasProvider:s})}function M(t,n){const e=u(window,a.ACTIVATE,()=>{m(window,e),p(t,h.UPDATE_APP_DATA,{metaData:n?{appCode:n}:void 0})});return e}function q(t,n=W){return u(window,a.UPDATE_HEIGHT,e=>{t.style.height=e.height?`${e.height+F}px`:n})}exports.COWSWAP_URLS=A;exports.TradeType=w;exports.WidgetMethodsEmit=a;exports.WidgetMethodsListen=h;exports.createCowSwapWidget=b;exports.listenToMessageFromWindow=u;exports.postMessageToWindow=p;exports.stopListeningToMessageFromWindow=g;exports.stopListeningWindowListener=m;
|
package/index.mjs
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
const
|
|
2
|
-
function
|
|
3
|
-
const
|
|
4
|
-
key:
|
|
1
|
+
const R = "cowSwapWidget";
|
|
2
|
+
function p(t, n, e) {
|
|
3
|
+
const i = {
|
|
4
|
+
key: R,
|
|
5
5
|
method: n,
|
|
6
6
|
...typeof e == "object" ? e : {}
|
|
7
7
|
};
|
|
8
8
|
t.postMessage(
|
|
9
|
-
|
|
9
|
+
i,
|
|
10
10
|
"*"
|
|
11
11
|
// TODO: Change to CoW specific origin in production. https://github.com/cowprotocol/cowswap/issues/3828
|
|
12
12
|
);
|
|
13
13
|
}
|
|
14
|
-
function
|
|
15
|
-
const
|
|
16
|
-
!
|
|
14
|
+
function u(t, n, e) {
|
|
15
|
+
const s = (i) => {
|
|
16
|
+
!I(i.data) || i.data.key !== R || i.data.method !== n || e(i.data);
|
|
17
17
|
};
|
|
18
|
-
return t.addEventListener("message",
|
|
18
|
+
return t.addEventListener("message", s), s;
|
|
19
19
|
}
|
|
20
|
-
function
|
|
20
|
+
function I(t) {
|
|
21
21
|
return typeof t == "object" && t !== null && "key" in t && "method" in t && typeof t.key == "string" && typeof t.method == "string";
|
|
22
22
|
}
|
|
23
23
|
function C(t, n, e) {
|
|
24
24
|
t.removeEventListener("message", e);
|
|
25
25
|
}
|
|
26
|
-
function
|
|
26
|
+
function T(t, n) {
|
|
27
27
|
t.removeEventListener("message", n);
|
|
28
28
|
}
|
|
29
|
-
var
|
|
30
|
-
const
|
|
29
|
+
var d = /* @__PURE__ */ ((t) => (t.ACTIVATE = "ACTIVATE", t.UPDATE_HEIGHT = "UPDATE_HEIGHT", t.EMIT_COW_EVENT = "EMIT_COW_EVENT", t.PROVIDER_RPC_REQUEST = "PROVIDER_RPC_REQUEST", t))(d || {}), h = /* @__PURE__ */ ((t) => (t.UPDATE_PARAMS = "UPDATE_PARAMS", t.UPDATE_APP_DATA = "UPDATE_APP_DATA", t.PROVIDER_RPC_RESPONSE = "PROVIDER_RPC_RESPONSE", t.PROVIDER_ON_EVENT = "PROVIDER_ON_EVENT", t))(h || {}), _ = /* @__PURE__ */ ((t) => (t.SWAP = "swap", t.LIMIT = "limit", t.ADVANCED = "advanced", t))(_ || {});
|
|
30
|
+
const D = ["connect", "disconnect", "close", "chainChanged", "accountsChanged"];
|
|
31
31
|
class W {
|
|
32
32
|
/**
|
|
33
33
|
* Creates an instance of IframeRpcProviderBridge.
|
|
@@ -47,15 +47,15 @@ class W {
|
|
|
47
47
|
* Disconnects the JSON-RPC bridge from the Ethereum provider.
|
|
48
48
|
*/
|
|
49
49
|
disconnect() {
|
|
50
|
-
this.ethereumProvider = null, C(window,
|
|
50
|
+
this.ethereumProvider = null, C(window, d.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow);
|
|
51
51
|
}
|
|
52
52
|
/**
|
|
53
53
|
* Handles the 'connect' event and sets up event listeners for Ethereum provider events.
|
|
54
54
|
* @param newProvider - The Ethereum provider to connect.
|
|
55
55
|
*/
|
|
56
56
|
onConnect(n) {
|
|
57
|
-
this.ethereumProvider ? this.disconnect() :
|
|
58
|
-
n.on(e, (
|
|
57
|
+
this.ethereumProvider ? this.disconnect() : u(window, d.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow), this.ethereumProvider = n, this.processPendingRequests(), D.forEach((e) => {
|
|
58
|
+
n.on(e, (s) => this.onProviderEvent(e, s));
|
|
59
59
|
});
|
|
60
60
|
}
|
|
61
61
|
processPendingRequests() {
|
|
@@ -68,21 +68,21 @@ class W {
|
|
|
68
68
|
* @param request - The JSON-RPC request to be processed.
|
|
69
69
|
*/
|
|
70
70
|
processRpcRequest(n) {
|
|
71
|
-
const { id: e, jsonrpc:
|
|
71
|
+
const { id: e, jsonrpc: s, method: i } = n;
|
|
72
72
|
if (!this.ethereumProvider || !e)
|
|
73
73
|
return;
|
|
74
|
-
(
|
|
74
|
+
(i === "enable" ? this.ethereumProvider.enable() : this.ethereumProvider.request({ ...n, id: e })).then(
|
|
75
75
|
(o) => this.forwardRpcResponseToIframe({
|
|
76
|
-
rpcResponse: { jsonrpc:
|
|
76
|
+
rpcResponse: { jsonrpc: s, id: e, result: o }
|
|
77
77
|
})
|
|
78
78
|
).catch(
|
|
79
79
|
(o) => this.forwardRpcResponseToIframe({
|
|
80
|
-
rpcResponse: { jsonrpc:
|
|
80
|
+
rpcResponse: { jsonrpc: s, id: e, error: o }
|
|
81
81
|
})
|
|
82
82
|
);
|
|
83
83
|
}
|
|
84
84
|
onProviderEvent(n, e) {
|
|
85
|
-
|
|
85
|
+
p(this.iframeWidow, h.PROVIDER_ON_EVENT, {
|
|
86
86
|
event: n,
|
|
87
87
|
params: e
|
|
88
88
|
});
|
|
@@ -91,58 +91,57 @@ class W {
|
|
|
91
91
|
* Forward a JSON-RPC message to the content window.
|
|
92
92
|
*/
|
|
93
93
|
forwardRpcResponseToIframe(n) {
|
|
94
|
-
|
|
94
|
+
p(this.iframeWidow, h.PROVIDER_RPC_RESPONSE, n);
|
|
95
95
|
}
|
|
96
96
|
}
|
|
97
|
-
function
|
|
97
|
+
function O() {
|
|
98
98
|
return typeof window > "u" || !window ? "" : `https://swap-dev-git-${window.location.hostname.replace("widget-configurator-git-", "").replace("-cowswap.vercel.app", "")}-cowswap.vercel.app`;
|
|
99
99
|
}
|
|
100
|
-
const
|
|
100
|
+
const S = {
|
|
101
101
|
local: "http://localhost:3000",
|
|
102
102
|
prod: "https://swap.cow.fi",
|
|
103
103
|
dev: "https://dev.swap.cow.fi",
|
|
104
|
-
pr:
|
|
105
|
-
},
|
|
106
|
-
function
|
|
107
|
-
const n =
|
|
108
|
-
return n + "/#" + e + "?" +
|
|
109
|
-
}
|
|
110
|
-
function
|
|
111
|
-
const { chainId: n = 1, sell: e, buy:
|
|
112
|
-
return `/${n}/widget/${
|
|
113
|
-
}
|
|
114
|
-
function
|
|
115
|
-
const { sell: n, buy: e, theme:
|
|
116
|
-
return n != null && n.amount &&
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
class V {
|
|
104
|
+
pr: O()
|
|
105
|
+
}, w = "_";
|
|
106
|
+
function V(t) {
|
|
107
|
+
const n = S[t.env || "prod"], e = A(t), s = v(t);
|
|
108
|
+
return n + "/#" + e + "?" + s;
|
|
109
|
+
}
|
|
110
|
+
function A(t) {
|
|
111
|
+
const { chainId: n = 1, sell: e, buy: s, tradeType: i = _.SWAP } = t, r = [(e == null ? void 0 : e.asset) || w, (s == null ? void 0 : s.asset) || w].map(encodeURIComponent).join("/");
|
|
112
|
+
return `/${n}/widget/${i}/${r}`;
|
|
113
|
+
}
|
|
114
|
+
function v(t) {
|
|
115
|
+
const { sell: n, buy: e, theme: s } = t, i = new URLSearchParams();
|
|
116
|
+
return n != null && n.amount && i.append("sellAmount", n.amount), e != null && e.amount && i.append("buyAmount", e.amount), s && i.append("theme", typeof s == "string" ? s : s.baseTheme), i;
|
|
117
|
+
}
|
|
118
|
+
class L {
|
|
120
119
|
constructor() {
|
|
121
120
|
this.subscriptions = {};
|
|
122
121
|
}
|
|
123
122
|
on(n) {
|
|
124
|
-
const { event: e, handler:
|
|
125
|
-
this.subscriptions[e] || (this.subscriptions[e] = []), this.subscriptions[e].push(
|
|
123
|
+
const { event: e, handler: s } = n;
|
|
124
|
+
this.subscriptions[e] || (this.subscriptions[e] = []), this.subscriptions[e].push(s);
|
|
126
125
|
}
|
|
127
126
|
off(n) {
|
|
128
|
-
const { event: e, handler:
|
|
129
|
-
this.subscriptions[e] && (this.subscriptions[e] = this.subscriptions[e].filter((
|
|
127
|
+
const { event: e, handler: s } = n;
|
|
128
|
+
this.subscriptions[e] && (this.subscriptions[e] = this.subscriptions[e].filter((i) => i !== s));
|
|
130
129
|
}
|
|
131
130
|
emit(n, e) {
|
|
132
|
-
this.subscriptions[n] && this.subscriptions[n].forEach((
|
|
131
|
+
this.subscriptions[n] && this.subscriptions[n].forEach((s) => s(e));
|
|
133
132
|
}
|
|
134
133
|
}
|
|
135
|
-
let
|
|
136
|
-
class
|
|
134
|
+
let U = 1;
|
|
135
|
+
class y {
|
|
137
136
|
constructor(n, e = []) {
|
|
138
|
-
this.contentWindow = n, this.eventEmitter = new
|
|
137
|
+
this.contentWindow = n, this.eventEmitter = new L(), this.listeners = [], this.instanceNumber = U++, this.updateListeners(e), this.widgetListener = u(
|
|
139
138
|
this.contentWindow,
|
|
140
|
-
|
|
141
|
-
(
|
|
139
|
+
d.EMIT_COW_EVENT,
|
|
140
|
+
(s) => this.eventEmitter.emit(s.event, s.payload)
|
|
142
141
|
);
|
|
143
142
|
}
|
|
144
143
|
stopListeningIframe() {
|
|
145
|
-
|
|
144
|
+
T(this.contentWindow, this.widgetListener);
|
|
146
145
|
}
|
|
147
146
|
updateListeners(n) {
|
|
148
147
|
for (const e of this.listeners)
|
|
@@ -152,76 +151,73 @@ class F {
|
|
|
152
151
|
this.eventEmitter.on(e);
|
|
153
152
|
}
|
|
154
153
|
}
|
|
155
|
-
const
|
|
156
|
-
function
|
|
157
|
-
const { params: e, provider:
|
|
158
|
-
let r =
|
|
159
|
-
const o =
|
|
154
|
+
const g = "640px", N = "450px", H = 20;
|
|
155
|
+
function M(t, n) {
|
|
156
|
+
const { params: e, provider: s, listeners: i } = n;
|
|
157
|
+
let r = s;
|
|
158
|
+
const o = F(e);
|
|
160
159
|
t.innerHTML = "", t.appendChild(o);
|
|
161
|
-
const { contentWindow:
|
|
162
|
-
if (!
|
|
160
|
+
const { contentWindow: a } = o;
|
|
161
|
+
if (!a)
|
|
163
162
|
throw console.error("Iframe does not contain a window", o), new Error("Iframe does not contain a window!");
|
|
164
|
-
const
|
|
165
|
-
|
|
166
|
-
const
|
|
167
|
-
let
|
|
168
|
-
return o.addEventListener("load", () =>
|
|
169
|
-
updateParams: (c) =>
|
|
170
|
-
updateListeners: (c) =>
|
|
163
|
+
const E = [];
|
|
164
|
+
E.push(b(a, e.appCode)), E.push(q(o, e.height));
|
|
165
|
+
const m = new y(window, i);
|
|
166
|
+
let f = P(a, null, r);
|
|
167
|
+
return o.addEventListener("load", () => l(a, e, r)), {
|
|
168
|
+
updateParams: (c) => l(a, c, r),
|
|
169
|
+
updateListeners: (c) => m.updateListeners(c),
|
|
171
170
|
updateProvider: (c) => {
|
|
172
|
-
r = c,
|
|
171
|
+
r = c, f = P(a, f, c);
|
|
173
172
|
},
|
|
174
173
|
destroy: () => {
|
|
175
|
-
|
|
174
|
+
m.stopListeningIframe(), E.forEach((c) => window.removeEventListener("message", c)), t.removeChild(o);
|
|
176
175
|
}
|
|
177
176
|
};
|
|
178
177
|
}
|
|
179
|
-
function
|
|
178
|
+
function P(t, n, e) {
|
|
180
179
|
n && n.disconnect();
|
|
181
|
-
const
|
|
182
|
-
return e &&
|
|
180
|
+
const s = n || new W(t);
|
|
181
|
+
return e && s.onConnect(e), s;
|
|
183
182
|
}
|
|
184
|
-
function
|
|
185
|
-
const { width: n =
|
|
186
|
-
return
|
|
183
|
+
function F(t) {
|
|
184
|
+
const { width: n = N, height: e = g } = t, s = document.createElement("iframe");
|
|
185
|
+
return s.src = V(t), s.width = n, s.height = e, s.style.border = "0", s;
|
|
187
186
|
}
|
|
188
|
-
function
|
|
189
|
-
const
|
|
190
|
-
|
|
187
|
+
function l(t, n, e) {
|
|
188
|
+
const s = !!e, i = A(n), r = v(n).toString();
|
|
189
|
+
p(t, h.UPDATE_PARAMS, {
|
|
191
190
|
urlParams: {
|
|
192
|
-
pathname:
|
|
191
|
+
pathname: i,
|
|
193
192
|
search: r
|
|
194
193
|
},
|
|
195
194
|
appParams: {
|
|
196
195
|
...n
|
|
197
196
|
},
|
|
198
|
-
hasProvider:
|
|
197
|
+
hasProvider: s
|
|
199
198
|
});
|
|
200
199
|
}
|
|
201
|
-
function
|
|
202
|
-
const e =
|
|
203
|
-
|
|
200
|
+
function b(t, n) {
|
|
201
|
+
const e = u(window, d.ACTIVATE, () => {
|
|
202
|
+
T(window, e), p(t, h.UPDATE_APP_DATA, {
|
|
204
203
|
metaData: n ? { appCode: n } : void 0
|
|
205
204
|
});
|
|
206
205
|
});
|
|
207
206
|
return e;
|
|
208
207
|
}
|
|
209
|
-
function
|
|
210
|
-
return
|
|
211
|
-
t.style.height = e.height ? `${e.height +
|
|
208
|
+
function q(t, n = g) {
|
|
209
|
+
return u(window, d.UPDATE_HEIGHT, (e) => {
|
|
210
|
+
t.style.height = e.height ? `${e.height + H}px` : n;
|
|
212
211
|
});
|
|
213
212
|
}
|
|
214
213
|
export {
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
X as createCowSwapWidget,
|
|
223
|
-
_ as listenToMessageFromWindow,
|
|
224
|
-
h as postMessageToWindow,
|
|
214
|
+
S as COWSWAP_URLS,
|
|
215
|
+
_ as TradeType,
|
|
216
|
+
d as WidgetMethodsEmit,
|
|
217
|
+
h as WidgetMethodsListen,
|
|
218
|
+
M as createCowSwapWidget,
|
|
219
|
+
u as listenToMessageFromWindow,
|
|
220
|
+
p as postMessageToWindow,
|
|
225
221
|
C as stopListeningToMessageFromWindow,
|
|
226
|
-
|
|
222
|
+
T as stopListeningWindowListener
|
|
227
223
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cowprotocol/widget-lib",
|
|
3
|
-
"version": "0.6.0-RC.
|
|
3
|
+
"version": "0.6.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 website.",
|
|
6
6
|
"main": "index.js",
|
|
@@ -20,5 +20,7 @@
|
|
|
20
20
|
"dex",
|
|
21
21
|
"widget-lib"
|
|
22
22
|
],
|
|
23
|
-
"dependencies": {
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@cowprotocol/events": "^0.1.0-RC.0"
|
|
25
|
+
}
|
|
24
26
|
}
|