@cowprotocol/widget-lib 0.8.0 → 0.9.0
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 +102 -79
- package/package.json +2 -2
- package/themeUtils.d.ts +2 -0
- package/types.d.ts +9 -12
- package/urlUtils.d.ts +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 T="cowSwapWidget";function f(e,n,t){const s={key:T,method:n,...typeof t=="object"?t:{}};e.postMessage(s,"*")}function E(e,n,t){const i=s=>{!D(s.data)||s.data.key!==T||s.data.method!==n||t(s.data)};return e.addEventListener("message",i),i}function D(e){return typeof e=="object"&&e!==null&&"key"in e&&"method"in e&&typeof e.key=="string"&&typeof e.method=="string"}function R(e,n,t){e.removeEventListener("message",t)}function m(e,n){e.removeEventListener("message",n)}var h=(e=>(e.ACTIVATE="ACTIVATE",e.UPDATE_HEIGHT="UPDATE_HEIGHT",e.EMIT_COW_EVENT="EMIT_COW_EVENT",e.PROVIDER_RPC_REQUEST="PROVIDER_RPC_REQUEST",e))(h||{}),p=(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))(p||{}),w=(e=>(e.SWAP="swap",e.LIMIT="limit",e.ADVANCED="advanced",e))(w||{});const L=["primary","background","paper","text","danger","warning","alert","info","success"],O=["connect","disconnect","close","chainChanged","accountsChanged"];class V{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,R(window,h.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow)}onConnect(n){this.ethereumProvider?this.disconnect():E(window,h.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),this.ethereumProvider=n,this.processPendingRequests(),O.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(a=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:i,id:t,result:a}})).catch(a=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:i,id:t,error:a}}))}onProviderEvent(n,t){f(this.iframeWidow,p.PROVIDER_ON_EVENT,{event:n,params:t})}forwardRpcResponseToIframe(n){f(this.iframeWidow,p.PROVIDER_RPC_RESPONSE,n)}}function g(e){return!!(e&&typeof e=="object")}const _="_";function y(e){const n=typeof e.baseUrl=="string"?e.baseUrl:"https://swap.cow.fi",t=A(e);return n+"/#"+t+"?"+W(e)}function A(e){const{chainId:n=1,sell:t,buy:i,tradeType:s=w.SWAP}=e,r=[(t==null?void 0:t.asset)||_,(i==null?void 0:i.asset)||_].map(encodeURIComponent).join("/");return`/${n}/widget/${s}/${r}`}function W(e){const n=new URLSearchParams;return U(M(n,e),e)}function M(e,n){const{sell:t,buy:i}=n;return t!=null&&t.amount&&e.append("sellAmount",t.amount),i!=null&&i.amount&&e.append("buyAmount",i.amount),e}function U(e,n){const t=n.theme;return t&&(g(t)?(e.append("palette",encodeURIComponent(JSON.stringify(t))),e.append("theme",t.baseTheme)):e.append("theme",t)),e}class b{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))}}class F{constructor(n,t=[]){this.contentWindow=n,this.eventEmitter=new b,this.listeners=[],this.updateListeners(t),this.widgetListener=E(this.contentWindow,h.EMIT_COW_EVENT,i=>this.eventEmitter.emit(i.event,i.payload))}stopListeningIframe(){m(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)}}class N{constructor(n,t){this.appWindow=n,this.iframeWidow=t,this.forwardSdkMessage=i=>{H(i.data)&&(k(i.data)?this.appWindow.parent.postMessage(i.data,"*"):G(i.data)&&this.iframeWidow.postMessage(i.data,"*"))},this.startListening()}startListening(){this.appWindow.addEventListener("message",this.forwardSdkMessage)}stopListening(){this.appWindow.removeEventListener("message",this.forwardSdkMessage)}}function H(e){return typeof e=="object"&&e!==null&&"id"in e&&typeof e.id=="string"}function k(e){return"method"in e&&typeof e.method=="string"&&"params"in e&&"env"in e&&typeof e.env=="object"&&e.env!==null&&"sdkVersion"in e.env}function G(e){return"success"in e&&typeof e.success=="boolean"&&"version"in e&&typeof e.version=="string"}const v="640px",Q="450px",q=20;function B(e,n){const{params:t,provider:i,listeners:s}=n;let r=i,a=t;const c=$(t);e.innerHTML="",e.appendChild(c);const{contentWindow:u}=c;if(!u)throw console.error("Iframe does not contain a window",c),new Error("Iframe does not contain a window!");const l=[];l.push(K(u,t.appCode)),l.push(Y(c,t.height));const S=new F(window,s);let P=C(u,null,r);c.addEventListener("load",()=>I(u,a,r));const j=new N(window,u);return{updateParams:d=>{a=d,I(u,a,r)},updateListeners:d=>S.updateListeners(d),updateProvider:d=>{r=d,P=C(u,P,d)},destroy:()=>{P.disconnect(),S.stopListeningIframe(),l.forEach(d=>window.removeEventListener("message",d)),j.stopListening(),e.removeChild(c)}}}function C(e,n,t){n&&n.disconnect();const i=n||new V(e);return t&&i.onConnect(t),i}function $(e){const{width:n=Q,height:t=v}=e,i=document.createElement("iframe");return i.src=y(e),i.width=n,i.height=t,i.style.border="0",i}function I(e,n,t){const i=!!t,s=A(n),r=W(n).toString(),{theme:a,...c}=n;f(e,p.UPDATE_PARAMS,{urlParams:{pathname:s,search:r},appParams:c,hasProvider:i})}function K(e,n){const t=E(window,h.ACTIVATE,()=>{m(window,t),f(e,p.UPDATE_APP_DATA,{metaData:n?{appCode:n}:void 0})});return t}function Y(e,n=v){return E(window,h.UPDATE_HEIGHT,t=>{e.style.height=t.height?`${t.height+q}px`:n})}return o.TradeType=w,o.WIDGET_PALETTE_COLORS=L,o.WidgetMethodsEmit=h,o.WidgetMethodsListen=p,o.createCowSwapWidget=B,o.isCowSwapWidgetPalette=g,o.listenToMessageFromWindow=E,o.postMessageToWindow=f,o.stopListeningToMessageFromWindow=R,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 u(e,n,t){const i={key:_,method:n,...typeof t=="object"?t:{}};e.postMessage(i,"*")}function f(e,n,t){const s=i=>{!D(i.data)||i.data.key!==_||i.data.method!==n||t(i.data)};return e.addEventListener("message",s),s}function D(e){return typeof e=="object"&&e!==null&&"key"in e&&"method"in e&&typeof e.key=="string"&&typeof e.method=="string"}function A(e,n,t){e.removeEventListener("message",t)}function w(e,n){e.removeEventListener("message",n)}var h=(e=>(e.ACTIVATE="ACTIVATE",e.UPDATE_HEIGHT="UPDATE_HEIGHT",e.EMIT_COW_EVENT="EMIT_COW_EVENT",e.PROVIDER_RPC_REQUEST="PROVIDER_RPC_REQUEST",e))(h||{}),p=(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))(p||{}),l=(e=>(e.SWAP="swap",e.LIMIT="limit",e.ADVANCED="advanced",e))(l||{});const L=["primary","background","paper","text","danger","warning","alert","info","success"],O=["connect","disconnect","close","chainChanged","accountsChanged"];class V{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,A(window,h.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow)}onConnect(n){this.ethereumProvider?this.disconnect():f(window,h.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),this.ethereumProvider=n,this.processPendingRequests(),O.forEach(t=>{n.on(t,s=>this.onProviderEvent(t,s))})}processPendingRequests(){Object.keys(this.requestWaitingForConnection).forEach(n=>{this.processRpcRequest(this.requestWaitingForConnection[n])}),this.requestWaitingForConnection={}}processRpcRequest(n){const{id:t,jsonrpc:s,method:i}=n;if(!this.ethereumProvider||!t)return;(i==="enable"?this.ethereumProvider.enable():this.ethereumProvider.request({...n,id:t})).then(r=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:s,id:t,result:r}})).catch(r=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:s,id:t,error:r}}))}onProviderEvent(n,t){u(this.iframeWidow,p.PROVIDER_ON_EVENT,{event:n,params:t})}forwardRpcResponseToIframe(n){u(this.iframeWidow,p.PROVIDER_RPC_RESPONSE,n)}}function W(e){return!!(e&&typeof e=="object")}const T="_";function y(e){const n=typeof e.baseUrl=="string"?e.baseUrl:"https://swap.cow.fi",t=v(e);return n+"/#"+t+"?"+C(e)}function v(e){const{chainId:n=1,sell:t,buy:s,tradeType:i=l.SWAP}=e,o=[(t==null?void 0:t.asset)||T,(s==null?void 0:s.asset)||T].map(encodeURIComponent).join("/");return`/${n}/widget/${i}/${o}`}function C(e){const n=new URLSearchParams;return U(M(n,e),e)}function M(e,n){const{sell:t,buy:s}=n;return t!=null&&t.amount&&e.append("sellAmount",t.amount),s!=null&&s.amount&&e.append("buyAmount",s.amount),e}function U(e,n){const t=n.theme;return t&&(W(t)?(e.append("palette",encodeURIComponent(JSON.stringify(t))),e.append("theme",t.baseTheme)):e.append("theme",t)),e}class b{constructor(){this.subscriptions={}}on(n){const{event:t,handler:s}=n;this.subscriptions[t]||(this.subscriptions[t]=[]),this.subscriptions[t].push(s)}off(n){const{event:t,handler:s}=n;this.subscriptions[t]&&(this.subscriptions[t]=this.subscriptions[t].filter(i=>i!==s))}emit(n,t){this.subscriptions[n]&&this.subscriptions[n].forEach(s=>s(t))}}class F{constructor(n,t=[]){this.contentWindow=n,this.eventEmitter=new b,this.listeners=[],this.updateListeners(t),this.widgetListener=f(this.contentWindow,h.EMIT_COW_EVENT,s=>this.eventEmitter.emit(s.event,s.payload))}stopListeningIframe(){w(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)}}class N{constructor(n,t){this.appWindow=n,this.iframeWidow=t,this.forwardSdkMessage=s=>{H(s.data)&&(k(s.data)?this.appWindow.parent.postMessage(s.data,"*"):G(s.data)&&this.iframeWidow.postMessage(s.data,"*"))},this.startListening()}startListening(){this.appWindow.addEventListener("message",this.forwardSdkMessage)}stopListening(){this.appWindow.removeEventListener("message",this.forwardSdkMessage)}}function H(e){return typeof e=="object"&&e!==null&&"id"in e&&typeof e.id=="string"}function k(e){return"method"in e&&typeof e.method=="string"&&"params"in e&&"env"in e&&typeof e.env=="object"&&e.env!==null&&"sdkVersion"in e.env}function G(e){return"success"in e&&typeof e.success=="boolean"&&"version"in e&&typeof e.version=="string"}const I="640px",Q="450px",q=20;function x(e,n){const{params:t,provider:s,listeners:i}=n;let o=s,r=t;const a=B(t);e.innerHTML="",e.appendChild(a);const{contentWindow:d}=a;if(!d)throw console.error("Iframe does not contain a window",a),new Error("Iframe does not contain a window!");const E=[];E.push($(d,t.appCode)),E.push(K(a,t.height));const P=new F(window,i);let m=R(d,null,o);a.addEventListener("load",()=>g(d,r,o));const S=new N(window,d);return{updateParams:c=>{r=c,g(d,r,o)},updateListeners:c=>P.updateListeners(c),updateProvider:c=>{o=c,m=R(d,m,c)},destroy:()=>{m.disconnect(),P.stopListeningIframe(),E.forEach(c=>window.removeEventListener("message",c)),S.stopListening(),e.removeChild(a)}}}function R(e,n,t){n&&n.disconnect();const s=n||new V(e);return t&&s.onConnect(t),s}function B(e){const{width:n=Q,height:t=I}=e,s=document.createElement("iframe");return s.src=y(e),s.width=n,s.height=t,s.style.border="0",s}function g(e,n,t){const s=!!t,i=v(n),o=C(n).toString(),{theme:r,...a}=n;u(e,p.UPDATE_PARAMS,{urlParams:{pathname:i,search:o},appParams:a,hasProvider:s})}function $(e,n){const t=f(window,h.ACTIVATE,()=>{w(window,t),u(e,p.UPDATE_APP_DATA,{metaData:n?{appCode:n}:void 0})});return t}function K(e,n=I){return f(window,h.UPDATE_HEIGHT,t=>{e.style.height=t.height?`${t.height+q}px`:n})}exports.TradeType=l;exports.WIDGET_PALETTE_COLORS=L;exports.WidgetMethodsEmit=h;exports.WidgetMethodsListen=p;exports.createCowSwapWidget=x;exports.isCowSwapWidgetPalette=W;exports.listenToMessageFromWindow=f;exports.postMessageToWindow=u;exports.stopListeningToMessageFromWindow=A;exports.stopListeningWindowListener=w;
|
package/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
const
|
|
1
|
+
const T = "cowSwapWidget";
|
|
2
2
|
function u(e, n, t) {
|
|
3
3
|
const i = {
|
|
4
|
-
key:
|
|
4
|
+
key: T,
|
|
5
5
|
method: n,
|
|
6
6
|
...typeof t == "object" ? t : {}
|
|
7
7
|
};
|
|
@@ -11,24 +11,34 @@ function u(e, n, t) {
|
|
|
11
11
|
// TODO: Change to CoW specific origin in production. https://github.com/cowprotocol/cowswap/issues/3828
|
|
12
12
|
);
|
|
13
13
|
}
|
|
14
|
-
function
|
|
14
|
+
function f(e, n, t) {
|
|
15
15
|
const s = (i) => {
|
|
16
|
-
!
|
|
16
|
+
!W(i.data) || i.data.key !== T || i.data.method !== n || t(i.data);
|
|
17
17
|
};
|
|
18
18
|
return e.addEventListener("message", s), s;
|
|
19
19
|
}
|
|
20
|
-
function
|
|
20
|
+
function W(e) {
|
|
21
21
|
return typeof e == "object" && e !== null && "key" in e && "method" in e && typeof e.key == "string" && typeof e.method == "string";
|
|
22
22
|
}
|
|
23
|
-
function
|
|
23
|
+
function S(e, n, t) {
|
|
24
24
|
e.removeEventListener("message", t);
|
|
25
25
|
}
|
|
26
|
-
function
|
|
26
|
+
function _(e, n) {
|
|
27
27
|
e.removeEventListener("message", n);
|
|
28
28
|
}
|
|
29
|
-
var
|
|
30
|
-
const
|
|
31
|
-
|
|
29
|
+
var h = /* @__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))(h || {}), p = /* @__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))(p || {}), g = /* @__PURE__ */ ((e) => (e.SWAP = "swap", e.LIMIT = "limit", e.ADVANCED = "advanced", e))(g || {});
|
|
30
|
+
const $ = [
|
|
31
|
+
"primary",
|
|
32
|
+
"background",
|
|
33
|
+
"paper",
|
|
34
|
+
"text",
|
|
35
|
+
"danger",
|
|
36
|
+
"warning",
|
|
37
|
+
"alert",
|
|
38
|
+
"info",
|
|
39
|
+
"success"
|
|
40
|
+
], D = ["connect", "disconnect", "close", "chainChanged", "accountsChanged"];
|
|
41
|
+
class O {
|
|
32
42
|
/**
|
|
33
43
|
* Creates an instance of IframeRpcProviderBridge.
|
|
34
44
|
* @param iframeWidow - The iFrame window that will post up general RPC messages and to which the IframeRpcProviderBridge will forward the RPC result.
|
|
@@ -47,14 +57,14 @@ class D {
|
|
|
47
57
|
* Disconnects the JSON-RPC bridge from the Ethereum provider.
|
|
48
58
|
*/
|
|
49
59
|
disconnect() {
|
|
50
|
-
this.ethereumProvider = null,
|
|
60
|
+
this.ethereumProvider = null, S(window, h.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow);
|
|
51
61
|
}
|
|
52
62
|
/**
|
|
53
63
|
* Handles the 'connect' event and sets up event listeners for Ethereum provider events.
|
|
54
64
|
* @param newProvider - The Ethereum provider to connect.
|
|
55
65
|
*/
|
|
56
66
|
onConnect(n) {
|
|
57
|
-
this.ethereumProvider ? this.disconnect() :
|
|
67
|
+
this.ethereumProvider ? this.disconnect() : f(window, h.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow), this.ethereumProvider = n, this.processPendingRequests(), D.forEach((t) => {
|
|
58
68
|
n.on(t, (s) => this.onProviderEvent(t, s));
|
|
59
69
|
});
|
|
60
70
|
}
|
|
@@ -72,17 +82,17 @@ class D {
|
|
|
72
82
|
if (!this.ethereumProvider || !t)
|
|
73
83
|
return;
|
|
74
84
|
(i === "enable" ? this.ethereumProvider.enable() : this.ethereumProvider.request({ ...n, id: t })).then(
|
|
75
|
-
(
|
|
76
|
-
rpcResponse: { jsonrpc: s, id: t, result:
|
|
85
|
+
(r) => this.forwardRpcResponseToIframe({
|
|
86
|
+
rpcResponse: { jsonrpc: s, id: t, result: r }
|
|
77
87
|
})
|
|
78
88
|
).catch(
|
|
79
|
-
(
|
|
80
|
-
rpcResponse: { jsonrpc: s, id: t, error:
|
|
89
|
+
(r) => this.forwardRpcResponseToIframe({
|
|
90
|
+
rpcResponse: { jsonrpc: s, id: t, error: r }
|
|
81
91
|
})
|
|
82
92
|
);
|
|
83
93
|
}
|
|
84
94
|
onProviderEvent(n, t) {
|
|
85
|
-
u(this.iframeWidow,
|
|
95
|
+
u(this.iframeWidow, p.PROVIDER_ON_EVENT, {
|
|
86
96
|
event: n,
|
|
87
97
|
params: t
|
|
88
98
|
});
|
|
@@ -91,23 +101,34 @@ class D {
|
|
|
91
101
|
* Forward a JSON-RPC message to the content window.
|
|
92
102
|
*/
|
|
93
103
|
forwardRpcResponseToIframe(n) {
|
|
94
|
-
u(this.iframeWidow,
|
|
104
|
+
u(this.iframeWidow, p.PROVIDER_RPC_RESPONSE, n);
|
|
95
105
|
}
|
|
96
106
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
const n = typeof e.baseUrl == "string" ? e.baseUrl : "https://swap.cow.fi", t = g(e), s = A(e);
|
|
100
|
-
return n + "/#" + t + "?" + s;
|
|
107
|
+
function L(e) {
|
|
108
|
+
return !!(e && typeof e == "object");
|
|
101
109
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
110
|
+
const P = "_";
|
|
111
|
+
function V(e) {
|
|
112
|
+
const n = typeof e.baseUrl == "string" ? e.baseUrl : "https://swap.cow.fi", t = A(e);
|
|
113
|
+
return n + "/#" + t + "?" + v(e);
|
|
105
114
|
}
|
|
106
115
|
function A(e) {
|
|
107
|
-
const {
|
|
108
|
-
return n
|
|
116
|
+
const { chainId: n = 1, sell: t, buy: s, tradeType: i = g.SWAP } = e, o = [(t == null ? void 0 : t.asset) || P, (s == null ? void 0 : s.asset) || P].map(encodeURIComponent).join("/");
|
|
117
|
+
return `/${n}/widget/${i}/${o}`;
|
|
109
118
|
}
|
|
110
|
-
|
|
119
|
+
function v(e) {
|
|
120
|
+
const n = new URLSearchParams();
|
|
121
|
+
return y(U(n, e), e);
|
|
122
|
+
}
|
|
123
|
+
function U(e, n) {
|
|
124
|
+
const { sell: t, buy: s } = n;
|
|
125
|
+
return t != null && t.amount && e.append("sellAmount", t.amount), s != null && s.amount && e.append("buyAmount", s.amount), e;
|
|
126
|
+
}
|
|
127
|
+
function y(e, n) {
|
|
128
|
+
const t = n.theme;
|
|
129
|
+
return t && (L(t) ? (e.append("palette", encodeURIComponent(JSON.stringify(t))), e.append("theme", t.baseTheme)) : e.append("theme", t)), e;
|
|
130
|
+
}
|
|
131
|
+
class b {
|
|
111
132
|
constructor() {
|
|
112
133
|
this.subscriptions = {};
|
|
113
134
|
}
|
|
@@ -123,16 +144,16 @@ class L {
|
|
|
123
144
|
this.subscriptions[n] && this.subscriptions[n].forEach((s) => s(t));
|
|
124
145
|
}
|
|
125
146
|
}
|
|
126
|
-
class
|
|
147
|
+
class N {
|
|
127
148
|
constructor(n, t = []) {
|
|
128
|
-
this.contentWindow = n, this.eventEmitter = new
|
|
149
|
+
this.contentWindow = n, this.eventEmitter = new b(), this.listeners = [], this.updateListeners(t), this.widgetListener = f(
|
|
129
150
|
this.contentWindow,
|
|
130
|
-
|
|
151
|
+
h.EMIT_COW_EVENT,
|
|
131
152
|
(s) => this.eventEmitter.emit(s.event, s.payload)
|
|
132
153
|
);
|
|
133
154
|
}
|
|
134
155
|
stopListeningIframe() {
|
|
135
|
-
|
|
156
|
+
_(this.contentWindow, this.widgetListener);
|
|
136
157
|
}
|
|
137
158
|
updateListeners(n) {
|
|
138
159
|
for (const t of this.listeners)
|
|
@@ -142,10 +163,10 @@ class V {
|
|
|
142
163
|
this.eventEmitter.on(t);
|
|
143
164
|
}
|
|
144
165
|
}
|
|
145
|
-
class
|
|
166
|
+
class M {
|
|
146
167
|
constructor(n, t) {
|
|
147
168
|
this.appWindow = n, this.iframeWidow = t, this.forwardSdkMessage = (s) => {
|
|
148
|
-
|
|
169
|
+
F(s.data) && (H(s.data) ? this.appWindow.parent.postMessage(s.data, "*") : k(s.data) && this.iframeWidow.postMessage(s.data, "*"));
|
|
149
170
|
}, this.startListening();
|
|
150
171
|
}
|
|
151
172
|
startListening() {
|
|
@@ -155,83 +176,85 @@ class y {
|
|
|
155
176
|
this.appWindow.removeEventListener("message", this.forwardSdkMessage);
|
|
156
177
|
}
|
|
157
178
|
}
|
|
158
|
-
function
|
|
179
|
+
function F(e) {
|
|
159
180
|
return typeof e == "object" && e !== null && "id" in e && typeof e.id == "string";
|
|
160
181
|
}
|
|
161
|
-
function
|
|
182
|
+
function H(e) {
|
|
162
183
|
return "method" in e && typeof e.method == "string" && "params" in e && "env" in e && typeof e.env == "object" && e.env !== null && "sdkVersion" in e.env;
|
|
163
184
|
}
|
|
164
|
-
function
|
|
185
|
+
function k(e) {
|
|
165
186
|
return "success" in e && typeof e.success == "boolean" && "version" in e && typeof e.version == "string";
|
|
166
187
|
}
|
|
167
|
-
const
|
|
168
|
-
function
|
|
188
|
+
const I = "640px", G = "450px", Q = 20;
|
|
189
|
+
function K(e, n) {
|
|
169
190
|
const { params: t, provider: s, listeners: i } = n;
|
|
170
|
-
let r =
|
|
171
|
-
const
|
|
172
|
-
e.innerHTML = "", e.appendChild(
|
|
173
|
-
const { contentWindow:
|
|
174
|
-
if (!
|
|
175
|
-
throw console.error("Iframe does not contain a window",
|
|
176
|
-
const
|
|
177
|
-
|
|
178
|
-
const
|
|
179
|
-
let
|
|
180
|
-
|
|
181
|
-
const
|
|
191
|
+
let o = s, r = t;
|
|
192
|
+
const a = x(t);
|
|
193
|
+
e.innerHTML = "", e.appendChild(a);
|
|
194
|
+
const { contentWindow: d } = a;
|
|
195
|
+
if (!d)
|
|
196
|
+
throw console.error("Iframe does not contain a window", a), new Error("Iframe does not contain a window!");
|
|
197
|
+
const E = [];
|
|
198
|
+
E.push(q(d, t.appCode)), E.push(B(a, t.height));
|
|
199
|
+
const w = new N(window, i);
|
|
200
|
+
let m = l(d, null, o);
|
|
201
|
+
a.addEventListener("load", () => R(d, r, o));
|
|
202
|
+
const C = new M(window, d);
|
|
182
203
|
return {
|
|
183
|
-
updateParams: (
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
204
|
+
updateParams: (c) => {
|
|
205
|
+
r = c, R(d, r, o);
|
|
206
|
+
},
|
|
207
|
+
updateListeners: (c) => w.updateListeners(c),
|
|
208
|
+
updateProvider: (c) => {
|
|
209
|
+
o = c, m = l(d, m, c);
|
|
187
210
|
},
|
|
188
211
|
destroy: () => {
|
|
189
|
-
|
|
212
|
+
m.disconnect(), w.stopListeningIframe(), E.forEach((c) => window.removeEventListener("message", c)), C.stopListening(), e.removeChild(a);
|
|
190
213
|
}
|
|
191
214
|
};
|
|
192
215
|
}
|
|
193
|
-
function
|
|
216
|
+
function l(e, n, t) {
|
|
194
217
|
n && n.disconnect();
|
|
195
|
-
const s = n || new
|
|
218
|
+
const s = n || new O(e);
|
|
196
219
|
return t && s.onConnect(t), s;
|
|
197
220
|
}
|
|
198
|
-
function
|
|
199
|
-
const { width: n =
|
|
200
|
-
return s.src =
|
|
221
|
+
function x(e) {
|
|
222
|
+
const { width: n = G, height: t = I } = e, s = document.createElement("iframe");
|
|
223
|
+
return s.src = V(e), s.width = n, s.height = t, s.style.border = "0", s;
|
|
201
224
|
}
|
|
202
225
|
function R(e, n, t) {
|
|
203
|
-
const s = !!t, i =
|
|
204
|
-
u(e,
|
|
226
|
+
const s = !!t, i = A(n), o = v(n).toString(), { theme: r, ...a } = n;
|
|
227
|
+
u(e, p.UPDATE_PARAMS, {
|
|
205
228
|
urlParams: {
|
|
206
229
|
pathname: i,
|
|
207
|
-
search:
|
|
208
|
-
},
|
|
209
|
-
appParams: {
|
|
210
|
-
...n
|
|
230
|
+
search: o
|
|
211
231
|
},
|
|
232
|
+
appParams: a,
|
|
212
233
|
hasProvider: s
|
|
213
234
|
});
|
|
214
235
|
}
|
|
215
236
|
function q(e, n) {
|
|
216
|
-
const t =
|
|
217
|
-
|
|
237
|
+
const t = f(window, h.ACTIVATE, () => {
|
|
238
|
+
_(window, t), u(e, p.UPDATE_APP_DATA, {
|
|
218
239
|
metaData: n ? { appCode: n } : void 0
|
|
219
240
|
});
|
|
220
241
|
});
|
|
221
242
|
return t;
|
|
222
243
|
}
|
|
223
|
-
function
|
|
224
|
-
return
|
|
225
|
-
e.style.height = t.height ? `${t.height +
|
|
244
|
+
function B(e, n = I) {
|
|
245
|
+
return f(window, h.UPDATE_HEIGHT, (t) => {
|
|
246
|
+
e.style.height = t.height ? `${t.height + Q}px` : n;
|
|
226
247
|
});
|
|
227
248
|
}
|
|
228
249
|
export {
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
h as
|
|
232
|
-
|
|
233
|
-
|
|
250
|
+
g as TradeType,
|
|
251
|
+
$ as WIDGET_PALETTE_COLORS,
|
|
252
|
+
h as WidgetMethodsEmit,
|
|
253
|
+
p as WidgetMethodsListen,
|
|
254
|
+
K as createCowSwapWidget,
|
|
255
|
+
L as isCowSwapWidgetPalette,
|
|
256
|
+
f as listenToMessageFromWindow,
|
|
234
257
|
u as postMessageToWindow,
|
|
235
|
-
|
|
236
|
-
|
|
258
|
+
S as stopListeningToMessageFromWindow,
|
|
259
|
+
_ as stopListeningWindowListener
|
|
237
260
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cowprotocol/widget-lib",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
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",
|
|
@@ -21,6 +21,6 @@
|
|
|
21
21
|
"widget-lib"
|
|
22
22
|
],
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@cowprotocol/events": "^1.
|
|
24
|
+
"@cowprotocol/events": "^1.2.0"
|
|
25
25
|
}
|
|
26
26
|
}
|
package/themeUtils.d.ts
ADDED
package/types.d.ts
CHANGED
|
@@ -93,18 +93,14 @@ export type TokenInfo = {
|
|
|
93
93
|
symbol: string;
|
|
94
94
|
logoURI?: string;
|
|
95
95
|
};
|
|
96
|
-
export
|
|
96
|
+
export declare const WIDGET_PALETTE_COLORS: readonly ["primary", "background", "paper", "text", "danger", "warning", "alert", "info", "success"];
|
|
97
|
+
export type CowSwapWidgetPaletteColors = (typeof WIDGET_PALETTE_COLORS)[number];
|
|
98
|
+
export type CowSwapWidgetPaletteParams = {
|
|
99
|
+
[K in CowSwapWidgetPaletteColors]: string;
|
|
100
|
+
};
|
|
101
|
+
export type CowSwapWidgetPalette = {
|
|
97
102
|
baseTheme: CowSwapTheme;
|
|
98
|
-
|
|
99
|
-
background: string;
|
|
100
|
-
paper: string;
|
|
101
|
-
text: string;
|
|
102
|
-
danger: string;
|
|
103
|
-
warning: string;
|
|
104
|
-
alert: string;
|
|
105
|
-
info: string;
|
|
106
|
-
success: string;
|
|
107
|
-
}
|
|
103
|
+
} & CowSwapWidgetPaletteParams;
|
|
108
104
|
export interface CowSwapWidgetSounds {
|
|
109
105
|
/**
|
|
110
106
|
* The sound to play when the order is executed. Defaults to world wide famous CoW Swap moooooooooo!
|
|
@@ -254,12 +250,13 @@ export interface WidgetMethodsListenPayloadMap {
|
|
|
254
250
|
}
|
|
255
251
|
export type WidgetMethodsEmitPayloads = WidgetMethodsEmitPayloadMap[WidgetMethodsEmit];
|
|
256
252
|
export type WidgetMethodsListenPayloads = WidgetMethodsListenPayloadMap[WidgetMethodsListen];
|
|
253
|
+
export type CowSwapWidgetAppParams = Omit<CowSwapWidgetParams, 'theme'>;
|
|
257
254
|
export interface UpdateParamsPayload {
|
|
258
255
|
urlParams: {
|
|
259
256
|
pathname: string;
|
|
260
257
|
search: string;
|
|
261
258
|
};
|
|
262
|
-
appParams:
|
|
259
|
+
appParams: CowSwapWidgetAppParams;
|
|
263
260
|
hasProvider: boolean;
|
|
264
261
|
}
|
|
265
262
|
export interface UpdateAppDataPayload {
|
package/urlUtils.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { CowSwapWidgetParams } from './types';
|
|
2
2
|
export declare function buildWidgetUrl(params: Partial<CowSwapWidgetParams>): string;
|
|
3
3
|
export declare function buildWidgetPath(params: Partial<CowSwapWidgetParams>): string;
|
|
4
|
-
export declare function
|
|
4
|
+
export declare function buildWidgetUrlQuery(params: Partial<CowSwapWidgetParams>): URLSearchParams;
|