@cowprotocol/widget-lib 2.2.5 → 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/README.md +2 -2
- package/index.cjs +1 -1
- package/index.js +93 -93
- package/package.json +4 -4
- package/types.d.ts +1 -1
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"),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.
|
|
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
|
@@ -3,8 +3,8 @@ import { SimpleCowEventEmitter as U } from "@cowprotocol/events";
|
|
|
3
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
4
|
const fe = ["primary", "background", "paper", "text", "danger", "warning", "alert", "info", "success"], s = new D("cowSwapWidget");
|
|
5
5
|
class b {
|
|
6
|
-
constructor(
|
|
7
|
-
this.contentWindow =
|
|
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
9
|
eventEmitter = new U();
|
|
10
10
|
listeners = [];
|
|
@@ -12,17 +12,17 @@ class b {
|
|
|
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
23
|
class H {
|
|
24
|
-
constructor(
|
|
25
|
-
this.appWindow =
|
|
24
|
+
constructor(n, t) {
|
|
25
|
+
this.appWindow = n, this.iframeWidow = t, this.forwardSdkMessage = (o) => {
|
|
26
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
|
}
|
|
@@ -43,8 +43,8 @@ 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 M(e = "Default",
|
|
47
|
-
console.debug(`%c [COW][${e}]`, `font-weight: bold; color: ${
|
|
46
|
+
function M(e = "Default", n = "#1c5dbf", ...t) {
|
|
47
|
+
console.debug(`%c [COW][${e}]`, `font-weight: bold; color: ${n}`, ...t);
|
|
48
48
|
}
|
|
49
49
|
function V(...e) {
|
|
50
50
|
M("Widget", "#47aba8", ...e);
|
|
@@ -54,41 +54,41 @@ function k(e) {
|
|
|
54
54
|
}
|
|
55
55
|
const C = "_";
|
|
56
56
|
function j(e) {
|
|
57
|
-
const
|
|
58
|
-
return
|
|
57
|
+
const n = typeof e.baseUrl == "string" ? e.baseUrl : "https://swap.cow.finance", t = S(e);
|
|
58
|
+
return n + "/#" + t + "?" + I(e);
|
|
59
59
|
}
|
|
60
60
|
function S(e) {
|
|
61
61
|
const {
|
|
62
|
-
chainId:
|
|
63
|
-
sell:
|
|
62
|
+
chainId: n = 1,
|
|
63
|
+
sell: t,
|
|
64
64
|
buy: o,
|
|
65
65
|
tradeType: r = A.SWAP
|
|
66
|
-
} = e, a = [
|
|
67
|
-
return `/${
|
|
66
|
+
} = e, a = [t?.asset || C, o?.asset || C].map(encodeURIComponent).join("/");
|
|
67
|
+
return `/${n}/widget/${r}/${a}`;
|
|
68
68
|
}
|
|
69
69
|
function I(e) {
|
|
70
|
-
const
|
|
71
|
-
return K($(Q(x(G(
|
|
70
|
+
const n = new URLSearchParams();
|
|
71
|
+
return K($(Q(x(G(n, e), e), e), e), e);
|
|
72
72
|
}
|
|
73
|
-
function G(e,
|
|
73
|
+
function G(e, n) {
|
|
74
74
|
const {
|
|
75
|
-
sell:
|
|
75
|
+
sell: t,
|
|
76
76
|
buy: o
|
|
77
|
-
} =
|
|
78
|
-
return
|
|
77
|
+
} = n;
|
|
78
|
+
return t?.amount && e.append("sellAmount", t.amount), o?.amount && e.append("buyAmount", o.amount), e;
|
|
79
79
|
}
|
|
80
|
-
function x(e,
|
|
81
|
-
const
|
|
82
|
-
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);
|
|
83
83
|
}
|
|
84
|
-
function Q(e,
|
|
85
|
-
return
|
|
84
|
+
function Q(e, n) {
|
|
85
|
+
return n.targetChainId && e.append("targetChainId", n.targetChainId.toString()), e;
|
|
86
86
|
}
|
|
87
|
-
function $(e,
|
|
88
|
-
return
|
|
87
|
+
function $(e, n) {
|
|
88
|
+
return n.locale && e.append("lng", n.locale), e;
|
|
89
89
|
}
|
|
90
|
-
function K(e,
|
|
91
|
-
return Y(
|
|
90
|
+
function K(e, n) {
|
|
91
|
+
return Y(n.hooks) ? e.append("hooksEnabled", "true") : e.delete("hooksEnabled"), e;
|
|
92
92
|
}
|
|
93
93
|
function Y(e) {
|
|
94
94
|
return !!e && Object.values(e).some(Boolean);
|
|
@@ -103,27 +103,27 @@ const N = "640px", J = "450px", X = 20, z = {
|
|
|
103
103
|
destroy: () => {
|
|
104
104
|
}
|
|
105
105
|
};
|
|
106
|
-
function pe(e,
|
|
106
|
+
function pe(e, n) {
|
|
107
107
|
const {
|
|
108
|
-
params:
|
|
108
|
+
params: t,
|
|
109
109
|
provider: o,
|
|
110
110
|
listeners: r,
|
|
111
111
|
onReady: a
|
|
112
|
-
} =
|
|
113
|
-
let E = o, w =
|
|
112
|
+
} = n;
|
|
113
|
+
let E = o, w = t;
|
|
114
114
|
if (typeof window > "u") return z;
|
|
115
|
-
const d = Z(
|
|
115
|
+
const d = Z(t), i = q(d);
|
|
116
116
|
V("Resolved trusted iframe origin", {
|
|
117
117
|
iframeOrigin: i
|
|
118
118
|
});
|
|
119
119
|
const f = [];
|
|
120
|
-
a && f.push(
|
|
120
|
+
a && f.push(ne(d.contentWindow || window, i, a)), e.innerHTML = "", e.appendChild(d);
|
|
121
121
|
const {
|
|
122
122
|
contentWindow: u
|
|
123
123
|
} = d;
|
|
124
124
|
if (!u)
|
|
125
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,
|
|
126
|
+
f.push(ee(u, i, t.appCode)), f.push(...ie(d, i, t.height, t.maxHeight)), f.push(te(i));
|
|
127
127
|
let _ = null;
|
|
128
128
|
function h() {
|
|
129
129
|
u && (_ && window.removeEventListener("message", _), _ = de(u, i, w.hooks), f.push(_));
|
|
@@ -148,27 +148,27 @@ function pe(e, t) {
|
|
|
148
148
|
}
|
|
149
149
|
};
|
|
150
150
|
}
|
|
151
|
-
function g(e,
|
|
152
|
-
|
|
153
|
-
const r =
|
|
151
|
+
function g(e, n, t, o) {
|
|
152
|
+
t && t.disconnect();
|
|
153
|
+
const r = t || new W(e, n);
|
|
154
154
|
return o && r.onConnect(o), r;
|
|
155
155
|
}
|
|
156
156
|
function Z(e) {
|
|
157
157
|
const {
|
|
158
|
-
width:
|
|
159
|
-
height:
|
|
158
|
+
width: n = J,
|
|
159
|
+
height: t = N
|
|
160
160
|
} = e, o = document.createElement("iframe");
|
|
161
|
-
return o.src = j(e), o.width =
|
|
161
|
+
return o.src = j(e), o.width = n, o.height = t, o.style.border = "0", o.allow = "clipboard-read; clipboard-write", o;
|
|
162
162
|
}
|
|
163
163
|
function q(e) {
|
|
164
164
|
return new URL(e.src).origin;
|
|
165
165
|
}
|
|
166
|
-
function L(e,
|
|
167
|
-
const r = !!o, a = S(
|
|
166
|
+
function L(e, n, t, o) {
|
|
167
|
+
const r = !!o, a = S(t), E = I(t).toString(), {
|
|
168
168
|
theme: w,
|
|
169
169
|
hooks: d,
|
|
170
170
|
...i
|
|
171
|
-
} =
|
|
171
|
+
} = t;
|
|
172
172
|
s.postMessageToWindow(e, T.UPDATE_PARAMS, {
|
|
173
173
|
urlParams: {
|
|
174
174
|
pathname: a,
|
|
@@ -176,105 +176,105 @@ function L(e, t, n, o) {
|
|
|
176
176
|
},
|
|
177
177
|
appParams: i,
|
|
178
178
|
hasProvider: r
|
|
179
|
-
},
|
|
179
|
+
}, n);
|
|
180
180
|
}
|
|
181
|
-
function ee(e,
|
|
181
|
+
function ee(e, n, t) {
|
|
182
182
|
return s.listenToMessageFromWindow(window, l.ACTIVATE, () => {
|
|
183
183
|
s.postMessageToWindow(e, T.UPDATE_APP_DATA, {
|
|
184
|
-
metaData:
|
|
185
|
-
appCode:
|
|
184
|
+
metaData: t ? {
|
|
185
|
+
appCode: t
|
|
186
186
|
} : void 0
|
|
187
|
-
},
|
|
188
|
-
},
|
|
187
|
+
}, n);
|
|
188
|
+
}, n);
|
|
189
189
|
}
|
|
190
|
-
function
|
|
190
|
+
function ne(e, n, t) {
|
|
191
191
|
let o = !1;
|
|
192
192
|
return s.listenToMessageFromWindow(window, l.READY, () => {
|
|
193
|
-
o || (o = !0,
|
|
194
|
-
},
|
|
193
|
+
o || (o = !0, t());
|
|
194
|
+
}, n);
|
|
195
195
|
}
|
|
196
|
-
function
|
|
196
|
+
function te(e) {
|
|
197
197
|
return s.listenToMessageFromWindow(window, l.INTERCEPT_WINDOW_OPEN, ({
|
|
198
|
-
href:
|
|
199
|
-
rel:
|
|
198
|
+
href: n,
|
|
199
|
+
rel: t,
|
|
200
200
|
target: o
|
|
201
201
|
}) => {
|
|
202
|
-
const r = oe(
|
|
203
|
-
r && re(r) && window.open(r, o,
|
|
202
|
+
const r = oe(n.toString(), e);
|
|
203
|
+
r && re(r) && window.open(r, o, t);
|
|
204
204
|
}, e);
|
|
205
205
|
}
|
|
206
|
-
function oe(e,
|
|
207
|
-
const
|
|
208
|
-
if (!
|
|
206
|
+
function oe(e, n) {
|
|
207
|
+
const t = e.trim();
|
|
208
|
+
if (!t)
|
|
209
209
|
return null;
|
|
210
210
|
try {
|
|
211
|
-
return new URL(
|
|
211
|
+
return new URL(t, n).toString();
|
|
212
212
|
} catch {
|
|
213
213
|
return null;
|
|
214
214
|
}
|
|
215
215
|
}
|
|
216
216
|
function re(e) {
|
|
217
217
|
try {
|
|
218
|
-
const
|
|
219
|
-
return
|
|
218
|
+
const n = new URL(e).protocol;
|
|
219
|
+
return n === "http:" || n === "https:";
|
|
220
220
|
} catch {
|
|
221
221
|
return !1;
|
|
222
222
|
}
|
|
223
223
|
}
|
|
224
|
-
function ie(e,
|
|
224
|
+
function ie(e, n, t = N, o) {
|
|
225
225
|
return [s.listenToMessageFromWindow(window, l.UPDATE_HEIGHT, (r) => {
|
|
226
226
|
const a = r.height ? r.height + X : void 0;
|
|
227
|
-
e.style.height = a ? `${o ? Math.min(a, o) : a}px` :
|
|
228
|
-
},
|
|
227
|
+
e.style.height = a ? `${o ? Math.min(a, o) : a}px` : t;
|
|
228
|
+
}, n), s.listenToMessageFromWindow(window, l.SET_FULL_HEIGHT, ({
|
|
229
229
|
isUpToSmall: r
|
|
230
230
|
}) => {
|
|
231
|
-
e.style.height = r ?
|
|
232
|
-
},
|
|
231
|
+
e.style.height = r ? t : `${o || document.body.offsetHeight}px`;
|
|
232
|
+
}, n)];
|
|
233
233
|
}
|
|
234
234
|
const se = {
|
|
235
|
-
[p.ON_BEFORE_APPROVAL]: (e,
|
|
236
|
-
[p.ON_BEFORE_TRADE]: (e,
|
|
237
|
-
[p.ON_BEFORE_WRAP_UNWRAP]: (e,
|
|
238
|
-
[p.ON_BEFORE_ORDER_CANCEL]: (e,
|
|
239
|
-
[p.ON_BEFORE_ORDERS_CANCEL]: (e,
|
|
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
|
|
240
240
|
};
|
|
241
|
-
function ae(e,
|
|
242
|
-
return se[e.event](e.payload,
|
|
241
|
+
function ae(e, n) {
|
|
242
|
+
return se[e.event](e.payload, n);
|
|
243
243
|
}
|
|
244
|
-
function de(e,
|
|
244
|
+
function de(e, n, t) {
|
|
245
245
|
return s.listenToMessageFromWindow(window, l.PROCESS_HOOK, async (o) => {
|
|
246
246
|
let r = !1;
|
|
247
247
|
try {
|
|
248
|
-
r = await ae(o,
|
|
248
|
+
r = await ae(o, t);
|
|
249
249
|
} catch {
|
|
250
250
|
}
|
|
251
251
|
s.postMessageToWindow(e, T.HOOK_RESULT, {
|
|
252
252
|
id: o.id,
|
|
253
253
|
result: r
|
|
254
|
-
},
|
|
255
|
-
},
|
|
254
|
+
}, n);
|
|
255
|
+
}, n);
|
|
256
256
|
}
|
|
257
257
|
const ue = Object.values(A);
|
|
258
|
-
function we(e,
|
|
258
|
+
function we(e, n, t) {
|
|
259
259
|
if (O(e)) {
|
|
260
|
-
const o = e[
|
|
261
|
-
return R(o) ? o[
|
|
260
|
+
const o = e[t];
|
|
261
|
+
return R(o) ? o[n] : o;
|
|
262
262
|
}
|
|
263
263
|
if (R(e)) {
|
|
264
|
-
const o = e[
|
|
265
|
-
return O(o) ? o[
|
|
264
|
+
const o = e[n];
|
|
265
|
+
return O(o) ? o[t] : o;
|
|
266
266
|
}
|
|
267
267
|
return e;
|
|
268
268
|
}
|
|
269
269
|
function O(e) {
|
|
270
|
-
return typeof e != "object" ? !1 : Object.keys(e).every((
|
|
270
|
+
return typeof e != "object" ? !1 : Object.keys(e).every((n) => ue.includes(n));
|
|
271
271
|
}
|
|
272
272
|
const ce = /^\d+$/;
|
|
273
273
|
function R(e) {
|
|
274
|
-
return typeof e != "object" ? !1 : Object.keys(e).every((
|
|
274
|
+
return typeof e != "object" ? !1 : Object.keys(e).every((n) => ce.test(n));
|
|
275
275
|
}
|
|
276
276
|
function _e(e) {
|
|
277
|
-
return O(e) ? Object.values(e).flatMap((
|
|
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];
|
|
278
278
|
}
|
|
279
279
|
export {
|
|
280
280
|
A as TradeType,
|
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
|
/**
|