@cowprotocol/widget-lib 0.10.0 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.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 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 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=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 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?(g(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(){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",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,()=>{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+B}px`:n})}return o.TradeType=w,o.WIDGET_PALETTE_COLORS=L,o.WidgetMethodsEmit=h,o.WidgetMethodsListen=p,o.createCowSwapWidget=$,o.isCowSwapWidgetPalette=g,o.listenToMessageFromWindow=E,o.postMessageToWindow=f,o.stopListeningToMessageFromWindow=R,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 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}({});
package/index.js CHANGED
@@ -1 +1 @@
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 M(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 y(U(n,e),e)}function U(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 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: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",x=20;function B(e,n){const{params:t,provider:s,listeners:i}=n;let o=s,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,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 $(e){const{width:n=Q,height:t=I}=e,s=document.createElement("iframe");return s.src=M(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 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=I){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"});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;
package/index.mjs CHANGED
@@ -1,21 +1,21 @@
1
1
  const T = "cowSwapWidget";
2
2
  function u(e, n, t) {
3
- const i = {
3
+ const s = {
4
4
  key: T,
5
5
  method: n,
6
6
  ...typeof t == "object" ? t : {}
7
7
  };
8
8
  e.postMessage(
9
- i,
9
+ s,
10
10
  "*"
11
11
  // TODO: Change to CoW specific origin in production. https://github.com/cowprotocol/cowswap/issues/3828
12
12
  );
13
13
  }
14
14
  function f(e, n, t) {
15
- const s = (i) => {
16
- !W(i.data) || i.data.key !== T || i.data.method !== n || t(i.data);
15
+ const i = (s) => {
16
+ !W(s.data) || s.data.key !== T || s.data.method !== n || t(s.data);
17
17
  };
18
- return e.addEventListener("message", s), s;
18
+ return e.addEventListener("message", i), i;
19
19
  }
20
20
  function W(e) {
21
21
  return typeof e == "object" && e !== null && "key" in e && "method" in e && typeof e.key == "string" && typeof e.method == "string";
@@ -65,7 +65,7 @@ class O {
65
65
  */
66
66
  onConnect(n) {
67
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));
68
+ n.on(t, (i) => this.onProviderEvent(t, i));
69
69
  });
70
70
  }
71
71
  processPendingRequests() {
@@ -78,16 +78,16 @@ class O {
78
78
  * @param request - The JSON-RPC request to be processed.
79
79
  */
80
80
  processRpcRequest(n) {
81
- const { id: t, jsonrpc: s, method: i } = n;
81
+ const { id: t, jsonrpc: i, method: s } = n;
82
82
  if (!this.ethereumProvider || !t)
83
83
  return;
84
- (i === "enable" ? this.ethereumProvider.enable() : this.ethereumProvider.request({ ...n, id: t })).then(
84
+ (s === "enable" ? this.ethereumProvider.enable() : this.ethereumProvider.request({ ...n, id: t })).then(
85
85
  (r) => this.forwardRpcResponseToIframe({
86
- rpcResponse: { jsonrpc: s, id: t, result: r }
86
+ rpcResponse: { jsonrpc: i, id: t, result: r }
87
87
  })
88
88
  ).catch(
89
89
  (r) => this.forwardRpcResponseToIframe({
90
- rpcResponse: { jsonrpc: s, id: t, error: r }
90
+ rpcResponse: { jsonrpc: i, id: t, error: r }
91
91
  })
92
92
  );
93
93
  }
@@ -110,19 +110,19 @@ function L(e) {
110
110
  const w = "_";
111
111
  function V(e) {
112
112
  const n = typeof e.baseUrl == "string" ? e.baseUrl : "https://swap.cow.fi", t = A(e);
113
- return n + "/#" + t + "?" + v(e);
113
+ return n + "/#" + t + "?" + I(e);
114
114
  }
115
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) || w, (s == null ? void 0 : s.asset) || w].map(encodeURIComponent).join("/");
117
- return `/${n}/widget/${i}/${o}`;
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
118
  }
119
- function v(e) {
119
+ function I(e) {
120
120
  const n = new URLSearchParams();
121
121
  return b(U(n, e), e);
122
122
  }
123
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;
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
126
  }
127
127
  function b(e, n) {
128
128
  const t = n.theme;
@@ -133,15 +133,15 @@ class y {
133
133
  this.subscriptions = {};
134
134
  }
135
135
  on(n) {
136
- const { event: t, handler: s } = n;
137
- this.subscriptions[t] || (this.subscriptions[t] = []), this.subscriptions[t].push(s);
136
+ const { event: t, handler: i } = n;
137
+ this.subscriptions[t] || (this.subscriptions[t] = []), this.subscriptions[t].push(i);
138
138
  }
139
139
  off(n) {
140
- const { event: t, handler: s } = n;
141
- this.subscriptions[t] && (this.subscriptions[t] = this.subscriptions[t].filter((i) => i !== s));
140
+ const { event: t, handler: i } = n;
141
+ this.subscriptions[t] && (this.subscriptions[t] = this.subscriptions[t].filter((s) => s !== i));
142
142
  }
143
143
  emit(n, t) {
144
- this.subscriptions[n] && this.subscriptions[n].forEach((s) => s(t));
144
+ this.subscriptions[n] && this.subscriptions[n].forEach((i) => i(t));
145
145
  }
146
146
  }
147
147
  class N {
@@ -149,7 +149,7 @@ class N {
149
149
  this.contentWindow = n, this.eventEmitter = new y(), this.listeners = [], this.updateListeners(t), this.widgetListener = f(
150
150
  this.contentWindow,
151
151
  h.EMIT_COW_EVENT,
152
- (s) => this.eventEmitter.emit(s.event, s.payload)
152
+ (i) => this.eventEmitter.emit(i.event, i.payload)
153
153
  );
154
154
  }
155
155
  stopListeningIframe() {
@@ -165,8 +165,8 @@ class N {
165
165
  }
166
166
  class M {
167
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, "*"));
168
+ 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, "*"));
170
170
  }, this.startListening();
171
171
  }
172
172
  startListening() {
@@ -185,10 +185,10 @@ function H(e) {
185
185
  function k(e) {
186
186
  return "success" in e && typeof e.success == "boolean" && "version" in e && typeof e.version == "string";
187
187
  }
188
- const I = "640px", G = "450px", Q = 20;
188
+ const v = "640px", G = "450px", Q = 20;
189
189
  function K(e, n) {
190
- const { params: t, provider: s, listeners: i } = n;
191
- let o = s, r = t;
190
+ const { params: t, provider: i, listeners: s } = n;
191
+ let o = i, r = t;
192
192
  const a = x(t);
193
193
  e.innerHTML = "", e.appendChild(a);
194
194
  const { contentWindow: d } = a;
@@ -196,7 +196,7 @@ function K(e, n) {
196
196
  throw console.error("Iframe does not contain a window", a), new Error("Iframe does not contain a window!");
197
197
  const E = [];
198
198
  E.push(B(d, t.appCode)), E.push($(a, t.height));
199
- const l = new N(window, i);
199
+ const l = new N(window, s);
200
200
  let m = P(d, null, o);
201
201
  a.addEventListener("load", () => R(d, r, o));
202
202
  const C = new M(window, d);
@@ -215,22 +215,22 @@ function K(e, n) {
215
215
  }
216
216
  function P(e, n, t) {
217
217
  n && n.disconnect();
218
- const s = n || new O(e);
219
- return t && s.onConnect(t), s;
218
+ const i = n || new O(e);
219
+ return t && i.onConnect(t), i;
220
220
  }
221
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;
222
+ 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
224
  }
225
225
  function R(e, n, t) {
226
- const s = !!t, i = A(n), o = v(n).toString(), { theme: r, ...a } = n;
226
+ const i = !!t, s = A(n), o = I(n).toString(), { theme: r, ...a } = n;
227
227
  u(e, p.UPDATE_PARAMS, {
228
228
  urlParams: {
229
- pathname: i,
229
+ pathname: s,
230
230
  search: o
231
231
  },
232
232
  appParams: a,
233
- hasProvider: s
233
+ hasProvider: i
234
234
  });
235
235
  }
236
236
  function B(e, n) {
@@ -241,7 +241,7 @@ function B(e, n) {
241
241
  });
242
242
  return t;
243
243
  }
244
- function $(e, n = I) {
244
+ function $(e, n = v) {
245
245
  return f(window, h.UPDATE_HEIGHT, (t) => {
246
246
  e.style.height = t.height ? `${t.height + Q}px` : n;
247
247
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cowprotocol/widget-lib",
3
- "version": "0.10.0",
3
+ "version": "0.11.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",
package/types.d.ts CHANGED
@@ -138,6 +138,10 @@ export interface CowSwapWidgetBanners {
138
138
  */
139
139
  hideSafeWebAppBanner?: boolean;
140
140
  }
141
+ export interface CowSwapWidgetContent {
142
+ feeLabel?: string;
143
+ feeTooltipMarkdown?: string;
144
+ }
141
145
  export interface CowSwapWidgetParams {
142
146
  /**
143
147
  * The unique identifier of the widget consumer.
@@ -235,6 +239,10 @@ export interface CowSwapWidgetParams {
235
239
  * In case when widget does not support some tokens, you can provide a list of tokens to be used in the widget
236
240
  */
237
241
  customTokens?: TokenInfo[];
242
+ /**
243
+ * Customizable labels and content for the widget.
244
+ */
245
+ content?: CowSwapWidgetContent;
238
246
  }
239
247
  export interface WidgetMethodsEmitPayloadMap {
240
248
  [WidgetMethodsEmit.ACTIVATE]: void;