@cowprotocol/widget-react 0.7.0 → 0.8.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/index.js +1 -1
- package/index.mjs +19 -19
- package/package.json +2 -2
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var V=Object.defineProperty;var b=(e,t,n)=>t in e?V(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n;var h=(e,t,n)=>(b(e,typeof t!="symbol"?t+"":t,n),n);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const P=require("react/jsx-runtime"),f=require("react"),L="cowSwapWidget";function g(e,t,n){const r={key:L,method:t,...typeof n=="object"?n:{}};e.postMessage(r,"*")}function R(e,t,n){const s=r=>{!N(r.data)||r.data.key!==L||r.data.method!==t||n(r.data)};return e.addEventListener("message",s),s}function N(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 _(e,t){e.removeEventListener("message",t)}var w=(e=>(e.ACTIVATE="ACTIVATE",e.UPDATE_HEIGHT="UPDATE_HEIGHT",e.EMIT_COW_EVENT="EMIT_COW_EVENT",e.PROVIDER_RPC_REQUEST="PROVIDER_RPC_REQUEST",e))(w||{}),m=(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))(m||{}),W=(e=>(e.SWAP="swap",e.LIMIT="limit",e.ADVANCED="advanced",e))(W||{});const F=["primary","background","paper","text","danger","warning","alert","info","success"],H=["connect","disconnect","close","chainChanged","accountsChanged"];class k{constructor(t){h(this,"ethereumProvider",null);h(this,"requestWaitingForConnection",{});h(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():R(window,w.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),this.ethereumProvider=t,this.processPendingRequests(),H.forEach(n=>{t.on(n,s=>this.onProviderEvent(n,s))})}processPendingRequests(){Object.keys(this.requestWaitingForConnection).forEach(t=>{this.processRpcRequest(this.requestWaitingForConnection[t])}),this.requestWaitingForConnection={}}processRpcRequest(t){const{id:n,jsonrpc:s,method:r}=t;if(!this.ethereumProvider||!n)return;(r==="enable"?this.ethereumProvider.enable():this.ethereumProvider.request({...t,id:n})).then(c=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:s,id:n,result:c}})).catch(c=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:s,id:n,error:c}}))}onProviderEvent(t,n){g(this.iframeWidow,m.PROVIDER_ON_EVENT,{event:t,params:n})}forwardRpcResponseToIframe(t){g(this.iframeWidow,m.PROVIDER_RPC_RESPONSE,t)}}function O(e){return!!(e&&typeof e=="object")}const A="_";function G(e){const t=typeof e.baseUrl=="string"?e.baseUrl:"https://swap.cow.fi",n=y(e);return t+"/#"+n+"?"+U(e)}function y(e){const{chainId:t=1,sell:n,buy:s,tradeType:r=W.SWAP}=e,a=[(n==null?void 0:n.asset)||A,(s==null?void 0:s.asset)||A].map(encodeURIComponent).join("/");return`/${t}/widget/${r}/${a}`}function U(e){const t=new URLSearchParams;return
|
|
1
|
+
"use strict";var V=Object.defineProperty;var b=(e,t,n)=>t in e?V(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n;var h=(e,t,n)=>(b(e,typeof t!="symbol"?t+"":t,n),n);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const P=require("react/jsx-runtime"),f=require("react"),L="cowSwapWidget";function g(e,t,n){const r={key:L,method:t,...typeof n=="object"?n:{}};e.postMessage(r,"*")}function R(e,t,n){const s=r=>{!N(r.data)||r.data.key!==L||r.data.method!==t||n(r.data)};return e.addEventListener("message",s),s}function N(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 _(e,t){e.removeEventListener("message",t)}var w=(e=>(e.ACTIVATE="ACTIVATE",e.UPDATE_HEIGHT="UPDATE_HEIGHT",e.EMIT_COW_EVENT="EMIT_COW_EVENT",e.PROVIDER_RPC_REQUEST="PROVIDER_RPC_REQUEST",e))(w||{}),m=(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))(m||{}),W=(e=>(e.SWAP="swap",e.LIMIT="limit",e.ADVANCED="advanced",e))(W||{});const F=["primary","background","paper","text","danger","warning","alert","info","success"],H=["connect","disconnect","close","chainChanged","accountsChanged"];class k{constructor(t){h(this,"ethereumProvider",null);h(this,"requestWaitingForConnection",{});h(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():R(window,w.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),this.ethereumProvider=t,this.processPendingRequests(),H.forEach(n=>{t.on(n,s=>this.onProviderEvent(n,s))})}processPendingRequests(){Object.keys(this.requestWaitingForConnection).forEach(t=>{this.processRpcRequest(this.requestWaitingForConnection[t])}),this.requestWaitingForConnection={}}processRpcRequest(t){const{id:n,jsonrpc:s,method:r}=t;if(!this.ethereumProvider||!n)return;(r==="enable"?this.ethereumProvider.enable():this.ethereumProvider.request({...t,id:n})).then(c=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:s,id:n,result:c}})).catch(c=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:s,id:n,error:c}}))}onProviderEvent(t,n){g(this.iframeWidow,m.PROVIDER_ON_EVENT,{event:t,params:n})}forwardRpcResponseToIframe(t){g(this.iframeWidow,m.PROVIDER_RPC_RESPONSE,t)}}function O(e){return!!(e&&typeof e=="object")}const A="_";function G(e){const t=typeof e.baseUrl=="string"?e.baseUrl:"https://swap.cow.fi",n=y(e);return t+"/#"+n+"?"+U(e)}function y(e){const{chainId:t=1,sell:n,buy:s,tradeType:r=W.SWAP}=e,a=[(n==null?void 0:n.asset)||A,(s==null?void 0:s.asset)||A].map(encodeURIComponent).join("/");return`/${t}/widget/${r}/${a}`}function U(e){const t=new URLSearchParams;return Q(x(t,e),e)}function x(e,t){const{sell:n,buy:s}=t;return n!=null&&n.amount&&e.append("sellAmount",n.amount),s!=null&&s.amount&&e.append("buyAmount",s.amount),e}function Q(e,t){const n=t.theme;return n?(O(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)}class ${constructor(){h(this,"subscriptions",{})}on(t){const{event:n,handler:s}=t;this.subscriptions[n]||(this.subscriptions[n]=[]),this.subscriptions[n].push(s)}off(t){const{event:n,handler:s}=t;this.subscriptions[n]&&(this.subscriptions[n]=this.subscriptions[n].filter(r=>r!==s))}emit(t,n){this.subscriptions[t]&&this.subscriptions[t].forEach(s=>s(n))}}class j{constructor(t,n=[]){h(this,"eventEmitter",new $);h(this,"listeners",[]);h(this,"widgetListener");this.contentWindow=t,this.updateListeners(n),this.widgetListener=R(this.contentWindow,w.EMIT_COW_EVENT,s=>this.eventEmitter.emit(s.event,s.payload))}stopListeningIframe(){_(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 q{constructor(t,n){h(this,"forwardSdkMessage");this.appWindow=t,this.iframeWidow=n,this.forwardSdkMessage=s=>{B(s.data)&&(J(s.data)?this.appWindow.parent.postMessage(s.data,"*"):K(s.data)&&this.iframeWidow.postMessage(s.data,"*"))},this.startListening()}startListening(){this.appWindow.addEventListener("message",this.forwardSdkMessage)}stopListening(){this.appWindow.removeEventListener("message",this.forwardSdkMessage)}}function B(e){return typeof e=="object"&&e!==null&&"id"in e&&typeof e.id=="string"}function J(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 K(e){return"success"in e&&typeof e.success=="boolean"&&"version"in e&&typeof e.version=="string"}const M="640px",Y="450px",z=20;function T(e,t){const{params:n,provider:s,listeners:r}=t;let a=s,c=n;const o=X(n);e.innerHTML="",e.appendChild(o);const{contentWindow:u}=o;if(!u)throw console.error("Iframe does not contain a window",o),new Error("Iframe does not contain a window!");const l=[];l.push(Z(u,n.appCode)),l.push(ee(o,n.height));const p=new j(window,r);let E=C(u,null,a);o.addEventListener("load",()=>I(u,c,a));const d=new q(window,u);return{updateParams:i=>{c=i,I(u,c,a)},updateListeners:i=>p.updateListeners(i),updateProvider:i=>{a=i,E=C(u,E,i)},destroy:()=>{E.disconnect(),p.stopListeningIframe(),l.forEach(i=>window.removeEventListener("message",i)),d.stopListening(),e.removeChild(o)}}}function C(e,t,n){t&&t.disconnect();const s=t||new k(e);return n&&s.onConnect(n),s}function X(e){const{width:t=Y,height:n=M}=e,s=document.createElement("iframe");return s.src=G(e),s.width=t,s.height=n,s.style.border="0",s}function I(e,t,n){const s=!!n,r=y(t),a=U(t).toString(),{theme:c,...o}=t;g(e,m.UPDATE_PARAMS,{urlParams:{pathname:r,search:a},appParams:o,hasProvider:s})}function Z(e,t){const n=R(window,w.ACTIVATE,()=>{_(window,n),g(e,m.UPDATE_APP_DATA,{metaData:t?{appCode:t}:void 0})});return n}function ee(e,t=M){return R(window,w.UPDATE_HEIGHT,n=>{e.style.height=n.height?`${n.height+z}px`:t})}function te(e){const{params:t,provider:n,listeners:s}=e,[r,a]=f.useState(null),c=f.useRef(null),o=f.useRef(n),u=f.useRef(s),l=f.useRef(null),p=f.useRef(null),E=f.useCallback((d,i)=>{try{console.log(`[WIDGET] ${d}`),i()}catch(v){const S=`Error ${d.toLowerCase()}`;console.error(`[WIDGET] ${S}`,v),a({message:S,error:v})}},[a]);return f.useEffect(()=>{if(!l.current||JSON.stringify(c.current)===JSON.stringify(t))return;const d=l.current,i=p.current;c.current=t,i===null?E("Creating a new widget",()=>{p.current=T(d,{params:t,provider:o.current,listeners:s}),u.current=s}):E("Updating the widget",()=>i.updateParams(t))},[t]),f.useEffect(()=>{if(!p.current||o.current===n)return;o.current=n;const d=l.current;d&&E("Updating the provider",()=>{var i;(i=p.current)==null||i.destroy(),p.current=T(d,{params:t,provider:o.current,listeners:s})})},[n]),f.useEffect(()=>{if(!p.current||u.current===s)return;const d=p.current;E("Updating the listeners",()=>d.updateListeners(s))},[s]),r?P.jsxs("div",{style:{color:"#ff3a3a"},children:[r.message,r.error.message&&P.jsx("pre",{style:{whiteSpace:"pre-wrap",fontSize:"0.75em"},children:r.error.message})]}):P.jsx("div",{ref:l,style:{width:"100%"}})}exports.CowSwapWidget=te;exports.TradeType=W;exports.WIDGET_PALETTE_COLORS=F;exports.WidgetMethodsEmit=w;exports.WidgetMethodsListen=m;exports.createCowSwapWidget=T;exports.isCowSwapWidgetPalette=O;exports.listenToMessageFromWindow=R;exports.postMessageToWindow=g;exports.stopListeningToMessageFromWindow=D;exports.stopListeningWindowListener=_;
|
package/index.mjs
CHANGED
|
@@ -31,7 +31,7 @@ function k(e, t, n) {
|
|
|
31
31
|
function D(e, t) {
|
|
32
32
|
e.removeEventListener("message", t);
|
|
33
33
|
}
|
|
34
|
-
var
|
|
34
|
+
var E = /* @__PURE__ */ ((e) => (e.ACTIVATE = "ACTIVATE", e.UPDATE_HEIGHT = "UPDATE_HEIGHT", e.EMIT_COW_EVENT = "EMIT_COW_EVENT", e.PROVIDER_RPC_REQUEST = "PROVIDER_RPC_REQUEST", e))(E || {}), w = /* @__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))(w || {}), O = /* @__PURE__ */ ((e) => (e.SWAP = "swap", e.LIMIT = "limit", e.ADVANCED = "advanced", e))(O || {});
|
|
35
35
|
const ae = [
|
|
36
36
|
"primary",
|
|
37
37
|
"background",
|
|
@@ -70,14 +70,14 @@ class x {
|
|
|
70
70
|
* Disconnects the JSON-RPC bridge from the Ethereum provider.
|
|
71
71
|
*/
|
|
72
72
|
disconnect() {
|
|
73
|
-
this.ethereumProvider = null, k(window,
|
|
73
|
+
this.ethereumProvider = null, k(window, E.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow);
|
|
74
74
|
}
|
|
75
75
|
/**
|
|
76
76
|
* Handles the 'connect' event and sets up event listeners for Ethereum provider events.
|
|
77
77
|
* @param newProvider - The Ethereum provider to connect.
|
|
78
78
|
*/
|
|
79
79
|
onConnect(t) {
|
|
80
|
-
this.ethereumProvider ? this.disconnect() : P(window,
|
|
80
|
+
this.ethereumProvider ? this.disconnect() : P(window, E.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow), this.ethereumProvider = t, this.processPendingRequests(), G.forEach((n) => {
|
|
81
81
|
t.on(n, (r) => this.onProviderEvent(n, r));
|
|
82
82
|
});
|
|
83
83
|
}
|
|
@@ -131,15 +131,15 @@ function L(e) {
|
|
|
131
131
|
}
|
|
132
132
|
function y(e) {
|
|
133
133
|
const t = new URLSearchParams();
|
|
134
|
-
return B(
|
|
134
|
+
return q(B(t, e), e);
|
|
135
135
|
}
|
|
136
|
-
function
|
|
136
|
+
function B(e, t) {
|
|
137
137
|
const { sell: n, buy: r } = t;
|
|
138
138
|
return n != null && n.amount && e.append("sellAmount", n.amount), r != null && r.amount && e.append("buyAmount", r.amount), e;
|
|
139
139
|
}
|
|
140
|
-
function
|
|
140
|
+
function q(e, t) {
|
|
141
141
|
const n = t.theme;
|
|
142
|
-
return n
|
|
142
|
+
return n ? (Q(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);
|
|
143
143
|
}
|
|
144
144
|
class J {
|
|
145
145
|
constructor() {
|
|
@@ -164,7 +164,7 @@ class j {
|
|
|
164
164
|
h(this, "widgetListener");
|
|
165
165
|
this.contentWindow = t, this.updateListeners(n), this.widgetListener = P(
|
|
166
166
|
this.contentWindow,
|
|
167
|
-
|
|
167
|
+
E.EMIT_COW_EVENT,
|
|
168
168
|
(r) => this.eventEmitter.emit(r.event, r.payload)
|
|
169
169
|
);
|
|
170
170
|
}
|
|
@@ -214,7 +214,7 @@ function C(e, t) {
|
|
|
214
214
|
const f = [];
|
|
215
215
|
f.push(ne(u, n.appCode)), f.push(re(o, n.height));
|
|
216
216
|
const p = new j(window, s);
|
|
217
|
-
let
|
|
217
|
+
let l = I(u, null, a);
|
|
218
218
|
o.addEventListener("load", () => S(u, c, a));
|
|
219
219
|
const d = new K(window, u);
|
|
220
220
|
return {
|
|
@@ -223,10 +223,10 @@ function C(e, t) {
|
|
|
223
223
|
},
|
|
224
224
|
updateListeners: (i) => p.updateListeners(i),
|
|
225
225
|
updateProvider: (i) => {
|
|
226
|
-
a = i,
|
|
226
|
+
a = i, l = I(u, l, i);
|
|
227
227
|
},
|
|
228
228
|
destroy: () => {
|
|
229
|
-
|
|
229
|
+
l.disconnect(), p.stopListeningIframe(), f.forEach((i) => window.removeEventListener("message", i)), d.stopListening(), e.removeChild(o);
|
|
230
230
|
}
|
|
231
231
|
};
|
|
232
232
|
}
|
|
@@ -251,7 +251,7 @@ function S(e, t, n) {
|
|
|
251
251
|
});
|
|
252
252
|
}
|
|
253
253
|
function ne(e, t) {
|
|
254
|
-
const n = P(window,
|
|
254
|
+
const n = P(window, E.ACTIVATE, () => {
|
|
255
255
|
D(window, n), g(e, w.UPDATE_APP_DATA, {
|
|
256
256
|
metaData: t ? { appCode: t } : void 0
|
|
257
257
|
});
|
|
@@ -259,12 +259,12 @@ function ne(e, t) {
|
|
|
259
259
|
return n;
|
|
260
260
|
}
|
|
261
261
|
function re(e, t = U) {
|
|
262
|
-
return P(window,
|
|
262
|
+
return P(window, E.UPDATE_HEIGHT, (n) => {
|
|
263
263
|
e.style.height = n.height ? `${n.height + ee}px` : t;
|
|
264
264
|
});
|
|
265
265
|
}
|
|
266
266
|
function ce(e) {
|
|
267
|
-
const { params: t, provider: n, listeners: r } = e, [s, a] = M(null), c = m(null), o = m(n), u = m(r), f = m(null), p = m(null),
|
|
267
|
+
const { params: t, provider: n, listeners: r } = e, [s, a] = M(null), c = m(null), o = m(n), u = m(r), f = m(null), p = m(null), l = H(
|
|
268
268
|
(d, i) => {
|
|
269
269
|
try {
|
|
270
270
|
console.log(`[WIDGET] ${d}`), i();
|
|
@@ -279,15 +279,15 @@ function ce(e) {
|
|
|
279
279
|
if (!f.current || JSON.stringify(c.current) === JSON.stringify(t))
|
|
280
280
|
return;
|
|
281
281
|
const d = f.current, i = p.current;
|
|
282
|
-
c.current = t, i === null ?
|
|
282
|
+
c.current = t, i === null ? l("Creating a new widget", () => {
|
|
283
283
|
p.current = C(d, { params: t, provider: o.current, listeners: r }), u.current = r;
|
|
284
|
-
}) :
|
|
284
|
+
}) : l("Updating the widget", () => i.updateParams(t));
|
|
285
285
|
}, [t]), R(() => {
|
|
286
286
|
if (!p.current || o.current === n)
|
|
287
287
|
return;
|
|
288
288
|
o.current = n;
|
|
289
289
|
const d = f.current;
|
|
290
|
-
d &&
|
|
290
|
+
d && l("Updating the provider", () => {
|
|
291
291
|
var i;
|
|
292
292
|
(i = p.current) == null || i.destroy(), p.current = C(d, { params: t, provider: o.current, listeners: r });
|
|
293
293
|
});
|
|
@@ -295,7 +295,7 @@ function ce(e) {
|
|
|
295
295
|
if (!p.current || u.current === r)
|
|
296
296
|
return;
|
|
297
297
|
const d = p.current;
|
|
298
|
-
|
|
298
|
+
l("Updating the listeners", () => d.updateListeners(r));
|
|
299
299
|
}, [r]), s ? /* @__PURE__ */ b("div", { style: { color: "#ff3a3a" }, children: [
|
|
300
300
|
s.message,
|
|
301
301
|
s.error.message && /* @__PURE__ */ v("pre", { style: { whiteSpace: "pre-wrap", fontSize: "0.75em" }, children: s.error.message })
|
|
@@ -305,7 +305,7 @@ export {
|
|
|
305
305
|
ce as CowSwapWidget,
|
|
306
306
|
O as TradeType,
|
|
307
307
|
ae as WIDGET_PALETTE_COLORS,
|
|
308
|
-
|
|
308
|
+
E as WidgetMethodsEmit,
|
|
309
309
|
w as WidgetMethodsListen,
|
|
310
310
|
C as createCowSwapWidget,
|
|
311
311
|
Q as isCowSwapWidgetPalette,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cowprotocol/widget-react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
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.10.0"
|
|
26
26
|
}
|
|
27
27
|
}
|