@cowprotocol/widget-lib
Advanced tools
Comparing version
@@ -6,1 +6,2 @@ export { createCowSwapWidget } from './cowSwapWidget'; | ||
export * from './messages'; | ||
export * from '@cowprotocol/events'; |
@@ -1,1 +0,1 @@ | ||
var cowSwapWidget=function(o){"use strict";const P="cowSwapWidget";function p(t,n,e){const s={key:P,method:n,...typeof e=="object"?e:{}};t.postMessage(s,"*")}function E(t,n,e){const i=s=>{!D(s.data)||s.data.key!==P||s.data.method!==n||e(s.data)};return t.addEventListener("message",i),i}function D(t){return typeof t=="object"&&t!==null&&"key"in t&&"method"in t&&typeof t.key=="string"&&typeof t.method=="string"}function l(t,n,e){t.removeEventListener("message",e)}function m(t,n){t.removeEventListener("message",n)}var d=(t=>(t.ACTIVATE="ACTIVATE",t.UPDATE_HEIGHT="UPDATE_HEIGHT",t.EMIT_COW_EVENT="EMIT_COW_EVENT",t.PROVIDER_RPC_REQUEST="PROVIDER_RPC_REQUEST",t))(d||{}),h=(t=>(t.UPDATE_PARAMS="UPDATE_PARAMS",t.UPDATE_APP_DATA="UPDATE_APP_DATA",t.PROVIDER_RPC_RESPONSE="PROVIDER_RPC_RESPONSE",t.PROVIDER_ON_EVENT="PROVIDER_ON_EVENT",t))(h||{}),w=(t=>(t.SWAP="swap",t.LIMIT="limit",t.ADVANCED="advanced",t))(w||{});const S=["connect","disconnect","close","chainChanged","accountsChanged"];class O{constructor(n){this.iframeWidow=n,this.ethereumProvider=null,this.requestWaitingForConnection={},this.processRpcCallFromWindow=({rpcRequest:e})=>{if(!this.ethereumProvider){e.id&&(this.requestWaitingForConnection[e.id]=e);return}this.processRpcRequest(e)}}disconnect(){this.ethereumProvider=null,l(window,d.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow)}onConnect(n){this.ethereumProvider?this.disconnect():E(window,d.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),this.ethereumProvider=n,this.processPendingRequests(),S.forEach(e=>{n.on(e,i=>this.onProviderEvent(e,i))})}processPendingRequests(){Object.keys(this.requestWaitingForConnection).forEach(n=>{this.processRpcRequest(this.requestWaitingForConnection[n])}),this.requestWaitingForConnection={}}processRpcRequest(n){const{id:e,jsonrpc:i,method:s}=n;if(!this.ethereumProvider||!e)return;(s==="enable"?this.ethereumProvider.enable():this.ethereumProvider.request({...n,id:e})).then(r=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:i,id:e,result:r}})).catch(r=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:i,id:e,error:r}}))}onProviderEvent(n,e){p(this.iframeWidow,h.PROVIDER_ON_EVENT,{event:n,params:e})}forwardRpcResponseToIframe(n){p(this.iframeWidow,h.PROVIDER_RPC_RESPONSE,n)}}function L(){return typeof window>"u"||!window?"":`https://swap-dev-git-${window.location.hostname.replace("widget-configurator-git-","").replace("-cowswap.vercel.app","")}-cowswap.vercel.app`}const R={local:"http://localhost:3000",prod:"https://swap.cow.fi",dev:"https://dev.swap.cow.fi",pr:L()},T="_";function V(t){const n=R[t.env||"prod"],e=_(t),i=g(t);return n+"/#"+e+"?"+i}function _(t){const{chainId:n=1,sell:e,buy:i,tradeType:s=w.SWAP}=t,c=[(e==null?void 0:e.asset)||T,(i==null?void 0:i.asset)||T].map(encodeURIComponent).join("/");return`/${n}/widget/${s}/${c}`}function g(t){const{sell:n,buy:e,theme:i}=t,s=new URLSearchParams;return n!=null&&n.amount&&s.append("sellAmount",n.amount),e!=null&&e.amount&&s.append("buyAmount",e.amount),i&&s.append("theme",typeof i=="string"?i:i.baseTheme),s}class y{constructor(){this.subscriptions={}}on(n){const{event:e,handler:i}=n;this.subscriptions[e]||(this.subscriptions[e]=[]),this.subscriptions[e].push(i)}off(n){const{event:e,handler:i}=n;this.subscriptions[e]&&(this.subscriptions[e]=this.subscriptions[e].filter(s=>s!==i))}emit(n,e){this.subscriptions[n]&&this.subscriptions[n].forEach(i=>i(e))}}let U=1;class N{constructor(n,e=[]){this.contentWindow=n,this.eventEmitter=new y,this.listeners=[],this.instanceNumber=U++,this.updateListeners(e),this.widgetListener=E(this.contentWindow,d.EMIT_COW_EVENT,i=>this.eventEmitter.emit(i.event,i.payload))}stopListeningIframe(){m(this.contentWindow,this.widgetListener)}updateListeners(n){for(const e of this.listeners)this.eventEmitter.off(e);this.listeners=n||[];for(const e of this.listeners)this.eventEmitter.on(e)}}const A="640px",F="450px",b=20;function H(t,n){const{params:e,provider:i,listeners:s}=n;let c=i;const r=M(e);t.innerHTML="",t.appendChild(r);const{contentWindow:u}=r;if(!u)throw console.error("Iframe does not contain a window",r),new Error("Iframe does not contain a window!");const f=[];f.push(q(u,e.appCode)),f.push(G(r,e.height));const C=new N(window,s);let I=v(u,null,c);return r.addEventListener("load",()=>W(u,e,c)),{updateParams:a=>W(u,a,c),updateListeners:a=>C.updateListeners(a),updateProvider:a=>{c=a,I=v(u,I,a)},destroy:()=>{C.stopListeningIframe(),f.forEach(a=>window.removeEventListener("message",a)),t.removeChild(r)}}}function v(t,n,e){n&&n.disconnect();const i=n||new O(t);return e&&i.onConnect(e),i}function M(t){const{width:n=F,height:e=A}=t,i=document.createElement("iframe");return i.src=V(t),i.width=n,i.height=e,i.style.border="0",i}function W(t,n,e){const i=!!e,s=_(n),c=g(n).toString();p(t,h.UPDATE_PARAMS,{urlParams:{pathname:s,search:c},appParams:{...n},hasProvider:i})}function q(t,n){const e=E(window,d.ACTIVATE,()=>{m(window,e),p(t,h.UPDATE_APP_DATA,{metaData:n?{appCode:n}:void 0})});return e}function G(t,n=A){return E(window,d.UPDATE_HEIGHT,e=>{t.style.height=e.height?`${e.height+b}px`:n})}return o.COWSWAP_URLS=R,o.TradeType=w,o.WidgetMethodsEmit=d,o.WidgetMethodsListen=h,o.createCowSwapWidget=H,o.listenToMessageFromWindow=E,o.postMessageToWindow=p,o.stopListeningToMessageFromWindow=l,o.stopListeningWindowListener=m,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"}),o}({}); | ||
var cowSwapWidget=function(o){"use strict";const m="cowSwapWidget";function u(e,n,t){const s={key:m,method:n,...typeof t=="object"?t:{}};e.postMessage(s,"*")}function _(e,n,t){const i=s=>{!L(s.data)||s.data.key!==m||s.data.method!==n||t(s.data)};return e.addEventListener("message",i),i}function L(e){return typeof e=="object"&&e!==null&&"key"in e&&"method"in e&&typeof e.key=="string"&&typeof e.method=="string"}function T(e,n,t){e.removeEventListener("message",t)}function p(e,n){e.removeEventListener("message",n)}var E=(e=>(e.ACTIVATE="ACTIVATE",e.UPDATE_HEIGHT="UPDATE_HEIGHT",e.EMIT_COW_EVENT="EMIT_COW_EVENT",e.PROVIDER_RPC_REQUEST="PROVIDER_RPC_REQUEST",e))(E||{}),d=(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))(d||{}),R=(e=>(e.SWAP="swap",e.LIMIT="limit",e.ADVANCED="advanced",e))(R||{});const N=["connect","disconnect","close","chainChanged","accountsChanged"];class U{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,T(window,E.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow)}onConnect(n){this.ethereumProvider?this.disconnect():_(window,E.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),this.ethereumProvider=n,this.processPendingRequests(),N.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,d.PROVIDER_ON_EVENT,{event:n,params:t})}forwardRpcResponseToIframe(n){u(this.iframeWidow,d.PROVIDER_RPC_RESPONSE,n)}}function V(){return typeof window>"u"||!window?"":`https://swap-dev-git-${window.location.hostname.replace("widget-configurator-git-","").replace("-cowswap.vercel.app","")}-cowswap.vercel.app`}const w={local:"http://localhost:3000",prod:"https://swap.cow.fi",dev:"https://dev.swap.cow.fi",pr:V()},f="_";function F(e){const n=w[e.env||"prod"],t=A(e),i=l(e);return n+"/#"+t+"?"+i}function A(e){const{chainId:n=1,sell:t,buy:i,tradeType:s=R.SWAP}=e,c=[(t==null?void 0:t.asset)||f,(i==null?void 0:i.asset)||f].map(encodeURIComponent).join("/");return`/${n}/widget/${s}/${c}`}function l(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}var O=(e=>(e.SWAP_ETH_FLOW_SENT_TX="SWAP_ETH_FLOW_SENT_TX",e.SWAP_POSTED_API="SWAP_POSTED_API",e.SWAP_SIGNING_ERROR="SWAP_SIGNING_ERROR",e.SWAP_TRADE_EXECUTED="SWAP_TRADE_EXECUTED",e.SWAP_ORDER_CANCELLED="SWAP_ORDER_CANCELLED",e))(O||{}),D=(e=>(e.ON_TOAST_MESSAGE="ON_TOAST_MESSAGE",e.ON_POSTED_ORDER="ON_POSTED_ORDER",e.ON_POSTED_ETH_FLOW_ORDER="ON_POSTED_ETH_FLOW_ORDER",e.ON_EXECUTED_ORDER="ON_EXECUTED_ORDER",e.ON_CANCELLED_ORDER="ON_CANCELLED_ORDER",e))(D||{});class S{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))}}let H=1;class y{constructor(n,t=[]){this.contentWindow=n,this.eventEmitter=new S,this.listeners=[],this.instanceNumber=H++,this.updateListeners(t),this.widgetListener=_(this.contentWindow,E.EMIT_COW_EVENT,i=>this.eventEmitter.emit(i.event,i.payload))}stopListeningIframe(){p(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 W="640px",b="450px",M=20;function G(e,n){const{params:t,provider:i,listeners:s}=n;let c=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 P=[];P.push(X(h,t.appCode)),P.push(k(r,t.height));const v=new y(window,s);let I=C(h,null,c);return r.addEventListener("load",()=>g(h,t,c)),{updateParams:a=>g(h,a,c),updateListeners:a=>v.updateListeners(a),updateProvider:a=>{c=a,I=C(h,I,a)},destroy:()=>{v.stopListeningIframe(),P.forEach(a=>window.removeEventListener("message",a)),e.removeChild(r)}}}function C(e,n,t){n&&n.disconnect();const i=n||new U(e);return t&&i.onConnect(t),i}function q(e){const{width:n=b,height:t=W}=e,i=document.createElement("iframe");return i.src=F(e),i.width=n,i.height=t,i.style.border="0",i}function g(e,n,t){const i=!!t,s=A(n),c=l(n).toString();u(e,d.UPDATE_PARAMS,{urlParams:{pathname:s,search:c},appParams:{...n},hasProvider:i})}function X(e,n){const t=_(window,E.ACTIVATE,()=>{p(window,t),u(e,d.UPDATE_APP_DATA,{metaData:n?{appCode:n}:void 0})});return t}function k(e,n=W){return _(window,E.UPDATE_HEIGHT,t=>{e.style.height=t.height?`${t.height+M}px`:n})}return o.COWSWAP_URLS=w,o.CowEvents=D,o.SimpleCowEventEmitter=S,o.ToastMessageType=O,o.TradeType=R,o.WidgetMethodsEmit=E,o.WidgetMethodsListen=d,o.createCowSwapWidget=G,o.listenToMessageFromWindow=_,o.postMessageToWindow=u,o.stopListeningToMessageFromWindow=T,o.stopListeningWindowListener=p,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"}),o}({}); |
@@ -1,1 +0,1 @@ | ||
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const _="cowSwapWidget";function p(t,n,e){const i={key:_,method:n,...typeof e=="object"?e:{}};t.postMessage(i,"*")}function u(t,n,e){const s=i=>{!I(i.data)||i.data.key!==_||i.data.method!==n||e(i.data)};return t.addEventListener("message",s),s}function I(t){return typeof t=="object"&&t!==null&&"key"in t&&"method"in t&&typeof t.key=="string"&&typeof t.method=="string"}function g(t,n,e){t.removeEventListener("message",e)}function m(t,n){t.removeEventListener("message",n)}var a=(t=>(t.ACTIVATE="ACTIVATE",t.UPDATE_HEIGHT="UPDATE_HEIGHT",t.EMIT_COW_EVENT="EMIT_COW_EVENT",t.PROVIDER_RPC_REQUEST="PROVIDER_RPC_REQUEST",t))(a||{}),h=(t=>(t.UPDATE_PARAMS="UPDATE_PARAMS",t.UPDATE_APP_DATA="UPDATE_APP_DATA",t.PROVIDER_RPC_RESPONSE="PROVIDER_RPC_RESPONSE",t.PROVIDER_ON_EVENT="PROVIDER_ON_EVENT",t))(h||{}),w=(t=>(t.SWAP="swap",t.LIMIT="limit",t.ADVANCED="advanced",t))(w||{});const D=["connect","disconnect","close","chainChanged","accountsChanged"];class O{constructor(n){this.iframeWidow=n,this.ethereumProvider=null,this.requestWaitingForConnection={},this.processRpcCallFromWindow=({rpcRequest:e})=>{if(!this.ethereumProvider){e.id&&(this.requestWaitingForConnection[e.id]=e);return}this.processRpcRequest(e)}}disconnect(){this.ethereumProvider=null,g(window,a.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow)}onConnect(n){this.ethereumProvider?this.disconnect():u(window,a.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),this.ethereumProvider=n,this.processPendingRequests(),D.forEach(e=>{n.on(e,s=>this.onProviderEvent(e,s))})}processPendingRequests(){Object.keys(this.requestWaitingForConnection).forEach(n=>{this.processRpcRequest(this.requestWaitingForConnection[n])}),this.requestWaitingForConnection={}}processRpcRequest(n){const{id:e,jsonrpc:s,method:i}=n;if(!this.ethereumProvider||!e)return;(i==="enable"?this.ethereumProvider.enable():this.ethereumProvider.request({...n,id:e})).then(o=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:s,id:e,result:o}})).catch(o=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:s,id:e,error:o}}))}onProviderEvent(n,e){p(this.iframeWidow,h.PROVIDER_ON_EVENT,{event:n,params:e})}forwardRpcResponseToIframe(n){p(this.iframeWidow,h.PROVIDER_RPC_RESPONSE,n)}}function S(){return typeof window>"u"||!window?"":`https://swap-dev-git-${window.location.hostname.replace("widget-configurator-git-","").replace("-cowswap.vercel.app","")}-cowswap.vercel.app`}const A={local:"http://localhost:3000",prod:"https://swap.cow.fi",dev:"https://dev.swap.cow.fi",pr:S()},l="_";function L(t){const n=A[t.env||"prod"],e=v(t),s=C(t);return n+"/#"+e+"?"+s}function v(t){const{chainId:n=1,sell:e,buy:s,tradeType:i=w.SWAP}=t,r=[(e==null?void 0:e.asset)||l,(s==null?void 0:s.asset)||l].map(encodeURIComponent).join("/");return`/${n}/widget/${i}/${r}`}function C(t){const{sell:n,buy:e,theme:s}=t,i=new URLSearchParams;return n!=null&&n.amount&&i.append("sellAmount",n.amount),e!=null&&e.amount&&i.append("buyAmount",e.amount),s&&i.append("theme",typeof s=="string"?s:s.baseTheme),i}class V{constructor(){this.subscriptions={}}on(n){const{event:e,handler:s}=n;this.subscriptions[e]||(this.subscriptions[e]=[]),this.subscriptions[e].push(s)}off(n){const{event:e,handler:s}=n;this.subscriptions[e]&&(this.subscriptions[e]=this.subscriptions[e].filter(i=>i!==s))}emit(n,e){this.subscriptions[n]&&this.subscriptions[n].forEach(s=>s(e))}}let y=1;class U{constructor(n,e=[]){this.contentWindow=n,this.eventEmitter=new V,this.listeners=[],this.instanceNumber=y++,this.updateListeners(e),this.widgetListener=u(this.contentWindow,a.EMIT_COW_EVENT,s=>this.eventEmitter.emit(s.event,s.payload))}stopListeningIframe(){m(this.contentWindow,this.widgetListener)}updateListeners(n){for(const e of this.listeners)this.eventEmitter.off(e);this.listeners=n||[];for(const e of this.listeners)this.eventEmitter.on(e)}}const W="640px",N="450px",F=20;function b(t,n){const{params:e,provider:s,listeners:i}=n;let r=s;const o=H(e);t.innerHTML="",t.appendChild(o);const{contentWindow:d}=o;if(!d)throw console.error("Iframe does not contain a window",o),new Error("Iframe does not contain a window!");const E=[];E.push(M(d,e.appCode)),E.push(q(o,e.height));const f=new U(window,i);let P=R(d,null,r);return o.addEventListener("load",()=>T(d,e,r)),{updateParams:c=>T(d,c,r),updateListeners:c=>f.updateListeners(c),updateProvider:c=>{r=c,P=R(d,P,c)},destroy:()=>{f.stopListeningIframe(),E.forEach(c=>window.removeEventListener("message",c)),t.removeChild(o)}}}function R(t,n,e){n&&n.disconnect();const s=n||new O(t);return e&&s.onConnect(e),s}function H(t){const{width:n=N,height:e=W}=t,s=document.createElement("iframe");return s.src=L(t),s.width=n,s.height=e,s.style.border="0",s}function T(t,n,e){const s=!!e,i=v(n),r=C(n).toString();p(t,h.UPDATE_PARAMS,{urlParams:{pathname:i,search:r},appParams:{...n},hasProvider:s})}function M(t,n){const e=u(window,a.ACTIVATE,()=>{m(window,e),p(t,h.UPDATE_APP_DATA,{metaData:n?{appCode:n}:void 0})});return e}function q(t,n=W){return u(window,a.UPDATE_HEIGHT,e=>{t.style.height=e.height?`${e.height+F}px`:n})}exports.COWSWAP_URLS=A;exports.TradeType=w;exports.WidgetMethodsEmit=a;exports.WidgetMethodsListen=h;exports.createCowSwapWidget=b;exports.listenToMessageFromWindow=u;exports.postMessageToWindow=p;exports.stopListeningToMessageFromWindow=g;exports.stopListeningWindowListener=m; | ||
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const A="cowSwapWidget";function h(e,n,t){const s={key:A,method:n,...typeof t=="object"?t:{}};e.postMessage(s,"*")}function _(e,n,t){const i=s=>{!v(s.data)||s.data.key!==A||s.data.method!==n||t(s.data)};return e.addEventListener("message",i),i}function v(e){return typeof e=="object"&&e!==null&&"key"in e&&"method"in e&&typeof e.key=="string"&&typeof e.method=="string"}function l(e,n,t){e.removeEventListener("message",t)}function u(e,n){e.removeEventListener("message",n)}var a=(e=>(e.ACTIVATE="ACTIVATE",e.UPDATE_HEIGHT="UPDATE_HEIGHT",e.EMIT_COW_EVENT="EMIT_COW_EVENT",e.PROVIDER_RPC_REQUEST="PROVIDER_RPC_REQUEST",e))(a||{}),d=(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))(d||{}),R=(e=>(e.SWAP="swap",e.LIMIT="limit",e.ADVANCED="advanced",e))(R||{});const L=["connect","disconnect","close","chainChanged","accountsChanged"];class N{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,l(window,a.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow)}onConnect(n){this.ethereumProvider?this.disconnect():_(window,a.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),this.ethereumProvider=n,this.processPendingRequests(),L.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(o=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:i,id:t,result:o}})).catch(o=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:i,id:t,error:o}}))}onProviderEvent(n,t){h(this.iframeWidow,d.PROVIDER_ON_EVENT,{event:n,params:t})}forwardRpcResponseToIframe(n){h(this.iframeWidow,d.PROVIDER_RPC_RESPONSE,n)}}function U(){return typeof window>"u"||!window?"":`https://swap-dev-git-${window.location.hostname.replace("widget-configurator-git-","").replace("-cowswap.vercel.app","")}-cowswap.vercel.app`}const O={local:"http://localhost:3000",prod:"https://swap.cow.fi",dev:"https://dev.swap.cow.fi",pr:U()},T="_";function V(e){const n=O[e.env||"prod"],t=D(e),i=S(e);return n+"/#"+t+"?"+i}function D(e){const{chainId:n=1,sell:t,buy:i,tradeType:s=R.SWAP}=e,r=[(t==null?void 0:t.asset)||T,(i==null?void 0:i.asset)||T].map(encodeURIComponent).join("/");return`/${n}/widget/${s}/${r}`}function S(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}var W=(e=>(e.SWAP_ETH_FLOW_SENT_TX="SWAP_ETH_FLOW_SENT_TX",e.SWAP_POSTED_API="SWAP_POSTED_API",e.SWAP_SIGNING_ERROR="SWAP_SIGNING_ERROR",e.SWAP_TRADE_EXECUTED="SWAP_TRADE_EXECUTED",e.SWAP_ORDER_CANCELLED="SWAP_ORDER_CANCELLED",e))(W||{}),C=(e=>(e.ON_TOAST_MESSAGE="ON_TOAST_MESSAGE",e.ON_POSTED_ORDER="ON_POSTED_ORDER",e.ON_POSTED_ETH_FLOW_ORDER="ON_POSTED_ETH_FLOW_ORDER",e.ON_EXECUTED_ORDER="ON_EXECUTED_ORDER",e.ON_CANCELLED_ORDER="ON_CANCELLED_ORDER",e))(C||{});class g{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))}}let F=1;class H{constructor(n,t=[]){this.contentWindow=n,this.eventEmitter=new g,this.listeners=[],this.instanceNumber=F++,this.updateListeners(t),this.widgetListener=_(this.contentWindow,a.EMIT_COW_EVENT,i=>this.eventEmitter.emit(i.event,i.payload))}stopListeningIframe(){u(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 I="640px",y="450px",b=20;function M(e,n){const{params:t,provider:i,listeners:s}=n;let r=i;const o=G(t);e.innerHTML="",e.appendChild(o);const{contentWindow:E}=o;if(!E)throw console.error("Iframe does not contain a window",o),new Error("Iframe does not contain a window!");const p=[];p.push(q(E,t.appCode)),p.push(X(o,t.height));const P=new H(window,s);let m=f(E,null,r);return o.addEventListener("load",()=>w(E,t,r)),{updateParams:c=>w(E,c,r),updateListeners:c=>P.updateListeners(c),updateProvider:c=>{r=c,m=f(E,m,c)},destroy:()=>{P.stopListeningIframe(),p.forEach(c=>window.removeEventListener("message",c)),e.removeChild(o)}}}function f(e,n,t){n&&n.disconnect();const i=n||new N(e);return t&&i.onConnect(t),i}function G(e){const{width:n=y,height:t=I}=e,i=document.createElement("iframe");return i.src=V(e),i.width=n,i.height=t,i.style.border="0",i}function w(e,n,t){const i=!!t,s=D(n),r=S(n).toString();h(e,d.UPDATE_PARAMS,{urlParams:{pathname:s,search:r},appParams:{...n},hasProvider:i})}function q(e,n){const t=_(window,a.ACTIVATE,()=>{u(window,t),h(e,d.UPDATE_APP_DATA,{metaData:n?{appCode:n}:void 0})});return t}function X(e,n=I){return _(window,a.UPDATE_HEIGHT,t=>{e.style.height=t.height?`${t.height+b}px`:n})}exports.COWSWAP_URLS=O;exports.CowEvents=C;exports.SimpleCowEventEmitter=g;exports.ToastMessageType=W;exports.TradeType=R;exports.WidgetMethodsEmit=a;exports.WidgetMethodsListen=d;exports.createCowSwapWidget=M;exports.listenToMessageFromWindow=_;exports.postMessageToWindow=h;exports.stopListeningToMessageFromWindow=l;exports.stopListeningWindowListener=u; |
{ | ||
"name": "@cowprotocol/widget-lib", | ||
"version": "0.6.0-RC.0", | ||
"version": "0.6.0-RC.1", | ||
"type": "commonjs", | ||
@@ -5,0 +5,0 @@ "description": "CoW Swap Widget Library. Allows you to easily embed a CoW Swap widget on your website.", |
Sorry, the diff of this file is not supported yet
36214
5.01%647
1.57%