@cowprotocol/widget-react 0.5.0-RC.4 → 0.5.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.
Files changed (3) hide show
  1. package/index.js +1 -1
  2. package/index.mjs +80 -90
  3. package/package.json +2 -2
package/index.js CHANGED
@@ -1 +1 @@
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=_;
1
+ "use strict";var V=Object.defineProperty;var b=(n,e,t)=>e in n?V(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t;var p=(n,e,t)=>(b(n,typeof e!="symbol"?e+"":e,t),t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const g=require("react/jsx-runtime"),d=require("react"),S="cowSwapWidget";function R(n,e,t){const s={key:S,method:e,...typeof t=="object"?t:{}};n.postMessage(s,"*")}function P(n,e,t){const r=s=>{!N(s.data)||s.data.key!==S||s.data.method!==e||t(s.data)};return n.addEventListener("message",r),r}function N(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||{}),w=(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))(w||{}),A=(n=>(n.SWAP="swap",n.LIMIT="limit",n.ADVANCED="advanced",n))(A||{});const F=["connect","disconnect","close","chainChanged","accountsChanged"];class H{constructor(e){p(this,"ethereumProvider",null);p(this,"requestWaitingForConnection",{});p(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():P(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(i=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:r,id:t,result:i}})).catch(i=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:r,id:t,error:i}}))}onProviderEvent(e,t){R(this.iframeWidow,w.PROVIDER_ON_EVENT,{event:e,params:t})}forwardRpcResponseToIframe(e){R(this.iframeWidow,w.PROVIDER_RPC_RESPONSE,e)}}const I="_";function M(n){const e=typeof n.baseUrl=="string"?n.baseUrl:"https://swap.cow.fi",t=L(n),r=y(n);return e+"/#"+t+"?"+r}function L(n){const{chainId:e=1,sell:t,buy:r,tradeType:s=A.SWAP}=n,c=[(t==null?void 0:t.asset)||I,(r==null?void 0:r.asset)||I].map(encodeURIComponent).join("/");return`/${e}/widget/${s}/${c}`}function y(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 q{constructor(){p(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 x=1;class G{constructor(e,t=[]){p(this,"eventEmitter",new q);p(this,"listeners",[]);p(this,"widgetListener");p(this,"instanceNumber",x++);this.contentWindow=e,this.updateListeners(t),this.widgetListener=P(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 U="640px",$="450px",k=20;function T(n,e){const{params:t,provider:r,listeners:s}=e;let c=r;const i=j(t);n.innerHTML="",n.appendChild(i);const{contentWindow:a}=i;if(!a)throw console.error("Iframe does not contain a window",i),new Error("Iframe does not contain a window!");const m=[];m.push(Q(a,t.appCode)),m.push(J(i,t.height));const E=new G(window,s);let u=W(a,null,c);return i.addEventListener("load",()=>D(a,t,c)),{updateParams:o=>D(a,o,c),updateListeners:o=>E.updateListeners(o),updateProvider:o=>{c=o,u=W(a,u,o)},destroy:()=>{E.stopListeningIframe(),m.forEach(o=>window.removeEventListener("message",o)),n.removeChild(i)}}}function W(n,e,t){e&&e.disconnect();const r=e||new H(n);return t&&r.onConnect(t),r}function j(n){const{width:e=$,height:t=U}=n,r=document.createElement("iframe");return r.src=M(n),r.width=e,r.height=t,r.style.border="0",r}function D(n,e,t){const r=!!t,s=L(e),c=y(e).toString();R(n,w.UPDATE_PARAMS,{urlParams:{pathname:s,search:c},appParams:{...e},hasProvider:r})}function Q(n,e){const t=P(window,l.ACTIVATE,()=>{_(window,t),R(n,w.UPDATE_APP_DATA,{metaData:e?{appCode:e}:void 0})});return t}function J(n,e=U){return P(window,l.UPDATE_HEIGHT,t=>{n.style.height=t.height?`${t.height+k}px`:e})}function K(n){const{params:e,provider:t,listeners:r}=n,[s,c]=d.useState(null),i=d.useRef(null),a=d.useRef(t),m=d.useRef(r),E=d.useRef(null),u=d.useRef(null),o=d.useCallback((h,f)=>{try{console.log(`[WIDGET] ${h}`),f()}catch(v){const C=`Error ${h.toLowerCase()}`;console.error(`[WIDGET] ${C}`,v),c({message:C,error:v})}},[c]);return d.useEffect(()=>{if(!E.current||JSON.stringify(i.current)===JSON.stringify(e))return;const h=E.current,f=u.current;i.current=e,f===null?o("Creating a new widget",()=>{u.current=T(h,{params:e,provider:a.current,listeners:r}),m.current=r}):o("Updating the widget",()=>f.updateParams(e))},[e]),d.useEffect(()=>{if(!u.current||a.current===t)return;a.current=t;const h=E.current;h&&o("Updating the provider",()=>{var f;(f=u.current)==null||f.destroy(),u.current=T(h,{params:e,provider:a.current,listeners:r})})},[t]),d.useEffect(()=>{if(!u.current||m.current===r)return;const h=u.current;o("Updating the listeners",()=>h.updateListeners(r))},[r]),s?g.jsxs("div",{style:{color:"#ff3a3a"},children:[s.message,s.error.message&&g.jsx("pre",{style:{whiteSpace:"pre-wrap",fontSize:"0.75em"},children:s.error.message})]}):g.jsx("div",{ref:E})}exports.CowSwapWidget=K;exports.TradeType=A;exports.WidgetMethodsEmit=l;exports.WidgetMethodsListen=w;exports.createCowSwapWidget=T;exports.listenToMessageFromWindow=P;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
- var p = (n, e, t) => (N(n, typeof e != "symbol" ? e + "" : e, t), t);
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";
3
+ var h = (n, e, t) => (N(n, typeof e != "symbol" ? e + "" : e, t), t);
4
+ import { jsxs as b, jsx as A } from "react/jsx-runtime";
5
+ import { useState as H, useRef as l, useCallback as F, useEffect as T } from "react";
6
6
  const W = "cowSwapWidget";
7
7
  function P(n, e, t) {
8
8
  const s = {
@@ -31,9 +31,9 @@ function M(n, e, t) {
31
31
  function O(n, e) {
32
32
  n.removeEventListener("message", e);
33
33
  }
34
- var l = /* @__PURE__ */ ((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 || {}), w = /* @__PURE__ */ ((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))(w || {}), S = /* @__PURE__ */ ((n) => (n.SWAP = "swap", n.LIMIT = "limit", n.ADVANCED = "advanced", n))(S || {});
34
+ var m = /* @__PURE__ */ ((n) => (n.ACTIVATE = "ACTIVATE", n.UPDATE_HEIGHT = "UPDATE_HEIGHT", n.EMIT_COW_EVENT = "EMIT_COW_EVENT", n.PROVIDER_RPC_REQUEST = "PROVIDER_RPC_REQUEST", n))(m || {}), w = /* @__PURE__ */ ((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))(w || {}), S = /* @__PURE__ */ ((n) => (n.SWAP = "swap", n.LIMIT = "limit", n.ADVANCED = "advanced", n))(S || {});
35
35
  const G = ["connect", "disconnect", "close", "chainChanged", "accountsChanged"];
36
- class $ {
36
+ class x {
37
37
  /**
38
38
  * Creates an instance of IframeRpcProviderBridge.
39
39
  * @param iframeWidow - The iFrame window that will post up general RPC messages and to which the IframeRpcProviderBridge will forward the RPC result.
@@ -44,10 +44,10 @@ class $ {
44
44
  * The Ethereum provider instance.
45
45
  * When is null the JSON-RPC bridge is disconnected from the Ethereum provider.
46
46
  * */
47
- p(this, "ethereumProvider", null);
47
+ h(this, "ethereumProvider", null);
48
48
  /** Stored JSON-RPC requests, to queue them when disconnected. */
49
- p(this, "requestWaitingForConnection", {});
50
- p(this, "processRpcCallFromWindow", ({ rpcRequest: e }) => {
49
+ h(this, "requestWaitingForConnection", {});
50
+ h(this, "processRpcCallFromWindow", ({ rpcRequest: e }) => {
51
51
  if (!this.ethereumProvider) {
52
52
  e.id && (this.requestWaitingForConnection[e.id] = e);
53
53
  return;
@@ -60,14 +60,14 @@ class $ {
60
60
  * Disconnects the JSON-RPC bridge from the Ethereum provider.
61
61
  */
62
62
  disconnect() {
63
- this.ethereumProvider = null, M(window, l.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow);
63
+ this.ethereumProvider = null, M(window, m.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow);
64
64
  }
65
65
  /**
66
66
  * Handles the 'connect' event and sets up event listeners for Ethereum provider events.
67
67
  * @param newProvider - The Ethereum provider to connect.
68
68
  */
69
69
  onConnect(e) {
70
- this.ethereumProvider ? this.disconnect() : R(window, l.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow), this.ethereumProvider = e, this.processPendingRequests(), G.forEach((t) => {
70
+ this.ethereumProvider ? this.disconnect() : R(window, m.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow), this.ethereumProvider = e, this.processPendingRequests(), G.forEach((t) => {
71
71
  e.on(t, (r) => this.onProviderEvent(t, r));
72
72
  });
73
73
  }
@@ -85,12 +85,12 @@ class $ {
85
85
  if (!this.ethereumProvider || !t)
86
86
  return;
87
87
  (s === "enable" ? this.ethereumProvider.enable() : this.ethereumProvider.request({ ...e, id: t })).then(
88
- (o) => this.forwardRpcResponseToIframe({
89
- rpcResponse: { jsonrpc: r, id: t, result: o }
88
+ (i) => this.forwardRpcResponseToIframe({
89
+ rpcResponse: { jsonrpc: r, id: t, result: i }
90
90
  })
91
91
  ).catch(
92
- (o) => this.forwardRpcResponseToIframe({
93
- rpcResponse: { jsonrpc: r, id: t, error: o }
92
+ (i) => this.forwardRpcResponseToIframe({
93
+ rpcResponse: { jsonrpc: r, id: t, error: i }
94
94
  })
95
95
  );
96
96
  }
@@ -107,31 +107,22 @@ class $ {
107
107
  P(this.iframeWidow, w.PROVIDER_RPC_RESPONSE, e);
108
108
  }
109
109
  }
110
- function x() {
111
- return typeof window > "u" || !window ? "" : `https://swap-dev-git-${window.location.hostname.replace("widget-configurator-git-", "").replace("-cowswap.vercel.app", "")}-cowswap.vercel.app`;
112
- }
113
- const k = {
114
- local: "http://localhost:3000",
115
- prod: "https://swap.cow.fi",
116
- dev: "https://dev.swap.cow.fi",
117
- pr: x()
118
- }, A = "_";
119
- function Q(n) {
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);
110
+ const v = "_";
111
+ function $(n) {
112
+ const e = typeof n.baseUrl == "string" ? n.baseUrl : "https://swap.cow.fi", t = y(n), r = L(n);
122
113
  return e + "/#" + t + "?" + r;
123
114
  }
124
115
  function y(n) {
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("/");
116
+ const { chainId: e = 1, sell: t, buy: r, tradeType: s = S.SWAP } = n, c = [(t == null ? void 0 : t.asset) || v, (r == null ? void 0 : r.asset) || v].map(encodeURIComponent).join("/");
126
117
  return `/${e}/widget/${s}/${c}`;
127
118
  }
128
- function U(n) {
119
+ function L(n) {
129
120
  const { sell: e, buy: t, theme: r } = n, s = new URLSearchParams();
130
121
  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;
131
122
  }
132
- class K {
123
+ class k {
133
124
  constructor() {
134
- p(this, "subscriptions", {});
125
+ h(this, "subscriptions", {});
135
126
  }
136
127
  on(e) {
137
128
  const { event: t, handler: r } = e;
@@ -145,16 +136,16 @@ class K {
145
136
  this.subscriptions[e] && this.subscriptions[e].forEach((r) => r(t));
146
137
  }
147
138
  }
148
- let j = 1;
149
- class J {
139
+ let Q = 1;
140
+ class j {
150
141
  constructor(e, t = []) {
151
- p(this, "eventEmitter", new K());
152
- p(this, "listeners", []);
153
- p(this, "widgetListener");
154
- p(this, "instanceNumber", j++);
142
+ h(this, "eventEmitter", new k());
143
+ h(this, "listeners", []);
144
+ h(this, "widgetListener");
145
+ h(this, "instanceNumber", Q++);
155
146
  this.contentWindow = e, this.updateListeners(t), this.widgetListener = R(
156
147
  this.contentWindow,
157
- l.EMIT_COW_EVENT,
148
+ m.EMIT_COW_EVENT,
158
149
  (r) => this.eventEmitter.emit(r.event, r.payload)
159
150
  );
160
151
  }
@@ -169,41 +160,41 @@ class J {
169
160
  this.eventEmitter.on(t);
170
161
  }
171
162
  }
172
- const L = "640px", Y = "450px", z = 20;
163
+ const U = "640px", J = "450px", K = 20;
173
164
  function C(n, e) {
174
165
  const { params: t, provider: r, listeners: s } = e;
175
166
  let c = r;
176
- const o = B(t);
177
- n.innerHTML = "", n.appendChild(o);
178
- const { contentWindow: a } = o;
167
+ const i = Y(t);
168
+ n.innerHTML = "", n.appendChild(i);
169
+ const { contentWindow: a } = i;
179
170
  if (!a)
180
- throw console.error("Iframe does not contain a window", o), new Error("Iframe does not contain a window!");
181
- const E = [];
182
- E.push(X(a, t.appCode)), E.push(Z(o, t.height));
183
- const h = new J(window, s);
184
- let d = I(a, null, c);
185
- return o.addEventListener("load", () => D(a, t, c)), {
186
- updateParams: (i) => D(a, i, c),
187
- updateListeners: (i) => h.updateListeners(i),
188
- updateProvider: (i) => {
189
- c = i, d = I(a, d, i);
171
+ throw console.error("Iframe does not contain a window", i), new Error("Iframe does not contain a window!");
172
+ const f = [];
173
+ f.push(z(a, t.appCode)), f.push(B(i, t.height));
174
+ const p = new j(window, s);
175
+ let u = I(a, null, c);
176
+ return i.addEventListener("load", () => D(a, t, c)), {
177
+ updateParams: (o) => D(a, o, c),
178
+ updateListeners: (o) => p.updateListeners(o),
179
+ updateProvider: (o) => {
180
+ c = o, u = I(a, u, o);
190
181
  },
191
182
  destroy: () => {
192
- h.stopListeningIframe(), E.forEach((i) => window.removeEventListener("message", i)), n.removeChild(o);
183
+ p.stopListeningIframe(), f.forEach((o) => window.removeEventListener("message", o)), n.removeChild(i);
193
184
  }
194
185
  };
195
186
  }
196
187
  function I(n, e, t) {
197
188
  e && e.disconnect();
198
- const r = e || new $(n);
189
+ const r = e || new x(n);
199
190
  return t && r.onConnect(t), r;
200
191
  }
201
- function B(n) {
202
- const { width: e = Y, height: t = L } = n, r = document.createElement("iframe");
203
- return r.src = Q(n), r.width = e, r.height = t, r.style.border = "0", r;
192
+ function Y(n) {
193
+ const { width: e = J, height: t = U } = n, r = document.createElement("iframe");
194
+ return r.src = $(n), r.width = e, r.height = t, r.style.border = "0", r;
204
195
  }
205
196
  function D(n, e, t) {
206
- const r = !!t, s = y(e), c = U(e).toString();
197
+ const r = !!t, s = y(e), c = L(e).toString();
207
198
  P(n, w.UPDATE_PARAMS, {
208
199
  urlParams: {
209
200
  pathname: s,
@@ -215,62 +206,61 @@ function D(n, e, t) {
215
206
  hasProvider: r
216
207
  });
217
208
  }
218
- function X(n, e) {
219
- const t = R(window, l.ACTIVATE, () => {
209
+ function z(n, e) {
210
+ const t = R(window, m.ACTIVATE, () => {
220
211
  O(window, t), P(n, w.UPDATE_APP_DATA, {
221
212
  metaData: e ? { appCode: e } : void 0
222
213
  });
223
214
  });
224
215
  return t;
225
216
  }
226
- function Z(n, e = L) {
227
- return R(window, l.UPDATE_HEIGHT, (t) => {
228
- n.style.height = t.height ? `${t.height + z}px` : e;
217
+ function B(n, e = U) {
218
+ return R(window, m.UPDATE_HEIGHT, (t) => {
219
+ n.style.height = t.height ? `${t.height + K}px` : e;
229
220
  });
230
221
  }
231
- function re(n) {
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(
233
- (u, f) => {
222
+ function te(n) {
223
+ const { params: e, provider: t, listeners: r } = n, [s, c] = H(null), i = l(null), a = l(t), f = l(r), p = l(null), u = l(null), o = F(
224
+ (d, E) => {
234
225
  try {
235
- console.log(`[WIDGET] ${u}`), f();
236
- } catch (T) {
237
- const _ = `Error ${u.toLowerCase()}`;
238
- console.error(`[WIDGET] ${_}`, T), c({ message: _, error: T });
226
+ console.log(`[WIDGET] ${d}`), E();
227
+ } catch (g) {
228
+ const _ = `Error ${d.toLowerCase()}`;
229
+ console.error(`[WIDGET] ${_}`, g), c({ message: _, error: g });
239
230
  }
240
231
  },
241
232
  [c]
242
233
  );
243
- return g(() => {
244
- if (!h.current || JSON.stringify(o.current) === JSON.stringify(e))
234
+ return T(() => {
235
+ if (!p.current || JSON.stringify(i.current) === JSON.stringify(e))
245
236
  return;
246
- const u = h.current, f = d.current;
247
- o.current = e, f === null ? i("Creating a new widget", () => {
248
- d.current = C(u, { params: e, provider: a.current, listeners: r }), E.current = r;
249
- }) : i("Updating the widget", () => f.updateParams(e));
250
- }, [e]), g(() => {
251
- if (!d.current || a.current === t)
237
+ const d = p.current, E = u.current;
238
+ i.current = e, E === null ? o("Creating a new widget", () => {
239
+ u.current = C(d, { params: e, provider: a.current, listeners: r }), f.current = r;
240
+ }) : o("Updating the widget", () => E.updateParams(e));
241
+ }, [e]), T(() => {
242
+ if (!u.current || a.current === t)
252
243
  return;
253
244
  a.current = t;
254
- const u = h.current;
255
- u && i("Updating the provider", () => {
256
- var f;
257
- (f = d.current) == null || f.destroy(), d.current = C(u, { params: e, provider: a.current, listeners: r });
245
+ const d = p.current;
246
+ d && o("Updating the provider", () => {
247
+ var E;
248
+ (E = u.current) == null || E.destroy(), u.current = C(d, { params: e, provider: a.current, listeners: r });
258
249
  });
259
- }, [t]), g(() => {
260
- if (!d.current || E.current === r)
250
+ }, [t]), T(() => {
251
+ if (!u.current || f.current === r)
261
252
  return;
262
- const u = d.current;
263
- i("Updating the listeners", () => u.updateListeners(r));
253
+ const d = u.current;
254
+ o("Updating the listeners", () => d.updateListeners(r));
264
255
  }, [r]), s ? /* @__PURE__ */ b("div", { style: { color: "#ff3a3a" }, children: [
265
256
  s.message,
266
- s.error.message && /* @__PURE__ */ v("pre", { style: { whiteSpace: "pre-wrap", fontSize: "0.75em" }, children: s.error.message })
267
- ] }) : /* @__PURE__ */ v("div", { ref: h });
257
+ s.error.message && /* @__PURE__ */ A("pre", { style: { whiteSpace: "pre-wrap", fontSize: "0.75em" }, children: s.error.message })
258
+ ] }) : /* @__PURE__ */ A("div", { ref: p });
268
259
  }
269
260
  export {
270
- k as COWSWAP_URLS,
271
- re as CowSwapWidget,
261
+ te as CowSwapWidget,
272
262
  S as TradeType,
273
- l as WidgetMethodsEmit,
263
+ m as WidgetMethodsEmit,
274
264
  w as WidgetMethodsListen,
275
265
  C as createCowSwapWidget,
276
266
  R as listenToMessageFromWindow,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cowprotocol/widget-react",
3
- "version": "0.5.0-RC.4",
3
+ "version": "0.5.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.6.0-RC.4"
25
+ "@cowprotocol/widget-lib": "^0.7.0"
26
26
  }
27
27
  }