@cowprotocol/widget-lib 0.7.1 → 0.9.0-RC.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.
@@ -4,7 +4,6 @@ export declare class IframeCowEventEmitter {
4
4
  private eventEmitter;
5
5
  private listeners;
6
6
  private widgetListener;
7
- private instanceNumber;
8
7
  constructor(contentWindow: Window, listeners?: CowEventListeners);
9
8
  stopListeningIframe(): void;
10
9
  updateListeners(listeners?: CowEventListeners): void;
@@ -0,0 +1,8 @@
1
+ export declare class IframeSafeSdkBridge {
2
+ private appWindow;
3
+ private iframeWidow;
4
+ forwardSdkMessage: (event: MessageEvent<unknown>) => void;
5
+ constructor(appWindow: Window, iframeWidow: Window);
6
+ private startListening;
7
+ stopListening(): void;
8
+ }
package/index.d.ts CHANGED
@@ -2,3 +2,4 @@ export { createCowSwapWidget } from './cowSwapWidget';
2
2
  export type { CowSwapWidgetHandler } from './cowSwapWidget';
3
3
  export * from './types';
4
4
  export * from './messages';
5
+ export * from './themeUtils';
package/index.iife.js CHANGED
@@ -1 +1 @@
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}({});
1
+ var cowSwapWidget=function(o){"use strict";const T="cowSwapWidget";function f(e,n,t){const s={key:T,method:n,...typeof t=="object"?t:{}};e.postMessage(s,"*")}function E(e,n,t){const i=s=>{!D(s.data)||s.data.key!==T||s.data.method!==n||t(s.data)};return e.addEventListener("message",i),i}function D(e){return typeof e=="object"&&e!==null&&"key"in e&&"method"in e&&typeof e.key=="string"&&typeof e.method=="string"}function R(e,n,t){e.removeEventListener("message",t)}function m(e,n){e.removeEventListener("message",n)}var h=(e=>(e.ACTIVATE="ACTIVATE",e.UPDATE_HEIGHT="UPDATE_HEIGHT",e.EMIT_COW_EVENT="EMIT_COW_EVENT",e.PROVIDER_RPC_REQUEST="PROVIDER_RPC_REQUEST",e))(h||{}),p=(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))(p||{}),w=(e=>(e.SWAP="swap",e.LIMIT="limit",e.ADVANCED="advanced",e))(w||{});const L=["primary","background","paper","text","danger","warning","alert","info","success"],O=["connect","disconnect","close","chainChanged","accountsChanged"];class V{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,R(window,h.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow)}onConnect(n){this.ethereumProvider?this.disconnect():E(window,h.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),this.ethereumProvider=n,this.processPendingRequests(),O.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(a=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:i,id:t,result:a}})).catch(a=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:i,id:t,error:a}}))}onProviderEvent(n,t){f(this.iframeWidow,p.PROVIDER_ON_EVENT,{event:n,params:t})}forwardRpcResponseToIframe(n){f(this.iframeWidow,p.PROVIDER_RPC_RESPONSE,n)}}function g(e){return!!(e&&typeof e=="object")}const _="_";function y(e){const n=typeof e.baseUrl=="string"?e.baseUrl:"https://swap.cow.fi",t=A(e);return n+"/#"+t+"?"+W(e)}function A(e){const{chainId:n=1,sell:t,buy:i,tradeType:s=w.SWAP}=e,r=[(t==null?void 0:t.asset)||_,(i==null?void 0:i.asset)||_].map(encodeURIComponent).join("/");return`/${n}/widget/${s}/${r}`}function W(e){const n=new URLSearchParams;return U(M(n,e),e)}function M(e,n){const{sell:t,buy:i}=n;return t!=null&&t.amount&&e.append("sellAmount",t.amount),i!=null&&i.amount&&e.append("buyAmount",i.amount),e}function U(e,n){const t=n.theme;return t&&(g(t)?(e.append("palette",encodeURIComponent(JSON.stringify(t))),e.append("theme",t.baseTheme)):e.append("theme",t)),e}class b{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))}}class F{constructor(n,t=[]){this.contentWindow=n,this.eventEmitter=new b,this.listeners=[],this.updateListeners(t),this.widgetListener=E(this.contentWindow,h.EMIT_COW_EVENT,i=>this.eventEmitter.emit(i.event,i.payload))}stopListeningIframe(){m(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)}}class N{constructor(n,t){this.appWindow=n,this.iframeWidow=t,this.forwardSdkMessage=i=>{H(i.data)&&(k(i.data)?this.appWindow.parent.postMessage(i.data,"*"):G(i.data)&&this.iframeWidow.postMessage(i.data,"*"))},this.startListening()}startListening(){this.appWindow.addEventListener("message",this.forwardSdkMessage)}stopListening(){this.appWindow.removeEventListener("message",this.forwardSdkMessage)}}function H(e){return typeof e=="object"&&e!==null&&"id"in e&&typeof e.id=="string"}function k(e){return"method"in e&&typeof e.method=="string"&&"params"in e&&"env"in e&&typeof e.env=="object"&&e.env!==null&&"sdkVersion"in e.env}function G(e){return"success"in e&&typeof e.success=="boolean"&&"version"in e&&typeof e.version=="string"}const v="640px",Q="450px",q=20;function B(e,n){const{params:t,provider:i,listeners:s}=n;let r=i,a=t;const c=$(t);e.innerHTML="",e.appendChild(c);const{contentWindow:u}=c;if(!u)throw console.error("Iframe does not contain a window",c),new Error("Iframe does not contain a window!");const l=[];l.push(K(u,t.appCode)),l.push(Y(c,t.height));const S=new F(window,s);let P=C(u,null,r);c.addEventListener("load",()=>I(u,a,r));const j=new N(window,u);return{updateParams:d=>{a=d,I(u,a,r)},updateListeners:d=>S.updateListeners(d),updateProvider:d=>{r=d,P=C(u,P,d)},destroy:()=>{P.disconnect(),S.stopListeningIframe(),l.forEach(d=>window.removeEventListener("message",d)),j.stopListening(),e.removeChild(c)}}}function C(e,n,t){n&&n.disconnect();const i=n||new V(e);return t&&i.onConnect(t),i}function $(e){const{width:n=Q,height:t=v}=e,i=document.createElement("iframe");return i.src=y(e),i.width=n,i.height=t,i.style.border="0",i}function I(e,n,t){const i=!!t,s=A(n),r=W(n).toString(),{theme:a,...c}=n;f(e,p.UPDATE_PARAMS,{urlParams:{pathname:s,search:r},appParams:c,hasProvider:i})}function K(e,n){const t=E(window,h.ACTIVATE,()=>{m(window,t),f(e,p.UPDATE_APP_DATA,{metaData:n?{appCode:n}:void 0})});return t}function Y(e,n=v){return E(window,h.UPDATE_HEIGHT,t=>{e.style.height=t.height?`${t.height+q}px`:n})}return o.TradeType=w,o.WIDGET_PALETTE_COLORS=L,o.WidgetMethodsEmit=h,o.WidgetMethodsListen=p,o.createCowSwapWidget=B,o.isCowSwapWidgetPalette=g,o.listenToMessageFromWindow=E,o.postMessageToWindow=f,o.stopListeningToMessageFromWindow=R,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 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;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const _="cowSwapWidget";function u(e,n,t){const i={key:_,method:n,...typeof t=="object"?t:{}};e.postMessage(i,"*")}function f(e,n,t){const s=i=>{!D(i.data)||i.data.key!==_||i.data.method!==n||t(i.data)};return e.addEventListener("message",s),s}function D(e){return typeof e=="object"&&e!==null&&"key"in e&&"method"in e&&typeof e.key=="string"&&typeof e.method=="string"}function A(e,n,t){e.removeEventListener("message",t)}function w(e,n){e.removeEventListener("message",n)}var h=(e=>(e.ACTIVATE="ACTIVATE",e.UPDATE_HEIGHT="UPDATE_HEIGHT",e.EMIT_COW_EVENT="EMIT_COW_EVENT",e.PROVIDER_RPC_REQUEST="PROVIDER_RPC_REQUEST",e))(h||{}),p=(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))(p||{}),l=(e=>(e.SWAP="swap",e.LIMIT="limit",e.ADVANCED="advanced",e))(l||{});const L=["primary","background","paper","text","danger","warning","alert","info","success"],O=["connect","disconnect","close","chainChanged","accountsChanged"];class V{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,A(window,h.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow)}onConnect(n){this.ethereumProvider?this.disconnect():f(window,h.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),this.ethereumProvider=n,this.processPendingRequests(),O.forEach(t=>{n.on(t,s=>this.onProviderEvent(t,s))})}processPendingRequests(){Object.keys(this.requestWaitingForConnection).forEach(n=>{this.processRpcRequest(this.requestWaitingForConnection[n])}),this.requestWaitingForConnection={}}processRpcRequest(n){const{id:t,jsonrpc:s,method:i}=n;if(!this.ethereumProvider||!t)return;(i==="enable"?this.ethereumProvider.enable():this.ethereumProvider.request({...n,id:t})).then(r=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:s,id:t,result:r}})).catch(r=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:s,id:t,error:r}}))}onProviderEvent(n,t){u(this.iframeWidow,p.PROVIDER_ON_EVENT,{event:n,params:t})}forwardRpcResponseToIframe(n){u(this.iframeWidow,p.PROVIDER_RPC_RESPONSE,n)}}function W(e){return!!(e&&typeof e=="object")}const T="_";function y(e){const n=typeof e.baseUrl=="string"?e.baseUrl:"https://swap.cow.fi",t=v(e);return n+"/#"+t+"?"+C(e)}function v(e){const{chainId:n=1,sell:t,buy:s,tradeType:i=l.SWAP}=e,o=[(t==null?void 0:t.asset)||T,(s==null?void 0:s.asset)||T].map(encodeURIComponent).join("/");return`/${n}/widget/${i}/${o}`}function C(e){const n=new URLSearchParams;return U(M(n,e),e)}function M(e,n){const{sell:t,buy:s}=n;return t!=null&&t.amount&&e.append("sellAmount",t.amount),s!=null&&s.amount&&e.append("buyAmount",s.amount),e}function U(e,n){const t=n.theme;return t&&(W(t)?(e.append("palette",encodeURIComponent(JSON.stringify(t))),e.append("theme",t.baseTheme)):e.append("theme",t)),e}class b{constructor(){this.subscriptions={}}on(n){const{event:t,handler:s}=n;this.subscriptions[t]||(this.subscriptions[t]=[]),this.subscriptions[t].push(s)}off(n){const{event:t,handler:s}=n;this.subscriptions[t]&&(this.subscriptions[t]=this.subscriptions[t].filter(i=>i!==s))}emit(n,t){this.subscriptions[n]&&this.subscriptions[n].forEach(s=>s(t))}}class F{constructor(n,t=[]){this.contentWindow=n,this.eventEmitter=new b,this.listeners=[],this.updateListeners(t),this.widgetListener=f(this.contentWindow,h.EMIT_COW_EVENT,s=>this.eventEmitter.emit(s.event,s.payload))}stopListeningIframe(){w(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)}}class N{constructor(n,t){this.appWindow=n,this.iframeWidow=t,this.forwardSdkMessage=s=>{H(s.data)&&(k(s.data)?this.appWindow.parent.postMessage(s.data,"*"):G(s.data)&&this.iframeWidow.postMessage(s.data,"*"))},this.startListening()}startListening(){this.appWindow.addEventListener("message",this.forwardSdkMessage)}stopListening(){this.appWindow.removeEventListener("message",this.forwardSdkMessage)}}function H(e){return typeof e=="object"&&e!==null&&"id"in e&&typeof e.id=="string"}function k(e){return"method"in e&&typeof e.method=="string"&&"params"in e&&"env"in e&&typeof e.env=="object"&&e.env!==null&&"sdkVersion"in e.env}function G(e){return"success"in e&&typeof e.success=="boolean"&&"version"in e&&typeof e.version=="string"}const I="640px",Q="450px",q=20;function x(e,n){const{params:t,provider:s,listeners:i}=n;let o=s,r=t;const a=B(t);e.innerHTML="",e.appendChild(a);const{contentWindow:d}=a;if(!d)throw console.error("Iframe does not contain a window",a),new Error("Iframe does not contain a window!");const E=[];E.push($(d,t.appCode)),E.push(K(a,t.height));const P=new F(window,i);let m=R(d,null,o);a.addEventListener("load",()=>g(d,r,o));const S=new N(window,d);return{updateParams:c=>{r=c,g(d,r,o)},updateListeners:c=>P.updateListeners(c),updateProvider:c=>{o=c,m=R(d,m,c)},destroy:()=>{m.disconnect(),P.stopListeningIframe(),E.forEach(c=>window.removeEventListener("message",c)),S.stopListening(),e.removeChild(a)}}}function R(e,n,t){n&&n.disconnect();const s=n||new V(e);return t&&s.onConnect(t),s}function B(e){const{width:n=Q,height:t=I}=e,s=document.createElement("iframe");return s.src=y(e),s.width=n,s.height=t,s.style.border="0",s}function g(e,n,t){const s=!!t,i=v(n),o=C(n).toString(),{theme:r,...a}=n;u(e,p.UPDATE_PARAMS,{urlParams:{pathname:i,search:o},appParams:a,hasProvider:s})}function $(e,n){const t=f(window,h.ACTIVATE,()=>{w(window,t),u(e,p.UPDATE_APP_DATA,{metaData:n?{appCode:n}:void 0})});return t}function K(e,n=I){return f(window,h.UPDATE_HEIGHT,t=>{e.style.height=t.height?`${t.height+q}px`:n})}exports.TradeType=l;exports.WIDGET_PALETTE_COLORS=L;exports.WidgetMethodsEmit=h;exports.WidgetMethodsListen=p;exports.createCowSwapWidget=x;exports.isCowSwapWidgetPalette=W;exports.listenToMessageFromWindow=f;exports.postMessageToWindow=u;exports.stopListeningToMessageFromWindow=A;exports.stopListeningWindowListener=w;
package/index.mjs CHANGED
@@ -1,61 +1,71 @@
1
1
  const T = "cowSwapWidget";
2
- function u(t, n, e) {
2
+ function u(e, n, t) {
3
3
  const i = {
4
4
  key: T,
5
5
  method: n,
6
- ...typeof e == "object" ? e : {}
6
+ ...typeof t == "object" ? t : {}
7
7
  };
8
- t.postMessage(
8
+ e.postMessage(
9
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 E(t, n, e) {
14
+ function f(e, n, t) {
15
15
  const s = (i) => {
16
- !v(i.data) || i.data.key !== T || i.data.method !== n || e(i.data);
16
+ !W(i.data) || i.data.key !== T || i.data.method !== n || t(i.data);
17
17
  };
18
- return t.addEventListener("message", s), s;
18
+ return e.addEventListener("message", s), s;
19
19
  }
20
- function v(t) {
21
- return typeof t == "object" && t !== null && "key" in t && "method" in t && typeof t.key == "string" && typeof t.method == "string";
20
+ function W(e) {
21
+ return typeof e == "object" && e !== null && "key" in e && "method" in e && typeof e.key == "string" && typeof e.method == "string";
22
22
  }
23
- function C(t, n, e) {
24
- t.removeEventListener("message", e);
23
+ function S(e, n, t) {
24
+ e.removeEventListener("message", t);
25
25
  }
26
- function l(t, n) {
27
- t.removeEventListener("message", n);
26
+ function _(e, n) {
27
+ e.removeEventListener("message", n);
28
28
  }
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
- const D = ["connect", "disconnect", "close", "chainChanged", "accountsChanged"];
31
- class W {
29
+ var h = /* @__PURE__ */ ((e) => (e.ACTIVATE = "ACTIVATE", e.UPDATE_HEIGHT = "UPDATE_HEIGHT", e.EMIT_COW_EVENT = "EMIT_COW_EVENT", e.PROVIDER_RPC_REQUEST = "PROVIDER_RPC_REQUEST", e))(h || {}), p = /* @__PURE__ */ ((e) => (e.UPDATE_PARAMS = "UPDATE_PARAMS", e.UPDATE_APP_DATA = "UPDATE_APP_DATA", e.PROVIDER_RPC_RESPONSE = "PROVIDER_RPC_RESPONSE", e.PROVIDER_ON_EVENT = "PROVIDER_ON_EVENT", e))(p || {}), g = /* @__PURE__ */ ((e) => (e.SWAP = "swap", e.LIMIT = "limit", e.ADVANCED = "advanced", e))(g || {});
30
+ const $ = [
31
+ "primary",
32
+ "background",
33
+ "paper",
34
+ "text",
35
+ "danger",
36
+ "warning",
37
+ "alert",
38
+ "info",
39
+ "success"
40
+ ], D = ["connect", "disconnect", "close", "chainChanged", "accountsChanged"];
41
+ class O {
32
42
  /**
33
43
  * Creates an instance of IframeRpcProviderBridge.
34
44
  * @param iframeWidow - The iFrame window that will post up general RPC messages and to which the IframeRpcProviderBridge will forward the RPC result.
35
45
  * Also it will receive some special RPC events coming from the wallet, like connect/chainChanged,accountChanged
36
46
  */
37
47
  constructor(n) {
38
- this.iframeWidow = n, this.ethereumProvider = null, this.requestWaitingForConnection = {}, this.processRpcCallFromWindow = ({ rpcRequest: e }) => {
48
+ this.iframeWidow = n, this.ethereumProvider = null, this.requestWaitingForConnection = {}, this.processRpcCallFromWindow = ({ rpcRequest: t }) => {
39
49
  if (!this.ethereumProvider) {
40
- e.id && (this.requestWaitingForConnection[e.id] = e);
50
+ t.id && (this.requestWaitingForConnection[t.id] = t);
41
51
  return;
42
52
  }
43
- this.processRpcRequest(e);
53
+ this.processRpcRequest(t);
44
54
  };
45
55
  }
46
56
  /**
47
57
  * Disconnects the JSON-RPC bridge from the Ethereum provider.
48
58
  */
49
59
  disconnect() {
50
- this.ethereumProvider = null, C(window, h.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow);
60
+ this.ethereumProvider = null, S(window, h.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow);
51
61
  }
52
62
  /**
53
63
  * Handles the 'connect' event and sets up event listeners for Ethereum provider events.
54
64
  * @param newProvider - The Ethereum provider to connect.
55
65
  */
56
66
  onConnect(n) {
57
- this.ethereumProvider ? this.disconnect() : E(window, h.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow), this.ethereumProvider = n, this.processPendingRequests(), D.forEach((e) => {
58
- n.on(e, (s) => this.onProviderEvent(e, s));
67
+ this.ethereumProvider ? this.disconnect() : f(window, h.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow), this.ethereumProvider = n, this.processPendingRequests(), D.forEach((t) => {
68
+ n.on(t, (s) => this.onProviderEvent(t, s));
59
69
  });
60
70
  }
61
71
  processPendingRequests() {
@@ -68,147 +78,183 @@ class W {
68
78
  * @param request - The JSON-RPC request to be processed.
69
79
  */
70
80
  processRpcRequest(n) {
71
- const { id: e, jsonrpc: s, method: i } = n;
72
- if (!this.ethereumProvider || !e)
81
+ const { id: t, jsonrpc: s, method: i } = n;
82
+ if (!this.ethereumProvider || !t)
73
83
  return;
74
- (i === "enable" ? this.ethereumProvider.enable() : this.ethereumProvider.request({ ...n, id: e })).then(
75
- (o) => this.forwardRpcResponseToIframe({
76
- rpcResponse: { jsonrpc: s, id: e, result: o }
84
+ (i === "enable" ? this.ethereumProvider.enable() : this.ethereumProvider.request({ ...n, id: t })).then(
85
+ (r) => this.forwardRpcResponseToIframe({
86
+ rpcResponse: { jsonrpc: s, id: t, result: r }
77
87
  })
78
88
  ).catch(
79
- (o) => this.forwardRpcResponseToIframe({
80
- rpcResponse: { jsonrpc: s, id: e, error: o }
89
+ (r) => this.forwardRpcResponseToIframe({
90
+ rpcResponse: { jsonrpc: s, id: t, error: r }
81
91
  })
82
92
  );
83
93
  }
84
- onProviderEvent(n, e) {
85
- u(this.iframeWidow, d.PROVIDER_ON_EVENT, {
94
+ onProviderEvent(n, t) {
95
+ u(this.iframeWidow, p.PROVIDER_ON_EVENT, {
86
96
  event: n,
87
- params: e
97
+ params: t
88
98
  });
89
99
  }
90
100
  /**
91
101
  * Forward a JSON-RPC message to the content window.
92
102
  */
93
103
  forwardRpcResponseToIframe(n) {
94
- u(this.iframeWidow, d.PROVIDER_RPC_RESPONSE, n);
104
+ u(this.iframeWidow, p.PROVIDER_RPC_RESPONSE, n);
95
105
  }
96
106
  }
107
+ function L(e) {
108
+ return !!(e && typeof e == "object");
109
+ }
97
110
  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);
100
- return n + "/#" + e + "?" + s;
111
+ function V(e) {
112
+ const n = typeof e.baseUrl == "string" ? e.baseUrl : "https://swap.cow.fi", t = A(e);
113
+ return n + "/#" + t + "?" + v(e);
114
+ }
115
+ function A(e) {
116
+ const { chainId: n = 1, sell: t, buy: s, tradeType: i = g.SWAP } = e, o = [(t == null ? void 0 : t.asset) || P, (s == null ? void 0 : s.asset) || P].map(encodeURIComponent).join("/");
117
+ return `/${n}/widget/${i}/${o}`;
101
118
  }
102
- function A(t) {
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("/");
104
- return `/${n}/widget/${i}/${r}`;
119
+ function v(e) {
120
+ const n = new URLSearchParams();
121
+ return y(U(n, e), e);
105
122
  }
106
- function I(t) {
107
- const { sell: n, buy: e, theme: s } = t, i = new URLSearchParams();
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;
123
+ function U(e, n) {
124
+ const { sell: t, buy: s } = n;
125
+ return t != null && t.amount && e.append("sellAmount", t.amount), s != null && s.amount && e.append("buyAmount", s.amount), e;
109
126
  }
110
- class S {
127
+ function y(e, n) {
128
+ const t = n.theme;
129
+ return t && (L(t) ? (e.append("palette", encodeURIComponent(JSON.stringify(t))), e.append("theme", t.baseTheme)) : e.append("theme", t)), e;
130
+ }
131
+ class b {
111
132
  constructor() {
112
133
  this.subscriptions = {};
113
134
  }
114
135
  on(n) {
115
- const { event: e, handler: s } = n;
116
- this.subscriptions[e] || (this.subscriptions[e] = []), this.subscriptions[e].push(s);
136
+ const { event: t, handler: s } = n;
137
+ this.subscriptions[t] || (this.subscriptions[t] = []), this.subscriptions[t].push(s);
117
138
  }
118
139
  off(n) {
119
- const { event: e, handler: s } = n;
120
- this.subscriptions[e] && (this.subscriptions[e] = this.subscriptions[e].filter((i) => i !== s));
140
+ const { event: t, handler: s } = n;
141
+ this.subscriptions[t] && (this.subscriptions[t] = this.subscriptions[t].filter((i) => i !== s));
121
142
  }
122
- emit(n, e) {
123
- this.subscriptions[n] && this.subscriptions[n].forEach((s) => s(e));
143
+ emit(n, t) {
144
+ this.subscriptions[n] && this.subscriptions[n].forEach((s) => s(t));
124
145
  }
125
146
  }
126
- let V = 1;
127
- class L {
128
- constructor(n, e = []) {
129
- this.contentWindow = n, this.eventEmitter = new S(), this.listeners = [], this.instanceNumber = V++, this.updateListeners(e), this.widgetListener = E(
147
+ class N {
148
+ constructor(n, t = []) {
149
+ this.contentWindow = n, this.eventEmitter = new b(), this.listeners = [], this.updateListeners(t), this.widgetListener = f(
130
150
  this.contentWindow,
131
151
  h.EMIT_COW_EVENT,
132
152
  (s) => this.eventEmitter.emit(s.event, s.payload)
133
153
  );
134
154
  }
135
155
  stopListeningIframe() {
136
- l(this.contentWindow, this.widgetListener);
156
+ _(this.contentWindow, this.widgetListener);
137
157
  }
138
158
  updateListeners(n) {
139
- for (const e of this.listeners)
140
- this.eventEmitter.off(e);
159
+ for (const t of this.listeners)
160
+ this.eventEmitter.off(t);
141
161
  this.listeners = n || [];
142
- for (const e of this.listeners)
143
- this.eventEmitter.on(e);
144
- }
145
- }
146
- const g = "640px", U = "450px", N = 20;
147
- function H(t, n) {
148
- const { params: e, provider: s, listeners: i } = n;
149
- let r = s;
150
- const o = b(e);
151
- t.innerHTML = "", t.appendChild(o);
152
- const { contentWindow: a } = o;
153
- if (!a)
154
- throw console.error("Iframe does not contain a window", o), new Error("Iframe does not contain a window!");
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),
161
- updateListeners: (c) => m.updateListeners(c),
162
+ for (const t of this.listeners)
163
+ this.eventEmitter.on(t);
164
+ }
165
+ }
166
+ class M {
167
+ constructor(n, t) {
168
+ this.appWindow = n, this.iframeWidow = t, this.forwardSdkMessage = (s) => {
169
+ F(s.data) && (H(s.data) ? this.appWindow.parent.postMessage(s.data, "*") : k(s.data) && this.iframeWidow.postMessage(s.data, "*"));
170
+ }, this.startListening();
171
+ }
172
+ startListening() {
173
+ this.appWindow.addEventListener("message", this.forwardSdkMessage);
174
+ }
175
+ stopListening() {
176
+ this.appWindow.removeEventListener("message", this.forwardSdkMessage);
177
+ }
178
+ }
179
+ function F(e) {
180
+ return typeof e == "object" && e !== null && "id" in e && typeof e.id == "string";
181
+ }
182
+ function H(e) {
183
+ return "method" in e && typeof e.method == "string" && "params" in e && "env" in e && typeof e.env == "object" && e.env !== null && "sdkVersion" in e.env;
184
+ }
185
+ function k(e) {
186
+ return "success" in e && typeof e.success == "boolean" && "version" in e && typeof e.version == "string";
187
+ }
188
+ const I = "640px", G = "450px", Q = 20;
189
+ function K(e, n) {
190
+ const { params: t, provider: s, listeners: i } = n;
191
+ let o = s, r = t;
192
+ const a = x(t);
193
+ e.innerHTML = "", e.appendChild(a);
194
+ const { contentWindow: d } = a;
195
+ if (!d)
196
+ throw console.error("Iframe does not contain a window", a), new Error("Iframe does not contain a window!");
197
+ const E = [];
198
+ E.push(q(d, t.appCode)), E.push(B(a, t.height));
199
+ const w = new N(window, i);
200
+ let m = l(d, null, o);
201
+ a.addEventListener("load", () => R(d, r, o));
202
+ const C = new M(window, d);
203
+ return {
204
+ updateParams: (c) => {
205
+ r = c, R(d, r, o);
206
+ },
207
+ updateListeners: (c) => w.updateListeners(c),
162
208
  updateProvider: (c) => {
163
- r = c, f = R(a, f, c);
209
+ o = c, m = l(d, m, c);
164
210
  },
165
211
  destroy: () => {
166
- m.stopListeningIframe(), p.forEach((c) => window.removeEventListener("message", c)), t.removeChild(o);
212
+ m.disconnect(), w.stopListeningIframe(), E.forEach((c) => window.removeEventListener("message", c)), C.stopListening(), e.removeChild(a);
167
213
  }
168
214
  };
169
215
  }
170
- function R(t, n, e) {
216
+ function l(e, n, t) {
171
217
  n && n.disconnect();
172
- const s = n || new W(t);
173
- return e && s.onConnect(e), s;
218
+ const s = n || new O(e);
219
+ return t && s.onConnect(t), s;
174
220
  }
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;
221
+ function x(e) {
222
+ const { width: n = G, height: t = I } = e, s = document.createElement("iframe");
223
+ return s.src = V(e), s.width = n, s.height = t, s.style.border = "0", s;
178
224
  }
179
- function w(t, n, e) {
180
- const s = !!e, i = A(n), r = I(n).toString();
181
- u(t, d.UPDATE_PARAMS, {
225
+ function R(e, n, t) {
226
+ const s = !!t, i = A(n), o = v(n).toString(), { theme: r, ...a } = n;
227
+ u(e, p.UPDATE_PARAMS, {
182
228
  urlParams: {
183
229
  pathname: i,
184
- search: r
185
- },
186
- appParams: {
187
- ...n
230
+ search: o
188
231
  },
232
+ appParams: a,
189
233
  hasProvider: s
190
234
  });
191
235
  }
192
- function y(t, n) {
193
- const e = E(window, h.ACTIVATE, () => {
194
- l(window, e), u(t, d.UPDATE_APP_DATA, {
236
+ function q(e, n) {
237
+ const t = f(window, h.ACTIVATE, () => {
238
+ _(window, t), u(e, p.UPDATE_APP_DATA, {
195
239
  metaData: n ? { appCode: n } : void 0
196
240
  });
197
241
  });
198
- return e;
242
+ return t;
199
243
  }
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;
244
+ function B(e, n = I) {
245
+ return f(window, h.UPDATE_HEIGHT, (t) => {
246
+ e.style.height = t.height ? `${t.height + Q}px` : n;
203
247
  });
204
248
  }
205
249
  export {
206
- _ as TradeType,
250
+ g as TradeType,
251
+ $ as WIDGET_PALETTE_COLORS,
207
252
  h as WidgetMethodsEmit,
208
- d as WidgetMethodsListen,
209
- H as createCowSwapWidget,
210
- E as listenToMessageFromWindow,
253
+ p as WidgetMethodsListen,
254
+ K as createCowSwapWidget,
255
+ L as isCowSwapWidgetPalette,
256
+ f as listenToMessageFromWindow,
211
257
  u as postMessageToWindow,
212
- C as stopListeningToMessageFromWindow,
213
- l as stopListeningWindowListener
258
+ S as stopListeningToMessageFromWindow,
259
+ _ as stopListeningWindowListener
214
260
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cowprotocol/widget-lib",
3
- "version": "0.7.1",
3
+ "version": "0.9.0-RC.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.2.0"
24
+ "@cowprotocol/events": "^1.2.0"
25
25
  }
26
26
  }
@@ -0,0 +1,2 @@
1
+ import { CowSwapTheme, CowSwapWidgetPalette } from './types';
2
+ export declare function isCowSwapWidgetPalette(palette: CowSwapTheme | CowSwapWidgetPalette | undefined): palette is CowSwapWidgetPalette;
package/types.d.ts CHANGED
@@ -82,17 +82,61 @@ export interface PartnerFee {
82
82
  */
83
83
  recipient: string;
84
84
  }
85
- export interface CowSwapWidgetPalette {
85
+ /**
86
+ * ERC-20 token information
87
+ */
88
+ export type TokenInfo = {
89
+ chainId: number;
90
+ address: string;
91
+ name: string;
92
+ decimals: number;
93
+ symbol: string;
94
+ logoURI?: string;
95
+ };
96
+ export declare const WIDGET_PALETTE_COLORS: readonly ["primary", "background", "paper", "text", "danger", "warning", "alert", "info", "success"];
97
+ export type CowSwapWidgetPaletteColors = (typeof WIDGET_PALETTE_COLORS)[number];
98
+ export type CowSwapWidgetPaletteParams = {
99
+ [K in CowSwapWidgetPaletteColors]: string;
100
+ };
101
+ export type CowSwapWidgetPalette = {
86
102
  baseTheme: CowSwapTheme;
87
- primary: string;
88
- background: string;
89
- paper: string;
90
- text: string;
91
- danger: string;
92
- warning: string;
93
- alert: string;
94
- info: string;
95
- success: string;
103
+ } & CowSwapWidgetPaletteParams;
104
+ export interface CowSwapWidgetSounds {
105
+ /**
106
+ * The sound to play when the order is executed. Defaults to world wide famous CoW Swap moooooooooo!
107
+ * Alternatively, you can use a URL to a custom sound file, or set to null to disable the sound.
108
+ */
109
+ postOrder?: string | null;
110
+ /**
111
+ * The sound to play when the order is executed. Defaults to world wide famous CoW Swap happy moooooooooo!
112
+ * Alternatively, you can use a URL to a custom sound file, or set to null to disable the sound.
113
+ */
114
+ orderExecuted?: string | null;
115
+ /**
116
+ * The sound to play when the order is executed. Defaults to world wide famous CoW Swap unhappy moooooooooo!
117
+ * Alternatively, you can use a URL to a custom sound file, or set to null to disable the sound.
118
+ */
119
+ orderError?: string | null;
120
+ }
121
+ export interface CowSwapWidgetImages {
122
+ /**
123
+ * The image to display when the orders table is empty (no orders yet). It defaults to "Yoga CoW" image.
124
+ * Alternatively, you can use a URL to a custom image file, or set to null to disable the image.
125
+ */
126
+ emptyOrders?: string | null;
127
+ }
128
+ export interface CowSwapWidgetBanners {
129
+ /**
130
+ * Banner text: "Use Safe web app..."
131
+ *
132
+ * Conditions for displaying the banner:
133
+ * - Safe-like app is connected to CoW Swap via WalletConnect
134
+ * - Selling native token via Swap
135
+ * - Sell token needs approval
136
+ *
137
+ * If the flag is set to true, the banner will not be displayed
138
+ */
139
+ hideSafeWebAppBanner?: boolean;
96
140
  }
97
141
  export interface CowSwapWidgetParams {
98
142
  /**
@@ -175,40 +219,22 @@ export interface CowSwapWidgetParams {
175
219
  * The theme of the widget UI.
176
220
  */
177
221
  theme?: CowSwapTheme | CowSwapWidgetPalette;
178
- /**
179
- * Allows to set a custom logo for the widget.
180
- */
181
- logoUrl?: string;
182
222
  /**
183
223
  * Customizable images for the widget.
184
224
  */
185
- images?: {
186
- /**
187
- * The image to display when the orders table is empty (no orders yet). It defaults to "Yoga CoW" image.
188
- * Alternatively, you can use a URL to a custom image file, or set to null to disable the image.
189
- */
190
- emptyOrders?: string | null;
191
- };
225
+ images?: CowSwapWidgetImages;
192
226
  /**
193
227
  * Sounds configuration for the app.
194
228
  */
195
- sounds?: {
196
- /**
197
- * The sound to play when the order is executed. Defaults to world wide famous CoW Swap moooooooooo!
198
- * Alternatively, you can use a URL to a custom sound file, or set to null to disable the sound.
199
- */
200
- postOrder?: string | null;
201
- /**
202
- * The sound to play when the order is executed. Defaults to world wide famous CoW Swap happy moooooooooo!
203
- * Alternatively, you can use a URL to a custom sound file, or set to null to disable the sound.
204
- */
205
- orderExecuted?: string | null;
206
- /**
207
- * The sound to play when the order is executed. Defaults to world wide famous CoW Swap unhappy moooooooooo!
208
- * Alternatively, you can use a URL to a custom sound file, or set to null to disable the sound.
209
- */
210
- orderError?: string | null;
211
- };
229
+ sounds?: CowSwapWidgetSounds;
230
+ /**
231
+ * Flags to control the display of banners in the widget.
232
+ */
233
+ banners?: CowSwapWidgetBanners;
234
+ /**
235
+ * In case when widget does not support some tokens, you can provide a list of tokens to be used in the widget
236
+ */
237
+ customTokens?: TokenInfo[];
212
238
  }
213
239
  export interface WidgetMethodsEmitPayloadMap {
214
240
  [WidgetMethodsEmit.ACTIVATE]: void;
@@ -224,12 +250,13 @@ export interface WidgetMethodsListenPayloadMap {
224
250
  }
225
251
  export type WidgetMethodsEmitPayloads = WidgetMethodsEmitPayloadMap[WidgetMethodsEmit];
226
252
  export type WidgetMethodsListenPayloads = WidgetMethodsListenPayloadMap[WidgetMethodsListen];
253
+ export type CowSwapWidgetAppParams = Omit<CowSwapWidgetParams, 'theme'>;
227
254
  export interface UpdateParamsPayload {
228
255
  urlParams: {
229
256
  pathname: string;
230
257
  search: string;
231
258
  };
232
- appParams: CowSwapWidgetParams;
259
+ appParams: CowSwapWidgetAppParams;
233
260
  hasProvider: boolean;
234
261
  }
235
262
  export interface UpdateAppDataPayload {
package/urlUtils.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  import { CowSwapWidgetParams } from './types';
2
2
  export declare function buildWidgetUrl(params: Partial<CowSwapWidgetParams>): string;
3
3
  export declare function buildWidgetPath(params: Partial<CowSwapWidgetParams>): string;
4
- export declare function buildTradeAmountsQuery(params: Partial<CowSwapWidgetParams>): URLSearchParams;
4
+ export declare function buildWidgetUrlQuery(params: Partial<CowSwapWidgetParams>): URLSearchParams;