@cowprotocol/widget-lib 0.7.1 → 0.9.0-RC.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/IframeCowEventEmitter.d.ts +0 -1
- package/IframeSafeSdkBridge.d.ts +8 -0
- package/index.d.ts +1 -0
- package/index.iife.js +1 -1
- package/index.js +1 -1
- package/index.mjs +149 -103
- package/package.json +2 -2
- package/themeUtils.d.ts +2 -0
- package/types.d.ts +66 -39
- package/urlUtils.d.ts +1 -1
|
@@ -4,7 +4,6 @@ export declare class IframeCowEventEmitter {
|
|
|
4
4
|
private eventEmitter;
|
|
5
5
|
private listeners;
|
|
6
6
|
private widgetListener;
|
|
7
|
-
private instanceNumber;
|
|
8
7
|
constructor(contentWindow: Window, listeners?: CowEventListeners);
|
|
9
8
|
stopListeningIframe(): void;
|
|
10
9
|
updateListeners(listeners?: CowEventListeners): void;
|
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 _="cowSwapWidget";function u(
|
|
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,61 +1,71 @@
|
|
|
1
1
|
const T = "cowSwapWidget";
|
|
2
|
-
function u(
|
|
2
|
+
function u(e, n, t) {
|
|
3
3
|
const i = {
|
|
4
4
|
key: T,
|
|
5
5
|
method: n,
|
|
6
|
-
...typeof
|
|
6
|
+
...typeof t == "object" ? t : {}
|
|
7
7
|
};
|
|
8
|
-
|
|
8
|
+
e.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
|
|
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
|
-
return
|
|
18
|
+
return e.addEventListener("message", s), s;
|
|
19
19
|
}
|
|
20
|
-
function
|
|
21
|
-
return typeof
|
|
20
|
+
function W(e) {
|
|
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
|
|
24
|
-
|
|
23
|
+
function S(e, n, t) {
|
|
24
|
+
e.removeEventListener("message", t);
|
|
25
25
|
}
|
|
26
|
-
function
|
|
27
|
-
|
|
26
|
+
function _(e, n) {
|
|
27
|
+
e.removeEventListener("message", n);
|
|
28
28
|
}
|
|
29
|
-
var h = /* @__PURE__ */ ((
|
|
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.
|
|
35
45
|
* Also it will receive some special RPC events coming from the wallet, like connect/chainChanged,accountChanged
|
|
36
46
|
*/
|
|
37
47
|
constructor(n) {
|
|
38
|
-
this.iframeWidow = n, this.ethereumProvider = null, this.requestWaitingForConnection = {}, this.processRpcCallFromWindow = ({ rpcRequest:
|
|
48
|
+
this.iframeWidow = n, this.ethereumProvider = null, this.requestWaitingForConnection = {}, this.processRpcCallFromWindow = ({ rpcRequest: t }) => {
|
|
39
49
|
if (!this.ethereumProvider) {
|
|
40
|
-
|
|
50
|
+
t.id && (this.requestWaitingForConnection[t.id] = t);
|
|
41
51
|
return;
|
|
42
52
|
}
|
|
43
|
-
this.processRpcRequest(
|
|
53
|
+
this.processRpcRequest(t);
|
|
44
54
|
};
|
|
45
55
|
}
|
|
46
56
|
/**
|
|
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() :
|
|
58
|
-
n.on(
|
|
67
|
+
this.ethereumProvider ? this.disconnect() : f(window, h.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow), this.ethereumProvider = n, this.processPendingRequests(), D.forEach((t) => {
|
|
68
|
+
n.on(t, (s) => this.onProviderEvent(t, s));
|
|
59
69
|
});
|
|
60
70
|
}
|
|
61
71
|
processPendingRequests() {
|
|
@@ -68,147 +78,183 @@ class W {
|
|
|
68
78
|
* @param request - The JSON-RPC request to be processed.
|
|
69
79
|
*/
|
|
70
80
|
processRpcRequest(n) {
|
|
71
|
-
const { id:
|
|
72
|
-
if (!this.ethereumProvider || !
|
|
81
|
+
const { id: t, jsonrpc: s, method: i } = n;
|
|
82
|
+
if (!this.ethereumProvider || !t)
|
|
73
83
|
return;
|
|
74
|
-
(i === "enable" ? this.ethereumProvider.enable() : this.ethereumProvider.request({ ...n, id:
|
|
75
|
-
(
|
|
76
|
-
rpcResponse: { jsonrpc: s, id:
|
|
84
|
+
(i === "enable" ? this.ethereumProvider.enable() : this.ethereumProvider.request({ ...n, id: t })).then(
|
|
85
|
+
(r) => this.forwardRpcResponseToIframe({
|
|
86
|
+
rpcResponse: { jsonrpc: s, id: t, result: r }
|
|
77
87
|
})
|
|
78
88
|
).catch(
|
|
79
|
-
(
|
|
80
|
-
rpcResponse: { jsonrpc: s, id:
|
|
89
|
+
(r) => this.forwardRpcResponseToIframe({
|
|
90
|
+
rpcResponse: { jsonrpc: s, id: t, error: r }
|
|
81
91
|
})
|
|
82
92
|
);
|
|
83
93
|
}
|
|
84
|
-
onProviderEvent(n,
|
|
85
|
-
u(this.iframeWidow,
|
|
94
|
+
onProviderEvent(n, t) {
|
|
95
|
+
u(this.iframeWidow, p.PROVIDER_ON_EVENT, {
|
|
86
96
|
event: n,
|
|
87
|
-
params:
|
|
97
|
+
params: t
|
|
88
98
|
});
|
|
89
99
|
}
|
|
90
100
|
/**
|
|
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
|
}
|
|
107
|
+
function L(e) {
|
|
108
|
+
return !!(e && typeof e == "object");
|
|
109
|
+
}
|
|
97
110
|
const P = "_";
|
|
98
|
-
function
|
|
99
|
-
const n = typeof
|
|
100
|
-
return n + "/#" +
|
|
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);
|
|
114
|
+
}
|
|
115
|
+
function A(e) {
|
|
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}`;
|
|
101
118
|
}
|
|
102
|
-
function
|
|
103
|
-
const
|
|
104
|
-
return
|
|
119
|
+
function v(e) {
|
|
120
|
+
const n = new URLSearchParams();
|
|
121
|
+
return y(U(n, e), e);
|
|
105
122
|
}
|
|
106
|
-
function
|
|
107
|
-
const { sell:
|
|
108
|
-
return
|
|
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;
|
|
109
126
|
}
|
|
110
|
-
|
|
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
|
}
|
|
114
135
|
on(n) {
|
|
115
|
-
const { event:
|
|
116
|
-
this.subscriptions[
|
|
136
|
+
const { event: t, handler: s } = n;
|
|
137
|
+
this.subscriptions[t] || (this.subscriptions[t] = []), this.subscriptions[t].push(s);
|
|
117
138
|
}
|
|
118
139
|
off(n) {
|
|
119
|
-
const { event:
|
|
120
|
-
this.subscriptions[
|
|
140
|
+
const { event: t, handler: s } = n;
|
|
141
|
+
this.subscriptions[t] && (this.subscriptions[t] = this.subscriptions[t].filter((i) => i !== s));
|
|
121
142
|
}
|
|
122
|
-
emit(n,
|
|
123
|
-
this.subscriptions[n] && this.subscriptions[n].forEach((s) => s(
|
|
143
|
+
emit(n, t) {
|
|
144
|
+
this.subscriptions[n] && this.subscriptions[n].forEach((s) => s(t));
|
|
124
145
|
}
|
|
125
146
|
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
this.contentWindow = n, this.eventEmitter = new S(), this.listeners = [], this.instanceNumber = V++, this.updateListeners(e), this.widgetListener = E(
|
|
147
|
+
class N {
|
|
148
|
+
constructor(n, t = []) {
|
|
149
|
+
this.contentWindow = n, this.eventEmitter = new b(), this.listeners = [], this.updateListeners(t), this.widgetListener = f(
|
|
130
150
|
this.contentWindow,
|
|
131
151
|
h.EMIT_COW_EVENT,
|
|
132
152
|
(s) => this.eventEmitter.emit(s.event, s.payload)
|
|
133
153
|
);
|
|
134
154
|
}
|
|
135
155
|
stopListeningIframe() {
|
|
136
|
-
|
|
156
|
+
_(this.contentWindow, this.widgetListener);
|
|
137
157
|
}
|
|
138
158
|
updateListeners(n) {
|
|
139
|
-
for (const
|
|
140
|
-
this.eventEmitter.off(
|
|
159
|
+
for (const t of this.listeners)
|
|
160
|
+
this.eventEmitter.off(t);
|
|
141
161
|
this.listeners = n || [];
|
|
142
|
-
for (const
|
|
143
|
-
this.eventEmitter.on(
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
+
for (const t of this.listeners)
|
|
163
|
+
this.eventEmitter.on(t);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
class M {
|
|
167
|
+
constructor(n, t) {
|
|
168
|
+
this.appWindow = n, this.iframeWidow = t, this.forwardSdkMessage = (s) => {
|
|
169
|
+
F(s.data) && (H(s.data) ? this.appWindow.parent.postMessage(s.data, "*") : k(s.data) && this.iframeWidow.postMessage(s.data, "*"));
|
|
170
|
+
}, this.startListening();
|
|
171
|
+
}
|
|
172
|
+
startListening() {
|
|
173
|
+
this.appWindow.addEventListener("message", this.forwardSdkMessage);
|
|
174
|
+
}
|
|
175
|
+
stopListening() {
|
|
176
|
+
this.appWindow.removeEventListener("message", this.forwardSdkMessage);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
function F(e) {
|
|
180
|
+
return typeof e == "object" && e !== null && "id" in e && typeof e.id == "string";
|
|
181
|
+
}
|
|
182
|
+
function H(e) {
|
|
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;
|
|
184
|
+
}
|
|
185
|
+
function k(e) {
|
|
186
|
+
return "success" in e && typeof e.success == "boolean" && "version" in e && typeof e.version == "string";
|
|
187
|
+
}
|
|
188
|
+
const I = "640px", G = "450px", Q = 20;
|
|
189
|
+
function K(e, n) {
|
|
190
|
+
const { params: t, provider: s, listeners: i } = n;
|
|
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);
|
|
203
|
+
return {
|
|
204
|
+
updateParams: (c) => {
|
|
205
|
+
r = c, R(d, r, o);
|
|
206
|
+
},
|
|
207
|
+
updateListeners: (c) => w.updateListeners(c),
|
|
162
208
|
updateProvider: (c) => {
|
|
163
|
-
|
|
209
|
+
o = c, m = l(d, m, c);
|
|
164
210
|
},
|
|
165
211
|
destroy: () => {
|
|
166
|
-
m.stopListeningIframe(),
|
|
212
|
+
m.disconnect(), w.stopListeningIframe(), E.forEach((c) => window.removeEventListener("message", c)), C.stopListening(), e.removeChild(a);
|
|
167
213
|
}
|
|
168
214
|
};
|
|
169
215
|
}
|
|
170
|
-
function
|
|
216
|
+
function l(e, n, t) {
|
|
171
217
|
n && n.disconnect();
|
|
172
|
-
const s = n || new
|
|
173
|
-
return
|
|
218
|
+
const s = n || new O(e);
|
|
219
|
+
return t && s.onConnect(t), s;
|
|
174
220
|
}
|
|
175
|
-
function
|
|
176
|
-
const { width: n =
|
|
177
|
-
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;
|
|
178
224
|
}
|
|
179
|
-
function
|
|
180
|
-
const s = !!
|
|
181
|
-
u(
|
|
225
|
+
function R(e, n, t) {
|
|
226
|
+
const s = !!t, i = A(n), o = v(n).toString(), { theme: r, ...a } = n;
|
|
227
|
+
u(e, p.UPDATE_PARAMS, {
|
|
182
228
|
urlParams: {
|
|
183
229
|
pathname: i,
|
|
184
|
-
search:
|
|
185
|
-
},
|
|
186
|
-
appParams: {
|
|
187
|
-
...n
|
|
230
|
+
search: o
|
|
188
231
|
},
|
|
232
|
+
appParams: a,
|
|
189
233
|
hasProvider: s
|
|
190
234
|
});
|
|
191
235
|
}
|
|
192
|
-
function
|
|
193
|
-
const
|
|
194
|
-
|
|
236
|
+
function q(e, n) {
|
|
237
|
+
const t = f(window, h.ACTIVATE, () => {
|
|
238
|
+
_(window, t), u(e, p.UPDATE_APP_DATA, {
|
|
195
239
|
metaData: n ? { appCode: n } : void 0
|
|
196
240
|
});
|
|
197
241
|
});
|
|
198
|
-
return
|
|
242
|
+
return t;
|
|
199
243
|
}
|
|
200
|
-
function
|
|
201
|
-
return
|
|
202
|
-
|
|
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;
|
|
203
247
|
});
|
|
204
248
|
}
|
|
205
249
|
export {
|
|
206
|
-
|
|
250
|
+
g as TradeType,
|
|
251
|
+
$ as WIDGET_PALETTE_COLORS,
|
|
207
252
|
h as WidgetMethodsEmit,
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
253
|
+
p as WidgetMethodsListen,
|
|
254
|
+
K as createCowSwapWidget,
|
|
255
|
+
L as isCowSwapWidgetPalette,
|
|
256
|
+
f as listenToMessageFromWindow,
|
|
211
257
|
u as postMessageToWindow,
|
|
212
|
-
|
|
213
|
-
|
|
258
|
+
S as stopListeningToMessageFromWindow,
|
|
259
|
+
_ as stopListeningWindowListener
|
|
214
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-RC.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": "^
|
|
24
|
+
"@cowprotocol/events": "^1.2.0"
|
|
25
25
|
}
|
|
26
26
|
}
|
package/themeUtils.d.ts
ADDED
package/types.d.ts
CHANGED
|
@@ -82,17 +82,61 @@ export interface PartnerFee {
|
|
|
82
82
|
*/
|
|
83
83
|
recipient: string;
|
|
84
84
|
}
|
|
85
|
-
|
|
85
|
+
/**
|
|
86
|
+
* ERC-20 token information
|
|
87
|
+
*/
|
|
88
|
+
export type TokenInfo = {
|
|
89
|
+
chainId: number;
|
|
90
|
+
address: string;
|
|
91
|
+
name: string;
|
|
92
|
+
decimals: number;
|
|
93
|
+
symbol: string;
|
|
94
|
+
logoURI?: string;
|
|
95
|
+
};
|
|
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 = {
|
|
86
102
|
baseTheme: CowSwapTheme;
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
103
|
+
} & CowSwapWidgetPaletteParams;
|
|
104
|
+
export interface CowSwapWidgetSounds {
|
|
105
|
+
/**
|
|
106
|
+
* The sound to play when the order is executed. Defaults to world wide famous CoW Swap moooooooooo!
|
|
107
|
+
* Alternatively, you can use a URL to a custom sound file, or set to null to disable the sound.
|
|
108
|
+
*/
|
|
109
|
+
postOrder?: string | null;
|
|
110
|
+
/**
|
|
111
|
+
* The sound to play when the order is executed. Defaults to world wide famous CoW Swap happy moooooooooo!
|
|
112
|
+
* Alternatively, you can use a URL to a custom sound file, or set to null to disable the sound.
|
|
113
|
+
*/
|
|
114
|
+
orderExecuted?: string | null;
|
|
115
|
+
/**
|
|
116
|
+
* The sound to play when the order is executed. Defaults to world wide famous CoW Swap unhappy moooooooooo!
|
|
117
|
+
* Alternatively, you can use a URL to a custom sound file, or set to null to disable the sound.
|
|
118
|
+
*/
|
|
119
|
+
orderError?: string | null;
|
|
120
|
+
}
|
|
121
|
+
export interface CowSwapWidgetImages {
|
|
122
|
+
/**
|
|
123
|
+
* The image to display when the orders table is empty (no orders yet). It defaults to "Yoga CoW" image.
|
|
124
|
+
* Alternatively, you can use a URL to a custom image file, or set to null to disable the image.
|
|
125
|
+
*/
|
|
126
|
+
emptyOrders?: string | null;
|
|
127
|
+
}
|
|
128
|
+
export interface CowSwapWidgetBanners {
|
|
129
|
+
/**
|
|
130
|
+
* Banner text: "Use Safe web app..."
|
|
131
|
+
*
|
|
132
|
+
* Conditions for displaying the banner:
|
|
133
|
+
* - Safe-like app is connected to CoW Swap via WalletConnect
|
|
134
|
+
* - Selling native token via Swap
|
|
135
|
+
* - Sell token needs approval
|
|
136
|
+
*
|
|
137
|
+
* If the flag is set to true, the banner will not be displayed
|
|
138
|
+
*/
|
|
139
|
+
hideSafeWebAppBanner?: boolean;
|
|
96
140
|
}
|
|
97
141
|
export interface CowSwapWidgetParams {
|
|
98
142
|
/**
|
|
@@ -175,40 +219,22 @@ export interface CowSwapWidgetParams {
|
|
|
175
219
|
* The theme of the widget UI.
|
|
176
220
|
*/
|
|
177
221
|
theme?: CowSwapTheme | CowSwapWidgetPalette;
|
|
178
|
-
/**
|
|
179
|
-
* Allows to set a custom logo for the widget.
|
|
180
|
-
*/
|
|
181
|
-
logoUrl?: string;
|
|
182
222
|
/**
|
|
183
223
|
* Customizable images for the widget.
|
|
184
224
|
*/
|
|
185
|
-
images?:
|
|
186
|
-
/**
|
|
187
|
-
* The image to display when the orders table is empty (no orders yet). It defaults to "Yoga CoW" image.
|
|
188
|
-
* Alternatively, you can use a URL to a custom image file, or set to null to disable the image.
|
|
189
|
-
*/
|
|
190
|
-
emptyOrders?: string | null;
|
|
191
|
-
};
|
|
225
|
+
images?: CowSwapWidgetImages;
|
|
192
226
|
/**
|
|
193
227
|
* Sounds configuration for the app.
|
|
194
228
|
*/
|
|
195
|
-
sounds?:
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
*/
|
|
205
|
-
orderExecuted?: string | null;
|
|
206
|
-
/**
|
|
207
|
-
* The sound to play when the order is executed. Defaults to world wide famous CoW Swap unhappy moooooooooo!
|
|
208
|
-
* Alternatively, you can use a URL to a custom sound file, or set to null to disable the sound.
|
|
209
|
-
*/
|
|
210
|
-
orderError?: string | null;
|
|
211
|
-
};
|
|
229
|
+
sounds?: CowSwapWidgetSounds;
|
|
230
|
+
/**
|
|
231
|
+
* Flags to control the display of banners in the widget.
|
|
232
|
+
*/
|
|
233
|
+
banners?: CowSwapWidgetBanners;
|
|
234
|
+
/**
|
|
235
|
+
* In case when widget does not support some tokens, you can provide a list of tokens to be used in the widget
|
|
236
|
+
*/
|
|
237
|
+
customTokens?: TokenInfo[];
|
|
212
238
|
}
|
|
213
239
|
export interface WidgetMethodsEmitPayloadMap {
|
|
214
240
|
[WidgetMethodsEmit.ACTIVATE]: void;
|
|
@@ -224,12 +250,13 @@ export interface WidgetMethodsListenPayloadMap {
|
|
|
224
250
|
}
|
|
225
251
|
export type WidgetMethodsEmitPayloads = WidgetMethodsEmitPayloadMap[WidgetMethodsEmit];
|
|
226
252
|
export type WidgetMethodsListenPayloads = WidgetMethodsListenPayloadMap[WidgetMethodsListen];
|
|
253
|
+
export type CowSwapWidgetAppParams = Omit<CowSwapWidgetParams, 'theme'>;
|
|
227
254
|
export interface UpdateParamsPayload {
|
|
228
255
|
urlParams: {
|
|
229
256
|
pathname: string;
|
|
230
257
|
search: string;
|
|
231
258
|
};
|
|
232
|
-
appParams:
|
|
259
|
+
appParams: CowSwapWidgetAppParams;
|
|
233
260
|
hasProvider: boolean;
|
|
234
261
|
}
|
|
235
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;
|