@cowprotocol/widget-lib 2.1.1 → 2.2.3
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 +1 -1
- package/index.cjs +1 -1
- package/index.js +146 -140
- package/package.json +4 -4
- package/types.d.ts +8 -0
package/README.md
CHANGED
|
@@ -44,7 +44,7 @@ const params: CowSwapWidgetParams = {
|
|
|
44
44
|
const {updateParams} = createCowSwapWidget(
|
|
45
45
|
widgetContainer,
|
|
46
46
|
// Optionally, you can provide some additional params to customise your widget
|
|
47
|
-
{params, provider}
|
|
47
|
+
{params, provider, onReady: () => console.log('Widget ready')}
|
|
48
48
|
)
|
|
49
49
|
|
|
50
50
|
// You also can change widget configuration on the fly
|
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 S=require("@cowprotocol/iframe-transport"),D=require("@cowprotocol/events");var d=(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))(d||{}),O=(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))(O||{}),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||{}),R=(e=>(e.SWAP="swap",e.LIMIT="limit",e.ADVANCED="advanced",e.YIELD="yield",e))(R||{});const y=["primary","background","paper","text","danger","warning","alert","info","success"],r=new S.IframeTransport("cowSwapWidget");class H{constructor(t,n=[]){this.contentWindow=t,this.updateListeners(n),this.widgetListener=r.listenToMessageFromWindow(this.contentWindow,d.EMIT_COW_EVENT,o=>this.eventEmitter.emit(o.event,o.payload))}eventEmitter=new D.SimpleCowEventEmitter;listeners=[];widgetListener;stopListeningIframe(){r.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 b{constructor(t,n){this.appWindow=t,this.iframeWidow=n,this.forwardSdkMessage=o=>{F(o.data)&&(typeof window<"u"&&o.origin===window.location.origin||(U(o.data)?this.appWindow.parent.postMessage(o.data,"*"):B(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 U(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 B(e){return"success"in e&&typeof e.success=="boolean"&&"version"in e&&typeof e.version=="string"}function L(e){return!!(e&&typeof e=="object")}const C="_";function M(e){const t=typeof e.baseUrl=="string"?e.baseUrl:"https://swap.cow.fi",n=I(e);return t+"/#"+n+"?"+v(e)}function I(e){const{chainId:t=1,sell:n,buy:o,tradeType:i=R.SWAP}=e,c=[n?.asset||C,o?.asset||C].map(encodeURIComponent).join("/");return`/${t}/widget/${i}/${c}`}function v(e){const t=new URLSearchParams;return x(G(j(k(V(t,e),e),e),e),e)}function V(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 k(e,t){const n=t.theme;return n?(L(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 j(e,t){return t.targetChainId&&e.append("targetChainId",t.targetChainId.toString()),e}function G(e,t){return t.locale&&e.append("lng",t.locale),e}function x(e,t){return Q(t.hooks)&&e.append("hooksEnabled","true"),e}function Q(e){return!!e&&Object.values(e).some(Boolean)}const N="640px",K="450px",$=20,Y={updateParams:()=>{},updateListeners:()=>{},updateProvider:()=>{},destroy:()=>{}};function z(e,t){const{params:n,provider:o,listeners:i,onReady:c}=t;let f=o,p=n;if(typeof window>"u")return Y;const s=J(n),l=[];c&&l.push(Z(s.contentWindow||window,c)),e.innerHTML="",e.appendChild(s);const{contentWindow:a}=s;if(!a)throw console.error("Iframe does not contain a window",s),new Error("Iframe does not contain a window!");l.push(X(a,n.appCode)),l.push(...ee(s,n.height,n.maxHeight)),l.push(q());let T=null;function A(){a&&(T&&window.removeEventListener("message",T),T=oe(a,p.hooks),l.push(T))}A();const h=new H(window,i);let P=g(a,null,f);s.addEventListener("load",()=>{m(a,p,f)});const W=new b(window,a);return{updateParams:u=>{p=u,m(a,p,f),A()},updateListeners:u=>h.updateListeners(u),updateProvider:u=>{f=u,P=g(a,P,u)},destroy:()=>{P.disconnect(),h.stopListeningIframe(),l.forEach(u=>window.removeEventListener("message",u)),W.stopListening(),e.removeChild(s)}}}function g(e,t,n){t&&t.disconnect();const o=t||new S.IframeRpcProviderBridge(e);return n&&o.onConnect(n),o}function J(e){const{width:t=K,height:n=N}=e,o=document.createElement("iframe");return o.src=M(e),o.width=t,o.height=n,o.style.border="0",o.allow="clipboard-read; clipboard-write",o}function m(e,t,n){const o=!!n,i=I(t),c=v(t).toString(),{theme:f,hooks:p,...s}=t;r.postMessageToWindow(e,O.UPDATE_PARAMS,{urlParams:{pathname:i,search:c},appParams:s,hasProvider:o})}function X(e,t){return r.listenToMessageFromWindow(window,d.ACTIVATE,()=>{r.postMessageToWindow(e,O.UPDATE_APP_DATA,{metaData:t?{appCode:t}:void 0})})}function Z(e,t){let n=!1;return r.listenToMessageFromWindow(window,d.READY,()=>{n||(n=!0,t())})}function q(){return r.listenToMessageFromWindow(window,d.INTERCEPT_WINDOW_OPEN,({href:e,rel:t,target:n})=>{const o=e.toString();if(!o.startsWith("http")&&o.match(/^[a-zA-Z0-9]+:\/\//)){window.open(o,n,t);return}})}function ee(e,t=N,n){return[r.listenToMessageFromWindow(window,d.UPDATE_HEIGHT,o=>{const i=o.height?o.height+$:void 0;e.style.height=i?`${n?Math.min(i,n):i}px`:t}),r.listenToMessageFromWindow(window,d.SET_FULL_HEIGHT,({isUpToSmall:o})=>{e.style.height=o?t:`${n||document.body.offsetHeight}px`})]}const te={[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 ne(e,t){return te[e.event](e.payload,t)}function oe(e,t){return r.listenToMessageFromWindow(window,d.PROCESS_HOOK,async n=>{let o=!1;try{o=await ne(n,t)}catch{}r.postMessageToWindow(e,O.HOOK_RESULT,{id:n.id,result:o})})}const re=Object.values(R);function ie(e,t,n){if(w(e)){const o=e[n];return _(o)?o[t]:o}if(_(e)){const o=e[t];return w(o)?o[n]:o}return e}function w(e){return typeof e!="object"?!1:Object.keys(e).every(t=>re.includes(t))}const se=/^\d+$/;function _(e){return typeof e!="object"?!1:Object.keys(e).every(t=>se.test(t))}function ae(e){return w(e)?Object.values(e).flatMap(t=>_(t)?Object.values(t):[t]):_(e)?Object.values(e).flatMap(t=>w(t)?Object.values(t):[t]):[e]}exports.TradeType=R;exports.WIDGET_PALETTE_COLORS=y;exports.WidgetHookEvents=E;exports.WidgetMethodsEmit=d;exports.WidgetMethodsListen=O;exports.createCowSwapWidget=z;exports.isCowSwapWidgetPalette=L;exports.isPerNetworkConfig=_;exports.isPerTradeTypeConfig=w;exports.resolveFlexibleConfig=ie;exports.resolveFlexibleConfigValues=ae;exports.widgetIframeTransport=r;
|
package/index.js
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
import { IframeTransport as
|
|
2
|
-
import { SimpleCowEventEmitter as
|
|
3
|
-
var
|
|
4
|
-
const
|
|
5
|
-
class
|
|
6
|
-
constructor(t,
|
|
7
|
-
this.contentWindow = t, this.updateListeners(
|
|
1
|
+
import { IframeTransport as D, IframeRpcProviderBridge as v } from "@cowprotocol/iframe-transport";
|
|
2
|
+
import { SimpleCowEventEmitter as W } from "@cowprotocol/events";
|
|
3
|
+
var E = /* @__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))(E || {}), R = /* @__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))(R || {}), f = /* @__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))(f || {}), P = /* @__PURE__ */ ((e) => (e.SWAP = "swap", e.LIMIT = "limit", e.ADVANCED = "advanced", e.YIELD = "yield", e))(P || {});
|
|
4
|
+
const ae = ["primary", "background", "paper", "text", "danger", "warning", "alert", "info", "success"], r = new D("cowSwapWidget");
|
|
5
|
+
class H {
|
|
6
|
+
constructor(t, n = []) {
|
|
7
|
+
this.contentWindow = t, this.updateListeners(n), this.widgetListener = r.listenToMessageFromWindow(this.contentWindow, E.EMIT_COW_EVENT, (o) => this.eventEmitter.emit(o.event, o.payload));
|
|
8
8
|
}
|
|
9
|
-
eventEmitter = new
|
|
9
|
+
eventEmitter = new W();
|
|
10
10
|
listeners = [];
|
|
11
11
|
widgetListener;
|
|
12
12
|
stopListeningIframe() {
|
|
13
|
-
|
|
13
|
+
r.stopListeningWindowListener(this.contentWindow, this.widgetListener);
|
|
14
14
|
}
|
|
15
15
|
updateListeners(t) {
|
|
16
|
-
for (const
|
|
17
|
-
this.eventEmitter.off(
|
|
16
|
+
for (const n of this.listeners)
|
|
17
|
+
this.eventEmitter.off(n);
|
|
18
18
|
this.listeners = t || [];
|
|
19
|
-
for (const
|
|
20
|
-
this.eventEmitter.on(
|
|
19
|
+
for (const n of this.listeners)
|
|
20
|
+
this.eventEmitter.on(n);
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
|
-
class
|
|
24
|
-
constructor(t,
|
|
25
|
-
this.appWindow = t, this.iframeWidow =
|
|
26
|
-
|
|
23
|
+
class U {
|
|
24
|
+
constructor(t, n) {
|
|
25
|
+
this.appWindow = t, this.iframeWidow = n, 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,60 +34,60 @@ class H {
|
|
|
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) {
|
|
41
41
|
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;
|
|
42
42
|
}
|
|
43
|
-
function
|
|
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 b(e) {
|
|
47
47
|
return !!(e && typeof e == "object");
|
|
48
48
|
}
|
|
49
|
-
const
|
|
50
|
-
function
|
|
51
|
-
const t = typeof e.baseUrl == "string" ? e.baseUrl : "https://swap.cow.fi",
|
|
52
|
-
return t + "/#" +
|
|
49
|
+
const m = "_";
|
|
50
|
+
function M(e) {
|
|
51
|
+
const t = typeof e.baseUrl == "string" ? e.baseUrl : "https://swap.cow.fi", n = S(e);
|
|
52
|
+
return t + "/#" + n + "?" + I(e);
|
|
53
53
|
}
|
|
54
|
-
function
|
|
54
|
+
function S(e) {
|
|
55
55
|
const {
|
|
56
56
|
chainId: t = 1,
|
|
57
|
-
sell:
|
|
58
|
-
buy:
|
|
59
|
-
tradeType:
|
|
60
|
-
} = e,
|
|
61
|
-
return `/${t}/widget/${
|
|
57
|
+
sell: n,
|
|
58
|
+
buy: o,
|
|
59
|
+
tradeType: i = P.SWAP
|
|
60
|
+
} = e, u = [n?.asset || m, o?.asset || m].map(encodeURIComponent).join("/");
|
|
61
|
+
return `/${t}/widget/${i}/${u}`;
|
|
62
62
|
}
|
|
63
|
-
function
|
|
63
|
+
function I(e) {
|
|
64
64
|
const t = new URLSearchParams();
|
|
65
|
-
return G(j(k(V(
|
|
65
|
+
return x(G(j(k(V(t, e), e), e), e), e);
|
|
66
66
|
}
|
|
67
|
-
function
|
|
67
|
+
function V(e, t) {
|
|
68
68
|
const {
|
|
69
|
-
sell:
|
|
70
|
-
buy:
|
|
69
|
+
sell: n,
|
|
70
|
+
buy: o
|
|
71
71
|
} = t;
|
|
72
|
-
return
|
|
73
|
-
}
|
|
74
|
-
function V(e, t) {
|
|
75
|
-
const o = t.theme;
|
|
76
|
-
return o ? (F(o) ? (e.append("palette", encodeURIComponent(JSON.stringify(o))), e.append("theme", o.baseTheme)) : (e.append("palette", "null"), e.append("theme", o)), e) : (e.append("palette", "null"), e);
|
|
72
|
+
return n?.amount && e.append("sellAmount", n.amount), o?.amount && e.append("buyAmount", o.amount), e;
|
|
77
73
|
}
|
|
78
74
|
function k(e, t) {
|
|
79
|
-
|
|
75
|
+
const n = t.theme;
|
|
76
|
+
return n ? (b(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);
|
|
80
77
|
}
|
|
81
78
|
function j(e, t) {
|
|
82
|
-
return t.
|
|
79
|
+
return t.targetChainId && e.append("targetChainId", t.targetChainId.toString()), e;
|
|
83
80
|
}
|
|
84
81
|
function G(e, t) {
|
|
85
|
-
return
|
|
82
|
+
return t.locale && e.append("lng", t.locale), e;
|
|
83
|
+
}
|
|
84
|
+
function x(e, t) {
|
|
85
|
+
return Q(t.hooks) && e.append("hooksEnabled", "true"), e;
|
|
86
86
|
}
|
|
87
|
-
function
|
|
87
|
+
function Q(e) {
|
|
88
88
|
return !!e && Object.values(e).some(Boolean);
|
|
89
89
|
}
|
|
90
|
-
const
|
|
90
|
+
const g = "640px", K = "450px", $ = 20, Y = {
|
|
91
91
|
updateParams: () => {
|
|
92
92
|
},
|
|
93
93
|
updateListeners: () => {
|
|
@@ -97,162 +97,168 @@ const I = "640px", Q = "450px", K = 20, $ = {
|
|
|
97
97
|
destroy: () => {
|
|
98
98
|
}
|
|
99
99
|
};
|
|
100
|
-
function
|
|
100
|
+
function de(e, t) {
|
|
101
101
|
const {
|
|
102
|
-
params:
|
|
103
|
-
provider:
|
|
104
|
-
listeners:
|
|
102
|
+
params: n,
|
|
103
|
+
provider: o,
|
|
104
|
+
listeners: i,
|
|
105
|
+
onReady: u
|
|
105
106
|
} = t;
|
|
106
|
-
let
|
|
107
|
-
if (typeof window > "u") return
|
|
108
|
-
const
|
|
109
|
-
e.innerHTML = "", e.appendChild(
|
|
107
|
+
let p = o, l = n;
|
|
108
|
+
if (typeof window > "u") return Y;
|
|
109
|
+
const s = z(n), c = [];
|
|
110
|
+
u && c.push(X(s.contentWindow || window, u)), e.innerHTML = "", e.appendChild(s);
|
|
110
111
|
const {
|
|
111
|
-
contentWindow:
|
|
112
|
-
} =
|
|
113
|
-
if (!
|
|
114
|
-
throw console.error("Iframe does not contain a window",
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
r && (l && window.removeEventListener("message", l), l = ee(r, p.hooks), f.push(l));
|
|
112
|
+
contentWindow: a
|
|
113
|
+
} = s;
|
|
114
|
+
if (!a)
|
|
115
|
+
throw console.error("Iframe does not contain a window", s), new Error("Iframe does not contain a window!");
|
|
116
|
+
c.push(J(a, n.appCode)), c.push(...q(s, n.height, n.maxHeight)), c.push(Z());
|
|
117
|
+
let w = null;
|
|
118
|
+
function A() {
|
|
119
|
+
a && (w && window.removeEventListener("message", w), w = ne(a, l.hooks), c.push(w));
|
|
120
120
|
}
|
|
121
|
-
|
|
122
|
-
const
|
|
123
|
-
let T =
|
|
124
|
-
|
|
125
|
-
|
|
121
|
+
A();
|
|
122
|
+
const h = new H(window, i);
|
|
123
|
+
let T = C(a, null, p);
|
|
124
|
+
s.addEventListener("load", () => {
|
|
125
|
+
L(a, l, p);
|
|
126
126
|
});
|
|
127
|
-
const
|
|
127
|
+
const N = new U(window, a);
|
|
128
128
|
return {
|
|
129
129
|
updateParams: (d) => {
|
|
130
|
-
|
|
130
|
+
l = d, L(a, l, p), A();
|
|
131
131
|
},
|
|
132
|
-
updateListeners: (d) =>
|
|
132
|
+
updateListeners: (d) => h.updateListeners(d),
|
|
133
133
|
updateProvider: (d) => {
|
|
134
|
-
|
|
134
|
+
p = d, T = C(a, T, d);
|
|
135
135
|
},
|
|
136
136
|
destroy: () => {
|
|
137
|
-
T.disconnect(),
|
|
137
|
+
T.disconnect(), h.stopListeningIframe(), c.forEach((d) => window.removeEventListener("message", d)), N.stopListening(), e.removeChild(s);
|
|
138
138
|
}
|
|
139
139
|
};
|
|
140
140
|
}
|
|
141
|
-
function
|
|
141
|
+
function C(e, t, n) {
|
|
142
142
|
t && t.disconnect();
|
|
143
|
-
const
|
|
144
|
-
return
|
|
143
|
+
const o = t || new v(e);
|
|
144
|
+
return n && o.onConnect(n), o;
|
|
145
145
|
}
|
|
146
|
-
function
|
|
146
|
+
function z(e) {
|
|
147
147
|
const {
|
|
148
|
-
width: t =
|
|
149
|
-
height:
|
|
150
|
-
} = e,
|
|
151
|
-
return
|
|
148
|
+
width: t = K,
|
|
149
|
+
height: n = g
|
|
150
|
+
} = e, o = document.createElement("iframe");
|
|
151
|
+
return o.src = M(e), o.width = t, o.height = n, o.style.border = "0", o.allow = "clipboard-read; clipboard-write", o;
|
|
152
152
|
}
|
|
153
|
-
function
|
|
154
|
-
const
|
|
153
|
+
function L(e, t, n) {
|
|
154
|
+
const o = !!n, i = S(t), u = I(t).toString(), {
|
|
155
155
|
theme: p,
|
|
156
|
-
hooks:
|
|
157
|
-
...
|
|
156
|
+
hooks: l,
|
|
157
|
+
...s
|
|
158
158
|
} = t;
|
|
159
|
-
|
|
159
|
+
r.postMessageToWindow(e, R.UPDATE_PARAMS, {
|
|
160
160
|
urlParams: {
|
|
161
|
-
pathname:
|
|
162
|
-
search:
|
|
161
|
+
pathname: i,
|
|
162
|
+
search: u
|
|
163
163
|
},
|
|
164
|
-
appParams:
|
|
165
|
-
hasProvider:
|
|
164
|
+
appParams: s,
|
|
165
|
+
hasProvider: o
|
|
166
166
|
});
|
|
167
167
|
}
|
|
168
|
-
function
|
|
169
|
-
return
|
|
170
|
-
|
|
168
|
+
function J(e, t) {
|
|
169
|
+
return r.listenToMessageFromWindow(window, E.ACTIVATE, () => {
|
|
170
|
+
r.postMessageToWindow(e, R.UPDATE_APP_DATA, {
|
|
171
171
|
metaData: t ? {
|
|
172
172
|
appCode: t
|
|
173
173
|
} : void 0
|
|
174
174
|
});
|
|
175
175
|
});
|
|
176
176
|
}
|
|
177
|
-
function
|
|
178
|
-
|
|
177
|
+
function X(e, t) {
|
|
178
|
+
let n = !1;
|
|
179
|
+
return r.listenToMessageFromWindow(window, E.READY, () => {
|
|
180
|
+
n || (n = !0, t());
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
function Z() {
|
|
184
|
+
return r.listenToMessageFromWindow(window, E.INTERCEPT_WINDOW_OPEN, ({
|
|
179
185
|
href: e,
|
|
180
186
|
rel: t,
|
|
181
|
-
target:
|
|
187
|
+
target: n
|
|
182
188
|
}) => {
|
|
183
|
-
const
|
|
184
|
-
if (!
|
|
185
|
-
window.open(
|
|
189
|
+
const o = e.toString();
|
|
190
|
+
if (!o.startsWith("http") && o.match(/^[a-zA-Z0-9]+:\/\//)) {
|
|
191
|
+
window.open(o, n, t);
|
|
186
192
|
return;
|
|
187
193
|
}
|
|
188
194
|
});
|
|
189
195
|
}
|
|
190
|
-
function
|
|
191
|
-
return [
|
|
192
|
-
const
|
|
193
|
-
e.style.height =
|
|
194
|
-
}),
|
|
195
|
-
isUpToSmall:
|
|
196
|
+
function q(e, t = g, n) {
|
|
197
|
+
return [r.listenToMessageFromWindow(window, E.UPDATE_HEIGHT, (o) => {
|
|
198
|
+
const i = o.height ? o.height + $ : void 0;
|
|
199
|
+
e.style.height = i ? `${n ? Math.min(i, n) : i}px` : t;
|
|
200
|
+
}), r.listenToMessageFromWindow(window, E.SET_FULL_HEIGHT, ({
|
|
201
|
+
isUpToSmall: o
|
|
196
202
|
}) => {
|
|
197
|
-
e.style.height =
|
|
203
|
+
e.style.height = o ? t : `${n || document.body.offsetHeight}px`;
|
|
198
204
|
})];
|
|
199
205
|
}
|
|
200
|
-
const
|
|
201
|
-
[
|
|
202
|
-
[
|
|
203
|
-
[
|
|
204
|
-
[
|
|
205
|
-
[
|
|
206
|
+
const ee = {
|
|
207
|
+
[f.ON_BEFORE_APPROVAL]: (e, t) => t?.onBeforeApproval ? t.onBeforeApproval(e) : !0,
|
|
208
|
+
[f.ON_BEFORE_TRADE]: (e, t) => t?.onBeforeTrade ? t.onBeforeTrade(e) : !0,
|
|
209
|
+
[f.ON_BEFORE_WRAP_UNWRAP]: (e, t) => t?.onBeforeWrapOrUnwrap ? t.onBeforeWrapOrUnwrap(e) : !0,
|
|
210
|
+
[f.ON_BEFORE_ORDER_CANCEL]: (e, t) => t?.onBeforeOrderCancel ? t.onBeforeOrderCancel(e) : !0,
|
|
211
|
+
[f.ON_BEFORE_ORDERS_CANCEL]: (e, t) => t?.onBeforeOrdersCancel ? t.onBeforeOrdersCancel(e) : !0
|
|
206
212
|
};
|
|
207
|
-
function
|
|
208
|
-
return
|
|
213
|
+
function te(e, t) {
|
|
214
|
+
return ee[e.event](e.payload, t);
|
|
209
215
|
}
|
|
210
|
-
function
|
|
211
|
-
return
|
|
212
|
-
let
|
|
216
|
+
function ne(e, t) {
|
|
217
|
+
return r.listenToMessageFromWindow(window, E.PROCESS_HOOK, async (n) => {
|
|
218
|
+
let o = !1;
|
|
213
219
|
try {
|
|
214
|
-
|
|
220
|
+
o = await te(n, t);
|
|
215
221
|
} catch {
|
|
216
222
|
}
|
|
217
|
-
|
|
218
|
-
id:
|
|
219
|
-
result:
|
|
223
|
+
r.postMessageToWindow(e, R.HOOK_RESULT, {
|
|
224
|
+
id: n.id,
|
|
225
|
+
result: o
|
|
220
226
|
});
|
|
221
227
|
});
|
|
222
228
|
}
|
|
223
|
-
const
|
|
224
|
-
function
|
|
229
|
+
const oe = Object.values(P);
|
|
230
|
+
function Ee(e, t, n) {
|
|
225
231
|
if (_(e)) {
|
|
226
|
-
const
|
|
227
|
-
return O(
|
|
232
|
+
const o = e[n];
|
|
233
|
+
return O(o) ? o[t] : o;
|
|
228
234
|
}
|
|
229
235
|
if (O(e)) {
|
|
230
|
-
const
|
|
231
|
-
return _(
|
|
236
|
+
const o = e[t];
|
|
237
|
+
return _(o) ? o[n] : o;
|
|
232
238
|
}
|
|
233
239
|
return e;
|
|
234
240
|
}
|
|
235
241
|
function _(e) {
|
|
236
|
-
return typeof e != "object" ? !1 : Object.keys(e).every((t) =>
|
|
242
|
+
return typeof e != "object" ? !1 : Object.keys(e).every((t) => oe.includes(t));
|
|
237
243
|
}
|
|
238
|
-
const
|
|
244
|
+
const re = /^\d+$/;
|
|
239
245
|
function O(e) {
|
|
240
|
-
return typeof e != "object" ? !1 : Object.keys(e).every((t) =>
|
|
246
|
+
return typeof e != "object" ? !1 : Object.keys(e).every((t) => re.test(t));
|
|
241
247
|
}
|
|
242
|
-
function
|
|
248
|
+
function ue(e) {
|
|
243
249
|
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];
|
|
244
250
|
}
|
|
245
251
|
export {
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
252
|
+
P as TradeType,
|
|
253
|
+
ae as WIDGET_PALETTE_COLORS,
|
|
254
|
+
f as WidgetHookEvents,
|
|
255
|
+
E as WidgetMethodsEmit,
|
|
256
|
+
R as WidgetMethodsListen,
|
|
257
|
+
de as createCowSwapWidget,
|
|
258
|
+
b as isCowSwapWidgetPalette,
|
|
253
259
|
O as isPerNetworkConfig,
|
|
254
260
|
_ as isPerTradeTypeConfig,
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
261
|
+
Ee as resolveFlexibleConfig,
|
|
262
|
+
ue as resolveFlexibleConfigValues,
|
|
263
|
+
r as widgetIframeTransport
|
|
258
264
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cowprotocol/widget-lib",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.2.3",
|
|
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.1.
|
|
20
|
-
"@cowprotocol/iframe-transport": "^2.0.
|
|
18
|
+
"@cowprotocol/cow-sdk": "8.0.5",
|
|
19
|
+
"@cowprotocol/events": "^4.1.1",
|
|
20
|
+
"@cowprotocol/iframe-transport": "^2.0.11"
|
|
21
21
|
},
|
|
22
22
|
"exports": {
|
|
23
23
|
".": {
|
package/types.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ export type PerNetworkConfig<T> = Partial<Record<SupportedChainId, T>>;
|
|
|
7
7
|
export type FlexibleConfig<T> = T | PerNetworkConfig<T> | PerTradeTypeConfig<T> | PerTradeTypeConfig<PerNetworkConfig<T>> | PerNetworkConfig<PerTradeTypeConfig<T>>;
|
|
8
8
|
export declare enum WidgetMethodsEmit {
|
|
9
9
|
ACTIVATE = "ACTIVATE",
|
|
10
|
+
READY = "READY",
|
|
10
11
|
UPDATE_HEIGHT = "UPDATE_HEIGHT",
|
|
11
12
|
SET_FULL_HEIGHT = "SET_FULL_HEIGHT",
|
|
12
13
|
EMIT_COW_EVENT = "EMIT_COW_EVENT",
|
|
@@ -40,6 +41,7 @@ export interface CowSwapWidgetProps {
|
|
|
40
41
|
params: CowSwapWidgetParams;
|
|
41
42
|
provider?: EthereumProvider;
|
|
42
43
|
listeners?: CowWidgetEventListeners;
|
|
44
|
+
onReady?(): void;
|
|
43
45
|
}
|
|
44
46
|
export interface JsonRpcRequest {
|
|
45
47
|
id: number;
|
|
@@ -277,6 +279,11 @@ export interface CowSwapWidgetParams {
|
|
|
277
279
|
* Defaults to false.
|
|
278
280
|
*/
|
|
279
281
|
disableProgressBar?: boolean;
|
|
282
|
+
/**
|
|
283
|
+
* Disables CoW Swap educational tips shown after a trade completes when no surplus message is available.
|
|
284
|
+
* Defaults to false.
|
|
285
|
+
*/
|
|
286
|
+
disablePostTradeTips?: boolean;
|
|
280
287
|
/**
|
|
281
288
|
* Disables showing the toast messages.
|
|
282
289
|
* Some UI might want to disable it and subscribe to WidgetMethodsEmit.ON_TOAST_MESSAGE event to handle the toast messages itself.
|
|
@@ -350,6 +357,7 @@ export interface CowSwapWidgetParams {
|
|
|
350
357
|
}
|
|
351
358
|
export interface WidgetMethodsEmitPayloadMap {
|
|
352
359
|
[WidgetMethodsEmit.ACTIVATE]: void;
|
|
360
|
+
[WidgetMethodsEmit.READY]: void;
|
|
353
361
|
[WidgetMethodsEmit.EMIT_COW_EVENT]: EmitCowEventPayload<CowWidgetEvents>;
|
|
354
362
|
[WidgetMethodsEmit.UPDATE_HEIGHT]: UpdateWidgetHeightPayload;
|
|
355
363
|
[WidgetMethodsEmit.SET_FULL_HEIGHT]: SetWidgetFullHeightPayload;
|