@cowprotocol/widget-lib 0.7.0 → 0.8.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/README.md CHANGED
@@ -10,7 +10,7 @@ See the widget in action in the [widget configurator](https://widget.cow.fi)
10
10
 
11
11
  ## Docs
12
12
 
13
- You can find a detailed description of all widget parameters in the [documentation](../../libs/widget-lib/docs/README.md)
13
+ You can find a detailed description of all widget parameters in the [documentation](https://docs.cow.fi/cow-protocol/tutorials/widget)
14
14
 
15
15
  ## Quick start
16
16
 
@@ -31,27 +31,28 @@ Create a container somewhere in your website, the widget will be rendered inside
31
31
  Import the widget and initialise it:
32
32
 
33
33
  ```js
34
- import { cowSwapWidget, CowSwapWidgetParams } from '@cowprotocol/widget-lib'
34
+ import { createCowSwapWidget, CowSwapWidgetParams } from '@cowprotocol/widget-lib'
35
35
 
36
36
  // Initialise the widget
37
37
  const widgetContainer = document.getElementById('cowswap-widget')
38
38
 
39
+ // instantiate your own web3 provider
40
+ const provider = window.ethereum
41
+
39
42
  const params: CowSwapWidgetParams = {
40
- appCode: '<YOUR_APP_KEY>', // Just an unique identifier for your app
43
+ appCode: 'NAME-OF-YOU-APP', // Add here the name of your app. e.g. "Pig Swap"
41
44
  sell: { asset: 'DAI' },
42
45
  buy: { asset: 'USDC', amount: '0.1' },
43
- // instantiate your own web3 provider
44
- provider: window.ethereum,
45
46
  }
46
47
 
47
- const updateWidget = cowSwapWidget(
48
+ const {updateParams} = createCowSwapWidget(
48
49
  widgetContainer,
49
50
  // Optionally, you can provide some additional params to customise your widget
50
- params
51
+ {params, provider}
51
52
  )
52
53
 
53
54
  // You also can change widget configuration on the fly
54
- updateWidget({ ...params, tradeType: 'limit' })
55
+ updateParams({ ...params, tradeType: 'limit' })
55
56
  ```
56
57
 
57
58
  ## Developers
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 l="cowSwapWidget";function p(e,n,t){const s={key:l,method:n,...typeof t=="object"?t:{}};e.postMessage(s,"*")}function f(e,n,t){const i=s=>{!C(s.data)||s.data.key!==l||s.data.method!==n||t(s.data)};return e.addEventListener("message",i),i}function C(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 E(e,n){e.removeEventListener("message",n)}var d=(e=>(e.ACTIVATE="ACTIVATE",e.UPDATE_HEIGHT="UPDATE_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||{}),w=(e=>(e.SWAP="swap",e.LIMIT="limit",e.ADVANCED="advanced",e))(w||{});const S=["connect","disconnect","close","chainChanged","accountsChanged"];class D{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,d.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow)}onConnect(n){this.ethereumProvider?this.disconnect():f(window,d.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),this.ethereumProvider=n,this.processPendingRequests(),S.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){p(this.iframeWidow,u.PROVIDER_ON_EVENT,{event:n,params:t})}forwardRpcResponseToIframe(n){p(this.iframeWidow,u.PROVIDER_RPC_RESPONSE,n)}}const T="_";function L(e){const n=typeof e.baseUrl=="string"?e.baseUrl:"https://swap.cow.fi",t=g(e),i=_(e);return n+"/#"+t+"?"+i}function g(e){const{chainId:n=1,sell:t,buy:i,tradeType:s=w.SWAP}=e,a=[(t==null?void 0:t.asset)||T,(i==null?void 0:i.asset)||T].map(encodeURIComponent).join("/");return`/${n}/widget/${s}/${a}`}function _(e){const{sell:n,buy:t,theme:i}=e,s=new URLSearchParams;return n!=null&&n.amount&&s.append("sellAmount",n.amount),t!=null&&t.amount&&s.append("buyAmount",t.amount),i&&s.append("theme",typeof i=="string"?i:i.baseTheme),s}class O{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 y{constructor(n,t=[]){this.contentWindow=n,this.eventEmitter=new O,this.listeners=[],this.updateListeners(t),this.widgetListener=f(this.contentWindow,d.EMIT_COW_EVENT,i=>this.eventEmitter.emit(i.event,i.payload))}stopListeningIframe(){E(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 V{constructor(n,t){this.appWindow=n,this.iframeWidow=t,this.forwardSdkMessage=i=>{M(i.data)&&(U(i.data)?this.appWindow.parent.postMessage(i.data,"*"):b(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 U(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 b(e){return"success"in e&&typeof e.success=="boolean"&&"version"in e&&typeof e.version=="string"}const A="640px",F="450px",N=20;function H(e,n){const{params:t,provider:i,listeners:s}=n;let a=i;const r=q(t);e.innerHTML="",e.appendChild(r);const{contentWindow:h}=r;if(!h)throw console.error("Iframe does not contain a window",r),new Error("Iframe does not contain a window!");const m=[];m.push(k(h,t.appCode)),m.push(G(r,t.height));const I=new y(window,s);let P=v(h,null,a);r.addEventListener("load",()=>W(h,t,a));const Q=new V(window,h);return{updateParams:c=>W(h,c,a),updateListeners:c=>I.updateListeners(c),updateProvider:c=>{a=c,P=v(h,P,c)},destroy:()=>{P.disconnect(),I.stopListeningIframe(),m.forEach(c=>window.removeEventListener("message",c)),Q.stopListening(),e.removeChild(r)}}}function v(e,n,t){n&&n.disconnect();const i=n||new D(e);return t&&i.onConnect(t),i}function q(e){const{width:n=F,height:t=A}=e,i=document.createElement("iframe");return i.src=L(e),i.width=n,i.height=t,i.style.border="0",i}function W(e,n,t){const i=!!t,s=g(n),a=_(n).toString();p(e,u.UPDATE_PARAMS,{urlParams:{pathname:s,search:a},appParams:{...n},hasProvider:i})}function k(e,n){const t=f(window,d.ACTIVATE,()=>{E(window,t),p(e,u.UPDATE_APP_DATA,{metaData:n?{appCode:n}:void 0})});return t}function G(e,n=A){return f(window,d.UPDATE_HEIGHT,t=>{e.style.height=t.height?`${t.height+N}px`:n})}return o.TradeType=w,o.WidgetMethodsEmit=d,o.WidgetMethodsListen=u,o.createCowSwapWidget=H,o.listenToMessageFromWindow=f,o.postMessageToWindow=p,o.stopListeningToMessageFromWindow=R,o.stopListeningWindowListener=E,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 g="cowSwapWidget";function u(e,n,t){const i={key:g,method:n,...typeof t=="object"?t:{}};e.postMessage(i,"*")}function p(e,n,t){const s=i=>{!C(i.data)||i.data.key!==g||i.data.method!==n||t(i.data)};return e.addEventListener("message",s),s}function C(e){return typeof e=="object"&&e!==null&&"key"in e&&"method"in e&&typeof e.key=="string"&&typeof e.method=="string"}function _(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.EMIT_COW_EVENT="EMIT_COW_EVENT",e.PROVIDER_RPC_REQUEST="PROVIDER_RPC_REQUEST",e))(d||{}),h=(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))(h||{}),w=(e=>(e.SWAP="swap",e.LIMIT="limit",e.ADVANCED="advanced",e))(w||{});const S=["connect","disconnect","close","chainChanged","accountsChanged"];class D{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,_(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(),S.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(o=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:s,id:t,result:o}})).catch(o=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:s,id:t,error:o}}))}onProviderEvent(n,t){u(this.iframeWidow,h.PROVIDER_ON_EVENT,{event:n,params:t})}forwardRpcResponseToIframe(n){u(this.iframeWidow,h.PROVIDER_RPC_RESPONSE,n)}}const l="_";function L(e){const n=typeof e.baseUrl=="string"?e.baseUrl:"https://swap.cow.fi",t=A(e),s=v(e);return n+"/#"+t+"?"+s}function A(e){const{chainId:n=1,sell:t,buy:s,tradeType:i=w.SWAP}=e,r=[(t==null?void 0:t.asset)||l,(s==null?void 0:s.asset)||l].map(encodeURIComponent).join("/");return`/${n}/widget/${i}/${r}`}function v(e){const{sell:n,buy:t,theme:s}=e,i=new URLSearchParams;return n!=null&&n.amount&&i.append("sellAmount",n.amount),t!=null&&t.amount&&i.append("buyAmount",t.amount),s&&i.append("theme",typeof s=="string"?s:s.baseTheme),i}class O{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 y{constructor(n,t=[]){this.contentWindow=n,this.eventEmitter=new O,this.listeners=[],this.updateListeners(t),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 t of this.listeners)this.eventEmitter.off(t);this.listeners=n||[];for(const t of this.listeners)this.eventEmitter.on(t)}}class V{constructor(n,t){this.appWindow=n,this.iframeWidow=t,this.forwardSdkMessage=s=>{M(s.data)&&(U(s.data)?this.appWindow.parent.postMessage(s.data,"*"):b(s.data)&&this.iframeWidow.postMessage(s.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 U(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 b(e){return"success"in e&&typeof e.success=="boolean"&&"version"in e&&typeof e.version=="string"}const W="640px",F="450px",N=20;function H(e,n){const{params:t,provider:s,listeners:i}=n;let r=s;const o=q(t);e.innerHTML="",e.appendChild(o);const{contentWindow:c}=o;if(!c)throw console.error("Iframe does not contain a window",o),new Error("Iframe does not contain a window!");const f=[];f.push(k(c,t.appCode)),f.push(G(o,t.height));const P=new y(window,i);let E=R(c,null,r);o.addEventListener("load",()=>T(c,t,r));const I=new V(window,c);return{updateParams:a=>T(c,a,r),updateListeners:a=>P.updateListeners(a),updateProvider:a=>{r=a,E=R(c,E,a)},destroy:()=>{E.disconnect(),P.stopListeningIframe(),f.forEach(a=>window.removeEventListener("message",a)),I.stopListening(),e.removeChild(o)}}}function R(e,n,t){n&&n.disconnect();const s=n||new D(e);return t&&s.onConnect(t),s}function q(e){const{width:n=F,height:t=W}=e,s=document.createElement("iframe");return s.src=L(e),s.width=n,s.height=t,s.style.border="0",s}function T(e,n,t){const s=!!t,i=A(n),r=v(n).toString();u(e,h.UPDATE_PARAMS,{urlParams:{pathname:i,search:r},appParams:{...n},hasProvider:s})}function k(e,n){const t=p(window,d.ACTIVATE,()=>{m(window,t),u(e,h.UPDATE_APP_DATA,{metaData:n?{appCode:n}:void 0})});return t}function G(e,n=W){return p(window,d.UPDATE_HEIGHT,t=>{e.style.height=t.height?`${t.height+N}px`:n})}exports.TradeType=w;exports.WidgetMethodsEmit=d;exports.WidgetMethodsListen=h;exports.createCowSwapWidget=H;exports.listenToMessageFromWindow=p;exports.postMessageToWindow=u;exports.stopListeningToMessageFromWindow=_;exports.stopListeningWindowListener=m;
package/index.mjs CHANGED
@@ -1,61 +1,61 @@
1
- const T = "cowSwapWidget";
2
- function u(t, n, e) {
1
+ const l = "cowSwapWidget";
2
+ function u(e, n, t) {
3
3
  const i = {
4
- key: T,
4
+ key: l,
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 p(e, n, t) {
15
15
  const s = (i) => {
16
- !v(i.data) || i.data.key !== T || i.data.method !== n || e(i.data);
16
+ !C(i.data) || i.data.key !== l || 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 C(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 W(e, n, t) {
24
+ e.removeEventListener("message", t);
25
25
  }
26
- function l(t, n) {
27
- t.removeEventListener("message", n);
26
+ function T(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 d = /* @__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))(d || {}), h = /* @__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))(h || {}), _ = /* @__PURE__ */ ((e) => (e.SWAP = "swap", e.LIMIT = "limit", e.ADVANCED = "advanced", e))(_ || {});
30
+ const S = ["connect", "disconnect", "close", "chainChanged", "accountsChanged"];
31
+ class D {
32
32
  /**
33
33
  * Creates an instance of IframeRpcProviderBridge.
34
34
  * @param iframeWidow - The iFrame window that will post up general RPC messages and to which the IframeRpcProviderBridge will forward the RPC result.
35
35
  * Also it will receive some special RPC events coming from the wallet, like connect/chainChanged,accountChanged
36
36
  */
37
37
  constructor(n) {
38
- this.iframeWidow = n, this.ethereumProvider = null, this.requestWaitingForConnection = {}, this.processRpcCallFromWindow = ({ rpcRequest: e }) => {
38
+ this.iframeWidow = n, this.ethereumProvider = null, this.requestWaitingForConnection = {}, this.processRpcCallFromWindow = ({ rpcRequest: t }) => {
39
39
  if (!this.ethereumProvider) {
40
- e.id && (this.requestWaitingForConnection[e.id] = e);
40
+ t.id && (this.requestWaitingForConnection[t.id] = t);
41
41
  return;
42
42
  }
43
- this.processRpcRequest(e);
43
+ this.processRpcRequest(t);
44
44
  };
45
45
  }
46
46
  /**
47
47
  * Disconnects the JSON-RPC bridge from the Ethereum provider.
48
48
  */
49
49
  disconnect() {
50
- this.ethereumProvider = null, C(window, h.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow);
50
+ this.ethereumProvider = null, W(window, d.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow);
51
51
  }
52
52
  /**
53
53
  * Handles the 'connect' event and sets up event listeners for Ethereum provider events.
54
54
  * @param newProvider - The Ethereum provider to connect.
55
55
  */
56
56
  onConnect(n) {
57
- this.ethereumProvider ? this.disconnect() : 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));
57
+ this.ethereumProvider ? this.disconnect() : p(window, d.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow), this.ethereumProvider = n, this.processPendingRequests(), S.forEach((t) => {
58
+ n.on(t, (s) => this.onProviderEvent(t, s));
59
59
  });
60
60
  }
61
61
  processPendingRequests() {
@@ -68,117 +68,140 @@ class W {
68
68
  * @param request - The JSON-RPC request to be processed.
69
69
  */
70
70
  processRpcRequest(n) {
71
- const { id: e, jsonrpc: s, method: i } = n;
72
- if (!this.ethereumProvider || !e)
71
+ const { id: t, jsonrpc: s, method: i } = n;
72
+ if (!this.ethereumProvider || !t)
73
73
  return;
74
- (i === "enable" ? this.ethereumProvider.enable() : this.ethereumProvider.request({ ...n, id: e })).then(
74
+ (i === "enable" ? this.ethereumProvider.enable() : this.ethereumProvider.request({ ...n, id: t })).then(
75
75
  (o) => this.forwardRpcResponseToIframe({
76
- rpcResponse: { jsonrpc: s, id: e, result: o }
76
+ rpcResponse: { jsonrpc: s, id: t, result: o }
77
77
  })
78
78
  ).catch(
79
79
  (o) => this.forwardRpcResponseToIframe({
80
- rpcResponse: { jsonrpc: s, id: e, error: o }
80
+ rpcResponse: { jsonrpc: s, id: t, error: o }
81
81
  })
82
82
  );
83
83
  }
84
- onProviderEvent(n, e) {
85
- u(this.iframeWidow, d.PROVIDER_ON_EVENT, {
84
+ onProviderEvent(n, t) {
85
+ u(this.iframeWidow, h.PROVIDER_ON_EVENT, {
86
86
  event: n,
87
- params: e
87
+ params: t
88
88
  });
89
89
  }
90
90
  /**
91
91
  * Forward a JSON-RPC message to the content window.
92
92
  */
93
93
  forwardRpcResponseToIframe(n) {
94
- u(this.iframeWidow, d.PROVIDER_RPC_RESPONSE, n);
94
+ u(this.iframeWidow, h.PROVIDER_RPC_RESPONSE, n);
95
95
  }
96
96
  }
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);
100
- return n + "/#" + e + "?" + s;
97
+ const w = "_";
98
+ function O(e) {
99
+ const n = typeof e.baseUrl == "string" ? e.baseUrl : "https://swap.cow.fi", t = g(e), s = A(e);
100
+ return n + "/#" + t + "?" + s;
101
101
  }
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("/");
102
+ function g(e) {
103
+ const { chainId: n = 1, sell: t, buy: s, tradeType: i = _.SWAP } = e, r = [(t == null ? void 0 : t.asset) || w, (s == null ? void 0 : s.asset) || w].map(encodeURIComponent).join("/");
104
104
  return `/${n}/widget/${i}/${r}`;
105
105
  }
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;
106
+ function A(e) {
107
+ const { sell: n, buy: t, theme: s } = e, i = new URLSearchParams();
108
+ return n != null && n.amount && i.append("sellAmount", n.amount), t != null && t.amount && i.append("buyAmount", t.amount), s && i.append("theme", typeof s == "string" ? s : s.baseTheme), i;
109
109
  }
110
- class S {
110
+ class L {
111
111
  constructor() {
112
112
  this.subscriptions = {};
113
113
  }
114
114
  on(n) {
115
- const { event: e, handler: s } = n;
116
- this.subscriptions[e] || (this.subscriptions[e] = []), this.subscriptions[e].push(s);
115
+ const { event: t, handler: s } = n;
116
+ this.subscriptions[t] || (this.subscriptions[t] = []), this.subscriptions[t].push(s);
117
117
  }
118
118
  off(n) {
119
- const { event: e, handler: s } = n;
120
- this.subscriptions[e] && (this.subscriptions[e] = this.subscriptions[e].filter((i) => i !== s));
119
+ const { event: t, handler: s } = n;
120
+ this.subscriptions[t] && (this.subscriptions[t] = this.subscriptions[t].filter((i) => i !== s));
121
121
  }
122
- emit(n, e) {
123
- this.subscriptions[n] && this.subscriptions[n].forEach((s) => s(e));
122
+ emit(n, t) {
123
+ this.subscriptions[n] && this.subscriptions[n].forEach((s) => s(t));
124
124
  }
125
125
  }
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(
126
+ class V {
127
+ constructor(n, t = []) {
128
+ this.contentWindow = n, this.eventEmitter = new L(), this.listeners = [], this.updateListeners(t), this.widgetListener = p(
130
129
  this.contentWindow,
131
- h.EMIT_COW_EVENT,
130
+ d.EMIT_COW_EVENT,
132
131
  (s) => this.eventEmitter.emit(s.event, s.payload)
133
132
  );
134
133
  }
135
134
  stopListeningIframe() {
136
- l(this.contentWindow, this.widgetListener);
135
+ T(this.contentWindow, this.widgetListener);
137
136
  }
138
137
  updateListeners(n) {
139
- for (const e of this.listeners)
140
- this.eventEmitter.off(e);
138
+ for (const t of this.listeners)
139
+ this.eventEmitter.off(t);
141
140
  this.listeners = n || [];
142
- for (const e of this.listeners)
143
- this.eventEmitter.on(e);
141
+ for (const t of this.listeners)
142
+ this.eventEmitter.on(t);
143
+ }
144
+ }
145
+ class y {
146
+ constructor(n, t) {
147
+ this.appWindow = n, this.iframeWidow = t, this.forwardSdkMessage = (s) => {
148
+ U(s.data) && (M(s.data) ? this.appWindow.parent.postMessage(s.data, "*") : N(s.data) && this.iframeWidow.postMessage(s.data, "*"));
149
+ }, this.startListening();
150
+ }
151
+ startListening() {
152
+ this.appWindow.addEventListener("message", this.forwardSdkMessage);
144
153
  }
154
+ stopListening() {
155
+ this.appWindow.removeEventListener("message", this.forwardSdkMessage);
156
+ }
157
+ }
158
+ function U(e) {
159
+ return typeof e == "object" && e !== null && "id" in e && typeof e.id == "string";
160
+ }
161
+ function M(e) {
162
+ 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;
163
+ }
164
+ function N(e) {
165
+ return "success" in e && typeof e.success == "boolean" && "version" in e && typeof e.version == "string";
145
166
  }
146
- const g = "640px", U = "450px", N = 20;
147
- function H(t, n) {
148
- const { params: e, provider: s, listeners: i } = n;
167
+ const v = "640px", b = "450px", F = 20;
168
+ function G(e, n) {
169
+ const { params: t, provider: s, listeners: i } = n;
149
170
  let r = s;
150
- const o = b(e);
151
- t.innerHTML = "", t.appendChild(o);
152
- const { contentWindow: a } = o;
153
- if (!a)
171
+ const o = H(t);
172
+ e.innerHTML = "", e.appendChild(o);
173
+ const { contentWindow: c } = o;
174
+ if (!c)
154
175
  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
- updateProvider: (c) => {
163
- r = c, f = R(a, f, c);
176
+ const f = [];
177
+ f.push(q(c, t.appCode)), f.push(k(o, t.height));
178
+ const m = new V(window, i);
179
+ let E = P(c, null, r);
180
+ o.addEventListener("load", () => R(c, t, r));
181
+ const I = new y(window, c);
182
+ return {
183
+ updateParams: (a) => R(c, a, r),
184
+ updateListeners: (a) => m.updateListeners(a),
185
+ updateProvider: (a) => {
186
+ r = a, E = P(c, E, a);
164
187
  },
165
188
  destroy: () => {
166
- m.stopListeningIframe(), p.forEach((c) => window.removeEventListener("message", c)), t.removeChild(o);
189
+ E.disconnect(), m.stopListeningIframe(), f.forEach((a) => window.removeEventListener("message", a)), I.stopListening(), e.removeChild(o);
167
190
  }
168
191
  };
169
192
  }
170
- function R(t, n, e) {
193
+ function P(e, n, t) {
171
194
  n && n.disconnect();
172
- const s = n || new W(t);
173
- return e && s.onConnect(e), s;
195
+ const s = n || new D(e);
196
+ return t && s.onConnect(t), s;
174
197
  }
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;
198
+ function H(e) {
199
+ const { width: n = b, height: t = v } = e, s = document.createElement("iframe");
200
+ return s.src = O(e), s.width = n, s.height = t, s.style.border = "0", s;
178
201
  }
179
- function w(t, n, e) {
180
- const s = !!e, i = A(n), r = I(n).toString();
181
- u(t, d.UPDATE_PARAMS, {
202
+ function R(e, n, t) {
203
+ const s = !!t, i = g(n), r = A(n).toString();
204
+ u(e, h.UPDATE_PARAMS, {
182
205
  urlParams: {
183
206
  pathname: i,
184
207
  search: r
@@ -189,26 +212,26 @@ function w(t, n, e) {
189
212
  hasProvider: s
190
213
  });
191
214
  }
192
- function y(t, n) {
193
- const e = E(window, h.ACTIVATE, () => {
194
- l(window, e), u(t, d.UPDATE_APP_DATA, {
215
+ function q(e, n) {
216
+ const t = p(window, d.ACTIVATE, () => {
217
+ T(window, t), u(e, h.UPDATE_APP_DATA, {
195
218
  metaData: n ? { appCode: n } : void 0
196
219
  });
197
220
  });
198
- return e;
221
+ return t;
199
222
  }
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;
223
+ function k(e, n = v) {
224
+ return p(window, d.UPDATE_HEIGHT, (t) => {
225
+ e.style.height = t.height ? `${t.height + F}px` : n;
203
226
  });
204
227
  }
205
228
  export {
206
229
  _ as TradeType,
207
- h as WidgetMethodsEmit,
208
- d as WidgetMethodsListen,
209
- H as createCowSwapWidget,
210
- E as listenToMessageFromWindow,
230
+ d as WidgetMethodsEmit,
231
+ h as WidgetMethodsListen,
232
+ G as createCowSwapWidget,
233
+ p as listenToMessageFromWindow,
211
234
  u as postMessageToWindow,
212
- C as stopListeningToMessageFromWindow,
213
- l as stopListeningWindowListener
235
+ W as stopListeningToMessageFromWindow,
236
+ T as stopListeningWindowListener
214
237
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cowprotocol/widget-lib",
3
- "version": "0.7.0",
3
+ "version": "0.8.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.1.0"
25
25
  }
26
26
  }
package/types.d.ts CHANGED
@@ -82,6 +82,17 @@ export interface PartnerFee {
82
82
  */
83
83
  recipient: string;
84
84
  }
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
+ };
85
96
  export interface CowSwapWidgetPalette {
86
97
  baseTheme: CowSwapTheme;
87
98
  primary: string;
@@ -94,6 +105,43 @@ export interface CowSwapWidgetPalette {
94
105
  info: string;
95
106
  success: string;
96
107
  }
108
+ export interface CowSwapWidgetSounds {
109
+ /**
110
+ * The sound to play when the order is executed. Defaults to world wide famous CoW Swap moooooooooo!
111
+ * Alternatively, you can use a URL to a custom sound file, or set to null to disable the sound.
112
+ */
113
+ postOrder?: string | null;
114
+ /**
115
+ * The sound to play when the order is executed. Defaults to world wide famous CoW Swap happy moooooooooo!
116
+ * Alternatively, you can use a URL to a custom sound file, or set to null to disable the sound.
117
+ */
118
+ orderExecuted?: string | null;
119
+ /**
120
+ * The sound to play when the order is executed. Defaults to world wide famous CoW Swap unhappy moooooooooo!
121
+ * Alternatively, you can use a URL to a custom sound file, or set to null to disable the sound.
122
+ */
123
+ orderError?: string | null;
124
+ }
125
+ export interface CowSwapWidgetImages {
126
+ /**
127
+ * The image to display when the orders table is empty (no orders yet). It defaults to "Yoga CoW" image.
128
+ * Alternatively, you can use a URL to a custom image file, or set to null to disable the image.
129
+ */
130
+ emptyOrders?: string | null;
131
+ }
132
+ export interface CowSwapWidgetBanners {
133
+ /**
134
+ * Banner text: "Use Safe web app..."
135
+ *
136
+ * Conditions for displaying the banner:
137
+ * - Safe-like app is connected to CoW Swap via WalletConnect
138
+ * - Selling native token via Swap
139
+ * - Sell token needs approval
140
+ *
141
+ * If the flag is set to true, the banner will not be displayed
142
+ */
143
+ hideSafeWebAppBanner?: boolean;
144
+ }
97
145
  export interface CowSwapWidgetParams {
98
146
  /**
99
147
  * The unique identifier of the widget consumer.
@@ -122,9 +170,7 @@ export interface CowSwapWidgetParams {
122
170
  tradeType?: TradeType;
123
171
  /**
124
172
  * The base url of the widget implementation
125
- *
126
173
  * The parameter can have the URL directly, or an object with the environment property,
127
- *
128
174
  * The base URL will default to the production environment if not specified, so it will use https://swap.cow.fi by default.
129
175
  */
130
176
  baseUrl?: string;
@@ -177,40 +223,22 @@ export interface CowSwapWidgetParams {
177
223
  * The theme of the widget UI.
178
224
  */
179
225
  theme?: CowSwapTheme | CowSwapWidgetPalette;
180
- /**
181
- * Allows to set a custom logo for the widget.
182
- */
183
- logoUrl?: string;
184
226
  /**
185
227
  * Customizable images for the widget.
186
228
  */
187
- images?: {
188
- /**
189
- * The image to display when the orders table is empty (no orders yet). It defaults to "Yoga CoW" image.
190
- * Alternatively, you can use a URL to a custom image file, or set to null to disable the image.
191
- */
192
- emptyOrders?: string | null;
193
- };
229
+ images?: CowSwapWidgetImages;
194
230
  /**
195
231
  * Sounds configuration for the app.
196
232
  */
197
- sounds?: {
198
- /**
199
- * The sound to play when the order is executed. Defaults to world wide famous CoW Swap moooooooooo!
200
- * Alternatively, you can use a URL to a custom sound file, or set to null to disable the sound.
201
- */
202
- postOrder?: string | null;
203
- /**
204
- * The sound to play when the order is executed. Defaults to world wide famous CoW Swap happy moooooooooo!
205
- * Alternatively, you can use a URL to a custom sound file, or set to null to disable the sound.
206
- */
207
- orderExecuted?: string | null;
208
- /**
209
- * The sound to play when the order is executed. Defaults to world wide famous CoW Swap unhappy moooooooooo!
210
- * Alternatively, you can use a URL to a custom sound file, or set to null to disable the sound.
211
- */
212
- orderError?: string | null;
213
- };
233
+ sounds?: CowSwapWidgetSounds;
234
+ /**
235
+ * Flags to control the display of banners in the widget.
236
+ */
237
+ banners?: CowSwapWidgetBanners;
238
+ /**
239
+ * In case when widget does not support some tokens, you can provide a list of tokens to be used in the widget
240
+ */
241
+ customTokens?: TokenInfo[];
214
242
  }
215
243
  export interface WidgetMethodsEmitPayloadMap {
216
244
  [WidgetMethodsEmit.ACTIVATE]: void;