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