@cowprotocol/widget-react 0.5.0-RC.3 → 0.5.0-RC.4
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 +11 -10
- package/package.json +2 -2
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var b=Object.defineProperty;var N=(n,e,t)=>e in n?b(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t;var h=(n,e,t)=>(N(n,typeof e!="symbol"?e+"":e,t),t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const P=require("react/jsx-runtime"),u=require("react"),D="cowSwapWidget";function R(n,e,t){const s={key:D,method:e,...typeof t=="object"?t:{}};n.postMessage(s,"*")}function g(n,e,t){const r=s=>{!H(s.data)||s.data.key!==D||s.data.method!==e||t(s.data)};return n.addEventListener("message",r),r}function H(n){return typeof n=="object"&&n!==null&&"key"in n&&"method"in n&&typeof n.key=="string"&&typeof n.method=="string"}function O(n,e,t){n.removeEventListener("message",t)}function _(n,e){n.removeEventListener("message",e)}var l=(n=>(n.ACTIVATE="ACTIVATE",n.UPDATE_HEIGHT="UPDATE_HEIGHT",n.EMIT_COW_EVENT="EMIT_COW_EVENT",n.PROVIDER_RPC_REQUEST="PROVIDER_RPC_REQUEST",n))(l||{}),m=(n=>(n.UPDATE_PARAMS="UPDATE_PARAMS",n.UPDATE_APP_DATA="UPDATE_APP_DATA",n.PROVIDER_RPC_RESPONSE="PROVIDER_RPC_RESPONSE",n.PROVIDER_ON_EVENT="PROVIDER_ON_EVENT",n))(m||{}),v=(n=>(n.SWAP="swap",n.LIMIT="limit",n.ADVANCED="advanced",n))(v||{});const F=["connect","disconnect","close","chainChanged","accountsChanged"];class M{constructor(e){h(this,"ethereumProvider",null);h(this,"requestWaitingForConnection",{});h(this,"processRpcCallFromWindow",({rpcRequest:e})=>{if(!this.ethereumProvider){e.id&&(this.requestWaitingForConnection[e.id]=e);return}this.processRpcRequest(e)});this.iframeWidow=e}disconnect(){this.ethereumProvider=null,O(window,l.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow)}onConnect(e){this.ethereumProvider?this.disconnect():g(window,l.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),this.ethereumProvider=e,this.processPendingRequests(),F.forEach(t=>{e.on(t,r=>this.onProviderEvent(t,r))})}processPendingRequests(){Object.keys(this.requestWaitingForConnection).forEach(e=>{this.processRpcRequest(this.requestWaitingForConnection[e])}),this.requestWaitingForConnection={}}processRpcRequest(e){const{id:t,jsonrpc:r,method:s}=e;if(!this.ethereumProvider||!t)return;(s==="enable"?this.ethereumProvider.enable():this.ethereumProvider.request({...e,id:t})).then(o=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:r,id:t,result:o}})).catch(o=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:r,id:t,error:o}}))}onProviderEvent(e,t){R(this.iframeWidow,m.PROVIDER_ON_EVENT,{event:e,params:t})}forwardRpcResponseToIframe(e){R(this.iframeWidow,m.PROVIDER_RPC_RESPONSE,e)}}function q(){return typeof window>"u"||!window?"":`https://swap-dev-git-${window.location.hostname.replace("widget-configurator-git-","").replace("-cowswap.vercel.app","")}-cowswap.vercel.app`}const y={local:"http://localhost:3000",prod:"https://swap.cow.fi",dev:"https://dev.swap.cow.fi",pr:q()},W="_";function x(n){var s;const e=typeof n.baseUrl=="string"?n.baseUrl:y[((s=n.baseUrl)==null?void 0:s.environment)||"prod"],t=L(n),r=U(n);return e+"/#"+t+"?"+r}function L(n){const{chainId:e=1,sell:t,buy:r,tradeType:s=v.SWAP}=n,c=[(t==null?void 0:t.asset)||W,(r==null?void 0:r.asset)||W].map(encodeURIComponent).join("/");return`/${e}/widget/${s}/${c}`}function U(n){const{sell:e,buy:t,theme:r}=n,s=new URLSearchParams;return e!=null&&e.amount&&s.append("sellAmount",e.amount),t!=null&&t.amount&&s.append("buyAmount",t.amount),r&&s.append("theme",typeof r=="string"?r:r.baseTheme),s}class G{constructor(){h(this,"subscriptions",{})}on(e){const{event:t,handler:r}=e;this.subscriptions[t]||(this.subscriptions[t]=[]),this.subscriptions[t].push(r)}off(e){const{event:t,handler:r}=e;this.subscriptions[t]&&(this.subscriptions[t]=this.subscriptions[t].filter(s=>s!==r))}emit(e,t){this.subscriptions[e]&&this.subscriptions[e].forEach(r=>r(t))}}let $=1;class k{constructor(e,t=[]){h(this,"eventEmitter",new G);h(this,"listeners",[]);h(this,"widgetListener");h(this,"instanceNumber",$++);this.contentWindow=e,this.updateListeners(t),this.widgetListener=g(this.contentWindow,l.EMIT_COW_EVENT,r=>this.eventEmitter.emit(r.event,r.payload))}stopListeningIframe(){_(this.contentWindow,this.widgetListener)}updateListeners(e){for(const t of this.listeners)this.eventEmitter.off(t);this.listeners=e||[];for(const t of this.listeners)this.eventEmitter.on(t)}}const V="640px",j="450px",Q=20;function T(n,e){const{params:t,provider:r,listeners:s}=e;let c=r;const o=K(t);n.innerHTML="",n.appendChild(o);const{contentWindow:a}=o;if(!a)throw console.error("Iframe does not contain a window",o),new Error("Iframe does not contain a window!");const w=[];w.push(J(a,t.appCode)),w.push(Y(o,t.height));const f=new k(window,s);let d=I(a,null,c);return o.addEventListener("load",()=>S(a,t,c)),{updateParams:i=>S(a,i,c),updateListeners:i=>f.updateListeners(i),updateProvider:i=>{c=i,d=I(a,d,i)},destroy:()=>{f.stopListeningIframe(),w.forEach(i=>window.removeEventListener("message",i)),n.removeChild(o)}}}function I(n,e,t){e&&e.disconnect();const r=e||new M(n);return t&&r.onConnect(t),r}function K(n){const{width:e=j,height:t=V}=n,r=document.createElement("iframe");return r.src=x(n),r.width=e,r.height=t,r.style.border="0",r}function S(n,e,t){const r=!!t,s=L(e),c=U(e).toString();R(n,m.UPDATE_PARAMS,{urlParams:{pathname:s,search:c},appParams:{...e},hasProvider:r})}function J(n,e){const t=g(window,l.ACTIVATE,()=>{_(window,t),R(n,m.UPDATE_APP_DATA,{metaData:e?{appCode:e}:void 0})});return t}function Y(n,e=V){return g(window,l.UPDATE_HEIGHT,t=>{n.style.height=t.height?`${t.height+Q}px`:e})}function z(n){const{params:e,provider:t,listeners:r}=n,[s,c]=u.useState(null),o=u.useRef(null),a=u.useRef(t),w=u.useRef(r),f=u.useRef(null),d=u.useRef(null),i=u.useCallback((p,E)=>{try{console.log(`[WIDGET] ${p}`),E()}catch(A){const C=`Error ${p.toLowerCase()}`;console.error(`[WIDGET] ${C}`,A),c({message:C,error:A})}},[c]);return u.useEffect(()=>{if(!f.current||JSON.stringify(o.current)===JSON.stringify(e))return;const p=f.current,E=d.current;o.current=e,E===null?i("Creating a new widget",()=>{d.current=T(p,{params:e,provider:a.current,listeners:r}),w.current=r}):i("Updating the widget",()=>E.updateParams(e))},[e]),u.useEffect(()=>{if(!d.current||a.current===t)return;a.current=t;const p=f.current;p&&i("Updating the provider",()=>{var E;(E=d.current)==null||E.destroy(),d.current=T(p,{params:e,provider:a.current,listeners:r})})},[t]),u.useEffect(()=>{if(!d.current||w.current===r)return;const p=d.current;i("Updating the listeners",()=>p.updateListeners(r))},[r]),s?P.jsxs("div",{style:{color:"#ff3a3a"},children:[s.message,s.error.message&&P.jsx("pre",{style:{whiteSpace:"pre-wrap",fontSize:"0.75em"},children:s.error.message})]}):P.jsx("div",{ref:f})}exports.COWSWAP_URLS=y;exports.CowSwapWidget=z;exports.TradeType=v;exports.WidgetMethodsEmit=l;exports.WidgetMethodsListen=m;exports.createCowSwapWidget=T;exports.listenToMessageFromWindow=g;exports.postMessageToWindow=R;exports.stopListeningToMessageFromWindow=O;exports.stopListeningWindowListener=_;
|
package/index.mjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
var V = Object.defineProperty;
|
|
2
2
|
var N = (n, e, t) => e in n ? V(n, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[e] = t;
|
|
3
3
|
var p = (n, e, t) => (N(n, typeof e != "symbol" ? e + "" : e, t), t);
|
|
4
|
-
import { jsxs as
|
|
5
|
-
import { useState as
|
|
4
|
+
import { jsxs as b, jsx as v } from "react/jsx-runtime";
|
|
5
|
+
import { useState as H, useRef as m, useCallback as F, useEffect as g } from "react";
|
|
6
6
|
const W = "cowSwapWidget";
|
|
7
7
|
function P(n, e, t) {
|
|
8
8
|
const s = {
|
|
@@ -117,14 +117,15 @@ const k = {
|
|
|
117
117
|
pr: x()
|
|
118
118
|
}, A = "_";
|
|
119
119
|
function Q(n) {
|
|
120
|
-
|
|
120
|
+
var s;
|
|
121
|
+
const e = typeof n.baseUrl == "string" ? n.baseUrl : k[((s = n.baseUrl) == null ? void 0 : s.environment) || "prod"], t = y(n), r = U(n);
|
|
121
122
|
return e + "/#" + t + "?" + r;
|
|
122
123
|
}
|
|
123
124
|
function y(n) {
|
|
124
125
|
const { chainId: e = 1, sell: t, buy: r, tradeType: s = S.SWAP } = n, c = [(t == null ? void 0 : t.asset) || A, (r == null ? void 0 : r.asset) || A].map(encodeURIComponent).join("/");
|
|
125
126
|
return `/${e}/widget/${s}/${c}`;
|
|
126
127
|
}
|
|
127
|
-
function
|
|
128
|
+
function U(n) {
|
|
128
129
|
const { sell: e, buy: t, theme: r } = n, s = new URLSearchParams();
|
|
129
130
|
return e != null && e.amount && s.append("sellAmount", e.amount), t != null && t.amount && s.append("buyAmount", t.amount), r && s.append("theme", typeof r == "string" ? r : r.baseTheme), s;
|
|
130
131
|
}
|
|
@@ -168,7 +169,7 @@ class J {
|
|
|
168
169
|
this.eventEmitter.on(t);
|
|
169
170
|
}
|
|
170
171
|
}
|
|
171
|
-
const
|
|
172
|
+
const L = "640px", Y = "450px", z = 20;
|
|
172
173
|
function C(n, e) {
|
|
173
174
|
const { params: t, provider: r, listeners: s } = e;
|
|
174
175
|
let c = r;
|
|
@@ -198,11 +199,11 @@ function I(n, e, t) {
|
|
|
198
199
|
return t && r.onConnect(t), r;
|
|
199
200
|
}
|
|
200
201
|
function B(n) {
|
|
201
|
-
const { width: e = Y, height: t =
|
|
202
|
+
const { width: e = Y, height: t = L } = n, r = document.createElement("iframe");
|
|
202
203
|
return r.src = Q(n), r.width = e, r.height = t, r.style.border = "0", r;
|
|
203
204
|
}
|
|
204
205
|
function D(n, e, t) {
|
|
205
|
-
const r = !!t, s = y(e), c =
|
|
206
|
+
const r = !!t, s = y(e), c = U(e).toString();
|
|
206
207
|
P(n, w.UPDATE_PARAMS, {
|
|
207
208
|
urlParams: {
|
|
208
209
|
pathname: s,
|
|
@@ -222,13 +223,13 @@ function X(n, e) {
|
|
|
222
223
|
});
|
|
223
224
|
return t;
|
|
224
225
|
}
|
|
225
|
-
function Z(n, e =
|
|
226
|
+
function Z(n, e = L) {
|
|
226
227
|
return R(window, l.UPDATE_HEIGHT, (t) => {
|
|
227
228
|
n.style.height = t.height ? `${t.height + z}px` : e;
|
|
228
229
|
});
|
|
229
230
|
}
|
|
230
231
|
function re(n) {
|
|
231
|
-
const { params: e, provider: t, listeners: r } = n, [s, c] =
|
|
232
|
+
const { params: e, provider: t, listeners: r } = n, [s, c] = H(null), o = m(null), a = m(t), E = m(r), h = m(null), d = m(null), i = F(
|
|
232
233
|
(u, f) => {
|
|
233
234
|
try {
|
|
234
235
|
console.log(`[WIDGET] ${u}`), f();
|
|
@@ -260,7 +261,7 @@ function re(n) {
|
|
|
260
261
|
return;
|
|
261
262
|
const u = d.current;
|
|
262
263
|
i("Updating the listeners", () => u.updateListeners(r));
|
|
263
|
-
}, [r]), s ? /* @__PURE__ */
|
|
264
|
+
}, [r]), s ? /* @__PURE__ */ b("div", { style: { color: "#ff3a3a" }, children: [
|
|
264
265
|
s.message,
|
|
265
266
|
s.error.message && /* @__PURE__ */ v("pre", { style: { whiteSpace: "pre-wrap", fontSize: "0.75em" }, children: s.error.message })
|
|
266
267
|
] }) : /* @__PURE__ */ v("div", { ref: h });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cowprotocol/widget-react",
|
|
3
|
-
"version": "0.5.0-RC.
|
|
3
|
+
"version": "0.5.0-RC.4",
|
|
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.6.0-RC.
|
|
25
|
+
"@cowprotocol/widget-lib": "^0.6.0-RC.4"
|
|
26
26
|
}
|
|
27
27
|
}
|