@cowprotocol/widget-lib 0.10.1 → 0.11.1

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.
@@ -1,4 +1,5 @@
1
- import { CowEventListeners } from '@cowprotocol/events';
1
+ import { CowEventListeners } from '../../events/src/index.ts';
2
+
2
3
  export declare class IframeCowEventEmitter {
3
4
  private contentWindow;
4
5
  private eventEmitter;
@@ -1,4 +1,5 @@
1
1
  import { EthereumProvider, JsonRpcRequestMessage } from './types';
2
+
2
3
  /**
3
4
  * Handles JSON-RPC request comming from an iFrame by delegating to a given Ethereum provider.
4
5
  * The result will be passed back to the iFrame.
@@ -1,5 +1,6 @@
1
- import { CowEventListeners } from '@cowprotocol/events';
2
1
  import { CowSwapWidgetParams, CowSwapWidgetProps, EthereumProvider } from './types';
2
+ import { CowEventListeners } from '../../events/src/index.ts';
3
+
3
4
  /**
4
5
  * Callback function signature for updating the CoW Swap Widget.
5
6
  */
package/index.iife.js CHANGED
@@ -1 +1 @@
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 g(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||{}),m=(e=>(e.SWAP="swap",e.LIMIT="limit",e.ADVANCED="advanced",e))(m||{});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,g(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 R(e){return!!(e&&typeof e=="object")}const _="_";function M(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=m.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 y(U(n,e),e)}function U(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 y(e,n){const t=n.theme;return t?(R(t)?(e.append("palette",encodeURIComponent(JSON.stringify(t))),e.append("theme",t.baseTheme)):(e.append("palette","null"),e.append("theme",t)),e):(e.append("palette","null"),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(){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=i=>{H(i.data)&&i.origin!==window.location.origin&&(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",B=20;function $(e,n){const{params:t,provider:i,listeners:s}=n;let r=i,a=t;const c=q(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 q(e){const{width:n=Q,height:t=v}=e,i=document.createElement("iframe");return i.src=M(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,()=>{w(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+B}px`:n})}return o.TradeType=m,o.WIDGET_PALETTE_COLORS=L,o.WidgetMethodsEmit=h,o.WidgetMethodsListen=p,o.createCowSwapWidget=$,o.isCowSwapWidgetPalette=R,o.listenToMessageFromWindow=E,o.postMessageToWindow=f,o.stopListeningToMessageFromWindow=g,o.stopListeningWindowListener=w,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"}),o}({});
1
+ var cowSwapWidget=function(o){"use strict";class L{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))}}const P="cowSwapWidget";function f(e,n,t){const s={key:P,method:n,...typeof t=="object"?t:{}};e.postMessage(s,"*")}function p(e,n,t){const i=s=>{!D(s.data)||s.data.key!==P||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 g(e,n,t){e.removeEventListener("message",t)}function w(e,n){e.removeEventListener("message",n)}var a=(e=>(e.ACTIVATE="ACTIVATE",e.UPDATE_HEIGHT="UPDATE_HEIGHT",e.SET_FULL_HEIGHT="SET_FULL_HEIGHT",e.EMIT_COW_EVENT="EMIT_COW_EVENT",e.PROVIDER_RPC_REQUEST="PROVIDER_RPC_REQUEST",e))(a||{}),E=(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))(E||{}),m=(e=>(e.SWAP="swap",e.LIMIT="limit",e.ADVANCED="advanced",e))(m||{});const O=["primary","background","paper","text","danger","warning","alert","info","success"];class U{constructor(n,t=[]){this.contentWindow=n,this.eventEmitter=new L,this.listeners=[],this.updateListeners(t),this.widgetListener=p(this.contentWindow,a.EMIT_COW_EVENT,i=>this.eventEmitter.emit(i.event,i.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)}}const V=["connect","disconnect","close","chainChanged","accountsChanged"];class y{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,g(window,a.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow)}onConnect(n){this.ethereumProvider?this.disconnect():p(window,a.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),this.ethereumProvider=n,this.processPendingRequests(),V.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(c=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:i,id:t,result:c}})).catch(c=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:i,id:t,error:c}}))}onProviderEvent(n,t){f(this.iframeWidow,E.PROVIDER_ON_EVENT,{event:n,params:t})}forwardRpcResponseToIframe(n){f(this.iframeWidow,E.PROVIDER_RPC_RESPONSE,n)}}class H{constructor(n,t){this.appWindow=n,this.iframeWidow=t,this.forwardSdkMessage=i=>{M(i.data)&&i.origin!==window.location.origin&&(b(i.data)?this.appWindow.parent.postMessage(i.data,"*"):F(i.data)&&this.iframeWidow.postMessage(i.data,"*"))},this.startListening()}startListening(){this.appWindow.addEventListener("message",this.forwardSdkMessage)}stopListening(){this.appWindow.removeEventListener("message",this.forwardSdkMessage)}}function M(e){return typeof e=="object"&&e!==null&&"id"in e&&typeof e.id=="string"}function b(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 F(e){return"success"in e&&typeof e.success=="boolean"&&"version"in e&&typeof e.version=="string"}function R(e){return!!(e&&typeof e=="object")}const _="_";function N(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=m.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 G(k(n,e),e)}function k(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 G(e,n){const t=n.theme;return t?(R(t)?(e.append("palette",encodeURIComponent(JSON.stringify(t))),e.append("theme",t.baseTheme)):(e.append("palette","null"),e.append("theme",t)),e):(e.append("palette","null"),e)}const I="640px",Q="450px",$=20;function B(e,n){const{params:t,provider:i,listeners:s}=n;let r=i,c=t;const d=q(t);e.innerHTML="",e.appendChild(d);const{contentWindow:u}=d;if(!u)throw console.error("Iframe does not contain a window",d),new Error("Iframe does not contain a window!");const l=[];l.push(K(u,t.appCode)),l.push(...Y(d,t.height));const C=new U(window,s);let T=S(u,null,r);d.addEventListener("load",()=>v(u,c,r));const j=new H(window,u);return{updateParams:h=>{c=h,v(u,c,r)},updateListeners:h=>C.updateListeners(h),updateProvider:h=>{r=h,T=S(u,T,h)},destroy:()=>{T.disconnect(),C.stopListeningIframe(),l.forEach(h=>window.removeEventListener("message",h)),j.stopListening(),e.removeChild(d)}}}function S(e,n,t){n&&n.disconnect();const i=n||new y(e);return t&&i.onConnect(t),i}function q(e){const{width:n=Q,height:t=I}=e,i=document.createElement("iframe");return i.src=N(e),i.width=n,i.height=t,i.style.border="0",i}function v(e,n,t){const i=!!t,s=A(n),r=W(n).toString(),{theme:c,...d}=n;f(e,E.UPDATE_PARAMS,{urlParams:{pathname:s,search:r},appParams:d,hasProvider:i})}function K(e,n){const t=p(window,a.ACTIVATE,()=>{w(window,t),f(e,E.UPDATE_APP_DATA,{metaData:n?{appCode:n}:void 0})});return t}function Y(e,n=I){return[p(window,a.UPDATE_HEIGHT,t=>{e.style.height=t.height?`${t.height+$}px`:n}),p(window,a.SET_FULL_HEIGHT,({isUpToSmall:t})=>{e.style.height=t?n:`${document.body.offsetHeight}px`})]}return o.TradeType=m,o.WIDGET_PALETTE_COLORS=O,o.WidgetMethodsEmit=a,o.WidgetMethodsListen=E,o.createCowSwapWidget=B,o.isCowSwapWidgetPalette=R,o.listenToMessageFromWindow=p,o.postMessageToWindow=f,o.stopListeningToMessageFromWindow=g,o.stopListeningWindowListener=w,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(e,n,t){const s={key:_,method:n,...typeof t=="object"?t:{}};e.postMessage(s,"*")}function f(e,n,t){const i=s=>{!D(s.data)||s.data.key!==_||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 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,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,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 M(e){const n=typeof e.baseUrl=="string"?e.baseUrl:"https://swap.cow.fi",t=C(e);return n+"/#"+t+"?"+I(e)}function C(e){const{chainId:n=1,sell:t,buy:i,tradeType:s=l.SWAP}=e,o=[(t==null?void 0:t.asset)||T,(i==null?void 0:i.asset)||T].map(encodeURIComponent).join("/");return`/${n}/widget/${s}/${o}`}function I(e){const n=new URLSearchParams;return y(U(n,e),e)}function U(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 y(e,n){const t=n.theme;return t?(W(t)?(e.append("palette",encodeURIComponent(JSON.stringify(t))),e.append("theme",t.baseTheme)):(e.append("palette","null"),e.append("theme",t)),e):(e.append("palette","null"),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=f(this.contentWindow,h.EMIT_COW_EVENT,i=>this.eventEmitter.emit(i.event,i.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=i=>{H(i.data)&&i.origin!==window.location.origin&&(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",x=20;function B(e,n){const{params:t,provider:i,listeners:s}=n;let o=i,r=t;const a=$(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(q(d,t.appCode)),E.push(K(a,t.height));const P=new F(window,s);let m=g(d,null,o);a.addEventListener("load",()=>R(d,r,o));const S=new N(window,d);return{updateParams:c=>{r=c,R(d,r,o)},updateListeners:c=>P.updateListeners(c),updateProvider:c=>{o=c,m=g(d,m,c)},destroy:()=>{m.disconnect(),P.stopListeningIframe(),E.forEach(c=>window.removeEventListener("message",c)),S.stopListening(),e.removeChild(a)}}}function g(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=M(e),i.width=n,i.height=t,i.style.border="0",i}function R(e,n,t){const i=!!t,s=C(n),o=I(n).toString(),{theme:r,...a}=n;u(e,p.UPDATE_PARAMS,{urlParams:{pathname:s,search:o},appParams:a,hasProvider:i})}function q(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=v){return f(window,h.UPDATE_HEIGHT,t=>{e.style.height=t.height?`${t.height+x}px`:n})}exports.TradeType=l;exports.WIDGET_PALETTE_COLORS=L;exports.WidgetMethodsEmit=h;exports.WidgetMethodsListen=p;exports.createCowSwapWidget=B;exports.isCowSwapWidgetPalette=W;exports.listenToMessageFromWindow=f;exports.postMessageToWindow=u;exports.stopListeningToMessageFromWindow=A;exports.stopListeningWindowListener=w;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class L{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))}}const _="cowSwapWidget";function E(e,n,t){const s={key:_,method:n,...typeof t=="object"?t:{}};e.postMessage(s,"*")}function p(e,n,t){const i=s=>{!D(s.data)||s.data.key!==_||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 A(e,n,t){e.removeEventListener("message",t)}function m(e,n){e.removeEventListener("message",n)}var d=(e=>(e.ACTIVATE="ACTIVATE",e.UPDATE_HEIGHT="UPDATE_HEIGHT",e.SET_FULL_HEIGHT="SET_FULL_HEIGHT",e.EMIT_COW_EVENT="EMIT_COW_EVENT",e.PROVIDER_RPC_REQUEST="PROVIDER_RPC_REQUEST",e))(d||{}),u=(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))(u||{}),l=(e=>(e.SWAP="swap",e.LIMIT="limit",e.ADVANCED="advanced",e))(l||{});const O=["primary","background","paper","text","danger","warning","alert","info","success"];class U{constructor(n,t=[]){this.contentWindow=n,this.eventEmitter=new L,this.listeners=[],this.updateListeners(t),this.widgetListener=p(this.contentWindow,d.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)}}const V=["connect","disconnect","close","chainChanged","accountsChanged"];class y{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,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(),V.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){E(this.iframeWidow,u.PROVIDER_ON_EVENT,{event:n,params:t})}forwardRpcResponseToIframe(n){E(this.iframeWidow,u.PROVIDER_RPC_RESPONSE,n)}}class H{constructor(n,t){this.appWindow=n,this.iframeWidow=t,this.forwardSdkMessage=i=>{M(i.data)&&i.origin!==window.location.origin&&(b(i.data)?this.appWindow.parent.postMessage(i.data,"*"):F(i.data)&&this.iframeWidow.postMessage(i.data,"*"))},this.startListening()}startListening(){this.appWindow.addEventListener("message",this.forwardSdkMessage)}stopListening(){this.appWindow.removeEventListener("message",this.forwardSdkMessage)}}function M(e){return typeof e=="object"&&e!==null&&"id"in e&&typeof e.id=="string"}function b(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 F(e){return"success"in e&&typeof e.success=="boolean"&&"version"in e&&typeof e.version=="string"}function I(e){return!!(e&&typeof e=="object")}const P="_";function N(e){const n=typeof e.baseUrl=="string"?e.baseUrl:"https://swap.cow.fi",t=W(e);return n+"/#"+t+"?"+S(e)}function W(e){const{chainId:n=1,sell:t,buy:i,tradeType:s=l.SWAP}=e,o=[(t==null?void 0:t.asset)||P,(i==null?void 0:i.asset)||P].map(encodeURIComponent).join("/");return`/${n}/widget/${s}/${o}`}function S(e){const n=new URLSearchParams;return G(k(n,e),e)}function k(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 G(e,n){const t=n.theme;return t?(I(t)?(e.append("palette",encodeURIComponent(JSON.stringify(t))),e.append("theme",t.baseTheme)):(e.append("palette","null"),e.append("theme",t)),e):(e.append("palette","null"),e)}const C="640px",Q="450px",x=20;function $(e,n){const{params:t,provider:i,listeners:s}=n;let o=i,r=t;const a=B(t);e.innerHTML="",e.appendChild(a);const{contentWindow:h}=a;if(!h)throw console.error("Iframe does not contain a window",a),new Error("Iframe does not contain a window!");const f=[];f.push(q(h,t.appCode)),f.push(...K(a,t.height));const T=new U(window,s);let w=g(h,null,o);a.addEventListener("load",()=>R(h,r,o));const v=new H(window,h);return{updateParams:c=>{r=c,R(h,r,o)},updateListeners:c=>T.updateListeners(c),updateProvider:c=>{o=c,w=g(h,w,c)},destroy:()=>{w.disconnect(),T.stopListeningIframe(),f.forEach(c=>window.removeEventListener("message",c)),v.stopListening(),e.removeChild(a)}}}function g(e,n,t){n&&n.disconnect();const i=n||new y(e);return t&&i.onConnect(t),i}function B(e){const{width:n=Q,height:t=C}=e,i=document.createElement("iframe");return i.src=N(e),i.width=n,i.height=t,i.style.border="0",i}function R(e,n,t){const i=!!t,s=W(n),o=S(n).toString(),{theme:r,...a}=n;E(e,u.UPDATE_PARAMS,{urlParams:{pathname:s,search:o},appParams:a,hasProvider:i})}function q(e,n){const t=p(window,d.ACTIVATE,()=>{m(window,t),E(e,u.UPDATE_APP_DATA,{metaData:n?{appCode:n}:void 0})});return t}function K(e,n=C){return[p(window,d.UPDATE_HEIGHT,t=>{e.style.height=t.height?`${t.height+x}px`:n}),p(window,d.SET_FULL_HEIGHT,({isUpToSmall:t})=>{e.style.height=t?n:`${document.body.offsetHeight}px`})]}exports.TradeType=l;exports.WIDGET_PALETTE_COLORS=O;exports.WidgetMethodsEmit=d;exports.WidgetMethodsListen=u;exports.createCowSwapWidget=$;exports.isCowSwapWidgetPalette=I;exports.listenToMessageFromWindow=p;exports.postMessageToWindow=E;exports.stopListeningToMessageFromWindow=A;exports.stopListeningWindowListener=m;
package/index.mjs CHANGED
@@ -1,7 +1,23 @@
1
- const T = "cowSwapWidget";
2
- function u(e, n, t) {
1
+ class S {
2
+ constructor() {
3
+ this.subscriptions = {};
4
+ }
5
+ on(n) {
6
+ const { event: t, handler: i } = n;
7
+ this.subscriptions[t] || (this.subscriptions[t] = []), this.subscriptions[t].push(i);
8
+ }
9
+ off(n) {
10
+ const { event: t, handler: i } = n;
11
+ this.subscriptions[t] && (this.subscriptions[t] = this.subscriptions[t].filter((s) => s !== i));
12
+ }
13
+ emit(n, t) {
14
+ this.subscriptions[n] && this.subscriptions[n].forEach((i) => i(t));
15
+ }
16
+ }
17
+ const R = "cowSwapWidget";
18
+ function E(e, n, t) {
3
19
  const s = {
4
- key: T,
20
+ key: R,
5
21
  method: n,
6
22
  ...typeof t == "object" ? t : {}
7
23
  };
@@ -11,22 +27,22 @@ function u(e, n, t) {
11
27
  // TODO: Change to CoW specific origin in production. https://github.com/cowprotocol/cowswap/issues/3828
12
28
  );
13
29
  }
14
- function f(e, n, t) {
30
+ function p(e, n, t) {
15
31
  const i = (s) => {
16
- !W(s.data) || s.data.key !== T || s.data.method !== n || t(s.data);
32
+ !W(s.data) || s.data.key !== R || s.data.method !== n || t(s.data);
17
33
  };
18
34
  return e.addEventListener("message", i), i;
19
35
  }
20
36
  function W(e) {
21
37
  return typeof e == "object" && e !== null && "key" in e && "method" in e && typeof e.key == "string" && typeof e.method == "string";
22
38
  }
23
- function S(e, n, t) {
39
+ function L(e, n, t) {
24
40
  e.removeEventListener("message", t);
25
41
  }
26
42
  function _(e, n) {
27
43
  e.removeEventListener("message", n);
28
44
  }
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 || {});
45
+ var h = /* @__PURE__ */ ((e) => (e.ACTIVATE = "ACTIVATE", e.UPDATE_HEIGHT = "UPDATE_HEIGHT", e.SET_FULL_HEIGHT = "SET_FULL_HEIGHT", e.EMIT_COW_EVENT = "EMIT_COW_EVENT", e.PROVIDER_RPC_REQUEST = "PROVIDER_RPC_REQUEST", e))(h || {}), u = /* @__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))(u || {}), g = /* @__PURE__ */ ((e) => (e.SWAP = "swap", e.LIMIT = "limit", e.ADVANCED = "advanced", e))(g || {});
30
46
  const q = [
31
47
  "primary",
32
48
  "background",
@@ -37,8 +53,28 @@ const q = [
37
53
  "alert",
38
54
  "info",
39
55
  "success"
40
- ], D = ["connect", "disconnect", "close", "chainChanged", "accountsChanged"];
41
- class O {
56
+ ];
57
+ class D {
58
+ constructor(n, t = []) {
59
+ this.contentWindow = n, this.eventEmitter = new S(), this.listeners = [], this.updateListeners(t), this.widgetListener = p(
60
+ this.contentWindow,
61
+ h.EMIT_COW_EVENT,
62
+ (i) => this.eventEmitter.emit(i.event, i.payload)
63
+ );
64
+ }
65
+ stopListeningIframe() {
66
+ _(this.contentWindow, this.widgetListener);
67
+ }
68
+ updateListeners(n) {
69
+ for (const t of this.listeners)
70
+ this.eventEmitter.off(t);
71
+ this.listeners = n || [];
72
+ for (const t of this.listeners)
73
+ this.eventEmitter.on(t);
74
+ }
75
+ }
76
+ const O = ["connect", "disconnect", "close", "chainChanged", "accountsChanged"];
77
+ class U {
42
78
  /**
43
79
  * Creates an instance of IframeRpcProviderBridge.
44
80
  * @param iframeWidow - The iFrame window that will post up general RPC messages and to which the IframeRpcProviderBridge will forward the RPC result.
@@ -57,14 +93,14 @@ class O {
57
93
  * Disconnects the JSON-RPC bridge from the Ethereum provider.
58
94
  */
59
95
  disconnect() {
60
- this.ethereumProvider = null, S(window, h.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow);
96
+ this.ethereumProvider = null, L(window, h.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow);
61
97
  }
62
98
  /**
63
99
  * Handles the 'connect' event and sets up event listeners for Ethereum provider events.
64
100
  * @param newProvider - The Ethereum provider to connect.
65
101
  */
66
102
  onConnect(n) {
67
- this.ethereumProvider ? this.disconnect() : f(window, h.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow), this.ethereumProvider = n, this.processPendingRequests(), D.forEach((t) => {
103
+ this.ethereumProvider ? this.disconnect() : p(window, h.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow), this.ethereumProvider = n, this.processPendingRequests(), O.forEach((t) => {
68
104
  n.on(t, (i) => this.onProviderEvent(t, i));
69
105
  });
70
106
  }
@@ -92,7 +128,7 @@ class O {
92
128
  );
93
129
  }
94
130
  onProviderEvent(n, t) {
95
- u(this.iframeWidow, p.PROVIDER_ON_EVENT, {
131
+ E(this.iframeWidow, u.PROVIDER_ON_EVENT, {
96
132
  event: n,
97
133
  params: t
98
134
  });
@@ -101,72 +137,13 @@ class O {
101
137
  * Forward a JSON-RPC message to the content window.
102
138
  */
103
139
  forwardRpcResponseToIframe(n) {
104
- u(this.iframeWidow, p.PROVIDER_RPC_RESPONSE, n);
140
+ E(this.iframeWidow, u.PROVIDER_RPC_RESPONSE, n);
105
141
  }
106
142
  }
107
- function L(e) {
108
- return !!(e && typeof e == "object");
109
- }
110
- const w = "_";
111
- function V(e) {
112
- const n = typeof e.baseUrl == "string" ? e.baseUrl : "https://swap.cow.fi", t = A(e);
113
- return n + "/#" + t + "?" + I(e);
114
- }
115
- function A(e) {
116
- const { chainId: n = 1, sell: t, buy: i, tradeType: s = g.SWAP } = e, o = [(t == null ? void 0 : t.asset) || w, (i == null ? void 0 : i.asset) || w].map(encodeURIComponent).join("/");
117
- return `/${n}/widget/${s}/${o}`;
118
- }
119
- function I(e) {
120
- const n = new URLSearchParams();
121
- return b(U(n, e), e);
122
- }
123
- function U(e, n) {
124
- const { sell: t, buy: i } = n;
125
- return t != null && t.amount && e.append("sellAmount", t.amount), i != null && i.amount && e.append("buyAmount", i.amount), e;
126
- }
127
- function b(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("palette", "null"), e.append("theme", t)), e) : (e.append("palette", "null"), e);
130
- }
131
- class y {
132
- constructor() {
133
- this.subscriptions = {};
134
- }
135
- on(n) {
136
- const { event: t, handler: i } = n;
137
- this.subscriptions[t] || (this.subscriptions[t] = []), this.subscriptions[t].push(i);
138
- }
139
- off(n) {
140
- const { event: t, handler: i } = n;
141
- this.subscriptions[t] && (this.subscriptions[t] = this.subscriptions[t].filter((s) => s !== i));
142
- }
143
- emit(n, t) {
144
- this.subscriptions[n] && this.subscriptions[n].forEach((i) => i(t));
145
- }
146
- }
147
- class N {
148
- constructor(n, t = []) {
149
- this.contentWindow = n, this.eventEmitter = new y(), this.listeners = [], this.updateListeners(t), this.widgetListener = f(
150
- this.contentWindow,
151
- h.EMIT_COW_EVENT,
152
- (i) => this.eventEmitter.emit(i.event, i.payload)
153
- );
154
- }
155
- stopListeningIframe() {
156
- _(this.contentWindow, this.widgetListener);
157
- }
158
- updateListeners(n) {
159
- for (const t of this.listeners)
160
- this.eventEmitter.off(t);
161
- this.listeners = n || [];
162
- for (const t of this.listeners)
163
- this.eventEmitter.on(t);
164
- }
165
- }
166
- class M {
143
+ class V {
167
144
  constructor(n, t) {
168
145
  this.appWindow = n, this.iframeWidow = t, this.forwardSdkMessage = (i) => {
169
- F(i.data) && i.origin !== window.location.origin && (H(i.data) ? this.appWindow.parent.postMessage(i.data, "*") : k(i.data) && this.iframeWidow.postMessage(i.data, "*"));
146
+ H(i.data) && i.origin !== window.location.origin && (y(i.data) ? this.appWindow.parent.postMessage(i.data, "*") : b(i.data) && this.iframeWidow.postMessage(i.data, "*"));
170
147
  }, this.startListening();
171
148
  }
172
149
  startListening() {
@@ -176,55 +153,79 @@ class M {
176
153
  this.appWindow.removeEventListener("message", this.forwardSdkMessage);
177
154
  }
178
155
  }
179
- function F(e) {
156
+ function H(e) {
180
157
  return typeof e == "object" && e !== null && "id" in e && typeof e.id == "string";
181
158
  }
182
- function H(e) {
159
+ function y(e) {
183
160
  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
161
  }
185
- function k(e) {
162
+ function b(e) {
186
163
  return "success" in e && typeof e.success == "boolean" && "version" in e && typeof e.version == "string";
187
164
  }
188
- const v = "640px", G = "450px", Q = 20;
165
+ function F(e) {
166
+ return !!(e && typeof e == "object");
167
+ }
168
+ const l = "_";
169
+ function N(e) {
170
+ const n = typeof e.baseUrl == "string" ? e.baseUrl : "https://swap.cow.fi", t = A(e);
171
+ return n + "/#" + t + "?" + I(e);
172
+ }
173
+ function A(e) {
174
+ const { chainId: n = 1, sell: t, buy: i, tradeType: s = g.SWAP } = e, o = [(t == null ? void 0 : t.asset) || l, (i == null ? void 0 : i.asset) || l].map(encodeURIComponent).join("/");
175
+ return `/${n}/widget/${s}/${o}`;
176
+ }
177
+ function I(e) {
178
+ const n = new URLSearchParams();
179
+ return k(M(n, e), e);
180
+ }
181
+ function M(e, n) {
182
+ const { sell: t, buy: i } = n;
183
+ return t != null && t.amount && e.append("sellAmount", t.amount), i != null && i.amount && e.append("buyAmount", i.amount), e;
184
+ }
185
+ function k(e, n) {
186
+ const t = n.theme;
187
+ return t ? (F(t) ? (e.append("palette", encodeURIComponent(JSON.stringify(t))), e.append("theme", t.baseTheme)) : (e.append("palette", "null"), e.append("theme", t)), e) : (e.append("palette", "null"), e);
188
+ }
189
+ const v = "640px", G = "450px", x = 20;
189
190
  function K(e, n) {
190
191
  const { params: t, provider: i, listeners: s } = n;
191
192
  let o = i, r = t;
192
- const a = x(t);
193
+ const a = Q(t);
193
194
  e.innerHTML = "", e.appendChild(a);
194
195
  const { contentWindow: d } = a;
195
196
  if (!d)
196
197
  throw console.error("Iframe does not contain a window", a), new Error("Iframe does not contain a window!");
197
- const E = [];
198
- E.push(B(d, t.appCode)), E.push($(a, t.height));
199
- const l = new N(window, s);
198
+ const f = [];
199
+ f.push($(d, t.appCode)), f.push(...B(a, t.height));
200
+ const w = new D(window, s);
200
201
  let m = P(d, null, o);
201
- a.addEventListener("load", () => R(d, r, o));
202
- const C = new M(window, d);
202
+ a.addEventListener("load", () => T(d, r, o));
203
+ const C = new V(window, d);
203
204
  return {
204
205
  updateParams: (c) => {
205
- r = c, R(d, r, o);
206
+ r = c, T(d, r, o);
206
207
  },
207
- updateListeners: (c) => l.updateListeners(c),
208
+ updateListeners: (c) => w.updateListeners(c),
208
209
  updateProvider: (c) => {
209
210
  o = c, m = P(d, m, c);
210
211
  },
211
212
  destroy: () => {
212
- m.disconnect(), l.stopListeningIframe(), E.forEach((c) => window.removeEventListener("message", c)), C.stopListening(), e.removeChild(a);
213
+ m.disconnect(), w.stopListeningIframe(), f.forEach((c) => window.removeEventListener("message", c)), C.stopListening(), e.removeChild(a);
213
214
  }
214
215
  };
215
216
  }
216
217
  function P(e, n, t) {
217
218
  n && n.disconnect();
218
- const i = n || new O(e);
219
+ const i = n || new U(e);
219
220
  return t && i.onConnect(t), i;
220
221
  }
221
- function x(e) {
222
+ function Q(e) {
222
223
  const { width: n = G, height: t = v } = e, i = document.createElement("iframe");
223
- return i.src = V(e), i.width = n, i.height = t, i.style.border = "0", i;
224
+ return i.src = N(e), i.width = n, i.height = t, i.style.border = "0", i;
224
225
  }
225
- function R(e, n, t) {
226
+ function T(e, n, t) {
226
227
  const i = !!t, s = A(n), o = I(n).toString(), { theme: r, ...a } = n;
227
- u(e, p.UPDATE_PARAMS, {
228
+ E(e, u.UPDATE_PARAMS, {
228
229
  urlParams: {
229
230
  pathname: s,
230
231
  search: o
@@ -233,28 +234,33 @@ function R(e, n, t) {
233
234
  hasProvider: i
234
235
  });
235
236
  }
236
- function B(e, n) {
237
- const t = f(window, h.ACTIVATE, () => {
238
- _(window, t), u(e, p.UPDATE_APP_DATA, {
237
+ function $(e, n) {
238
+ const t = p(window, h.ACTIVATE, () => {
239
+ _(window, t), E(e, u.UPDATE_APP_DATA, {
239
240
  metaData: n ? { appCode: n } : void 0
240
241
  });
241
242
  });
242
243
  return t;
243
244
  }
244
- function $(e, n = v) {
245
- return f(window, h.UPDATE_HEIGHT, (t) => {
246
- e.style.height = t.height ? `${t.height + Q}px` : n;
247
- });
245
+ function B(e, n = v) {
246
+ return [
247
+ p(window, h.UPDATE_HEIGHT, (t) => {
248
+ e.style.height = t.height ? `${t.height + x}px` : n;
249
+ }),
250
+ p(window, h.SET_FULL_HEIGHT, ({ isUpToSmall: t }) => {
251
+ e.style.height = t ? n : `${document.body.offsetHeight}px`;
252
+ })
253
+ ];
248
254
  }
249
255
  export {
250
256
  g as TradeType,
251
257
  q as WIDGET_PALETTE_COLORS,
252
258
  h as WidgetMethodsEmit,
253
- p as WidgetMethodsListen,
259
+ u as WidgetMethodsListen,
254
260
  K as createCowSwapWidget,
255
- L as isCowSwapWidgetPalette,
256
- f as listenToMessageFromWindow,
257
- u as postMessageToWindow,
258
- S as stopListeningToMessageFromWindow,
261
+ F as isCowSwapWidgetPalette,
262
+ p as listenToMessageFromWindow,
263
+ E as postMessageToWindow,
264
+ L as stopListeningToMessageFromWindow,
259
265
  _ as stopListeningWindowListener
260
266
  };
package/messages.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { WidgetMethodsEmit, WidgetMethodsEmitPayloadMap, WidgetMethodsListen, WidgetMethodsListenPayloadMap } from './types';
2
+
2
3
  export declare function postMessageToWindow<T extends WidgetMethodsEmit>(contentWindow: Window, method: T, payload: WidgetMethodsEmitPayloadMap[T]): void;
3
4
  export declare function postMessageToWindow<T extends WidgetMethodsListen>(contentWindow: Window, method: T, payload: WidgetMethodsListenPayloadMap[T]): void;
4
5
  export type WindowListener = (event: MessageEvent<unknown>) => void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cowprotocol/widget-lib",
3
- "version": "0.10.1",
3
+ "version": "0.11.1",
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": "^1.2.1"
24
+ "@cowprotocol/events": "^1.3.0"
25
25
  }
26
26
  }
package/themeUtils.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  import { CowSwapTheme, CowSwapWidgetPalette } from './types';
2
+
2
3
  export declare function isCowSwapWidgetPalette(palette: CowSwapTheme | CowSwapWidgetPalette | undefined): palette is CowSwapWidgetPalette;
package/types.d.ts CHANGED
@@ -1,9 +1,11 @@
1
- import type { SupportedChainId } from '@cowprotocol/cow-sdk';
2
- import { CowEventListeners, CowEventPayloadMap, CowEvents } from '@cowprotocol/events';
1
+ import { CowEventListeners, CowEventPayloadMap, CowEvents } from '../../events/src/index.ts';
2
+ import { SupportedChainId } from '@cowprotocol/cow-sdk';
3
+
3
4
  export type { SupportedChainId } from '@cowprotocol/cow-sdk';
4
5
  export declare enum WidgetMethodsEmit {
5
6
  ACTIVATE = "ACTIVATE",
6
7
  UPDATE_HEIGHT = "UPDATE_HEIGHT",
8
+ SET_FULL_HEIGHT = "SET_FULL_HEIGHT",
7
9
  EMIT_COW_EVENT = "EMIT_COW_EVENT",
8
10
  PROVIDER_RPC_REQUEST = "PROVIDER_RPC_REQUEST"
9
11
  }
@@ -80,7 +82,7 @@ export interface PartnerFee {
80
82
  /**
81
83
  * The Ethereum address of the partner to receive the fee.
82
84
  */
83
- recipient: string;
85
+ recipient: string | Record<SupportedChainId, string>;
84
86
  }
85
87
  /**
86
88
  * ERC-20 token information
@@ -138,6 +140,10 @@ export interface CowSwapWidgetBanners {
138
140
  */
139
141
  hideSafeWebAppBanner?: boolean;
140
142
  }
143
+ export interface CowSwapWidgetContent {
144
+ feeLabel?: string;
145
+ feeTooltipMarkdown?: string;
146
+ }
141
147
  export interface CowSwapWidgetParams {
142
148
  /**
143
149
  * The unique identifier of the widget consumer.
@@ -235,11 +241,16 @@ export interface CowSwapWidgetParams {
235
241
  * In case when widget does not support some tokens, you can provide a list of tokens to be used in the widget
236
242
  */
237
243
  customTokens?: TokenInfo[];
244
+ /**
245
+ * Customizable labels and content for the widget.
246
+ */
247
+ content?: CowSwapWidgetContent;
238
248
  }
239
249
  export interface WidgetMethodsEmitPayloadMap {
240
250
  [WidgetMethodsEmit.ACTIVATE]: void;
241
251
  [WidgetMethodsEmit.EMIT_COW_EVENT]: EmitCowEventPayload<CowEvents>;
242
252
  [WidgetMethodsEmit.UPDATE_HEIGHT]: UpdateWidgetHeightPayload;
253
+ [WidgetMethodsEmit.SET_FULL_HEIGHT]: SetWidgetFullHeightPayload;
243
254
  [WidgetMethodsEmit.PROVIDER_RPC_REQUEST]: ProviderRpcRequestPayload;
244
255
  }
245
256
  export interface WidgetMethodsListenPayloadMap {
@@ -267,6 +278,9 @@ export interface UpdateAppDataPayload {
267
278
  export interface UpdateWidgetHeightPayload {
268
279
  height?: number;
269
280
  }
281
+ export interface SetWidgetFullHeightPayload {
282
+ isUpToSmall?: boolean;
283
+ }
270
284
  export interface EmitCowEventPayload<T extends CowEvents> {
271
285
  event: T;
272
286
  payload: CowEventPayloadMap[T];
package/urlUtils.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { CowSwapWidgetParams } from './types';
2
+
2
3
  export declare function buildWidgetUrl(params: Partial<CowSwapWidgetParams>): string;
3
4
  export declare function buildWidgetPath(params: Partial<CowSwapWidgetParams>): string;
4
5
  export declare function buildWidgetUrlQuery(params: Partial<CowSwapWidgetParams>): URLSearchParams;