@cowprotocol/widget-lib 2.2.4 → 2.3.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 +1 -1
- package/README.md +2 -2
- package/index.cjs +1 -1
- package/index.js +194 -166
- package/logger.d.ts +1 -0
- package/package.json +4 -4
- package/types.d.ts +1 -1
|
@@ -4,7 +4,7 @@ export declare class IframeCowEventEmitter {
|
|
|
4
4
|
private eventEmitter;
|
|
5
5
|
private listeners;
|
|
6
6
|
private widgetListener;
|
|
7
|
-
constructor(contentWindow: Window, listeners?: CowWidgetEventListeners);
|
|
7
|
+
constructor(contentWindow: Window, iframeOrigin: string, listeners?: CowWidgetEventListeners);
|
|
8
8
|
stopListeningIframe(): void;
|
|
9
9
|
updateListeners(listeners?: CowWidgetEventListeners): void;
|
|
10
10
|
}
|
package/README.md
CHANGED
|
@@ -6,11 +6,11 @@ look and much more!
|
|
|
6
6
|
|
|
7
7
|
## Live example
|
|
8
8
|
|
|
9
|
-
See the widget in action in the [widget configurator](https://widget.cow.
|
|
9
|
+
See the widget in action in the [widget configurator](https://widget.cow.finance)
|
|
10
10
|
|
|
11
11
|
## Docs
|
|
12
12
|
|
|
13
|
-
You can find a detailed description of all widget parameters in the [documentation](https://docs.cow.
|
|
13
|
+
You can find a detailed description of all widget parameters in the [documentation](https://docs.cow.finance/cow-protocol/tutorials/widget)
|
|
14
14
|
|
|
15
15
|
## Quick start
|
|
16
16
|
|
package/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const S=require("@cowprotocol/iframe-transport"),
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const S=require("@cowprotocol/iframe-transport"),b=require("@cowprotocol/events");var c=(e=>(e.ACTIVATE="ACTIVATE",e.READY="READY",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.INTERCEPT_WINDOW_OPEN="INTERCEPT_WINDOW_OPEN",e.PROCESS_HOOK="PROCESS_HOOK",e))(c||{}),T=(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.HOOK_RESULT="HOOK_RESULT",e))(T||{}),E=(e=>(e.ON_BEFORE_APPROVAL="ON_BEFORE_APPROVAL",e.ON_BEFORE_TRADE="ON_BEFORE_TRADE",e.ON_BEFORE_WRAP_UNWRAP="ON_BEFORE_WRAP_UNWRAP",e.ON_BEFORE_ORDER_CANCEL="ON_BEFORE_ORDER_CANCEL",e.ON_BEFORE_ORDERS_CANCEL="ON_BEFORE_ORDERS_CANCEL",e))(E||{}),P=(e=>(e.SWAP="swap",e.LIMIT="limit",e.ADVANCED="advanced",e.YIELD="yield",e))(P||{});const U=["primary","background","paper","text","danger","warning","alert","info","success"],s=new S.IframeTransport("cowSwapWidget");class y{constructor(t,n,o=[]){this.contentWindow=t,this.updateListeners(o),this.widgetListener=s.listenToMessageFromWindow(this.contentWindow,c.EMIT_COW_EVENT,r=>this.eventEmitter.emit(r.event,r.payload),n)}eventEmitter=new b.SimpleCowEventEmitter;listeners=[];widgetListener;stopListeningIframe(){s.stopListeningWindowListener(this.contentWindow,this.widgetListener)}updateListeners(t){for(const n of this.listeners)this.eventEmitter.off(n);this.listeners=t||[];for(const n of this.listeners)this.eventEmitter.on(n)}}class H{constructor(t,n){this.appWindow=t,this.iframeWidow=n,this.forwardSdkMessage=o=>{F(o.data)&&(typeof window<"u"&&o.origin===window.location.origin||(B(o.data)?this.appWindow.parent.postMessage(o.data,"*"):M(o.data)&&this.iframeWidow.postMessage(o.data,"*")))},this.startListening()}forwardSdkMessage;startListening(){this.appWindow.addEventListener("message",this.forwardSdkMessage)}stopListening(){this.appWindow.removeEventListener("message",this.forwardSdkMessage)}}function F(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 M(e){return"success"in e&&typeof e.success=="boolean"&&"version"in e&&typeof e.version=="string"}function V(e="Default",t="#1c5dbf",...n){console.debug(`%c [COW][${e}]`,`font-weight: bold; color: ${t}`,...n)}function k(...e){V("Widget","#47aba8",...e)}function v(e){return!!(e&&typeof e=="object")}const C="_";function j(e){const t=typeof e.baseUrl=="string"?e.baseUrl:"https://swap.cow.finance",n=I(e);return t+"/#"+n+"?"+W(e)}function I(e){const{chainId:t=1,sell:n,buy:o,tradeType:r=P.SWAP}=e,a=[n?.asset||C,o?.asset||C].map(encodeURIComponent).join("/");return`/${t}/widget/${r}/${a}`}function W(e){const t=new URLSearchParams;return K($(Q(x(G(t,e),e),e),e),e)}function G(e,t){const{sell:n,buy:o}=t;return n?.amount&&e.append("sellAmount",n.amount),o?.amount&&e.append("buyAmount",o.amount),e}function x(e,t){const n=t.theme;return n?(v(n)?(e.append("palette",encodeURIComponent(JSON.stringify(n))),e.append("theme",n.baseTheme)):(e.append("palette","null"),e.append("theme",n)),e):(e.append("palette","null"),e)}function Q(e,t){return t.targetChainId&&e.append("targetChainId",t.targetChainId.toString()),e}function $(e,t){return t.locale&&e.append("lng",t.locale),e}function K(e,t){return Y(t.hooks)?e.append("hooksEnabled","true"):e.delete("hooksEnabled"),e}function Y(e){return!!e&&Object.values(e).some(Boolean)}const N="640px",J="450px",X=20,z={updateParams:()=>{},updateListeners:()=>{},updateProvider:()=>{},destroy:()=>{}};function Z(e,t){const{params:n,provider:o,listeners:r,onReady:a}=t;let f=o,w=n;if(typeof window>"u")return z;const d=q(n),i=ee(d);k("Resolved trusted iframe origin",{iframeOrigin:i});const p=[];a&&p.push(ne(d.contentWindow||window,i,a)),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!");p.push(te(u,i,n.appCode)),p.push(...se(d,i,n.height,n.maxHeight)),p.push(oe(i));let R=null;function A(){u&&(R&&window.removeEventListener("message",R),R=ue(u,i,w.hooks),p.push(R))}A();const g=new y(window,i,r);let h=m(u,i,null,f);d.addEventListener("load",()=>{L(u,i,w,f)});const D=new H(window,u);return{updateParams:l=>{w=l,L(u,i,w,f),A()},updateListeners:l=>g.updateListeners(l),updateProvider:l=>{f=l,h=m(u,i,h,l)},destroy:()=>{h.disconnect(),g.stopListeningIframe(),p.forEach(l=>window.removeEventListener("message",l)),D.stopListening(),e.removeChild(d)}}}function m(e,t,n,o){n&&n.disconnect();const r=n||new S.IframeRpcProviderBridge(e,t);return o&&r.onConnect(o),r}function q(e){const{width:t=J,height:n=N}=e,o=document.createElement("iframe");return o.src=j(e),o.width=t,o.height=n,o.style.border="0",o.allow="clipboard-read; clipboard-write",o}function ee(e){return new URL(e.src).origin}function L(e,t,n,o){const r=!!o,a=I(n),f=W(n).toString(),{theme:w,hooks:d,...i}=n;s.postMessageToWindow(e,T.UPDATE_PARAMS,{urlParams:{pathname:a,search:f},appParams:i,hasProvider:r},t)}function te(e,t,n){return s.listenToMessageFromWindow(window,c.ACTIVATE,()=>{s.postMessageToWindow(e,T.UPDATE_APP_DATA,{metaData:n?{appCode:n}:void 0},t)},t)}function ne(e,t,n){let o=!1;return s.listenToMessageFromWindow(window,c.READY,()=>{o||(o=!0,n())},t)}function oe(e){return s.listenToMessageFromWindow(window,c.INTERCEPT_WINDOW_OPEN,({href:t,rel:n,target:o})=>{const r=re(t.toString(),e);r&&ie(r)&&window.open(r,o,n)},e)}function re(e,t){const n=e.trim();if(!n)return null;try{return new URL(n,t).toString()}catch{return null}}function ie(e){try{const t=new URL(e).protocol;return t==="http:"||t==="https:"}catch{return!1}}function se(e,t,n=N,o){return[s.listenToMessageFromWindow(window,c.UPDATE_HEIGHT,r=>{const a=r.height?r.height+X:void 0;e.style.height=a?`${o?Math.min(a,o):a}px`:n},t),s.listenToMessageFromWindow(window,c.SET_FULL_HEIGHT,({isUpToSmall:r})=>{e.style.height=r?n:`${o||document.body.offsetHeight}px`},t)]}const ae={[E.ON_BEFORE_APPROVAL]:(e,t)=>t?.onBeforeApproval?t.onBeforeApproval(e):!0,[E.ON_BEFORE_TRADE]:(e,t)=>t?.onBeforeTrade?t.onBeforeTrade(e):!0,[E.ON_BEFORE_WRAP_UNWRAP]:(e,t)=>t?.onBeforeWrapOrUnwrap?t.onBeforeWrapOrUnwrap(e):!0,[E.ON_BEFORE_ORDER_CANCEL]:(e,t)=>t?.onBeforeOrderCancel?t.onBeforeOrderCancel(e):!0,[E.ON_BEFORE_ORDERS_CANCEL]:(e,t)=>t?.onBeforeOrdersCancel?t.onBeforeOrdersCancel(e):!0};function de(e,t){return ae[e.event](e.payload,t)}function ue(e,t,n){return s.listenToMessageFromWindow(window,c.PROCESS_HOOK,async o=>{let r=!1;try{r=await de(o,n)}catch{}s.postMessageToWindow(e,T.HOOK_RESULT,{id:o.id,result:r},t)},t)}const ce=Object.values(P);function le(e,t,n){if(_(e)){const o=e[n];return O(o)?o[t]:o}if(O(e)){const o=e[t];return _(o)?o[n]:o}return e}function _(e){return typeof e!="object"?!1:Object.keys(e).every(t=>ce.includes(t))}const Ee=/^\d+$/;function O(e){return typeof e!="object"?!1:Object.keys(e).every(t=>Ee.test(t))}function fe(e){return _(e)?Object.values(e).flatMap(t=>O(t)?Object.values(t):[t]):O(e)?Object.values(e).flatMap(t=>_(t)?Object.values(t):[t]):[e]}exports.TradeType=P;exports.WIDGET_PALETTE_COLORS=U;exports.WidgetHookEvents=E;exports.WidgetMethodsEmit=c;exports.WidgetMethodsListen=T;exports.createCowSwapWidget=Z;exports.isCowSwapWidgetPalette=v;exports.isPerNetworkConfig=O;exports.isPerTradeTypeConfig=_;exports.resolveFlexibleConfig=le;exports.resolveFlexibleConfigValues=fe;exports.widgetIframeTransport=s;
|
package/index.js
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
import { IframeTransport as D, IframeRpcProviderBridge as
|
|
2
|
-
import { SimpleCowEventEmitter as
|
|
3
|
-
var
|
|
4
|
-
const
|
|
5
|
-
class
|
|
6
|
-
constructor(t,
|
|
7
|
-
this.contentWindow =
|
|
1
|
+
import { IframeTransport as D, IframeRpcProviderBridge as W } from "@cowprotocol/iframe-transport";
|
|
2
|
+
import { SimpleCowEventEmitter as U } from "@cowprotocol/events";
|
|
3
|
+
var l = /* @__PURE__ */ ((e) => (e.ACTIVATE = "ACTIVATE", e.READY = "READY", 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.INTERCEPT_WINDOW_OPEN = "INTERCEPT_WINDOW_OPEN", e.PROCESS_HOOK = "PROCESS_HOOK", e))(l || {}), T = /* @__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.HOOK_RESULT = "HOOK_RESULT", e))(T || {}), p = /* @__PURE__ */ ((e) => (e.ON_BEFORE_APPROVAL = "ON_BEFORE_APPROVAL", e.ON_BEFORE_TRADE = "ON_BEFORE_TRADE", e.ON_BEFORE_WRAP_UNWRAP = "ON_BEFORE_WRAP_UNWRAP", e.ON_BEFORE_ORDER_CANCEL = "ON_BEFORE_ORDER_CANCEL", e.ON_BEFORE_ORDERS_CANCEL = "ON_BEFORE_ORDERS_CANCEL", e))(p || {}), A = /* @__PURE__ */ ((e) => (e.SWAP = "swap", e.LIMIT = "limit", e.ADVANCED = "advanced", e.YIELD = "yield", e))(A || {});
|
|
4
|
+
const fe = ["primary", "background", "paper", "text", "danger", "warning", "alert", "info", "success"], s = new D("cowSwapWidget");
|
|
5
|
+
class b {
|
|
6
|
+
constructor(n, t, o = []) {
|
|
7
|
+
this.contentWindow = n, this.updateListeners(o), this.widgetListener = s.listenToMessageFromWindow(this.contentWindow, l.EMIT_COW_EVENT, (r) => this.eventEmitter.emit(r.event, r.payload), t);
|
|
8
8
|
}
|
|
9
|
-
eventEmitter = new
|
|
9
|
+
eventEmitter = new U();
|
|
10
10
|
listeners = [];
|
|
11
11
|
widgetListener;
|
|
12
12
|
stopListeningIframe() {
|
|
13
|
-
|
|
13
|
+
s.stopListeningWindowListener(this.contentWindow, this.widgetListener);
|
|
14
14
|
}
|
|
15
|
-
updateListeners(
|
|
16
|
-
for (const
|
|
17
|
-
this.eventEmitter.off(
|
|
18
|
-
this.listeners =
|
|
19
|
-
for (const
|
|
20
|
-
this.eventEmitter.on(
|
|
15
|
+
updateListeners(n) {
|
|
16
|
+
for (const t of this.listeners)
|
|
17
|
+
this.eventEmitter.off(t);
|
|
18
|
+
this.listeners = n || [];
|
|
19
|
+
for (const t of this.listeners)
|
|
20
|
+
this.eventEmitter.on(t);
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
|
-
class
|
|
24
|
-
constructor(
|
|
25
|
-
this.appWindow =
|
|
26
|
-
|
|
23
|
+
class H {
|
|
24
|
+
constructor(n, t) {
|
|
25
|
+
this.appWindow = n, this.iframeWidow = t, this.forwardSdkMessage = (o) => {
|
|
26
|
+
y(o.data) && (typeof window < "u" && o.origin === window.location.origin || (B(o.data) ? this.appWindow.parent.postMessage(o.data, "*") : F(o.data) && this.iframeWidow.postMessage(o.data, "*")));
|
|
27
27
|
}, this.startListening();
|
|
28
28
|
}
|
|
29
29
|
forwardSdkMessage;
|
|
@@ -34,7 +34,7 @@ class U {
|
|
|
34
34
|
this.appWindow.removeEventListener("message", this.forwardSdkMessage);
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
|
-
function
|
|
37
|
+
function y(e) {
|
|
38
38
|
return typeof e == "object" && e !== null && "id" in e && typeof e.id == "string";
|
|
39
39
|
}
|
|
40
40
|
function B(e) {
|
|
@@ -43,51 +43,57 @@ function B(e) {
|
|
|
43
43
|
function F(e) {
|
|
44
44
|
return "success" in e && typeof e.success == "boolean" && "version" in e && typeof e.version == "string";
|
|
45
45
|
}
|
|
46
|
-
function
|
|
46
|
+
function M(e = "Default", n = "#1c5dbf", ...t) {
|
|
47
|
+
console.debug(`%c [COW][${e}]`, `font-weight: bold; color: ${n}`, ...t);
|
|
48
|
+
}
|
|
49
|
+
function V(...e) {
|
|
50
|
+
M("Widget", "#47aba8", ...e);
|
|
51
|
+
}
|
|
52
|
+
function k(e) {
|
|
47
53
|
return !!(e && typeof e == "object");
|
|
48
54
|
}
|
|
49
|
-
const
|
|
50
|
-
function
|
|
51
|
-
const
|
|
52
|
-
return
|
|
55
|
+
const C = "_";
|
|
56
|
+
function j(e) {
|
|
57
|
+
const n = typeof e.baseUrl == "string" ? e.baseUrl : "https://swap.cow.finance", t = S(e);
|
|
58
|
+
return n + "/#" + t + "?" + I(e);
|
|
53
59
|
}
|
|
54
60
|
function S(e) {
|
|
55
61
|
const {
|
|
56
|
-
chainId:
|
|
57
|
-
sell:
|
|
62
|
+
chainId: n = 1,
|
|
63
|
+
sell: t,
|
|
58
64
|
buy: o,
|
|
59
|
-
tradeType:
|
|
60
|
-
} = e,
|
|
61
|
-
return `/${
|
|
65
|
+
tradeType: r = A.SWAP
|
|
66
|
+
} = e, a = [t?.asset || C, o?.asset || C].map(encodeURIComponent).join("/");
|
|
67
|
+
return `/${n}/widget/${r}/${a}`;
|
|
62
68
|
}
|
|
63
69
|
function I(e) {
|
|
64
|
-
const
|
|
65
|
-
return
|
|
70
|
+
const n = new URLSearchParams();
|
|
71
|
+
return K($(Q(x(G(n, e), e), e), e), e);
|
|
66
72
|
}
|
|
67
|
-
function
|
|
73
|
+
function G(e, n) {
|
|
68
74
|
const {
|
|
69
|
-
sell:
|
|
75
|
+
sell: t,
|
|
70
76
|
buy: o
|
|
71
|
-
} =
|
|
72
|
-
return
|
|
77
|
+
} = n;
|
|
78
|
+
return t?.amount && e.append("sellAmount", t.amount), o?.amount && e.append("buyAmount", o.amount), e;
|
|
73
79
|
}
|
|
74
|
-
function
|
|
75
|
-
const
|
|
76
|
-
return
|
|
80
|
+
function x(e, n) {
|
|
81
|
+
const t = n.theme;
|
|
82
|
+
return t ? (k(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);
|
|
77
83
|
}
|
|
78
|
-
function
|
|
79
|
-
return
|
|
84
|
+
function Q(e, n) {
|
|
85
|
+
return n.targetChainId && e.append("targetChainId", n.targetChainId.toString()), e;
|
|
80
86
|
}
|
|
81
|
-
function
|
|
82
|
-
return
|
|
87
|
+
function $(e, n) {
|
|
88
|
+
return n.locale && e.append("lng", n.locale), e;
|
|
83
89
|
}
|
|
84
|
-
function
|
|
85
|
-
return
|
|
90
|
+
function K(e, n) {
|
|
91
|
+
return Y(n.hooks) ? e.append("hooksEnabled", "true") : e.delete("hooksEnabled"), e;
|
|
86
92
|
}
|
|
87
|
-
function
|
|
93
|
+
function Y(e) {
|
|
88
94
|
return !!e && Object.values(e).some(Boolean);
|
|
89
95
|
}
|
|
90
|
-
const
|
|
96
|
+
const N = "640px", J = "450px", X = 20, z = {
|
|
91
97
|
updateParams: () => {
|
|
92
98
|
},
|
|
93
99
|
updateListeners: () => {
|
|
@@ -97,168 +103,190 @@ const g = "640px", K = "450px", $ = 20, Y = {
|
|
|
97
103
|
destroy: () => {
|
|
98
104
|
}
|
|
99
105
|
};
|
|
100
|
-
function
|
|
106
|
+
function pe(e, n) {
|
|
101
107
|
const {
|
|
102
|
-
params:
|
|
108
|
+
params: t,
|
|
103
109
|
provider: o,
|
|
104
|
-
listeners:
|
|
105
|
-
onReady:
|
|
106
|
-
} =
|
|
107
|
-
let
|
|
108
|
-
if (typeof window > "u") return
|
|
109
|
-
const
|
|
110
|
-
|
|
110
|
+
listeners: r,
|
|
111
|
+
onReady: a
|
|
112
|
+
} = n;
|
|
113
|
+
let E = o, w = t;
|
|
114
|
+
if (typeof window > "u") return z;
|
|
115
|
+
const d = Z(t), i = q(d);
|
|
116
|
+
V("Resolved trusted iframe origin", {
|
|
117
|
+
iframeOrigin: i
|
|
118
|
+
});
|
|
119
|
+
const f = [];
|
|
120
|
+
a && f.push(ne(d.contentWindow || window, i, a)), e.innerHTML = "", e.appendChild(d);
|
|
111
121
|
const {
|
|
112
|
-
contentWindow:
|
|
113
|
-
} =
|
|
114
|
-
if (!
|
|
115
|
-
throw console.error("Iframe does not contain a window",
|
|
116
|
-
|
|
117
|
-
let
|
|
118
|
-
function
|
|
119
|
-
|
|
122
|
+
contentWindow: u
|
|
123
|
+
} = d;
|
|
124
|
+
if (!u)
|
|
125
|
+
throw console.error("Iframe does not contain a window", d), new Error("Iframe does not contain a window!");
|
|
126
|
+
f.push(ee(u, i, t.appCode)), f.push(...ie(d, i, t.height, t.maxHeight)), f.push(te(i));
|
|
127
|
+
let _ = null;
|
|
128
|
+
function h() {
|
|
129
|
+
u && (_ && window.removeEventListener("message", _), _ = de(u, i, w.hooks), f.push(_));
|
|
120
130
|
}
|
|
121
|
-
|
|
122
|
-
const
|
|
123
|
-
let
|
|
124
|
-
|
|
125
|
-
L(
|
|
131
|
+
h();
|
|
132
|
+
const m = new b(window, i, r);
|
|
133
|
+
let P = g(u, i, null, E);
|
|
134
|
+
d.addEventListener("load", () => {
|
|
135
|
+
L(u, i, w, E);
|
|
126
136
|
});
|
|
127
|
-
const
|
|
137
|
+
const v = new H(window, u);
|
|
128
138
|
return {
|
|
129
|
-
updateParams: (
|
|
130
|
-
|
|
139
|
+
updateParams: (c) => {
|
|
140
|
+
w = c, L(u, i, w, E), h();
|
|
131
141
|
},
|
|
132
|
-
updateListeners: (
|
|
133
|
-
updateProvider: (
|
|
134
|
-
|
|
142
|
+
updateListeners: (c) => m.updateListeners(c),
|
|
143
|
+
updateProvider: (c) => {
|
|
144
|
+
E = c, P = g(u, i, P, c);
|
|
135
145
|
},
|
|
136
146
|
destroy: () => {
|
|
137
|
-
|
|
147
|
+
P.disconnect(), m.stopListeningIframe(), f.forEach((c) => window.removeEventListener("message", c)), v.stopListening(), e.removeChild(d);
|
|
138
148
|
}
|
|
139
149
|
};
|
|
140
150
|
}
|
|
141
|
-
function
|
|
151
|
+
function g(e, n, t, o) {
|
|
142
152
|
t && t.disconnect();
|
|
143
|
-
const
|
|
144
|
-
return
|
|
153
|
+
const r = t || new W(e, n);
|
|
154
|
+
return o && r.onConnect(o), r;
|
|
145
155
|
}
|
|
146
|
-
function
|
|
156
|
+
function Z(e) {
|
|
147
157
|
const {
|
|
148
|
-
width:
|
|
149
|
-
height:
|
|
158
|
+
width: n = J,
|
|
159
|
+
height: t = N
|
|
150
160
|
} = e, o = document.createElement("iframe");
|
|
151
|
-
return o.src =
|
|
161
|
+
return o.src = j(e), o.width = n, o.height = t, o.style.border = "0", o.allow = "clipboard-read; clipboard-write", o;
|
|
162
|
+
}
|
|
163
|
+
function q(e) {
|
|
164
|
+
return new URL(e.src).origin;
|
|
152
165
|
}
|
|
153
|
-
function L(e, t,
|
|
154
|
-
const
|
|
155
|
-
theme:
|
|
156
|
-
hooks:
|
|
157
|
-
...
|
|
166
|
+
function L(e, n, t, o) {
|
|
167
|
+
const r = !!o, a = S(t), E = I(t).toString(), {
|
|
168
|
+
theme: w,
|
|
169
|
+
hooks: d,
|
|
170
|
+
...i
|
|
158
171
|
} = t;
|
|
159
|
-
|
|
172
|
+
s.postMessageToWindow(e, T.UPDATE_PARAMS, {
|
|
160
173
|
urlParams: {
|
|
161
|
-
pathname:
|
|
162
|
-
search:
|
|
174
|
+
pathname: a,
|
|
175
|
+
search: E
|
|
163
176
|
},
|
|
164
|
-
appParams:
|
|
165
|
-
hasProvider:
|
|
166
|
-
});
|
|
177
|
+
appParams: i,
|
|
178
|
+
hasProvider: r
|
|
179
|
+
}, n);
|
|
167
180
|
}
|
|
168
|
-
function
|
|
169
|
-
return
|
|
170
|
-
|
|
181
|
+
function ee(e, n, t) {
|
|
182
|
+
return s.listenToMessageFromWindow(window, l.ACTIVATE, () => {
|
|
183
|
+
s.postMessageToWindow(e, T.UPDATE_APP_DATA, {
|
|
171
184
|
metaData: t ? {
|
|
172
185
|
appCode: t
|
|
173
186
|
} : void 0
|
|
174
|
-
});
|
|
175
|
-
});
|
|
187
|
+
}, n);
|
|
188
|
+
}, n);
|
|
176
189
|
}
|
|
177
|
-
function
|
|
178
|
-
let
|
|
179
|
-
return
|
|
180
|
-
|
|
181
|
-
});
|
|
190
|
+
function ne(e, n, t) {
|
|
191
|
+
let o = !1;
|
|
192
|
+
return s.listenToMessageFromWindow(window, l.READY, () => {
|
|
193
|
+
o || (o = !0, t());
|
|
194
|
+
}, n);
|
|
182
195
|
}
|
|
183
|
-
function
|
|
184
|
-
return
|
|
185
|
-
href:
|
|
196
|
+
function te(e) {
|
|
197
|
+
return s.listenToMessageFromWindow(window, l.INTERCEPT_WINDOW_OPEN, ({
|
|
198
|
+
href: n,
|
|
186
199
|
rel: t,
|
|
187
|
-
target:
|
|
200
|
+
target: o
|
|
188
201
|
}) => {
|
|
189
|
-
const
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
return;
|
|
193
|
-
}
|
|
194
|
-
});
|
|
202
|
+
const r = oe(n.toString(), e);
|
|
203
|
+
r && re(r) && window.open(r, o, t);
|
|
204
|
+
}, e);
|
|
195
205
|
}
|
|
196
|
-
function
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
206
|
+
function oe(e, n) {
|
|
207
|
+
const t = e.trim();
|
|
208
|
+
if (!t)
|
|
209
|
+
return null;
|
|
210
|
+
try {
|
|
211
|
+
return new URL(t, n).toString();
|
|
212
|
+
} catch {
|
|
213
|
+
return null;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
function re(e) {
|
|
217
|
+
try {
|
|
218
|
+
const n = new URL(e).protocol;
|
|
219
|
+
return n === "http:" || n === "https:";
|
|
220
|
+
} catch {
|
|
221
|
+
return !1;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
function ie(e, n, t = N, o) {
|
|
225
|
+
return [s.listenToMessageFromWindow(window, l.UPDATE_HEIGHT, (r) => {
|
|
226
|
+
const a = r.height ? r.height + X : void 0;
|
|
227
|
+
e.style.height = a ? `${o ? Math.min(a, o) : a}px` : t;
|
|
228
|
+
}, n), s.listenToMessageFromWindow(window, l.SET_FULL_HEIGHT, ({
|
|
229
|
+
isUpToSmall: r
|
|
202
230
|
}) => {
|
|
203
|
-
e.style.height =
|
|
204
|
-
})];
|
|
205
|
-
}
|
|
206
|
-
const
|
|
207
|
-
[
|
|
208
|
-
[
|
|
209
|
-
[
|
|
210
|
-
[
|
|
211
|
-
[
|
|
231
|
+
e.style.height = r ? t : `${o || document.body.offsetHeight}px`;
|
|
232
|
+
}, n)];
|
|
233
|
+
}
|
|
234
|
+
const se = {
|
|
235
|
+
[p.ON_BEFORE_APPROVAL]: (e, n) => n?.onBeforeApproval ? n.onBeforeApproval(e) : !0,
|
|
236
|
+
[p.ON_BEFORE_TRADE]: (e, n) => n?.onBeforeTrade ? n.onBeforeTrade(e) : !0,
|
|
237
|
+
[p.ON_BEFORE_WRAP_UNWRAP]: (e, n) => n?.onBeforeWrapOrUnwrap ? n.onBeforeWrapOrUnwrap(e) : !0,
|
|
238
|
+
[p.ON_BEFORE_ORDER_CANCEL]: (e, n) => n?.onBeforeOrderCancel ? n.onBeforeOrderCancel(e) : !0,
|
|
239
|
+
[p.ON_BEFORE_ORDERS_CANCEL]: (e, n) => n?.onBeforeOrdersCancel ? n.onBeforeOrdersCancel(e) : !0
|
|
212
240
|
};
|
|
213
|
-
function
|
|
214
|
-
return
|
|
241
|
+
function ae(e, n) {
|
|
242
|
+
return se[e.event](e.payload, n);
|
|
215
243
|
}
|
|
216
|
-
function
|
|
217
|
-
return
|
|
218
|
-
let
|
|
244
|
+
function de(e, n, t) {
|
|
245
|
+
return s.listenToMessageFromWindow(window, l.PROCESS_HOOK, async (o) => {
|
|
246
|
+
let r = !1;
|
|
219
247
|
try {
|
|
220
|
-
|
|
248
|
+
r = await ae(o, t);
|
|
221
249
|
} catch {
|
|
222
250
|
}
|
|
223
|
-
|
|
224
|
-
id:
|
|
225
|
-
result:
|
|
226
|
-
});
|
|
227
|
-
});
|
|
251
|
+
s.postMessageToWindow(e, T.HOOK_RESULT, {
|
|
252
|
+
id: o.id,
|
|
253
|
+
result: r
|
|
254
|
+
}, n);
|
|
255
|
+
}, n);
|
|
228
256
|
}
|
|
229
|
-
const
|
|
230
|
-
function
|
|
231
|
-
if (_(e)) {
|
|
232
|
-
const o = e[n];
|
|
233
|
-
return O(o) ? o[t] : o;
|
|
234
|
-
}
|
|
257
|
+
const ue = Object.values(A);
|
|
258
|
+
function we(e, n, t) {
|
|
235
259
|
if (O(e)) {
|
|
236
260
|
const o = e[t];
|
|
237
|
-
return
|
|
261
|
+
return R(o) ? o[n] : o;
|
|
262
|
+
}
|
|
263
|
+
if (R(e)) {
|
|
264
|
+
const o = e[n];
|
|
265
|
+
return O(o) ? o[t] : o;
|
|
238
266
|
}
|
|
239
267
|
return e;
|
|
240
268
|
}
|
|
241
|
-
function _(e) {
|
|
242
|
-
return typeof e != "object" ? !1 : Object.keys(e).every((t) => oe.includes(t));
|
|
243
|
-
}
|
|
244
|
-
const re = /^\d+$/;
|
|
245
269
|
function O(e) {
|
|
246
|
-
return typeof e != "object" ? !1 : Object.keys(e).every((
|
|
270
|
+
return typeof e != "object" ? !1 : Object.keys(e).every((n) => ue.includes(n));
|
|
271
|
+
}
|
|
272
|
+
const ce = /^\d+$/;
|
|
273
|
+
function R(e) {
|
|
274
|
+
return typeof e != "object" ? !1 : Object.keys(e).every((n) => ce.test(n));
|
|
247
275
|
}
|
|
248
|
-
function
|
|
249
|
-
return
|
|
276
|
+
function _e(e) {
|
|
277
|
+
return O(e) ? Object.values(e).flatMap((n) => R(n) ? Object.values(n) : [n]) : R(e) ? Object.values(e).flatMap((n) => O(n) ? Object.values(n) : [n]) : [e];
|
|
250
278
|
}
|
|
251
279
|
export {
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
280
|
+
A as TradeType,
|
|
281
|
+
fe as WIDGET_PALETTE_COLORS,
|
|
282
|
+
p as WidgetHookEvents,
|
|
283
|
+
l as WidgetMethodsEmit,
|
|
284
|
+
T as WidgetMethodsListen,
|
|
285
|
+
pe as createCowSwapWidget,
|
|
286
|
+
k as isCowSwapWidgetPalette,
|
|
287
|
+
R as isPerNetworkConfig,
|
|
288
|
+
O as isPerTradeTypeConfig,
|
|
289
|
+
we as resolveFlexibleConfig,
|
|
290
|
+
_e as resolveFlexibleConfigValues,
|
|
291
|
+
s as widgetIframeTransport
|
|
264
292
|
};
|
package/logger.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function logWidget(...args: unknown[]): void;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cowprotocol/widget-lib",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.0",
|
|
4
4
|
"description": "CoW Swap Widget Library. Allows you to easily embed a CoW Swap widget on your website.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -15,9 +15,9 @@
|
|
|
15
15
|
"access": "public"
|
|
16
16
|
},
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@cowprotocol/cow-sdk": "8.0
|
|
19
|
-
"@cowprotocol/events": "^4.
|
|
20
|
-
"@cowprotocol/iframe-transport": "^2.0
|
|
18
|
+
"@cowprotocol/cow-sdk": "8.1.0",
|
|
19
|
+
"@cowprotocol/events": "^4.2.0",
|
|
20
|
+
"@cowprotocol/iframe-transport": "^2.1.0"
|
|
21
21
|
},
|
|
22
22
|
"exports": {
|
|
23
23
|
".": {
|
package/types.d.ts
CHANGED
|
@@ -227,7 +227,7 @@ export interface CowSwapWidgetParams {
|
|
|
227
227
|
/**
|
|
228
228
|
* The base url of the widget implementation
|
|
229
229
|
* The parameter can have the URL directly, or an object with the environment property,
|
|
230
|
-
* The base URL will default to the production environment if not specified, so it will use https://swap.cow.
|
|
230
|
+
* The base URL will default to the production environment if not specified, so it will use https://swap.cow.finance by default.
|
|
231
231
|
*/
|
|
232
232
|
baseUrl?: string;
|
|
233
233
|
/**
|