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