@cowprotocol/widget-react 0.9.1 → 0.9.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/index.js +1 -1
- package/index.mjs +182 -152
- package/package.json +2 -2
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var V=Object.defineProperty;var F=(e,t,n)=>t in e?V(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n;var E=(e,t,n)=>(F(e,typeof t!="symbol"?t+"":t,n),n);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const R=require("react/jsx-runtime"),f=require("react");class M{constructor(){E(this,"subscriptions",{})}on(t){const{event:n,handler:r}=t;this.subscriptions[n]||(this.subscriptions[n]=[]),this.subscriptions[n].push(r)}off(t){const{event:n,handler:r}=t;this.subscriptions[n]&&(this.subscriptions[n]=this.subscriptions[n].filter(s=>s!==r))}emit(t,n){this.subscriptions[t]&&this.subscriptions[t].forEach(r=>r(n))}}const L="cowSwapWidget";function g(e,t,n){const s={key:L,method:t,...typeof n=="object"?n:{}};e.postMessage(s,"*")}function m(e,t,n){const r=s=>{!k(s.data)||s.data.key!==L||s.data.method!==t||n(s.data)};return e.addEventListener("message",r),r}function k(e){return typeof e=="object"&&e!==null&&"key"in e&&"method"in e&&typeof e.key=="string"&&typeof e.method=="string"}function D(e,t,n){e.removeEventListener("message",n)}function y(e,t){e.removeEventListener("message",t)}var w=(e=>(e.ACTIVATE="ACTIVATE",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))(w||{}),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))(T||{}),P=(e=>(e.SWAP="swap",e.LIMIT="limit",e.ADVANCED="advanced",e))(P||{});const G=["primary","background","paper","text","danger","warning","alert","info","success"];class x{constructor(t,n=[]){E(this,"eventEmitter",new M);E(this,"listeners",[]);E(this,"widgetListener");this.contentWindow=t,this.updateListeners(n),this.widgetListener=m(this.contentWindow,w.EMIT_COW_EVENT,r=>this.eventEmitter.emit(r.event,r.payload))}stopListeningIframe(){y(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)}}const j=["connect","disconnect","close","chainChanged","accountsChanged"];class ${constructor(t){E(this,"ethereumProvider",null);E(this,"requestWaitingForConnection",{});E(this,"processRpcCallFromWindow",({rpcRequest:t})=>{if(!this.ethereumProvider){t.id&&(this.requestWaitingForConnection[t.id]=t);return}this.processRpcRequest(t)});this.iframeWidow=t}disconnect(){this.ethereumProvider=null,D(window,w.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow)}onConnect(t){this.ethereumProvider?this.disconnect():m(window,w.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),this.ethereumProvider=t,this.processPendingRequests(),j.forEach(n=>{t.on(n,r=>this.onProviderEvent(n,r))})}processPendingRequests(){Object.keys(this.requestWaitingForConnection).forEach(t=>{this.processRpcRequest(this.requestWaitingForConnection[t])}),this.requestWaitingForConnection={}}processRpcRequest(t){const{id:n,jsonrpc:r,method:s}=t;if(!this.ethereumProvider||!n)return;(s==="enable"?this.ethereumProvider.enable():this.ethereumProvider.request({...t,id:n})).then(d=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:r,id:n,result:d}})).catch(d=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:r,id:n,error:d}}))}onProviderEvent(t,n){g(this.iframeWidow,T.PROVIDER_ON_EVENT,{event:t,params:n})}forwardRpcResponseToIframe(t){g(this.iframeWidow,T.PROVIDER_RPC_RESPONSE,t)}}class Q{constructor(t,n){E(this,"forwardSdkMessage");this.appWindow=t,this.iframeWidow=n,this.forwardSdkMessage=r=>{q(r.data)&&r.origin!==window.location.origin&&(B(r.data)?this.appWindow.parent.postMessage(r.data,"*"):J(r.data)&&this.iframeWidow.postMessage(r.data,"*"))},this.startListening()}startListening(){this.appWindow.addEventListener("message",this.forwardSdkMessage)}stopListening(){this.appWindow.removeEventListener("message",this.forwardSdkMessage)}}function q(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 J(e){return"success"in e&&typeof e.success=="boolean"&&"version"in e&&typeof e.version=="string"}function b(e){return!!(e&&typeof e=="object")}const I="_";function z(e){const t=typeof e.baseUrl=="string"?e.baseUrl:"https://swap.cow.fi",n=N(e);return t+"/#"+n+"?"+U(e)}function N(e){const{chainId:t=1,sell:n,buy:r,tradeType:s=P.SWAP}=e,c=[(n==null?void 0:n.asset)||I,(r==null?void 0:r.asset)||I].map(encodeURIComponent).join("/");return`/${t}/widget/${s}/${c}`}function U(e){const t=new URLSearchParams;return Y(K(t,e),e)}function K(e,t){const{sell:n,buy:r}=t;return n!=null&&n.amount&&e.append("sellAmount",n.amount),r!=null&&r.amount&&e.append("buyAmount",r.amount),e}function Y(e,t){const n=t.theme;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)}const H="640px",X="450px",Z=20;function _(e,t){const{params:n,provider:r,listeners:s}=t;let c=r,d=n;const o=ee(n);e.innerHTML="",e.appendChild(o);const{contentWindow:p}=o;if(!p)throw console.error("Iframe does not contain a window",o),new Error("Iframe does not contain a window!");const h=[];h.push(te(p,n.appCode)),h.push(...re(o,n.height)),h.push(ne());const u=new x(window,s);let l=A(p,null,c);o.addEventListener("load",()=>O(p,d,c));const a=new Q(window,p);return{updateParams:i=>{d=i,O(p,d,c)},updateListeners:i=>u.updateListeners(i),updateProvider:i=>{c=i,l=A(p,l,i)},destroy:()=>{l.disconnect(),u.stopListeningIframe(),h.forEach(i=>window.removeEventListener("message",i)),a.stopListening(),e.removeChild(o)}}}function A(e,t,n){t&&t.disconnect();const r=t||new $(e);return n&&r.onConnect(n),r}function ee(e){const{width:t=X,height:n=H}=e,r=document.createElement("iframe");return r.src=z(e),r.width=t,r.height=n,r.style.border="0",r.allow="clipboard-read; clipboard-write",r}function O(e,t,n){const r=!!n,s=N(t),c=U(t).toString(),{theme:d,...o}=t;g(e,T.UPDATE_PARAMS,{urlParams:{pathname:s,search:c},appParams:o,hasProvider:r})}function te(e,t){return m(window,w.ACTIVATE,()=>{g(e,T.UPDATE_APP_DATA,{metaData:t?{appCode:t}:void 0})})}function ne(){return m(window,w.INTERCEPT_WINDOW_OPEN,({href:e,rel:t,target:n})=>{const r=e.toString();if(!r.startsWith("http")&&r.match(/^[a-zA-Z0-9]+:\/\//)){window.open(r,n,t);return}})}function re(e,t=H){return[m(window,w.UPDATE_HEIGHT,n=>{e.style.height=n.height?`${n.height+Z}px`:t}),m(window,w.SET_FULL_HEIGHT,({isUpToSmall:n})=>{e.style.height=n?t:`${document.body.offsetHeight}px`})]}const se=Object.values(P);function ie(e,t,n){if(v(e)){const r=e[n];return C(r)?r[t]:r}if(C(e)){const r=e[t];return v(r)?r[n]:r}return e}function v(e){return typeof e!="object"?!1:Object.keys(e).every(t=>se.includes(t))}const oe=/^\d+$/;function C(e){return typeof e!="object"?!1:Object.keys(e).every(t=>typeof t=="number"||oe.test(t))}function ae(e){const{params:t,provider:n,listeners:r}=e,[s,c]=f.useState(null),d=f.useRef(null),o=f.useRef(n),p=f.useRef(r),h=f.useRef(null),u=f.useRef(null),l=f.useCallback((a,i)=>{try{console.log(`[WIDGET] ${a}`),i()}catch(W){const S=`Error ${a.toLowerCase()}`;console.error(`[WIDGET] ${S}`,W),c({message:S,error:W})}},[c]);return f.useEffect(()=>()=>{d.current=null,o.current=void 0,p.current=void 0;const a=u.current;a&&(l("💥 Destroy widget",()=>a.destroy()),u.current=null)},[]),f.useEffect(()=>{if(!h.current||JSON.stringify(d.current)===JSON.stringify(t))return;const a=h.current,i=u.current;d.current=t,i===null?l("Creating a new widget",()=>{u.current=_(a,{params:t,provider:o.current,listeners:r}),p.current=r}):l("Updating the widget",()=>i.updateParams(t))},[t]),f.useEffect(()=>{if(!u.current||o.current===n)return;o.current=n;const a=h.current;a&&l("Updating the provider",()=>{var i;(i=u.current)==null||i.destroy(),u.current=_(a,{params:t,provider:o.current,listeners:r})})},[n]),f.useEffect(()=>{if(!u.current||p.current===r)return;const a=u.current;l("Updating the listeners",()=>a.updateListeners(r))},[r]),s?R.jsxs("div",{style:{color:"#ff3a3a"},children:[s.message,s.error.message&&R.jsx("pre",{style:{whiteSpace:"pre-wrap",fontSize:"0.75em"},children:s.error.message})]}):R.jsx("div",{ref:h,style:{width:"100%"}})}exports.CowSwapWidget=ae;exports.TradeType=P;exports.WIDGET_PALETTE_COLORS=G;exports.WidgetMethodsEmit=w;exports.WidgetMethodsListen=T;exports.createCowSwapWidget=_;exports.isCowSwapWidgetPalette=b;exports.isPerNetworkConfig=C;exports.isPerTradeTypeConfig=v;exports.listenToMessageFromWindow=m;exports.postMessageToWindow=g;exports.resolveFlexibleConfig=ie;exports.stopListeningToMessageFromWindow=D;exports.stopListeningWindowListener=y;
|
package/index.mjs
CHANGED
|
@@ -1,30 +1,30 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
import { jsxs as
|
|
5
|
-
import { useState as
|
|
1
|
+
var U = Object.defineProperty;
|
|
2
|
+
var H = (e, t, n) => t in e ? U(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n;
|
|
3
|
+
var l = (e, t, n) => (H(e, typeof t != "symbol" ? t + "" : t, n), n);
|
|
4
|
+
import { jsxs as V, jsx as I } from "react/jsx-runtime";
|
|
5
|
+
import { useState as F, useRef as m, useCallback as k, useEffect as P } from "react";
|
|
6
6
|
class M {
|
|
7
7
|
constructor() {
|
|
8
|
-
|
|
8
|
+
l(this, "subscriptions", {});
|
|
9
9
|
}
|
|
10
|
-
on(
|
|
11
|
-
const { event:
|
|
12
|
-
this.subscriptions[
|
|
10
|
+
on(t) {
|
|
11
|
+
const { event: n, handler: r } = t;
|
|
12
|
+
this.subscriptions[n] || (this.subscriptions[n] = []), this.subscriptions[n].push(r);
|
|
13
13
|
}
|
|
14
|
-
off(
|
|
15
|
-
const { event:
|
|
16
|
-
this.subscriptions[
|
|
14
|
+
off(t) {
|
|
15
|
+
const { event: n, handler: r } = t;
|
|
16
|
+
this.subscriptions[n] && (this.subscriptions[n] = this.subscriptions[n].filter((s) => s !== r));
|
|
17
17
|
}
|
|
18
|
-
emit(
|
|
19
|
-
this.subscriptions[
|
|
18
|
+
emit(t, n) {
|
|
19
|
+
this.subscriptions[t] && this.subscriptions[t].forEach((r) => r(n));
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
|
-
const
|
|
23
|
-
function R(e,
|
|
22
|
+
const L = "cowSwapWidget";
|
|
23
|
+
function R(e, t, n) {
|
|
24
24
|
const s = {
|
|
25
|
-
key:
|
|
26
|
-
method:
|
|
27
|
-
...typeof
|
|
25
|
+
key: L,
|
|
26
|
+
method: t,
|
|
27
|
+
...typeof n == "object" ? n : {}
|
|
28
28
|
};
|
|
29
29
|
e.postMessage(
|
|
30
30
|
s,
|
|
@@ -32,23 +32,23 @@ function R(e, n, t) {
|
|
|
32
32
|
// TODO: Change to CoW specific origin in production. https://github.com/cowprotocol/cowswap/issues/3828
|
|
33
33
|
);
|
|
34
34
|
}
|
|
35
|
-
function w(e,
|
|
35
|
+
function w(e, t, n) {
|
|
36
36
|
const r = (s) => {
|
|
37
|
-
!
|
|
37
|
+
!G(s.data) || s.data.key !== L || s.data.method !== t || n(s.data);
|
|
38
38
|
};
|
|
39
39
|
return e.addEventListener("message", r), r;
|
|
40
40
|
}
|
|
41
|
-
function
|
|
41
|
+
function G(e) {
|
|
42
42
|
return typeof e == "object" && e !== null && "key" in e && "method" in e && typeof e.key == "string" && typeof e.method == "string";
|
|
43
43
|
}
|
|
44
|
-
function
|
|
45
|
-
e.removeEventListener("message", t);
|
|
46
|
-
}
|
|
47
|
-
function L(e, n) {
|
|
44
|
+
function x(e, t, n) {
|
|
48
45
|
e.removeEventListener("message", n);
|
|
49
46
|
}
|
|
50
|
-
|
|
51
|
-
|
|
47
|
+
function $(e, t) {
|
|
48
|
+
e.removeEventListener("message", t);
|
|
49
|
+
}
|
|
50
|
+
var E = /* @__PURE__ */ ((e) => (e.ACTIVATE = "ACTIVATE", 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))(E || {}), 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))(T || {}), g = /* @__PURE__ */ ((e) => (e.SWAP = "swap", e.LIMIT = "limit", e.ADVANCED = "advanced", e))(g || {});
|
|
51
|
+
const fe = [
|
|
52
52
|
"primary",
|
|
53
53
|
"background",
|
|
54
54
|
"paper",
|
|
@@ -59,108 +59,108 @@ const ae = [
|
|
|
59
59
|
"info",
|
|
60
60
|
"success"
|
|
61
61
|
];
|
|
62
|
-
class
|
|
63
|
-
constructor(
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
this.contentWindow =
|
|
62
|
+
class j {
|
|
63
|
+
constructor(t, n = []) {
|
|
64
|
+
l(this, "eventEmitter", new M());
|
|
65
|
+
l(this, "listeners", []);
|
|
66
|
+
l(this, "widgetListener");
|
|
67
|
+
this.contentWindow = t, this.updateListeners(n), this.widgetListener = w(
|
|
68
68
|
this.contentWindow,
|
|
69
69
|
E.EMIT_COW_EVENT,
|
|
70
70
|
(r) => this.eventEmitter.emit(r.event, r.payload)
|
|
71
71
|
);
|
|
72
72
|
}
|
|
73
73
|
stopListeningIframe() {
|
|
74
|
-
|
|
74
|
+
$(this.contentWindow, this.widgetListener);
|
|
75
75
|
}
|
|
76
|
-
updateListeners(
|
|
77
|
-
for (const
|
|
78
|
-
this.eventEmitter.off(
|
|
79
|
-
this.listeners =
|
|
80
|
-
for (const
|
|
81
|
-
this.eventEmitter.on(
|
|
76
|
+
updateListeners(t) {
|
|
77
|
+
for (const n of this.listeners)
|
|
78
|
+
this.eventEmitter.off(n);
|
|
79
|
+
this.listeners = t || [];
|
|
80
|
+
for (const n of this.listeners)
|
|
81
|
+
this.eventEmitter.on(n);
|
|
82
82
|
}
|
|
83
83
|
}
|
|
84
|
-
const
|
|
85
|
-
class
|
|
84
|
+
const Q = ["connect", "disconnect", "close", "chainChanged", "accountsChanged"];
|
|
85
|
+
class B {
|
|
86
86
|
/**
|
|
87
87
|
* Creates an instance of IframeRpcProviderBridge.
|
|
88
88
|
* @param iframeWidow - The iFrame window that will post up general RPC messages and to which the IframeRpcProviderBridge will forward the RPC result.
|
|
89
89
|
* Also it will receive some special RPC events coming from the wallet, like connect/chainChanged,accountChanged
|
|
90
90
|
*/
|
|
91
|
-
constructor(
|
|
91
|
+
constructor(t) {
|
|
92
92
|
/**
|
|
93
93
|
* The Ethereum provider instance.
|
|
94
94
|
* When is null the JSON-RPC bridge is disconnected from the Ethereum provider.
|
|
95
95
|
* */
|
|
96
|
-
|
|
96
|
+
l(this, "ethereumProvider", null);
|
|
97
97
|
/** Stored JSON-RPC requests, to queue them when disconnected. */
|
|
98
|
-
|
|
99
|
-
|
|
98
|
+
l(this, "requestWaitingForConnection", {});
|
|
99
|
+
l(this, "processRpcCallFromWindow", ({ rpcRequest: t }) => {
|
|
100
100
|
if (!this.ethereumProvider) {
|
|
101
|
-
|
|
101
|
+
t.id && (this.requestWaitingForConnection[t.id] = t);
|
|
102
102
|
return;
|
|
103
103
|
}
|
|
104
|
-
this.processRpcRequest(
|
|
104
|
+
this.processRpcRequest(t);
|
|
105
105
|
});
|
|
106
|
-
this.iframeWidow =
|
|
106
|
+
this.iframeWidow = t;
|
|
107
107
|
}
|
|
108
108
|
/**
|
|
109
109
|
* Disconnects the JSON-RPC bridge from the Ethereum provider.
|
|
110
110
|
*/
|
|
111
111
|
disconnect() {
|
|
112
|
-
this.ethereumProvider = null,
|
|
112
|
+
this.ethereumProvider = null, x(window, E.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow);
|
|
113
113
|
}
|
|
114
114
|
/**
|
|
115
115
|
* Handles the 'connect' event and sets up event listeners for Ethereum provider events.
|
|
116
116
|
* @param newProvider - The Ethereum provider to connect.
|
|
117
117
|
*/
|
|
118
|
-
onConnect(
|
|
119
|
-
this.ethereumProvider ? this.disconnect() : w(window, E.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow), this.ethereumProvider =
|
|
120
|
-
|
|
118
|
+
onConnect(t) {
|
|
119
|
+
this.ethereumProvider ? this.disconnect() : w(window, E.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow), this.ethereumProvider = t, this.processPendingRequests(), Q.forEach((n) => {
|
|
120
|
+
t.on(n, (r) => this.onProviderEvent(n, r));
|
|
121
121
|
});
|
|
122
122
|
}
|
|
123
123
|
processPendingRequests() {
|
|
124
|
-
Object.keys(this.requestWaitingForConnection).forEach((
|
|
125
|
-
this.processRpcRequest(this.requestWaitingForConnection[
|
|
124
|
+
Object.keys(this.requestWaitingForConnection).forEach((t) => {
|
|
125
|
+
this.processRpcRequest(this.requestWaitingForConnection[t]);
|
|
126
126
|
}), this.requestWaitingForConnection = {};
|
|
127
127
|
}
|
|
128
128
|
/**
|
|
129
129
|
* Processes a JSON-RPC request and sends appropriate response or error via the content window.
|
|
130
130
|
* @param request - The JSON-RPC request to be processed.
|
|
131
131
|
*/
|
|
132
|
-
processRpcRequest(
|
|
133
|
-
const { id:
|
|
134
|
-
if (!this.ethereumProvider || !
|
|
132
|
+
processRpcRequest(t) {
|
|
133
|
+
const { id: n, jsonrpc: r, method: s } = t;
|
|
134
|
+
if (!this.ethereumProvider || !n)
|
|
135
135
|
return;
|
|
136
|
-
(s === "enable" ? this.ethereumProvider.enable() : this.ethereumProvider.request({ ...
|
|
136
|
+
(s === "enable" ? this.ethereumProvider.enable() : this.ethereumProvider.request({ ...t, id: n })).then(
|
|
137
137
|
(d) => this.forwardRpcResponseToIframe({
|
|
138
|
-
rpcResponse: { jsonrpc: r, id:
|
|
138
|
+
rpcResponse: { jsonrpc: r, id: n, result: d }
|
|
139
139
|
})
|
|
140
140
|
).catch(
|
|
141
141
|
(d) => this.forwardRpcResponseToIframe({
|
|
142
|
-
rpcResponse: { jsonrpc: r, id:
|
|
142
|
+
rpcResponse: { jsonrpc: r, id: n, error: d }
|
|
143
143
|
})
|
|
144
144
|
);
|
|
145
145
|
}
|
|
146
|
-
onProviderEvent(
|
|
147
|
-
R(this.iframeWidow,
|
|
148
|
-
event:
|
|
149
|
-
params:
|
|
146
|
+
onProviderEvent(t, n) {
|
|
147
|
+
R(this.iframeWidow, T.PROVIDER_ON_EVENT, {
|
|
148
|
+
event: t,
|
|
149
|
+
params: n
|
|
150
150
|
});
|
|
151
151
|
}
|
|
152
152
|
/**
|
|
153
153
|
* Forward a JSON-RPC message to the content window.
|
|
154
154
|
*/
|
|
155
|
-
forwardRpcResponseToIframe(
|
|
156
|
-
R(this.iframeWidow,
|
|
155
|
+
forwardRpcResponseToIframe(t) {
|
|
156
|
+
R(this.iframeWidow, T.PROVIDER_RPC_RESPONSE, t);
|
|
157
157
|
}
|
|
158
158
|
}
|
|
159
|
-
class
|
|
160
|
-
constructor(
|
|
161
|
-
|
|
162
|
-
this.appWindow =
|
|
163
|
-
|
|
159
|
+
class q {
|
|
160
|
+
constructor(t, n) {
|
|
161
|
+
l(this, "forwardSdkMessage");
|
|
162
|
+
this.appWindow = t, this.iframeWidow = n, this.forwardSdkMessage = (r) => {
|
|
163
|
+
J(r.data) && r.origin !== window.location.origin && (z(r.data) ? this.appWindow.parent.postMessage(r.data, "*") : K(r.data) && this.iframeWidow.postMessage(r.data, "*"));
|
|
164
164
|
}, this.startListening();
|
|
165
165
|
}
|
|
166
166
|
startListening() {
|
|
@@ -170,79 +170,79 @@ class B {
|
|
|
170
170
|
this.appWindow.removeEventListener("message", this.forwardSdkMessage);
|
|
171
171
|
}
|
|
172
172
|
}
|
|
173
|
-
function
|
|
173
|
+
function J(e) {
|
|
174
174
|
return typeof e == "object" && e !== null && "id" in e && typeof e.id == "string";
|
|
175
175
|
}
|
|
176
|
-
function
|
|
176
|
+
function z(e) {
|
|
177
177
|
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;
|
|
178
178
|
}
|
|
179
|
-
function j(e) {
|
|
180
|
-
return "success" in e && typeof e.success == "boolean" && "version" in e && typeof e.version == "string";
|
|
181
|
-
}
|
|
182
179
|
function K(e) {
|
|
183
|
-
return
|
|
180
|
+
return "success" in e && typeof e.success == "boolean" && "version" in e && typeof e.version == "string";
|
|
184
181
|
}
|
|
185
|
-
const A = "_";
|
|
186
182
|
function Y(e) {
|
|
187
|
-
|
|
188
|
-
return n + "/#" + t + "?" + y(e);
|
|
183
|
+
return !!(e && typeof e == "object");
|
|
189
184
|
}
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
185
|
+
const C = "_";
|
|
186
|
+
function X(e) {
|
|
187
|
+
const t = typeof e.baseUrl == "string" ? e.baseUrl : "https://swap.cow.fi", n = y(e);
|
|
188
|
+
return t + "/#" + n + "?" + N(e);
|
|
193
189
|
}
|
|
194
190
|
function y(e) {
|
|
195
|
-
const n =
|
|
196
|
-
return
|
|
191
|
+
const { chainId: t = 1, sell: n, buy: r, tradeType: s = g.SWAP } = e, c = [(n == null ? void 0 : n.asset) || C, (r == null ? void 0 : r.asset) || C].map(encodeURIComponent).join("/");
|
|
192
|
+
return `/${t}/widget/${s}/${c}`;
|
|
193
|
+
}
|
|
194
|
+
function N(e) {
|
|
195
|
+
const t = new URLSearchParams();
|
|
196
|
+
return ee(Z(t, e), e);
|
|
197
197
|
}
|
|
198
|
-
function
|
|
199
|
-
const { sell:
|
|
200
|
-
return
|
|
198
|
+
function Z(e, t) {
|
|
199
|
+
const { sell: n, buy: r } = t;
|
|
200
|
+
return n != null && n.amount && e.append("sellAmount", n.amount), r != null && r.amount && e.append("buyAmount", r.amount), e;
|
|
201
201
|
}
|
|
202
|
-
function
|
|
203
|
-
const
|
|
204
|
-
return
|
|
202
|
+
function ee(e, t) {
|
|
203
|
+
const n = t.theme;
|
|
204
|
+
return n ? (Y(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);
|
|
205
205
|
}
|
|
206
|
-
const
|
|
207
|
-
function
|
|
208
|
-
const { params:
|
|
209
|
-
let c = r, d =
|
|
210
|
-
const o =
|
|
206
|
+
const b = "640px", te = "450px", ne = 20;
|
|
207
|
+
function A(e, t) {
|
|
208
|
+
const { params: n, provider: r, listeners: s } = t;
|
|
209
|
+
let c = r, d = n;
|
|
210
|
+
const o = re(n);
|
|
211
211
|
e.innerHTML = "", e.appendChild(o);
|
|
212
212
|
const { contentWindow: p } = o;
|
|
213
213
|
if (!p)
|
|
214
214
|
throw console.error("Iframe does not contain a window", o), new Error("Iframe does not contain a window!");
|
|
215
|
-
const
|
|
216
|
-
|
|
217
|
-
const u = new
|
|
215
|
+
const f = [];
|
|
216
|
+
f.push(se(p, n.appCode)), f.push(...oe(o, n.height)), f.push(ie());
|
|
217
|
+
const u = new j(window, s);
|
|
218
218
|
let h = S(p, null, c);
|
|
219
|
-
o.addEventListener("load", () =>
|
|
220
|
-
const a = new
|
|
219
|
+
o.addEventListener("load", () => W(p, d, c));
|
|
220
|
+
const a = new q(window, p);
|
|
221
221
|
return {
|
|
222
222
|
updateParams: (i) => {
|
|
223
|
-
d = i,
|
|
223
|
+
d = i, W(p, d, c);
|
|
224
224
|
},
|
|
225
225
|
updateListeners: (i) => u.updateListeners(i),
|
|
226
226
|
updateProvider: (i) => {
|
|
227
227
|
c = i, h = S(p, h, i);
|
|
228
228
|
},
|
|
229
229
|
destroy: () => {
|
|
230
|
-
h.disconnect(), u.stopListeningIframe(),
|
|
230
|
+
h.disconnect(), u.stopListeningIframe(), f.forEach((i) => window.removeEventListener("message", i)), a.stopListening(), e.removeChild(o);
|
|
231
231
|
}
|
|
232
232
|
};
|
|
233
233
|
}
|
|
234
|
-
function S(e,
|
|
235
|
-
|
|
236
|
-
const r =
|
|
237
|
-
return
|
|
234
|
+
function S(e, t, n) {
|
|
235
|
+
t && t.disconnect();
|
|
236
|
+
const r = t || new B(e);
|
|
237
|
+
return n && r.onConnect(n), r;
|
|
238
238
|
}
|
|
239
|
-
function
|
|
240
|
-
const { width:
|
|
241
|
-
return r.src =
|
|
239
|
+
function re(e) {
|
|
240
|
+
const { width: t = te, height: n = b } = e, r = document.createElement("iframe");
|
|
241
|
+
return r.src = X(e), r.width = t, r.height = n, r.style.border = "0", r.allow = "clipboard-read; clipboard-write", r;
|
|
242
242
|
}
|
|
243
|
-
function
|
|
244
|
-
const r = !!
|
|
245
|
-
R(e,
|
|
243
|
+
function W(e, t, n) {
|
|
244
|
+
const r = !!n, s = y(t), c = N(t).toString(), { theme: d, ...o } = t;
|
|
245
|
+
R(e, T.UPDATE_PARAMS, {
|
|
246
246
|
urlParams: {
|
|
247
247
|
pathname: s,
|
|
248
248
|
search: c
|
|
@@ -251,32 +251,59 @@ function C(e, n, t) {
|
|
|
251
251
|
hasProvider: r
|
|
252
252
|
});
|
|
253
253
|
}
|
|
254
|
-
function
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
metaData:
|
|
254
|
+
function se(e, t) {
|
|
255
|
+
return w(window, E.ACTIVATE, () => {
|
|
256
|
+
R(e, T.UPDATE_APP_DATA, {
|
|
257
|
+
metaData: t ? { appCode: t } : void 0
|
|
258
258
|
});
|
|
259
259
|
});
|
|
260
|
-
return t;
|
|
261
260
|
}
|
|
262
|
-
function
|
|
261
|
+
function ie() {
|
|
262
|
+
return w(window, E.INTERCEPT_WINDOW_OPEN, ({ href: e, rel: t, target: n }) => {
|
|
263
|
+
const r = e.toString();
|
|
264
|
+
if (!r.startsWith("http") && r.match(/^[a-zA-Z0-9]+:\/\//)) {
|
|
265
|
+
window.open(r, n, t);
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
function oe(e, t = b) {
|
|
263
271
|
return [
|
|
264
|
-
w(window, E.UPDATE_HEIGHT, (
|
|
265
|
-
e.style.height =
|
|
272
|
+
w(window, E.UPDATE_HEIGHT, (n) => {
|
|
273
|
+
e.style.height = n.height ? `${n.height + ne}px` : t;
|
|
266
274
|
}),
|
|
267
|
-
w(window, E.SET_FULL_HEIGHT, ({ isUpToSmall:
|
|
268
|
-
e.style.height =
|
|
275
|
+
w(window, E.SET_FULL_HEIGHT, ({ isUpToSmall: n }) => {
|
|
276
|
+
e.style.height = n ? t : `${document.body.offsetHeight}px`;
|
|
269
277
|
})
|
|
270
278
|
];
|
|
271
279
|
}
|
|
272
|
-
|
|
273
|
-
|
|
280
|
+
const ae = Object.values(g);
|
|
281
|
+
function he(e, t, n) {
|
|
282
|
+
if (O(e)) {
|
|
283
|
+
const r = e[n];
|
|
284
|
+
return D(r) ? r[t] : r;
|
|
285
|
+
}
|
|
286
|
+
if (D(e)) {
|
|
287
|
+
const r = e[t];
|
|
288
|
+
return O(r) ? r[n] : r;
|
|
289
|
+
}
|
|
290
|
+
return e;
|
|
291
|
+
}
|
|
292
|
+
function O(e) {
|
|
293
|
+
return typeof e != "object" ? !1 : Object.keys(e).every((t) => ae.includes(t));
|
|
294
|
+
}
|
|
295
|
+
const ce = /^\d+$/;
|
|
296
|
+
function D(e) {
|
|
297
|
+
return typeof e != "object" ? !1 : Object.keys(e).every((t) => typeof t == "number" || ce.test(t));
|
|
298
|
+
}
|
|
299
|
+
function le(e) {
|
|
300
|
+
const { params: t, provider: n, listeners: r } = e, [s, c] = F(null), d = m(null), o = m(n), p = m(r), f = m(null), u = m(null), h = k(
|
|
274
301
|
(a, i) => {
|
|
275
302
|
try {
|
|
276
303
|
console.log(`[WIDGET] ${a}`), i();
|
|
277
|
-
} catch (
|
|
278
|
-
const
|
|
279
|
-
console.error(`[WIDGET] ${
|
|
304
|
+
} catch (_) {
|
|
305
|
+
const v = `Error ${a.toLowerCase()}`;
|
|
306
|
+
console.error(`[WIDGET] ${v}`, _), c({ message: v, error: _ });
|
|
280
307
|
}
|
|
281
308
|
},
|
|
282
309
|
[c]
|
|
@@ -286,41 +313,44 @@ function ce(e) {
|
|
|
286
313
|
const a = u.current;
|
|
287
314
|
a && (h("💥 Destroy widget", () => a.destroy()), u.current = null);
|
|
288
315
|
}, []), P(() => {
|
|
289
|
-
if (!
|
|
316
|
+
if (!f.current || JSON.stringify(d.current) === JSON.stringify(t))
|
|
290
317
|
return;
|
|
291
|
-
const a =
|
|
292
|
-
d.current =
|
|
293
|
-
u.current =
|
|
294
|
-
}) : h("Updating the widget", () => i.updateParams(
|
|
295
|
-
}, [
|
|
296
|
-
if (!u.current || o.current ===
|
|
318
|
+
const a = f.current, i = u.current;
|
|
319
|
+
d.current = t, i === null ? h("Creating a new widget", () => {
|
|
320
|
+
u.current = A(a, { params: t, provider: o.current, listeners: r }), p.current = r;
|
|
321
|
+
}) : h("Updating the widget", () => i.updateParams(t));
|
|
322
|
+
}, [t]), P(() => {
|
|
323
|
+
if (!u.current || o.current === n)
|
|
297
324
|
return;
|
|
298
|
-
o.current =
|
|
299
|
-
const a =
|
|
325
|
+
o.current = n;
|
|
326
|
+
const a = f.current;
|
|
300
327
|
a && h("Updating the provider", () => {
|
|
301
328
|
var i;
|
|
302
|
-
(i = u.current) == null || i.destroy(), u.current =
|
|
329
|
+
(i = u.current) == null || i.destroy(), u.current = A(a, { params: t, provider: o.current, listeners: r });
|
|
303
330
|
});
|
|
304
|
-
}, [
|
|
331
|
+
}, [n]), P(() => {
|
|
305
332
|
if (!u.current || p.current === r)
|
|
306
333
|
return;
|
|
307
334
|
const a = u.current;
|
|
308
335
|
h("Updating the listeners", () => a.updateListeners(r));
|
|
309
|
-
}, [r]), s ? /* @__PURE__ */
|
|
336
|
+
}, [r]), s ? /* @__PURE__ */ V("div", { style: { color: "#ff3a3a" }, children: [
|
|
310
337
|
s.message,
|
|
311
|
-
s.error.message && /* @__PURE__ */
|
|
312
|
-
] }) : /* @__PURE__ */
|
|
338
|
+
s.error.message && /* @__PURE__ */ I("pre", { style: { whiteSpace: "pre-wrap", fontSize: "0.75em" }, children: s.error.message })
|
|
339
|
+
] }) : /* @__PURE__ */ I("div", { ref: f, style: { width: "100%" } });
|
|
313
340
|
}
|
|
314
341
|
export {
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
342
|
+
le as CowSwapWidget,
|
|
343
|
+
g as TradeType,
|
|
344
|
+
fe as WIDGET_PALETTE_COLORS,
|
|
318
345
|
E as WidgetMethodsEmit,
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
346
|
+
T as WidgetMethodsListen,
|
|
347
|
+
A as createCowSwapWidget,
|
|
348
|
+
Y as isCowSwapWidgetPalette,
|
|
349
|
+
D as isPerNetworkConfig,
|
|
350
|
+
O as isPerTradeTypeConfig,
|
|
322
351
|
w as listenToMessageFromWindow,
|
|
323
352
|
R as postMessageToWindow,
|
|
324
|
-
|
|
325
|
-
|
|
353
|
+
he as resolveFlexibleConfig,
|
|
354
|
+
x as stopListeningToMessageFromWindow,
|
|
355
|
+
$ as stopListeningWindowListener
|
|
326
356
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cowprotocol/widget-react",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.3",
|
|
4
4
|
"type": "commonjs",
|
|
5
5
|
"description": "CoW Swap Widget Library. Allows you to easily embed a CoW Swap widget on your React application.",
|
|
6
6
|
"main": "index.js",
|
|
@@ -22,6 +22,6 @@
|
|
|
22
22
|
"react"
|
|
23
23
|
],
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@cowprotocol/widget-lib": "^0.
|
|
25
|
+
"@cowprotocol/widget-lib": "^0.13.2"
|
|
26
26
|
}
|
|
27
27
|
}
|