@cowprotocol/widget-lib 0.6.0-RC.1 → 0.6.0-RC.2

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.d.ts CHANGED
@@ -3,4 +3,3 @@ export type { CowSwapWidgetHandler } from './cowSwapWidget';
3
3
  export { COWSWAP_URLS } from './consts';
4
4
  export * from './types';
5
5
  export * from './messages';
6
- export * from '@cowprotocol/events';
package/index.iife.js CHANGED
@@ -1 +1 @@
1
- var cowSwapWidget=function(o){"use strict";const m="cowSwapWidget";function u(e,n,t){const s={key:m,method:n,...typeof t=="object"?t:{}};e.postMessage(s,"*")}function _(e,n,t){const i=s=>{!L(s.data)||s.data.key!==m||s.data.method!==n||t(s.data)};return e.addEventListener("message",i),i}function L(e){return typeof e=="object"&&e!==null&&"key"in e&&"method"in e&&typeof e.key=="string"&&typeof e.method=="string"}function T(e,n,t){e.removeEventListener("message",t)}function p(e,n){e.removeEventListener("message",n)}var E=(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||{}),d=(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))(d||{}),R=(e=>(e.SWAP="swap",e.LIMIT="limit",e.ADVANCED="advanced",e))(R||{});const N=["connect","disconnect","close","chainChanged","accountsChanged"];class U{constructor(n){this.iframeWidow=n,this.ethereumProvider=null,this.requestWaitingForConnection={},this.processRpcCallFromWindow=({rpcRequest:t})=>{if(!this.ethereumProvider){t.id&&(this.requestWaitingForConnection[t.id]=t);return}this.processRpcRequest(t)}}disconnect(){this.ethereumProvider=null,T(window,E.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow)}onConnect(n){this.ethereumProvider?this.disconnect():_(window,E.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),this.ethereumProvider=n,this.processPendingRequests(),N.forEach(t=>{n.on(t,i=>this.onProviderEvent(t,i))})}processPendingRequests(){Object.keys(this.requestWaitingForConnection).forEach(n=>{this.processRpcRequest(this.requestWaitingForConnection[n])}),this.requestWaitingForConnection={}}processRpcRequest(n){const{id:t,jsonrpc:i,method:s}=n;if(!this.ethereumProvider||!t)return;(s==="enable"?this.ethereumProvider.enable():this.ethereumProvider.request({...n,id:t})).then(r=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:i,id:t,result:r}})).catch(r=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:i,id:t,error:r}}))}onProviderEvent(n,t){u(this.iframeWidow,d.PROVIDER_ON_EVENT,{event:n,params:t})}forwardRpcResponseToIframe(n){u(this.iframeWidow,d.PROVIDER_RPC_RESPONSE,n)}}function V(){return typeof window>"u"||!window?"":`https://swap-dev-git-${window.location.hostname.replace("widget-configurator-git-","").replace("-cowswap.vercel.app","")}-cowswap.vercel.app`}const w={local:"http://localhost:3000",prod:"https://swap.cow.fi",dev:"https://dev.swap.cow.fi",pr:V()},f="_";function F(e){const n=w[e.env||"prod"],t=A(e),i=l(e);return n+"/#"+t+"?"+i}function A(e){const{chainId:n=1,sell:t,buy:i,tradeType:s=R.SWAP}=e,c=[(t==null?void 0:t.asset)||f,(i==null?void 0:i.asset)||f].map(encodeURIComponent).join("/");return`/${n}/widget/${s}/${c}`}function l(e){const{sell:n,buy:t,theme:i}=e,s=new URLSearchParams;return n!=null&&n.amount&&s.append("sellAmount",n.amount),t!=null&&t.amount&&s.append("buyAmount",t.amount),i&&s.append("theme",typeof i=="string"?i:i.baseTheme),s}var O=(e=>(e.SWAP_ETH_FLOW_SENT_TX="SWAP_ETH_FLOW_SENT_TX",e.SWAP_POSTED_API="SWAP_POSTED_API",e.SWAP_SIGNING_ERROR="SWAP_SIGNING_ERROR",e.SWAP_TRADE_EXECUTED="SWAP_TRADE_EXECUTED",e.SWAP_ORDER_CANCELLED="SWAP_ORDER_CANCELLED",e))(O||{}),D=(e=>(e.ON_TOAST_MESSAGE="ON_TOAST_MESSAGE",e.ON_POSTED_ORDER="ON_POSTED_ORDER",e.ON_POSTED_ETH_FLOW_ORDER="ON_POSTED_ETH_FLOW_ORDER",e.ON_EXECUTED_ORDER="ON_EXECUTED_ORDER",e.ON_CANCELLED_ORDER="ON_CANCELLED_ORDER",e))(D||{});class S{constructor(){this.subscriptions={}}on(n){const{event:t,handler:i}=n;this.subscriptions[t]||(this.subscriptions[t]=[]),this.subscriptions[t].push(i)}off(n){const{event:t,handler:i}=n;this.subscriptions[t]&&(this.subscriptions[t]=this.subscriptions[t].filter(s=>s!==i))}emit(n,t){this.subscriptions[n]&&this.subscriptions[n].forEach(i=>i(t))}}let H=1;class y{constructor(n,t=[]){this.contentWindow=n,this.eventEmitter=new S,this.listeners=[],this.instanceNumber=H++,this.updateListeners(t),this.widgetListener=_(this.contentWindow,E.EMIT_COW_EVENT,i=>this.eventEmitter.emit(i.event,i.payload))}stopListeningIframe(){p(this.contentWindow,this.widgetListener)}updateListeners(n){for(const t of this.listeners)this.eventEmitter.off(t);this.listeners=n||[];for(const t of this.listeners)this.eventEmitter.on(t)}}const W="640px",b="450px",M=20;function G(e,n){const{params:t,provider:i,listeners:s}=n;let c=i;const r=q(t);e.innerHTML="",e.appendChild(r);const{contentWindow:h}=r;if(!h)throw console.error("Iframe does not contain a window",r),new Error("Iframe does not contain a window!");const P=[];P.push(X(h,t.appCode)),P.push(k(r,t.height));const v=new y(window,s);let I=C(h,null,c);return r.addEventListener("load",()=>g(h,t,c)),{updateParams:a=>g(h,a,c),updateListeners:a=>v.updateListeners(a),updateProvider:a=>{c=a,I=C(h,I,a)},destroy:()=>{v.stopListeningIframe(),P.forEach(a=>window.removeEventListener("message",a)),e.removeChild(r)}}}function C(e,n,t){n&&n.disconnect();const i=n||new U(e);return t&&i.onConnect(t),i}function q(e){const{width:n=b,height:t=W}=e,i=document.createElement("iframe");return i.src=F(e),i.width=n,i.height=t,i.style.border="0",i}function g(e,n,t){const i=!!t,s=A(n),c=l(n).toString();u(e,d.UPDATE_PARAMS,{urlParams:{pathname:s,search:c},appParams:{...n},hasProvider:i})}function X(e,n){const t=_(window,E.ACTIVATE,()=>{p(window,t),u(e,d.UPDATE_APP_DATA,{metaData:n?{appCode:n}:void 0})});return t}function k(e,n=W){return _(window,E.UPDATE_HEIGHT,t=>{e.style.height=t.height?`${t.height+M}px`:n})}return o.COWSWAP_URLS=w,o.CowEvents=D,o.SimpleCowEventEmitter=S,o.ToastMessageType=O,o.TradeType=R,o.WidgetMethodsEmit=E,o.WidgetMethodsListen=d,o.createCowSwapWidget=G,o.listenToMessageFromWindow=_,o.postMessageToWindow=u,o.stopListeningToMessageFromWindow=T,o.stopListeningWindowListener=p,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"}),o}({});
1
+ var cowSwapWidget=function(o){"use strict";const P="cowSwapWidget";function p(t,n,e){const s={key:P,method:n,...typeof e=="object"?e:{}};t.postMessage(s,"*")}function E(t,n,e){const i=s=>{!D(s.data)||s.data.key!==P||s.data.method!==n||e(s.data)};return t.addEventListener("message",i),i}function D(t){return typeof t=="object"&&t!==null&&"key"in t&&"method"in t&&typeof t.key=="string"&&typeof t.method=="string"}function l(t,n,e){t.removeEventListener("message",e)}function m(t,n){t.removeEventListener("message",n)}var d=(t=>(t.ACTIVATE="ACTIVATE",t.UPDATE_HEIGHT="UPDATE_HEIGHT",t.EMIT_COW_EVENT="EMIT_COW_EVENT",t.PROVIDER_RPC_REQUEST="PROVIDER_RPC_REQUEST",t))(d||{}),h=(t=>(t.UPDATE_PARAMS="UPDATE_PARAMS",t.UPDATE_APP_DATA="UPDATE_APP_DATA",t.PROVIDER_RPC_RESPONSE="PROVIDER_RPC_RESPONSE",t.PROVIDER_ON_EVENT="PROVIDER_ON_EVENT",t))(h||{}),w=(t=>(t.SWAP="swap",t.LIMIT="limit",t.ADVANCED="advanced",t))(w||{});const S=["connect","disconnect","close","chainChanged","accountsChanged"];class O{constructor(n){this.iframeWidow=n,this.ethereumProvider=null,this.requestWaitingForConnection={},this.processRpcCallFromWindow=({rpcRequest:e})=>{if(!this.ethereumProvider){e.id&&(this.requestWaitingForConnection[e.id]=e);return}this.processRpcRequest(e)}}disconnect(){this.ethereumProvider=null,l(window,d.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow)}onConnect(n){this.ethereumProvider?this.disconnect():E(window,d.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),this.ethereumProvider=n,this.processPendingRequests(),S.forEach(e=>{n.on(e,i=>this.onProviderEvent(e,i))})}processPendingRequests(){Object.keys(this.requestWaitingForConnection).forEach(n=>{this.processRpcRequest(this.requestWaitingForConnection[n])}),this.requestWaitingForConnection={}}processRpcRequest(n){const{id:e,jsonrpc:i,method:s}=n;if(!this.ethereumProvider||!e)return;(s==="enable"?this.ethereumProvider.enable():this.ethereumProvider.request({...n,id:e})).then(r=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:i,id:e,result:r}})).catch(r=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:i,id:e,error:r}}))}onProviderEvent(n,e){p(this.iframeWidow,h.PROVIDER_ON_EVENT,{event:n,params:e})}forwardRpcResponseToIframe(n){p(this.iframeWidow,h.PROVIDER_RPC_RESPONSE,n)}}function L(){return typeof window>"u"||!window?"":`https://swap-dev-git-${window.location.hostname.replace("widget-configurator-git-","").replace("-cowswap.vercel.app","")}-cowswap.vercel.app`}const R={local:"http://localhost:3000",prod:"https://swap.cow.fi",dev:"https://dev.swap.cow.fi",pr:L()},T="_";function V(t){const n=R[t.env||"prod"],e=_(t),i=g(t);return n+"/#"+e+"?"+i}function _(t){const{chainId:n=1,sell:e,buy:i,tradeType:s=w.SWAP}=t,c=[(e==null?void 0:e.asset)||T,(i==null?void 0:i.asset)||T].map(encodeURIComponent).join("/");return`/${n}/widget/${s}/${c}`}function g(t){const{sell:n,buy:e,theme:i}=t,s=new URLSearchParams;return n!=null&&n.amount&&s.append("sellAmount",n.amount),e!=null&&e.amount&&s.append("buyAmount",e.amount),i&&s.append("theme",typeof i=="string"?i:i.baseTheme),s}class y{constructor(){this.subscriptions={}}on(n){const{event:e,handler:i}=n;this.subscriptions[e]||(this.subscriptions[e]=[]),this.subscriptions[e].push(i)}off(n){const{event:e,handler:i}=n;this.subscriptions[e]&&(this.subscriptions[e]=this.subscriptions[e].filter(s=>s!==i))}emit(n,e){this.subscriptions[n]&&this.subscriptions[n].forEach(i=>i(e))}}let U=1;class N{constructor(n,e=[]){this.contentWindow=n,this.eventEmitter=new y,this.listeners=[],this.instanceNumber=U++,this.updateListeners(e),this.widgetListener=E(this.contentWindow,d.EMIT_COW_EVENT,i=>this.eventEmitter.emit(i.event,i.payload))}stopListeningIframe(){m(this.contentWindow,this.widgetListener)}updateListeners(n){for(const e of this.listeners)this.eventEmitter.off(e);this.listeners=n||[];for(const e of this.listeners)this.eventEmitter.on(e)}}const A="640px",F="450px",b=20;function H(t,n){const{params:e,provider:i,listeners:s}=n;let c=i;const r=M(e);t.innerHTML="",t.appendChild(r);const{contentWindow:u}=r;if(!u)throw console.error("Iframe does not contain a window",r),new Error("Iframe does not contain a window!");const f=[];f.push(q(u,e.appCode)),f.push(G(r,e.height));const C=new N(window,s);let I=v(u,null,c);return r.addEventListener("load",()=>W(u,e,c)),{updateParams:a=>W(u,a,c),updateListeners:a=>C.updateListeners(a),updateProvider:a=>{c=a,I=v(u,I,a)},destroy:()=>{C.stopListeningIframe(),f.forEach(a=>window.removeEventListener("message",a)),t.removeChild(r)}}}function v(t,n,e){n&&n.disconnect();const i=n||new O(t);return e&&i.onConnect(e),i}function M(t){const{width:n=F,height:e=A}=t,i=document.createElement("iframe");return i.src=V(t),i.width=n,i.height=e,i.style.border="0",i}function W(t,n,e){const i=!!e,s=_(n),c=g(n).toString();p(t,h.UPDATE_PARAMS,{urlParams:{pathname:s,search:c},appParams:{...n},hasProvider:i})}function q(t,n){const e=E(window,d.ACTIVATE,()=>{m(window,e),p(t,h.UPDATE_APP_DATA,{metaData:n?{appCode:n}:void 0})});return e}function G(t,n=A){return E(window,d.UPDATE_HEIGHT,e=>{t.style.height=e.height?`${e.height+b}px`:n})}return o.COWSWAP_URLS=R,o.TradeType=w,o.WidgetMethodsEmit=d,o.WidgetMethodsListen=h,o.createCowSwapWidget=H,o.listenToMessageFromWindow=E,o.postMessageToWindow=p,o.stopListeningToMessageFromWindow=l,o.stopListeningWindowListener=m,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"}),o}({});
package/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const A="cowSwapWidget";function h(e,n,t){const s={key:A,method:n,...typeof t=="object"?t:{}};e.postMessage(s,"*")}function _(e,n,t){const i=s=>{!v(s.data)||s.data.key!==A||s.data.method!==n||t(s.data)};return e.addEventListener("message",i),i}function v(e){return typeof e=="object"&&e!==null&&"key"in e&&"method"in e&&typeof e.key=="string"&&typeof e.method=="string"}function l(e,n,t){e.removeEventListener("message",t)}function u(e,n){e.removeEventListener("message",n)}var a=(e=>(e.ACTIVATE="ACTIVATE",e.UPDATE_HEIGHT="UPDATE_HEIGHT",e.EMIT_COW_EVENT="EMIT_COW_EVENT",e.PROVIDER_RPC_REQUEST="PROVIDER_RPC_REQUEST",e))(a||{}),d=(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))(d||{}),R=(e=>(e.SWAP="swap",e.LIMIT="limit",e.ADVANCED="advanced",e))(R||{});const L=["connect","disconnect","close","chainChanged","accountsChanged"];class N{constructor(n){this.iframeWidow=n,this.ethereumProvider=null,this.requestWaitingForConnection={},this.processRpcCallFromWindow=({rpcRequest:t})=>{if(!this.ethereumProvider){t.id&&(this.requestWaitingForConnection[t.id]=t);return}this.processRpcRequest(t)}}disconnect(){this.ethereumProvider=null,l(window,a.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow)}onConnect(n){this.ethereumProvider?this.disconnect():_(window,a.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),this.ethereumProvider=n,this.processPendingRequests(),L.forEach(t=>{n.on(t,i=>this.onProviderEvent(t,i))})}processPendingRequests(){Object.keys(this.requestWaitingForConnection).forEach(n=>{this.processRpcRequest(this.requestWaitingForConnection[n])}),this.requestWaitingForConnection={}}processRpcRequest(n){const{id:t,jsonrpc:i,method:s}=n;if(!this.ethereumProvider||!t)return;(s==="enable"?this.ethereumProvider.enable():this.ethereumProvider.request({...n,id:t})).then(o=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:i,id:t,result:o}})).catch(o=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:i,id:t,error:o}}))}onProviderEvent(n,t){h(this.iframeWidow,d.PROVIDER_ON_EVENT,{event:n,params:t})}forwardRpcResponseToIframe(n){h(this.iframeWidow,d.PROVIDER_RPC_RESPONSE,n)}}function U(){return typeof window>"u"||!window?"":`https://swap-dev-git-${window.location.hostname.replace("widget-configurator-git-","").replace("-cowswap.vercel.app","")}-cowswap.vercel.app`}const O={local:"http://localhost:3000",prod:"https://swap.cow.fi",dev:"https://dev.swap.cow.fi",pr:U()},T="_";function V(e){const n=O[e.env||"prod"],t=D(e),i=S(e);return n+"/#"+t+"?"+i}function D(e){const{chainId:n=1,sell:t,buy:i,tradeType:s=R.SWAP}=e,r=[(t==null?void 0:t.asset)||T,(i==null?void 0:i.asset)||T].map(encodeURIComponent).join("/");return`/${n}/widget/${s}/${r}`}function S(e){const{sell:n,buy:t,theme:i}=e,s=new URLSearchParams;return n!=null&&n.amount&&s.append("sellAmount",n.amount),t!=null&&t.amount&&s.append("buyAmount",t.amount),i&&s.append("theme",typeof i=="string"?i:i.baseTheme),s}var W=(e=>(e.SWAP_ETH_FLOW_SENT_TX="SWAP_ETH_FLOW_SENT_TX",e.SWAP_POSTED_API="SWAP_POSTED_API",e.SWAP_SIGNING_ERROR="SWAP_SIGNING_ERROR",e.SWAP_TRADE_EXECUTED="SWAP_TRADE_EXECUTED",e.SWAP_ORDER_CANCELLED="SWAP_ORDER_CANCELLED",e))(W||{}),C=(e=>(e.ON_TOAST_MESSAGE="ON_TOAST_MESSAGE",e.ON_POSTED_ORDER="ON_POSTED_ORDER",e.ON_POSTED_ETH_FLOW_ORDER="ON_POSTED_ETH_FLOW_ORDER",e.ON_EXECUTED_ORDER="ON_EXECUTED_ORDER",e.ON_CANCELLED_ORDER="ON_CANCELLED_ORDER",e))(C||{});class g{constructor(){this.subscriptions={}}on(n){const{event:t,handler:i}=n;this.subscriptions[t]||(this.subscriptions[t]=[]),this.subscriptions[t].push(i)}off(n){const{event:t,handler:i}=n;this.subscriptions[t]&&(this.subscriptions[t]=this.subscriptions[t].filter(s=>s!==i))}emit(n,t){this.subscriptions[n]&&this.subscriptions[n].forEach(i=>i(t))}}let F=1;class H{constructor(n,t=[]){this.contentWindow=n,this.eventEmitter=new g,this.listeners=[],this.instanceNumber=F++,this.updateListeners(t),this.widgetListener=_(this.contentWindow,a.EMIT_COW_EVENT,i=>this.eventEmitter.emit(i.event,i.payload))}stopListeningIframe(){u(this.contentWindow,this.widgetListener)}updateListeners(n){for(const t of this.listeners)this.eventEmitter.off(t);this.listeners=n||[];for(const t of this.listeners)this.eventEmitter.on(t)}}const I="640px",y="450px",b=20;function M(e,n){const{params:t,provider:i,listeners:s}=n;let r=i;const o=G(t);e.innerHTML="",e.appendChild(o);const{contentWindow:E}=o;if(!E)throw console.error("Iframe does not contain a window",o),new Error("Iframe does not contain a window!");const p=[];p.push(q(E,t.appCode)),p.push(X(o,t.height));const P=new H(window,s);let m=f(E,null,r);return o.addEventListener("load",()=>w(E,t,r)),{updateParams:c=>w(E,c,r),updateListeners:c=>P.updateListeners(c),updateProvider:c=>{r=c,m=f(E,m,c)},destroy:()=>{P.stopListeningIframe(),p.forEach(c=>window.removeEventListener("message",c)),e.removeChild(o)}}}function f(e,n,t){n&&n.disconnect();const i=n||new N(e);return t&&i.onConnect(t),i}function G(e){const{width:n=y,height:t=I}=e,i=document.createElement("iframe");return i.src=V(e),i.width=n,i.height=t,i.style.border="0",i}function w(e,n,t){const i=!!t,s=D(n),r=S(n).toString();h(e,d.UPDATE_PARAMS,{urlParams:{pathname:s,search:r},appParams:{...n},hasProvider:i})}function q(e,n){const t=_(window,a.ACTIVATE,()=>{u(window,t),h(e,d.UPDATE_APP_DATA,{metaData:n?{appCode:n}:void 0})});return t}function X(e,n=I){return _(window,a.UPDATE_HEIGHT,t=>{e.style.height=t.height?`${t.height+b}px`:n})}exports.COWSWAP_URLS=O;exports.CowEvents=C;exports.SimpleCowEventEmitter=g;exports.ToastMessageType=W;exports.TradeType=R;exports.WidgetMethodsEmit=a;exports.WidgetMethodsListen=d;exports.createCowSwapWidget=M;exports.listenToMessageFromWindow=_;exports.postMessageToWindow=h;exports.stopListeningToMessageFromWindow=l;exports.stopListeningWindowListener=u;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const _="cowSwapWidget";function p(t,n,e){const i={key:_,method:n,...typeof e=="object"?e:{}};t.postMessage(i,"*")}function u(t,n,e){const s=i=>{!I(i.data)||i.data.key!==_||i.data.method!==n||e(i.data)};return t.addEventListener("message",s),s}function I(t){return typeof t=="object"&&t!==null&&"key"in t&&"method"in t&&typeof t.key=="string"&&typeof t.method=="string"}function g(t,n,e){t.removeEventListener("message",e)}function m(t,n){t.removeEventListener("message",n)}var a=(t=>(t.ACTIVATE="ACTIVATE",t.UPDATE_HEIGHT="UPDATE_HEIGHT",t.EMIT_COW_EVENT="EMIT_COW_EVENT",t.PROVIDER_RPC_REQUEST="PROVIDER_RPC_REQUEST",t))(a||{}),h=(t=>(t.UPDATE_PARAMS="UPDATE_PARAMS",t.UPDATE_APP_DATA="UPDATE_APP_DATA",t.PROVIDER_RPC_RESPONSE="PROVIDER_RPC_RESPONSE",t.PROVIDER_ON_EVENT="PROVIDER_ON_EVENT",t))(h||{}),w=(t=>(t.SWAP="swap",t.LIMIT="limit",t.ADVANCED="advanced",t))(w||{});const D=["connect","disconnect","close","chainChanged","accountsChanged"];class O{constructor(n){this.iframeWidow=n,this.ethereumProvider=null,this.requestWaitingForConnection={},this.processRpcCallFromWindow=({rpcRequest:e})=>{if(!this.ethereumProvider){e.id&&(this.requestWaitingForConnection[e.id]=e);return}this.processRpcRequest(e)}}disconnect(){this.ethereumProvider=null,g(window,a.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow)}onConnect(n){this.ethereumProvider?this.disconnect():u(window,a.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),this.ethereumProvider=n,this.processPendingRequests(),D.forEach(e=>{n.on(e,s=>this.onProviderEvent(e,s))})}processPendingRequests(){Object.keys(this.requestWaitingForConnection).forEach(n=>{this.processRpcRequest(this.requestWaitingForConnection[n])}),this.requestWaitingForConnection={}}processRpcRequest(n){const{id:e,jsonrpc:s,method:i}=n;if(!this.ethereumProvider||!e)return;(i==="enable"?this.ethereumProvider.enable():this.ethereumProvider.request({...n,id:e})).then(o=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:s,id:e,result:o}})).catch(o=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:s,id:e,error:o}}))}onProviderEvent(n,e){p(this.iframeWidow,h.PROVIDER_ON_EVENT,{event:n,params:e})}forwardRpcResponseToIframe(n){p(this.iframeWidow,h.PROVIDER_RPC_RESPONSE,n)}}function S(){return typeof window>"u"||!window?"":`https://swap-dev-git-${window.location.hostname.replace("widget-configurator-git-","").replace("-cowswap.vercel.app","")}-cowswap.vercel.app`}const A={local:"http://localhost:3000",prod:"https://swap.cow.fi",dev:"https://dev.swap.cow.fi",pr:S()},l="_";function L(t){const n=A[t.env||"prod"],e=v(t),s=C(t);return n+"/#"+e+"?"+s}function v(t){const{chainId:n=1,sell:e,buy:s,tradeType:i=w.SWAP}=t,r=[(e==null?void 0:e.asset)||l,(s==null?void 0:s.asset)||l].map(encodeURIComponent).join("/");return`/${n}/widget/${i}/${r}`}function C(t){const{sell:n,buy:e,theme:s}=t,i=new URLSearchParams;return n!=null&&n.amount&&i.append("sellAmount",n.amount),e!=null&&e.amount&&i.append("buyAmount",e.amount),s&&i.append("theme",typeof s=="string"?s:s.baseTheme),i}class V{constructor(){this.subscriptions={}}on(n){const{event:e,handler:s}=n;this.subscriptions[e]||(this.subscriptions[e]=[]),this.subscriptions[e].push(s)}off(n){const{event:e,handler:s}=n;this.subscriptions[e]&&(this.subscriptions[e]=this.subscriptions[e].filter(i=>i!==s))}emit(n,e){this.subscriptions[n]&&this.subscriptions[n].forEach(s=>s(e))}}let y=1;class U{constructor(n,e=[]){this.contentWindow=n,this.eventEmitter=new V,this.listeners=[],this.instanceNumber=y++,this.updateListeners(e),this.widgetListener=u(this.contentWindow,a.EMIT_COW_EVENT,s=>this.eventEmitter.emit(s.event,s.payload))}stopListeningIframe(){m(this.contentWindow,this.widgetListener)}updateListeners(n){for(const e of this.listeners)this.eventEmitter.off(e);this.listeners=n||[];for(const e of this.listeners)this.eventEmitter.on(e)}}const W="640px",N="450px",F=20;function b(t,n){const{params:e,provider:s,listeners:i}=n;let r=s;const o=H(e);t.innerHTML="",t.appendChild(o);const{contentWindow:d}=o;if(!d)throw console.error("Iframe does not contain a window",o),new Error("Iframe does not contain a window!");const E=[];E.push(M(d,e.appCode)),E.push(q(o,e.height));const f=new U(window,i);let P=R(d,null,r);return o.addEventListener("load",()=>T(d,e,r)),{updateParams:c=>T(d,c,r),updateListeners:c=>f.updateListeners(c),updateProvider:c=>{r=c,P=R(d,P,c)},destroy:()=>{f.stopListeningIframe(),E.forEach(c=>window.removeEventListener("message",c)),t.removeChild(o)}}}function R(t,n,e){n&&n.disconnect();const s=n||new O(t);return e&&s.onConnect(e),s}function H(t){const{width:n=N,height:e=W}=t,s=document.createElement("iframe");return s.src=L(t),s.width=n,s.height=e,s.style.border="0",s}function T(t,n,e){const s=!!e,i=v(n),r=C(n).toString();p(t,h.UPDATE_PARAMS,{urlParams:{pathname:i,search:r},appParams:{...n},hasProvider:s})}function M(t,n){const e=u(window,a.ACTIVATE,()=>{m(window,e),p(t,h.UPDATE_APP_DATA,{metaData:n?{appCode:n}:void 0})});return e}function q(t,n=W){return u(window,a.UPDATE_HEIGHT,e=>{t.style.height=e.height?`${e.height+F}px`:n})}exports.COWSWAP_URLS=A;exports.TradeType=w;exports.WidgetMethodsEmit=a;exports.WidgetMethodsListen=h;exports.createCowSwapWidget=b;exports.listenToMessageFromWindow=u;exports.postMessageToWindow=p;exports.stopListeningToMessageFromWindow=g;exports.stopListeningWindowListener=m;
package/index.mjs CHANGED
@@ -1,33 +1,33 @@
1
- const T = "cowSwapWidget";
2
- function h(t, n, e) {
3
- const s = {
4
- key: T,
1
+ const R = "cowSwapWidget";
2
+ function p(t, n, e) {
3
+ const i = {
4
+ key: R,
5
5
  method: n,
6
6
  ...typeof e == "object" ? e : {}
7
7
  };
8
8
  t.postMessage(
9
- s,
9
+ i,
10
10
  "*"
11
11
  // TODO: Change to CoW specific origin in production. https://github.com/cowprotocol/cowswap/issues/3828
12
12
  );
13
13
  }
14
- function _(t, n, e) {
15
- const i = (s) => {
16
- !S(s.data) || s.data.key !== T || s.data.method !== n || e(s.data);
14
+ function u(t, n, e) {
15
+ const s = (i) => {
16
+ !I(i.data) || i.data.key !== R || i.data.method !== n || e(i.data);
17
17
  };
18
- return t.addEventListener("message", i), i;
18
+ return t.addEventListener("message", s), s;
19
19
  }
20
- function S(t) {
20
+ function I(t) {
21
21
  return typeof t == "object" && t !== null && "key" in t && "method" in t && typeof t.key == "string" && typeof t.method == "string";
22
22
  }
23
23
  function C(t, n, e) {
24
24
  t.removeEventListener("message", e);
25
25
  }
26
- function A(t, n) {
26
+ function T(t, n) {
27
27
  t.removeEventListener("message", n);
28
28
  }
29
- var a = /* @__PURE__ */ ((t) => (t.ACTIVATE = "ACTIVATE", t.UPDATE_HEIGHT = "UPDATE_HEIGHT", t.EMIT_COW_EVENT = "EMIT_COW_EVENT", t.PROVIDER_RPC_REQUEST = "PROVIDER_RPC_REQUEST", t))(a || {}), d = /* @__PURE__ */ ((t) => (t.UPDATE_PARAMS = "UPDATE_PARAMS", t.UPDATE_APP_DATA = "UPDATE_APP_DATA", t.PROVIDER_RPC_RESPONSE = "PROVIDER_RPC_RESPONSE", t.PROVIDER_ON_EVENT = "PROVIDER_ON_EVENT", t))(d || {}), w = /* @__PURE__ */ ((t) => (t.SWAP = "swap", t.LIMIT = "limit", t.ADVANCED = "advanced", t))(w || {});
30
- const I = ["connect", "disconnect", "close", "chainChanged", "accountsChanged"];
29
+ var d = /* @__PURE__ */ ((t) => (t.ACTIVATE = "ACTIVATE", t.UPDATE_HEIGHT = "UPDATE_HEIGHT", t.EMIT_COW_EVENT = "EMIT_COW_EVENT", t.PROVIDER_RPC_REQUEST = "PROVIDER_RPC_REQUEST", t))(d || {}), h = /* @__PURE__ */ ((t) => (t.UPDATE_PARAMS = "UPDATE_PARAMS", t.UPDATE_APP_DATA = "UPDATE_APP_DATA", t.PROVIDER_RPC_RESPONSE = "PROVIDER_RPC_RESPONSE", t.PROVIDER_ON_EVENT = "PROVIDER_ON_EVENT", t))(h || {}), _ = /* @__PURE__ */ ((t) => (t.SWAP = "swap", t.LIMIT = "limit", t.ADVANCED = "advanced", t))(_ || {});
30
+ const D = ["connect", "disconnect", "close", "chainChanged", "accountsChanged"];
31
31
  class W {
32
32
  /**
33
33
  * Creates an instance of IframeRpcProviderBridge.
@@ -47,15 +47,15 @@ class W {
47
47
  * Disconnects the JSON-RPC bridge from the Ethereum provider.
48
48
  */
49
49
  disconnect() {
50
- this.ethereumProvider = null, C(window, a.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow);
50
+ this.ethereumProvider = null, C(window, d.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow);
51
51
  }
52
52
  /**
53
53
  * Handles the 'connect' event and sets up event listeners for Ethereum provider events.
54
54
  * @param newProvider - The Ethereum provider to connect.
55
55
  */
56
56
  onConnect(n) {
57
- this.ethereumProvider ? this.disconnect() : _(window, a.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow), this.ethereumProvider = n, this.processPendingRequests(), I.forEach((e) => {
58
- n.on(e, (i) => this.onProviderEvent(e, i));
57
+ this.ethereumProvider ? this.disconnect() : u(window, d.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow), this.ethereumProvider = n, this.processPendingRequests(), D.forEach((e) => {
58
+ n.on(e, (s) => this.onProviderEvent(e, s));
59
59
  });
60
60
  }
61
61
  processPendingRequests() {
@@ -68,21 +68,21 @@ class W {
68
68
  * @param request - The JSON-RPC request to be processed.
69
69
  */
70
70
  processRpcRequest(n) {
71
- const { id: e, jsonrpc: i, method: s } = n;
71
+ const { id: e, jsonrpc: s, method: i } = n;
72
72
  if (!this.ethereumProvider || !e)
73
73
  return;
74
- (s === "enable" ? this.ethereumProvider.enable() : this.ethereumProvider.request({ ...n, id: e })).then(
74
+ (i === "enable" ? this.ethereumProvider.enable() : this.ethereumProvider.request({ ...n, id: e })).then(
75
75
  (o) => this.forwardRpcResponseToIframe({
76
- rpcResponse: { jsonrpc: i, id: e, result: o }
76
+ rpcResponse: { jsonrpc: s, id: e, result: o }
77
77
  })
78
78
  ).catch(
79
79
  (o) => this.forwardRpcResponseToIframe({
80
- rpcResponse: { jsonrpc: i, id: e, error: o }
80
+ rpcResponse: { jsonrpc: s, id: e, error: o }
81
81
  })
82
82
  );
83
83
  }
84
84
  onProviderEvent(n, e) {
85
- h(this.iframeWidow, d.PROVIDER_ON_EVENT, {
85
+ p(this.iframeWidow, h.PROVIDER_ON_EVENT, {
86
86
  event: n,
87
87
  params: e
88
88
  });
@@ -91,58 +91,57 @@ class W {
91
91
  * Forward a JSON-RPC message to the content window.
92
92
  */
93
93
  forwardRpcResponseToIframe(n) {
94
- h(this.iframeWidow, d.PROVIDER_RPC_RESPONSE, n);
94
+ p(this.iframeWidow, h.PROVIDER_RPC_RESPONSE, n);
95
95
  }
96
96
  }
97
- function v() {
97
+ function O() {
98
98
  return typeof window > "u" || !window ? "" : `https://swap-dev-git-${window.location.hostname.replace("widget-configurator-git-", "").replace("-cowswap.vercel.app", "")}-cowswap.vercel.app`;
99
99
  }
100
- const N = {
100
+ const S = {
101
101
  local: "http://localhost:3000",
102
102
  prod: "https://swap.cow.fi",
103
103
  dev: "https://dev.swap.cow.fi",
104
- pr: v()
105
- }, P = "_";
106
- function g(t) {
107
- const n = N[t.env || "prod"], e = O(t), i = l(t);
108
- return n + "/#" + e + "?" + i;
109
- }
110
- function O(t) {
111
- const { chainId: n = 1, sell: e, buy: i, tradeType: s = w.SWAP } = t, r = [(e == null ? void 0 : e.asset) || P, (i == null ? void 0 : i.asset) || P].map(encodeURIComponent).join("/");
112
- return `/${n}/widget/${s}/${r}`;
113
- }
114
- function l(t) {
115
- const { sell: n, buy: e, theme: i } = t, s = new URLSearchParams();
116
- return n != null && n.amount && s.append("sellAmount", n.amount), e != null && e.amount && s.append("buyAmount", e.amount), i && s.append("theme", typeof i == "string" ? i : i.baseTheme), s;
117
- }
118
- var L = /* @__PURE__ */ ((t) => (t.SWAP_ETH_FLOW_SENT_TX = "SWAP_ETH_FLOW_SENT_TX", t.SWAP_POSTED_API = "SWAP_POSTED_API", t.SWAP_SIGNING_ERROR = "SWAP_SIGNING_ERROR", t.SWAP_TRADE_EXECUTED = "SWAP_TRADE_EXECUTED", t.SWAP_ORDER_CANCELLED = "SWAP_ORDER_CANCELLED", t))(L || {}), U = /* @__PURE__ */ ((t) => (t.ON_TOAST_MESSAGE = "ON_TOAST_MESSAGE", t.ON_POSTED_ORDER = "ON_POSTED_ORDER", t.ON_POSTED_ETH_FLOW_ORDER = "ON_POSTED_ETH_FLOW_ORDER", t.ON_EXECUTED_ORDER = "ON_EXECUTED_ORDER", t.ON_CANCELLED_ORDER = "ON_CANCELLED_ORDER", t))(U || {});
119
- class V {
104
+ pr: O()
105
+ }, w = "_";
106
+ function V(t) {
107
+ const n = S[t.env || "prod"], e = A(t), s = v(t);
108
+ return n + "/#" + e + "?" + s;
109
+ }
110
+ function A(t) {
111
+ const { chainId: n = 1, sell: e, buy: s, tradeType: i = _.SWAP } = t, r = [(e == null ? void 0 : e.asset) || w, (s == null ? void 0 : s.asset) || w].map(encodeURIComponent).join("/");
112
+ return `/${n}/widget/${i}/${r}`;
113
+ }
114
+ function v(t) {
115
+ const { sell: n, buy: e, theme: s } = t, i = new URLSearchParams();
116
+ return n != null && n.amount && i.append("sellAmount", n.amount), e != null && e.amount && i.append("buyAmount", e.amount), s && i.append("theme", typeof s == "string" ? s : s.baseTheme), i;
117
+ }
118
+ class L {
120
119
  constructor() {
121
120
  this.subscriptions = {};
122
121
  }
123
122
  on(n) {
124
- const { event: e, handler: i } = n;
125
- this.subscriptions[e] || (this.subscriptions[e] = []), this.subscriptions[e].push(i);
123
+ const { event: e, handler: s } = n;
124
+ this.subscriptions[e] || (this.subscriptions[e] = []), this.subscriptions[e].push(s);
126
125
  }
127
126
  off(n) {
128
- const { event: e, handler: i } = n;
129
- this.subscriptions[e] && (this.subscriptions[e] = this.subscriptions[e].filter((s) => s !== i));
127
+ const { event: e, handler: s } = n;
128
+ this.subscriptions[e] && (this.subscriptions[e] = this.subscriptions[e].filter((i) => i !== s));
130
129
  }
131
130
  emit(n, e) {
132
- this.subscriptions[n] && this.subscriptions[n].forEach((i) => i(e));
131
+ this.subscriptions[n] && this.subscriptions[n].forEach((s) => s(e));
133
132
  }
134
133
  }
135
- let H = 1;
136
- class F {
134
+ let U = 1;
135
+ class y {
137
136
  constructor(n, e = []) {
138
- this.contentWindow = n, this.eventEmitter = new V(), this.listeners = [], this.instanceNumber = H++, this.updateListeners(e), this.widgetListener = _(
137
+ this.contentWindow = n, this.eventEmitter = new L(), this.listeners = [], this.instanceNumber = U++, this.updateListeners(e), this.widgetListener = u(
139
138
  this.contentWindow,
140
- a.EMIT_COW_EVENT,
141
- (i) => this.eventEmitter.emit(i.event, i.payload)
139
+ d.EMIT_COW_EVENT,
140
+ (s) => this.eventEmitter.emit(s.event, s.payload)
142
141
  );
143
142
  }
144
143
  stopListeningIframe() {
145
- A(this.contentWindow, this.widgetListener);
144
+ T(this.contentWindow, this.widgetListener);
146
145
  }
147
146
  updateListeners(n) {
148
147
  for (const e of this.listeners)
@@ -152,76 +151,73 @@ class F {
152
151
  this.eventEmitter.on(e);
153
152
  }
154
153
  }
155
- const D = "640px", b = "450px", y = 20;
156
- function X(t, n) {
157
- const { params: e, provider: i, listeners: s } = n;
158
- let r = i;
159
- const o = G(e);
154
+ const g = "640px", N = "450px", H = 20;
155
+ function M(t, n) {
156
+ const { params: e, provider: s, listeners: i } = n;
157
+ let r = s;
158
+ const o = F(e);
160
159
  t.innerHTML = "", t.appendChild(o);
161
- const { contentWindow: E } = o;
162
- if (!E)
160
+ const { contentWindow: a } = o;
161
+ if (!a)
163
162
  throw console.error("Iframe does not contain a window", o), new Error("Iframe does not contain a window!");
164
- const u = [];
165
- u.push(q(E, e.appCode)), u.push(M(o, e.height));
166
- const p = new F(window, s);
167
- let R = m(E, null, r);
168
- return o.addEventListener("load", () => f(E, e, r)), {
169
- updateParams: (c) => f(E, c, r),
170
- updateListeners: (c) => p.updateListeners(c),
163
+ const E = [];
164
+ E.push(b(a, e.appCode)), E.push(q(o, e.height));
165
+ const m = new y(window, i);
166
+ let f = P(a, null, r);
167
+ return o.addEventListener("load", () => l(a, e, r)), {
168
+ updateParams: (c) => l(a, c, r),
169
+ updateListeners: (c) => m.updateListeners(c),
171
170
  updateProvider: (c) => {
172
- r = c, R = m(E, R, c);
171
+ r = c, f = P(a, f, c);
173
172
  },
174
173
  destroy: () => {
175
- p.stopListeningIframe(), u.forEach((c) => window.removeEventListener("message", c)), t.removeChild(o);
174
+ m.stopListeningIframe(), E.forEach((c) => window.removeEventListener("message", c)), t.removeChild(o);
176
175
  }
177
176
  };
178
177
  }
179
- function m(t, n, e) {
178
+ function P(t, n, e) {
180
179
  n && n.disconnect();
181
- const i = n || new W(t);
182
- return e && i.onConnect(e), i;
180
+ const s = n || new W(t);
181
+ return e && s.onConnect(e), s;
183
182
  }
184
- function G(t) {
185
- const { width: n = b, height: e = D } = t, i = document.createElement("iframe");
186
- return i.src = g(t), i.width = n, i.height = e, i.style.border = "0", i;
183
+ function F(t) {
184
+ const { width: n = N, height: e = g } = t, s = document.createElement("iframe");
185
+ return s.src = V(t), s.width = n, s.height = e, s.style.border = "0", s;
187
186
  }
188
- function f(t, n, e) {
189
- const i = !!e, s = O(n), r = l(n).toString();
190
- h(t, d.UPDATE_PARAMS, {
187
+ function l(t, n, e) {
188
+ const s = !!e, i = A(n), r = v(n).toString();
189
+ p(t, h.UPDATE_PARAMS, {
191
190
  urlParams: {
192
- pathname: s,
191
+ pathname: i,
193
192
  search: r
194
193
  },
195
194
  appParams: {
196
195
  ...n
197
196
  },
198
- hasProvider: i
197
+ hasProvider: s
199
198
  });
200
199
  }
201
- function q(t, n) {
202
- const e = _(window, a.ACTIVATE, () => {
203
- A(window, e), h(t, d.UPDATE_APP_DATA, {
200
+ function b(t, n) {
201
+ const e = u(window, d.ACTIVATE, () => {
202
+ T(window, e), p(t, h.UPDATE_APP_DATA, {
204
203
  metaData: n ? { appCode: n } : void 0
205
204
  });
206
205
  });
207
206
  return e;
208
207
  }
209
- function M(t, n = D) {
210
- return _(window, a.UPDATE_HEIGHT, (e) => {
211
- t.style.height = e.height ? `${e.height + y}px` : n;
208
+ function q(t, n = g) {
209
+ return u(window, d.UPDATE_HEIGHT, (e) => {
210
+ t.style.height = e.height ? `${e.height + H}px` : n;
212
211
  });
213
212
  }
214
213
  export {
215
- N as COWSWAP_URLS,
216
- U as CowEvents,
217
- V as SimpleCowEventEmitter,
218
- L as ToastMessageType,
219
- w as TradeType,
220
- a as WidgetMethodsEmit,
221
- d as WidgetMethodsListen,
222
- X as createCowSwapWidget,
223
- _ as listenToMessageFromWindow,
224
- h as postMessageToWindow,
214
+ S as COWSWAP_URLS,
215
+ _ as TradeType,
216
+ d as WidgetMethodsEmit,
217
+ h as WidgetMethodsListen,
218
+ M as createCowSwapWidget,
219
+ u as listenToMessageFromWindow,
220
+ p as postMessageToWindow,
225
221
  C as stopListeningToMessageFromWindow,
226
- A as stopListeningWindowListener
222
+ T as stopListeningWindowListener
227
223
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cowprotocol/widget-lib",
3
- "version": "0.6.0-RC.1",
3
+ "version": "0.6.0-RC.2",
4
4
  "type": "commonjs",
5
5
  "description": "CoW Swap Widget Library. Allows you to easily embed a CoW Swap widget on your website.",
6
6
  "main": "index.js",
@@ -20,5 +20,7 @@
20
20
  "dex",
21
21
  "widget-lib"
22
22
  ],
23
- "dependencies": {}
23
+ "dependencies": {
24
+ "@cowprotocol/events": "^0.1.0-RC.0"
25
+ }
24
26
  }