@cowprotocol/widget-lib 0.6.0-RC.3 → 0.6.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.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  export { createCowSwapWidget } from './cowSwapWidget';
2
2
  export type { CowSwapWidgetHandler } from './cowSwapWidget';
3
- export { COWSWAP_URLS } from './consts';
4
3
  export * from './types';
5
4
  export * from './messages';
package/index.iife.js CHANGED
@@ -1 +1 @@
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}({});
1
+ var cowSwapWidget=function(o){"use strict";const w="cowSwapWidget";function E(t,n,e){const i={key:w,method:n,...typeof e=="object"?e:{}};t.postMessage(i,"*")}function p(t,n,e){const s=i=>{!W(i.data)||i.data.key!==w||i.data.method!==n||e(i.data)};return t.addEventListener("message",s),s}function W(t){return typeof t=="object"&&t!==null&&"key"in t&&"method"in t&&typeof t.key=="string"&&typeof t.method=="string"}function T(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||{}),f=(t=>(t.SWAP="swap",t.LIMIT="limit",t.ADVANCED="advanced",t))(f||{});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,T(window,d.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow)}onConnect(n){this.ethereumProvider?this.disconnect():p(window,d.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(r=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:s,id:e,result:r}})).catch(r=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:s,id:e,error:r}}))}onProviderEvent(n,e){E(this.iframeWidow,h.PROVIDER_ON_EVENT,{event:n,params:e})}forwardRpcResponseToIframe(n){E(this.iframeWidow,h.PROVIDER_RPC_RESPONSE,n)}}const R="_";function S(t){const n=typeof t.baseUrl=="string"?t.baseUrl:"https://swap.cow.fi",e=l(t),s=_(t);return n+"/#"+e+"?"+s}function l(t){const{chainId:n=1,sell:e,buy:s,tradeType:i=f.SWAP}=t,c=[(e==null?void 0:e.asset)||R,(s==null?void 0:s.asset)||R].map(encodeURIComponent).join("/");return`/${n}/widget/${i}/${c}`}function _(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 L{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 V=1;class U{constructor(n,e=[]){this.contentWindow=n,this.eventEmitter=new L,this.listeners=[],this.instanceNumber=V++,this.updateListeners(e),this.widgetListener=p(this.contentWindow,d.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 g="640px",y="450px",b=20;function F(t,n){const{params:e,provider:s,listeners:i}=n;let c=s;const r=N(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 P=[];P.push(H(u,e.appCode)),P.push(M(r,e.height));const I=new U(window,i);let C=A(u,null,c);return r.addEventListener("load",()=>v(u,e,c)),{updateParams:a=>v(u,a,c),updateListeners:a=>I.updateListeners(a),updateProvider:a=>{c=a,C=A(u,C,a)},destroy:()=>{I.stopListeningIframe(),P.forEach(a=>window.removeEventListener("message",a)),t.removeChild(r)}}}function A(t,n,e){n&&n.disconnect();const s=n||new O(t);return e&&s.onConnect(e),s}function N(t){const{width:n=y,height:e=g}=t,s=document.createElement("iframe");return s.src=S(t),s.width=n,s.height=e,s.style.border="0",s}function v(t,n,e){const s=!!e,i=l(n),c=_(n).toString();E(t,h.UPDATE_PARAMS,{urlParams:{pathname:i,search:c},appParams:{...n},hasProvider:s})}function H(t,n){const e=p(window,d.ACTIVATE,()=>{m(window,e),E(t,h.UPDATE_APP_DATA,{metaData:n?{appCode:n}:void 0})});return e}function M(t,n=g){return p(window,d.UPDATE_HEIGHT,e=>{t.style.height=e.height?`${e.height+b}px`:n})}return o.TradeType=f,o.WidgetMethodsEmit=d,o.WidgetMethodsListen=h,o.createCowSwapWidget=F,o.listenToMessageFromWindow=p,o.postMessageToWindow=E,o.stopListeningToMessageFromWindow=T,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 _="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;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const _="cowSwapWidget";function u(t,n,e){const i={key:_,method:n,...typeof e=="object"?e:{}};t.postMessage(i,"*")}function E(t,n,e){const s=i=>{!C(i.data)||i.data.key!==_||i.data.method!==n||e(i.data)};return t.addEventListener("message",s),s}function C(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||{}),f=(t=>(t.SWAP="swap",t.LIMIT="limit",t.ADVANCED="advanced",t))(f||{});const W=["connect","disconnect","close","chainChanged","accountsChanged"];class D{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():E(window,a.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),this.ethereumProvider=n,this.processPendingRequests(),W.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){u(this.iframeWidow,h.PROVIDER_ON_EVENT,{event:n,params:e})}forwardRpcResponseToIframe(n){u(this.iframeWidow,h.PROVIDER_RPC_RESPONSE,n)}}const T="_";function O(t){const n=typeof t.baseUrl=="string"?t.baseUrl:"https://swap.cow.fi",e=A(t),s=I(t);return n+"/#"+e+"?"+s}function A(t){const{chainId:n=1,sell:e,buy:s,tradeType:i=f.SWAP}=t,r=[(e==null?void 0:e.asset)||T,(s==null?void 0:s.asset)||T].map(encodeURIComponent).join("/");return`/${n}/widget/${i}/${r}`}function I(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 S{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 L=1;class V{constructor(n,e=[]){this.contentWindow=n,this.eventEmitter=new S,this.listeners=[],this.instanceNumber=L++,this.updateListeners(e),this.widgetListener=E(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 v="640px",U="450px",y=20;function b(t,n){const{params:e,provider:s,listeners:i}=n;let r=s;const o=F(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 p=[];p.push(N(d,e.appCode)),p.push(H(o,e.height));const P=new V(window,i);let w=R(d,null,r);return o.addEventListener("load",()=>l(d,e,r)),{updateParams:c=>l(d,c,r),updateListeners:c=>P.updateListeners(c),updateProvider:c=>{r=c,w=R(d,w,c)},destroy:()=>{P.stopListeningIframe(),p.forEach(c=>window.removeEventListener("message",c)),t.removeChild(o)}}}function R(t,n,e){n&&n.disconnect();const s=n||new D(t);return e&&s.onConnect(e),s}function F(t){const{width:n=U,height:e=v}=t,s=document.createElement("iframe");return s.src=O(t),s.width=n,s.height=e,s.style.border="0",s}function l(t,n,e){const s=!!e,i=A(n),r=I(n).toString();u(t,h.UPDATE_PARAMS,{urlParams:{pathname:i,search:r},appParams:{...n},hasProvider:s})}function N(t,n){const e=E(window,a.ACTIVATE,()=>{m(window,e),u(t,h.UPDATE_APP_DATA,{metaData:n?{appCode:n}:void 0})});return e}function H(t,n=v){return E(window,a.UPDATE_HEIGHT,e=>{t.style.height=e.height?`${e.height+y}px`:n})}exports.TradeType=f;exports.WidgetMethodsEmit=a;exports.WidgetMethodsListen=h;exports.createCowSwapWidget=b;exports.listenToMessageFromWindow=E;exports.postMessageToWindow=u;exports.stopListeningToMessageFromWindow=g;exports.stopListeningWindowListener=m;
package/index.mjs CHANGED
@@ -1,7 +1,7 @@
1
- const R = "cowSwapWidget";
2
- function p(t, n, e) {
1
+ const T = "cowSwapWidget";
2
+ function u(t, n, e) {
3
3
  const i = {
4
- key: R,
4
+ key: T,
5
5
  method: n,
6
6
  ...typeof e == "object" ? e : {}
7
7
  };
@@ -11,22 +11,22 @@ function p(t, n, e) {
11
11
  // TODO: Change to CoW specific origin in production. https://github.com/cowprotocol/cowswap/issues/3828
12
12
  );
13
13
  }
14
- function u(t, n, e) {
14
+ function E(t, n, e) {
15
15
  const s = (i) => {
16
- !I(i.data) || i.data.key !== R || i.data.method !== n || e(i.data);
16
+ !v(i.data) || i.data.key !== T || i.data.method !== n || e(i.data);
17
17
  };
18
18
  return t.addEventListener("message", s), s;
19
19
  }
20
- function I(t) {
20
+ function v(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 T(t, n) {
26
+ function l(t, n) {
27
27
  t.removeEventListener("message", n);
28
28
  }
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))(_ || {});
29
+ var h = /* @__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))(h || {}), 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 || {}), _ = /* @__PURE__ */ ((t) => (t.SWAP = "swap", t.LIMIT = "limit", t.ADVANCED = "advanced", t))(_ || {});
30
30
  const D = ["connect", "disconnect", "close", "chainChanged", "accountsChanged"];
31
31
  class W {
32
32
  /**
@@ -47,14 +47,14 @@ class W {
47
47
  * Disconnects the JSON-RPC bridge from the Ethereum provider.
48
48
  */
49
49
  disconnect() {
50
- this.ethereumProvider = null, C(window, d.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow);
50
+ this.ethereumProvider = null, C(window, h.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() : u(window, d.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow), this.ethereumProvider = n, this.processPendingRequests(), D.forEach((e) => {
57
+ this.ethereumProvider ? this.disconnect() : E(window, h.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow), this.ethereumProvider = n, this.processPendingRequests(), D.forEach((e) => {
58
58
  n.on(e, (s) => this.onProviderEvent(e, s));
59
59
  });
60
60
  }
@@ -82,7 +82,7 @@ class W {
82
82
  );
83
83
  }
84
84
  onProviderEvent(n, e) {
85
- p(this.iframeWidow, h.PROVIDER_ON_EVENT, {
85
+ u(this.iframeWidow, d.PROVIDER_ON_EVENT, {
86
86
  event: n,
87
87
  params: e
88
88
  });
@@ -91,31 +91,23 @@ class W {
91
91
  * Forward a JSON-RPC message to the content window.
92
92
  */
93
93
  forwardRpcResponseToIframe(n) {
94
- p(this.iframeWidow, h.PROVIDER_RPC_RESPONSE, n);
94
+ u(this.iframeWidow, d.PROVIDER_RPC_RESPONSE, n);
95
95
  }
96
96
  }
97
- function O() {
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
- }
100
- const S = {
101
- local: "http://localhost:3000",
102
- prod: "https://swap.cow.fi",
103
- dev: "https://dev.swap.cow.fi",
104
- pr: O()
105
- }, w = "_";
106
- function V(t) {
107
- const n = S[t.env || "prod"], e = A(t), s = v(t);
97
+ const P = "_";
98
+ function O(t) {
99
+ const n = typeof t.baseUrl == "string" ? t.baseUrl : "https://swap.cow.fi", e = A(t), s = I(t);
108
100
  return n + "/#" + e + "?" + s;
109
101
  }
110
102
  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("/");
103
+ const { chainId: n = 1, sell: e, buy: s, tradeType: i = _.SWAP } = t, r = [(e == null ? void 0 : e.asset) || P, (s == null ? void 0 : s.asset) || P].map(encodeURIComponent).join("/");
112
104
  return `/${n}/widget/${i}/${r}`;
113
105
  }
114
- function v(t) {
106
+ function I(t) {
115
107
  const { sell: n, buy: e, theme: s } = t, i = new URLSearchParams();
116
108
  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
109
  }
118
- class L {
110
+ class S {
119
111
  constructor() {
120
112
  this.subscriptions = {};
121
113
  }
@@ -131,17 +123,17 @@ class L {
131
123
  this.subscriptions[n] && this.subscriptions[n].forEach((s) => s(e));
132
124
  }
133
125
  }
134
- let U = 1;
135
- class y {
126
+ let V = 1;
127
+ class L {
136
128
  constructor(n, e = []) {
137
- this.contentWindow = n, this.eventEmitter = new L(), this.listeners = [], this.instanceNumber = U++, this.updateListeners(e), this.widgetListener = u(
129
+ this.contentWindow = n, this.eventEmitter = new S(), this.listeners = [], this.instanceNumber = V++, this.updateListeners(e), this.widgetListener = E(
138
130
  this.contentWindow,
139
- d.EMIT_COW_EVENT,
131
+ h.EMIT_COW_EVENT,
140
132
  (s) => this.eventEmitter.emit(s.event, s.payload)
141
133
  );
142
134
  }
143
135
  stopListeningIframe() {
144
- T(this.contentWindow, this.widgetListener);
136
+ l(this.contentWindow, this.widgetListener);
145
137
  }
146
138
  updateListeners(n) {
147
139
  for (const e of this.listeners)
@@ -151,42 +143,42 @@ class y {
151
143
  this.eventEmitter.on(e);
152
144
  }
153
145
  }
154
- const g = "640px", N = "450px", H = 20;
155
- function M(t, n) {
146
+ const g = "640px", U = "450px", N = 20;
147
+ function H(t, n) {
156
148
  const { params: e, provider: s, listeners: i } = n;
157
149
  let r = s;
158
- const o = F(e);
150
+ const o = b(e);
159
151
  t.innerHTML = "", t.appendChild(o);
160
152
  const { contentWindow: a } = o;
161
153
  if (!a)
162
154
  throw console.error("Iframe does not contain a window", o), new Error("Iframe does not contain a window!");
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),
155
+ const p = [];
156
+ p.push(y(a, e.appCode)), p.push(F(o, e.height));
157
+ const m = new L(window, i);
158
+ let f = R(a, null, r);
159
+ return o.addEventListener("load", () => w(a, e, r)), {
160
+ updateParams: (c) => w(a, c, r),
169
161
  updateListeners: (c) => m.updateListeners(c),
170
162
  updateProvider: (c) => {
171
- r = c, f = P(a, f, c);
163
+ r = c, f = R(a, f, c);
172
164
  },
173
165
  destroy: () => {
174
- m.stopListeningIframe(), E.forEach((c) => window.removeEventListener("message", c)), t.removeChild(o);
166
+ m.stopListeningIframe(), p.forEach((c) => window.removeEventListener("message", c)), t.removeChild(o);
175
167
  }
176
168
  };
177
169
  }
178
- function P(t, n, e) {
170
+ function R(t, n, e) {
179
171
  n && n.disconnect();
180
172
  const s = n || new W(t);
181
173
  return e && s.onConnect(e), s;
182
174
  }
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;
175
+ function b(t) {
176
+ const { width: n = U, height: e = g } = t, s = document.createElement("iframe");
177
+ return s.src = O(t), s.width = n, s.height = e, s.style.border = "0", s;
186
178
  }
187
- function l(t, n, e) {
188
- const s = !!e, i = A(n), r = v(n).toString();
189
- p(t, h.UPDATE_PARAMS, {
179
+ function w(t, n, e) {
180
+ const s = !!e, i = A(n), r = I(n).toString();
181
+ u(t, d.UPDATE_PARAMS, {
190
182
  urlParams: {
191
183
  pathname: i,
192
184
  search: r
@@ -197,27 +189,26 @@ function l(t, n, e) {
197
189
  hasProvider: s
198
190
  });
199
191
  }
200
- function b(t, n) {
201
- const e = u(window, d.ACTIVATE, () => {
202
- T(window, e), p(t, h.UPDATE_APP_DATA, {
192
+ function y(t, n) {
193
+ const e = E(window, h.ACTIVATE, () => {
194
+ l(window, e), u(t, d.UPDATE_APP_DATA, {
203
195
  metaData: n ? { appCode: n } : void 0
204
196
  });
205
197
  });
206
198
  return e;
207
199
  }
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;
200
+ function F(t, n = g) {
201
+ return E(window, h.UPDATE_HEIGHT, (e) => {
202
+ t.style.height = e.height ? `${e.height + N}px` : n;
211
203
  });
212
204
  }
213
205
  export {
214
- S as COWSWAP_URLS,
215
206
  _ as TradeType,
216
- d as WidgetMethodsEmit,
217
- h as WidgetMethodsListen,
218
- M as createCowSwapWidget,
219
- u as listenToMessageFromWindow,
220
- p as postMessageToWindow,
207
+ h as WidgetMethodsEmit,
208
+ d as WidgetMethodsListen,
209
+ H as createCowSwapWidget,
210
+ E as listenToMessageFromWindow,
211
+ u as postMessageToWindow,
221
212
  C as stopListeningToMessageFromWindow,
222
- T as stopListeningWindowListener
213
+ l as stopListeningWindowListener
223
214
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cowprotocol/widget-lib",
3
- "version": "0.6.0-RC.3",
3
+ "version": "0.6.0",
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",
@@ -21,6 +21,6 @@
21
21
  "widget-lib"
22
22
  ],
23
23
  "dependencies": {
24
- "@cowprotocol/events": "^0.1.0-RC.1"
24
+ "@cowprotocol/events": "^0.1.0"
25
25
  }
26
26
  }
package/types.d.ts CHANGED
@@ -42,7 +42,6 @@ export interface EthereumProvider {
42
42
  */
43
43
  enable(): Promise<void>;
44
44
  }
45
- export type CowSwapWidgetEnv = 'local' | 'prod' | 'dev' | 'pr';
46
45
  export type CowSwapTheme = 'dark' | 'light';
47
46
  /**
48
47
  *Trade asset parameters, for example:
@@ -122,9 +121,13 @@ export interface CowSwapWidgetParams {
122
121
  */
123
122
  tradeType?: TradeType;
124
123
  /**
125
- * The environment of the widget. Default: prod
124
+ * The base url of the widget implementation
125
+ *
126
+ * The parameter can have the URL directly, or an object with the environment property,
127
+ *
128
+ * The base URL will default to the production environment if not specified, so it will use https://swap.cow.fi by default.
126
129
  */
127
- env?: CowSwapWidgetEnv;
130
+ baseUrl?: string;
128
131
  /**
129
132
  * Sell token, and optionally the amount.
130
133
  */
@@ -163,9 +166,13 @@ export interface CowSwapWidgetParams {
163
166
  */
164
167
  hideNetworkSelector?: boolean;
165
168
  /**
166
- * Hides the connect buttons, and the connected account button. Defaults to false.
169
+ * Defines the widget mode.
170
+ * - `true` (standalone mode): The widget is standalone, so it will use its own Ethereum provider. The user can connect from within the widget.
171
+ * - `false` (dapp mode): The widget is embedded in a dapp which is responsible of providing the Ethereum provider. Therefore, there won't be a connect button in the widget as this should happen in the host app.
172
+ *
173
+ * Defaults to standalone.
167
174
  */
168
- hideConnectButton?: boolean;
175
+ standaloneMode?: boolean;
169
176
  /**
170
177
  * The theme of the widget UI.
171
178
  */
package/consts.d.ts DELETED
@@ -1,2 +0,0 @@
1
- import { CowSwapWidgetEnv } from './types';
2
- export declare const COWSWAP_URLS: Record<CowSwapWidgetEnv, string>;