@cowprotocol/widget-lib 2.2.5 → 3.0.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/const.d.ts +1 -0
- package/index.cjs +1 -1
- package/index.js +129 -128
- 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/const.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const COWSWAP_ORIGIN = "https://swap.cow.finance";
|
package/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const L=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 L.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)}const j="https://swap.cow.finance";function I(e){return!!(e&&typeof e=="object")}const C="_";function G(e){const t=typeof e.baseUrl=="string"?e.baseUrl:j,n=W(e);return t+"/#"+n+"?"+v(e)}function W(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 v(e){const t=new URLSearchParams;return Y(K($(Q(x(t,e),e),e),e),e)}function x(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 Q(e,t){const n=t.theme;return n?(I(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 $(e,t){return t.targetChainId&&e.append("targetChainId",t.targetChainId.toString()),e}function K(e,t){return t.locale&&e.append("lng",t.locale),e}function Y(e,t){return J(t.hooks)?e.append("hooksEnabled","true"):e.delete("hooksEnabled"),e}function J(e){return!!e&&Object.values(e).some(Boolean)}const N="640px",X="450px",z=20,Z={updateParams:()=>{},updateListeners:()=>{},updateProvider:()=>{},destroy:()=>{}};function q(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=ee(n),i=te(d);k("Resolved trusted iframe origin",{iframeOrigin:i});const p=[];a&&p.push(oe(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(ne(u,i,n.appCode)),p.push(...ae(d,i,n.height,n.maxHeight)),p.push(re(i));let R=null;function h(){u&&(R&&window.removeEventListener("message",R),R=ce(u,i,w.hooks),p.push(R))}h();const g=new y(window,i,r);let A=m(u,i,null,f);d.addEventListener("load",()=>{S(u,i,w,f)});const D=new H(window,u);return{updateParams:l=>{w=l,S(u,i,w,f),h()},updateListeners:l=>g.updateListeners(l),updateProvider:l=>{f=l,A=m(u,i,A,l)},destroy:()=>{A.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 L.IframeRpcProviderBridge(e,t);return o&&r.onConnect(o),r}function ee(e){const{width:t=X,height:n=N}=e,o=document.createElement("iframe");return o.src=G(e),o.width=t,o.height=n,o.style.border="0",o.allow="clipboard-read; clipboard-write",o}function te(e){return new URL(e.src).origin}function S(e,t,n,o){const r=!!o,a=W(n),f=v(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 ne(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 oe(e,t,n){let o=!1;return s.listenToMessageFromWindow(window,c.READY,()=>{o||(o=!0,n())},t)}function re(e){return s.listenToMessageFromWindow(window,c.INTERCEPT_WINDOW_OPEN,({href:t,rel:n,target:o})=>{const r=ie(t.toString(),e);r&&se(r)&&window.open(r,o,n)},e)}function ie(e,t){const n=e.trim();if(!n)return null;try{return new URL(n,t).toString()}catch{return null}}function se(e){try{const t=new URL(e).protocol;return t==="http:"||t==="https:"}catch{return!1}}function ae(e,t,n=N,o){return[s.listenToMessageFromWindow(window,c.UPDATE_HEIGHT,r=>{const a=r.height?r.height+z: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 de={[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 ue(e,t){return de[e.event](e.payload,t)}function ce(e,t,n){return s.listenToMessageFromWindow(window,c.PROCESS_HOOK,async o=>{let r=!1;try{r=await ue(o,n)}catch{}s.postMessageToWindow(e,T.HOOK_RESULT,{id:o.id,result:r},t)},t)}const le=Object.values(P);function Ee(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=>le.includes(t))}const fe=/^\d+$/;function O(e){return typeof e!="object"?!1:Object.keys(e).every(t=>fe.test(t))}function pe(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=q;exports.isCowSwapWidgetPalette=I;exports.isPerNetworkConfig=O;exports.isPerTradeTypeConfig=_;exports.resolveFlexibleConfig=Ee;exports.resolveFlexibleConfigValues=pe;exports.widgetIframeTransport=s;
|
package/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { IframeTransport as
|
|
1
|
+
import { IframeTransport as v, IframeRpcProviderBridge as D } from "@cowprotocol/iframe-transport";
|
|
2
2
|
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
|
-
const
|
|
4
|
+
const pe = ["primary", "background", "paper", "text", "danger", "warning", "alert", "info", "success"], s = new v("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,57 +43,58 @@ 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);
|
|
51
51
|
}
|
|
52
|
-
|
|
52
|
+
const k = "https://swap.cow.finance";
|
|
53
|
+
function j(e) {
|
|
53
54
|
return !!(e && typeof e == "object");
|
|
54
55
|
}
|
|
55
|
-
const
|
|
56
|
-
function
|
|
57
|
-
const
|
|
58
|
-
return
|
|
56
|
+
const m = "_";
|
|
57
|
+
function G(e) {
|
|
58
|
+
const n = typeof e.baseUrl == "string" ? e.baseUrl : k, t = I(e);
|
|
59
|
+
return n + "/#" + t + "?" + S(e);
|
|
59
60
|
}
|
|
60
|
-
function
|
|
61
|
+
function I(e) {
|
|
61
62
|
const {
|
|
62
|
-
chainId:
|
|
63
|
-
sell:
|
|
63
|
+
chainId: n = 1,
|
|
64
|
+
sell: t,
|
|
64
65
|
buy: o,
|
|
65
66
|
tradeType: r = A.SWAP
|
|
66
|
-
} = e, a = [
|
|
67
|
-
return `/${
|
|
67
|
+
} = e, a = [t?.asset || m, o?.asset || m].map(encodeURIComponent).join("/");
|
|
68
|
+
return `/${n}/widget/${r}/${a}`;
|
|
68
69
|
}
|
|
69
|
-
function
|
|
70
|
-
const
|
|
71
|
-
return K($(Q(x(
|
|
70
|
+
function S(e) {
|
|
71
|
+
const n = new URLSearchParams();
|
|
72
|
+
return Y(K($(Q(x(n, e), e), e), e), e);
|
|
72
73
|
}
|
|
73
|
-
function
|
|
74
|
+
function x(e, n) {
|
|
74
75
|
const {
|
|
75
|
-
sell:
|
|
76
|
+
sell: t,
|
|
76
77
|
buy: o
|
|
77
|
-
} =
|
|
78
|
-
return
|
|
78
|
+
} = n;
|
|
79
|
+
return t?.amount && e.append("sellAmount", t.amount), o?.amount && e.append("buyAmount", o.amount), e;
|
|
79
80
|
}
|
|
80
|
-
function
|
|
81
|
-
const
|
|
82
|
-
return
|
|
81
|
+
function Q(e, n) {
|
|
82
|
+
const t = n.theme;
|
|
83
|
+
return t ? (j(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
84
|
}
|
|
84
|
-
function
|
|
85
|
-
return
|
|
85
|
+
function $(e, n) {
|
|
86
|
+
return n.targetChainId && e.append("targetChainId", n.targetChainId.toString()), e;
|
|
86
87
|
}
|
|
87
|
-
function
|
|
88
|
-
return
|
|
88
|
+
function K(e, n) {
|
|
89
|
+
return n.locale && e.append("lng", n.locale), e;
|
|
89
90
|
}
|
|
90
|
-
function
|
|
91
|
-
return
|
|
91
|
+
function Y(e, n) {
|
|
92
|
+
return J(n.hooks) ? e.append("hooksEnabled", "true") : e.delete("hooksEnabled"), e;
|
|
92
93
|
}
|
|
93
|
-
function
|
|
94
|
+
function J(e) {
|
|
94
95
|
return !!e && Object.values(e).some(Boolean);
|
|
95
96
|
}
|
|
96
|
-
const N = "640px",
|
|
97
|
+
const N = "640px", X = "450px", z = 20, Z = {
|
|
97
98
|
updateParams: () => {
|
|
98
99
|
},
|
|
99
100
|
updateListeners: () => {
|
|
@@ -103,72 +104,72 @@ const N = "640px", J = "450px", X = 20, z = {
|
|
|
103
104
|
destroy: () => {
|
|
104
105
|
}
|
|
105
106
|
};
|
|
106
|
-
function
|
|
107
|
+
function we(e, n) {
|
|
107
108
|
const {
|
|
108
|
-
params:
|
|
109
|
+
params: t,
|
|
109
110
|
provider: o,
|
|
110
111
|
listeners: r,
|
|
111
112
|
onReady: a
|
|
112
|
-
} =
|
|
113
|
-
let E = o, w =
|
|
114
|
-
if (typeof window > "u") return
|
|
115
|
-
const d =
|
|
113
|
+
} = n;
|
|
114
|
+
let E = o, w = t;
|
|
115
|
+
if (typeof window > "u") return Z;
|
|
116
|
+
const d = q(t), i = ee(d);
|
|
116
117
|
V("Resolved trusted iframe origin", {
|
|
117
118
|
iframeOrigin: i
|
|
118
119
|
});
|
|
119
120
|
const f = [];
|
|
120
121
|
a && f.push(te(d.contentWindow || window, i, a)), e.innerHTML = "", e.appendChild(d);
|
|
121
122
|
const {
|
|
122
|
-
contentWindow:
|
|
123
|
+
contentWindow: c
|
|
123
124
|
} = d;
|
|
124
|
-
if (!
|
|
125
|
+
if (!c)
|
|
125
126
|
throw console.error("Iframe does not contain a window", d), new Error("Iframe does not contain a window!");
|
|
126
|
-
f.push(
|
|
127
|
+
f.push(ne(c, i, t.appCode)), f.push(...se(d, i, t.height, t.maxHeight)), f.push(oe(i));
|
|
127
128
|
let _ = null;
|
|
128
129
|
function h() {
|
|
129
|
-
|
|
130
|
+
c && (_ && window.removeEventListener("message", _), _ = ce(c, i, w.hooks), f.push(_));
|
|
130
131
|
}
|
|
131
132
|
h();
|
|
132
|
-
const
|
|
133
|
-
let P = g(
|
|
133
|
+
const C = new b(window, i, r);
|
|
134
|
+
let P = g(c, i, null, E);
|
|
134
135
|
d.addEventListener("load", () => {
|
|
135
|
-
L(
|
|
136
|
+
L(c, i, w, E);
|
|
136
137
|
});
|
|
137
|
-
const
|
|
138
|
+
const W = new H(window, c);
|
|
138
139
|
return {
|
|
139
|
-
updateParams: (
|
|
140
|
-
w =
|
|
140
|
+
updateParams: (u) => {
|
|
141
|
+
w = u, L(c, i, w, E), h();
|
|
141
142
|
},
|
|
142
|
-
updateListeners: (
|
|
143
|
-
updateProvider: (
|
|
144
|
-
E =
|
|
143
|
+
updateListeners: (u) => C.updateListeners(u),
|
|
144
|
+
updateProvider: (u) => {
|
|
145
|
+
E = u, P = g(c, i, P, u);
|
|
145
146
|
},
|
|
146
147
|
destroy: () => {
|
|
147
|
-
P.disconnect(),
|
|
148
|
+
P.disconnect(), C.stopListeningIframe(), f.forEach((u) => window.removeEventListener("message", u)), W.stopListening(), e.removeChild(d);
|
|
148
149
|
}
|
|
149
150
|
};
|
|
150
151
|
}
|
|
151
|
-
function g(e,
|
|
152
|
-
|
|
153
|
-
const r =
|
|
152
|
+
function g(e, n, t, o) {
|
|
153
|
+
t && t.disconnect();
|
|
154
|
+
const r = t || new D(e, n);
|
|
154
155
|
return o && r.onConnect(o), r;
|
|
155
156
|
}
|
|
156
|
-
function
|
|
157
|
+
function q(e) {
|
|
157
158
|
const {
|
|
158
|
-
width:
|
|
159
|
-
height:
|
|
159
|
+
width: n = X,
|
|
160
|
+
height: t = N
|
|
160
161
|
} = e, o = document.createElement("iframe");
|
|
161
|
-
return o.src =
|
|
162
|
+
return o.src = G(e), o.width = n, o.height = t, o.style.border = "0", o.allow = "clipboard-read; clipboard-write", o;
|
|
162
163
|
}
|
|
163
|
-
function
|
|
164
|
+
function ee(e) {
|
|
164
165
|
return new URL(e.src).origin;
|
|
165
166
|
}
|
|
166
|
-
function L(e,
|
|
167
|
-
const r = !!o, a =
|
|
167
|
+
function L(e, n, t, o) {
|
|
168
|
+
const r = !!o, a = I(t), E = S(t).toString(), {
|
|
168
169
|
theme: w,
|
|
169
170
|
hooks: d,
|
|
170
171
|
...i
|
|
171
|
-
} =
|
|
172
|
+
} = t;
|
|
172
173
|
s.postMessageToWindow(e, T.UPDATE_PARAMS, {
|
|
173
174
|
urlParams: {
|
|
174
175
|
pathname: a,
|
|
@@ -176,117 +177,117 @@ function L(e, t, n, o) {
|
|
|
176
177
|
},
|
|
177
178
|
appParams: i,
|
|
178
179
|
hasProvider: r
|
|
179
|
-
},
|
|
180
|
+
}, n);
|
|
180
181
|
}
|
|
181
|
-
function
|
|
182
|
+
function ne(e, n, t) {
|
|
182
183
|
return s.listenToMessageFromWindow(window, l.ACTIVATE, () => {
|
|
183
184
|
s.postMessageToWindow(e, T.UPDATE_APP_DATA, {
|
|
184
|
-
metaData:
|
|
185
|
-
appCode:
|
|
185
|
+
metaData: t ? {
|
|
186
|
+
appCode: t
|
|
186
187
|
} : void 0
|
|
187
|
-
},
|
|
188
|
-
},
|
|
188
|
+
}, n);
|
|
189
|
+
}, n);
|
|
189
190
|
}
|
|
190
|
-
function te(e,
|
|
191
|
+
function te(e, n, t) {
|
|
191
192
|
let o = !1;
|
|
192
193
|
return s.listenToMessageFromWindow(window, l.READY, () => {
|
|
193
|
-
o || (o = !0,
|
|
194
|
-
},
|
|
194
|
+
o || (o = !0, t());
|
|
195
|
+
}, n);
|
|
195
196
|
}
|
|
196
|
-
function
|
|
197
|
+
function oe(e) {
|
|
197
198
|
return s.listenToMessageFromWindow(window, l.INTERCEPT_WINDOW_OPEN, ({
|
|
198
|
-
href:
|
|
199
|
-
rel:
|
|
199
|
+
href: n,
|
|
200
|
+
rel: t,
|
|
200
201
|
target: o
|
|
201
202
|
}) => {
|
|
202
|
-
const r =
|
|
203
|
-
r &&
|
|
203
|
+
const r = re(n.toString(), e);
|
|
204
|
+
r && ie(r) && window.open(r, o, t);
|
|
204
205
|
}, e);
|
|
205
206
|
}
|
|
206
|
-
function
|
|
207
|
-
const
|
|
208
|
-
if (!
|
|
207
|
+
function re(e, n) {
|
|
208
|
+
const t = e.trim();
|
|
209
|
+
if (!t)
|
|
209
210
|
return null;
|
|
210
211
|
try {
|
|
211
|
-
return new URL(
|
|
212
|
+
return new URL(t, n).toString();
|
|
212
213
|
} catch {
|
|
213
214
|
return null;
|
|
214
215
|
}
|
|
215
216
|
}
|
|
216
|
-
function
|
|
217
|
+
function ie(e) {
|
|
217
218
|
try {
|
|
218
|
-
const
|
|
219
|
-
return
|
|
219
|
+
const n = new URL(e).protocol;
|
|
220
|
+
return n === "http:" || n === "https:";
|
|
220
221
|
} catch {
|
|
221
222
|
return !1;
|
|
222
223
|
}
|
|
223
224
|
}
|
|
224
|
-
function
|
|
225
|
+
function se(e, n, t = N, o) {
|
|
225
226
|
return [s.listenToMessageFromWindow(window, l.UPDATE_HEIGHT, (r) => {
|
|
226
|
-
const a = r.height ? r.height +
|
|
227
|
-
e.style.height = a ? `${o ? Math.min(a, o) : a}px` :
|
|
228
|
-
},
|
|
227
|
+
const a = r.height ? r.height + z : void 0;
|
|
228
|
+
e.style.height = a ? `${o ? Math.min(a, o) : a}px` : t;
|
|
229
|
+
}, n), s.listenToMessageFromWindow(window, l.SET_FULL_HEIGHT, ({
|
|
229
230
|
isUpToSmall: r
|
|
230
231
|
}) => {
|
|
231
|
-
e.style.height = r ?
|
|
232
|
-
},
|
|
233
|
-
}
|
|
234
|
-
const
|
|
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,
|
|
232
|
+
e.style.height = r ? t : `${o || document.body.offsetHeight}px`;
|
|
233
|
+
}, n)];
|
|
234
|
+
}
|
|
235
|
+
const ae = {
|
|
236
|
+
[p.ON_BEFORE_APPROVAL]: (e, n) => n?.onBeforeApproval ? n.onBeforeApproval(e) : !0,
|
|
237
|
+
[p.ON_BEFORE_TRADE]: (e, n) => n?.onBeforeTrade ? n.onBeforeTrade(e) : !0,
|
|
238
|
+
[p.ON_BEFORE_WRAP_UNWRAP]: (e, n) => n?.onBeforeWrapOrUnwrap ? n.onBeforeWrapOrUnwrap(e) : !0,
|
|
239
|
+
[p.ON_BEFORE_ORDER_CANCEL]: (e, n) => n?.onBeforeOrderCancel ? n.onBeforeOrderCancel(e) : !0,
|
|
240
|
+
[p.ON_BEFORE_ORDERS_CANCEL]: (e, n) => n?.onBeforeOrdersCancel ? n.onBeforeOrdersCancel(e) : !0
|
|
240
241
|
};
|
|
241
|
-
function
|
|
242
|
-
return
|
|
242
|
+
function de(e, n) {
|
|
243
|
+
return ae[e.event](e.payload, n);
|
|
243
244
|
}
|
|
244
|
-
function
|
|
245
|
+
function ce(e, n, t) {
|
|
245
246
|
return s.listenToMessageFromWindow(window, l.PROCESS_HOOK, async (o) => {
|
|
246
247
|
let r = !1;
|
|
247
248
|
try {
|
|
248
|
-
r = await
|
|
249
|
+
r = await de(o, t);
|
|
249
250
|
} catch {
|
|
250
251
|
}
|
|
251
252
|
s.postMessageToWindow(e, T.HOOK_RESULT, {
|
|
252
253
|
id: o.id,
|
|
253
254
|
result: r
|
|
254
|
-
},
|
|
255
|
-
},
|
|
255
|
+
}, n);
|
|
256
|
+
}, n);
|
|
256
257
|
}
|
|
257
258
|
const ue = Object.values(A);
|
|
258
|
-
function
|
|
259
|
+
function _e(e, n, t) {
|
|
259
260
|
if (O(e)) {
|
|
260
|
-
const o = e[
|
|
261
|
-
return R(o) ? o[
|
|
261
|
+
const o = e[t];
|
|
262
|
+
return R(o) ? o[n] : o;
|
|
262
263
|
}
|
|
263
264
|
if (R(e)) {
|
|
264
|
-
const o = e[
|
|
265
|
-
return O(o) ? o[
|
|
265
|
+
const o = e[n];
|
|
266
|
+
return O(o) ? o[t] : o;
|
|
266
267
|
}
|
|
267
268
|
return e;
|
|
268
269
|
}
|
|
269
270
|
function O(e) {
|
|
270
|
-
return typeof e != "object" ? !1 : Object.keys(e).every((
|
|
271
|
+
return typeof e != "object" ? !1 : Object.keys(e).every((n) => ue.includes(n));
|
|
271
272
|
}
|
|
272
|
-
const
|
|
273
|
+
const le = /^\d+$/;
|
|
273
274
|
function R(e) {
|
|
274
|
-
return typeof e != "object" ? !1 : Object.keys(e).every((
|
|
275
|
+
return typeof e != "object" ? !1 : Object.keys(e).every((n) => le.test(n));
|
|
275
276
|
}
|
|
276
|
-
function
|
|
277
|
-
return O(e) ? Object.values(e).flatMap((
|
|
277
|
+
function Oe(e) {
|
|
278
|
+
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
279
|
}
|
|
279
280
|
export {
|
|
280
281
|
A as TradeType,
|
|
281
|
-
|
|
282
|
+
pe as WIDGET_PALETTE_COLORS,
|
|
282
283
|
p as WidgetHookEvents,
|
|
283
284
|
l as WidgetMethodsEmit,
|
|
284
285
|
T as WidgetMethodsListen,
|
|
285
|
-
|
|
286
|
-
|
|
286
|
+
we as createCowSwapWidget,
|
|
287
|
+
j as isCowSwapWidgetPalette,
|
|
287
288
|
R as isPerNetworkConfig,
|
|
288
289
|
O as isPerTradeTypeConfig,
|
|
289
|
-
|
|
290
|
-
|
|
290
|
+
_e as resolveFlexibleConfig,
|
|
291
|
+
Oe as resolveFlexibleConfigValues,
|
|
291
292
|
s as widgetIframeTransport
|
|
292
293
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cowprotocol/widget-lib",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.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
|
/**
|