@cowprotocol/widget-lib
Advanced tools
Comparing version
@@ -1,3 +0,4 @@ | ||
import { CowEventListeners } from '@cowprotocol/events'; | ||
import { CowSwapWidgetParams, CowSwapWidgetProps, EthereumProvider } from './types'; | ||
import { CowEventListeners } from '../../events/src/index.ts'; | ||
/** | ||
@@ -4,0 +5,0 @@ * Callback function signature for updating the CoW Swap Widget. |
@@ -1,2 +0,3 @@ | ||
import { CowEventListeners } from '@cowprotocol/events'; | ||
import { CowEventListeners } from '../../events/src/index.ts'; | ||
export declare class IframeCowEventEmitter { | ||
@@ -3,0 +4,0 @@ private contentWindow; |
import { EthereumProvider, JsonRpcRequestMessage } from './types'; | ||
/** | ||
@@ -3,0 +4,0 @@ * Handles JSON-RPC request comming from an iFrame by delegating to a given Ethereum provider. |
@@ -1,1 +0,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}({}); | ||
var cowSwapWidget=function(o){"use strict";class L{constructor(){this.subscriptions={}}on(n){const{event:t,handler:i}=n;this.subscriptions[t]||(this.subscriptions[t]=[]),this.subscriptions[t].push(i)}off(n){const{event:t,handler:i}=n;this.subscriptions[t]&&(this.subscriptions[t]=this.subscriptions[t].filter(s=>s!==i))}emit(n,t){this.subscriptions[n]&&this.subscriptions[n].forEach(i=>i(t))}}const P="cowSwapWidget";function f(e,n,t){const s={key:P,method:n,...typeof t=="object"?t:{}};e.postMessage(s,"*")}function p(e,n,t){const i=s=>{!D(s.data)||s.data.key!==P||s.data.method!==n||t(s.data)};return e.addEventListener("message",i),i}function D(e){return typeof e=="object"&&e!==null&&"key"in e&&"method"in e&&typeof e.key=="string"&&typeof e.method=="string"}function g(e,n,t){e.removeEventListener("message",t)}function w(e,n){e.removeEventListener("message",n)}var a=(e=>(e.ACTIVATE="ACTIVATE",e.UPDATE_HEIGHT="UPDATE_HEIGHT",e.SET_FULL_HEIGHT="SET_FULL_HEIGHT",e.EMIT_COW_EVENT="EMIT_COW_EVENT",e.PROVIDER_RPC_REQUEST="PROVIDER_RPC_REQUEST",e))(a||{}),E=(e=>(e.UPDATE_PARAMS="UPDATE_PARAMS",e.UPDATE_APP_DATA="UPDATE_APP_DATA",e.PROVIDER_RPC_RESPONSE="PROVIDER_RPC_RESPONSE",e.PROVIDER_ON_EVENT="PROVIDER_ON_EVENT",e))(E||{}),m=(e=>(e.SWAP="swap",e.LIMIT="limit",e.ADVANCED="advanced",e))(m||{});const O=["primary","background","paper","text","danger","warning","alert","info","success"];class U{constructor(n,t=[]){this.contentWindow=n,this.eventEmitter=new L,this.listeners=[],this.updateListeners(t),this.widgetListener=p(this.contentWindow,a.EMIT_COW_EVENT,i=>this.eventEmitter.emit(i.event,i.payload))}stopListeningIframe(){w(this.contentWindow,this.widgetListener)}updateListeners(n){for(const t of this.listeners)this.eventEmitter.off(t);this.listeners=n||[];for(const t of this.listeners)this.eventEmitter.on(t)}}const V=["connect","disconnect","close","chainChanged","accountsChanged"];class y{constructor(n){this.iframeWidow=n,this.ethereumProvider=null,this.requestWaitingForConnection={},this.processRpcCallFromWindow=({rpcRequest:t})=>{if(!this.ethereumProvider){t.id&&(this.requestWaitingForConnection[t.id]=t);return}this.processRpcRequest(t)}}disconnect(){this.ethereumProvider=null,g(window,a.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow)}onConnect(n){this.ethereumProvider?this.disconnect():p(window,a.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),this.ethereumProvider=n,this.processPendingRequests(),V.forEach(t=>{n.on(t,i=>this.onProviderEvent(t,i))})}processPendingRequests(){Object.keys(this.requestWaitingForConnection).forEach(n=>{this.processRpcRequest(this.requestWaitingForConnection[n])}),this.requestWaitingForConnection={}}processRpcRequest(n){const{id:t,jsonrpc:i,method:s}=n;if(!this.ethereumProvider||!t)return;(s==="enable"?this.ethereumProvider.enable():this.ethereumProvider.request({...n,id:t})).then(c=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:i,id:t,result:c}})).catch(c=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:i,id:t,error:c}}))}onProviderEvent(n,t){f(this.iframeWidow,E.PROVIDER_ON_EVENT,{event:n,params:t})}forwardRpcResponseToIframe(n){f(this.iframeWidow,E.PROVIDER_RPC_RESPONSE,n)}}class H{constructor(n,t){this.appWindow=n,this.iframeWidow=t,this.forwardSdkMessage=i=>{M(i.data)&&i.origin!==window.location.origin&&(b(i.data)?this.appWindow.parent.postMessage(i.data,"*"):F(i.data)&&this.iframeWidow.postMessage(i.data,"*"))},this.startListening()}startListening(){this.appWindow.addEventListener("message",this.forwardSdkMessage)}stopListening(){this.appWindow.removeEventListener("message",this.forwardSdkMessage)}}function M(e){return typeof e=="object"&&e!==null&&"id"in e&&typeof e.id=="string"}function b(e){return"method"in e&&typeof e.method=="string"&&"params"in e&&"env"in e&&typeof e.env=="object"&&e.env!==null&&"sdkVersion"in e.env}function F(e){return"success"in e&&typeof e.success=="boolean"&&"version"in e&&typeof e.version=="string"}function R(e){return!!(e&&typeof e=="object")}const _="_";function N(e){const n=typeof e.baseUrl=="string"?e.baseUrl:"https://swap.cow.fi",t=A(e);return n+"/#"+t+"?"+W(e)}function A(e){const{chainId:n=1,sell:t,buy:i,tradeType:s=m.SWAP}=e,r=[(t==null?void 0:t.asset)||_,(i==null?void 0:i.asset)||_].map(encodeURIComponent).join("/");return`/${n}/widget/${s}/${r}`}function W(e){const n=new URLSearchParams;return G(k(n,e),e)}function k(e,n){const{sell:t,buy:i}=n;return t!=null&&t.amount&&e.append("sellAmount",t.amount),i!=null&&i.amount&&e.append("buyAmount",i.amount),e}function G(e,n){const t=n.theme;return t?(R(t)?(e.append("palette",encodeURIComponent(JSON.stringify(t))),e.append("theme",t.baseTheme)):(e.append("palette","null"),e.append("theme",t)),e):(e.append("palette","null"),e)}const I="640px",Q="450px",$=20;function B(e,n){const{params:t,provider:i,listeners:s}=n;let r=i,c=t;const d=q(t);e.innerHTML="",e.appendChild(d);const{contentWindow:u}=d;if(!u)throw console.error("Iframe does not contain a window",d),new Error("Iframe does not contain a window!");const l=[];l.push(K(u,t.appCode)),l.push(...Y(d,t.height));const C=new U(window,s);let T=S(u,null,r);d.addEventListener("load",()=>v(u,c,r));const j=new H(window,u);return{updateParams:h=>{c=h,v(u,c,r)},updateListeners:h=>C.updateListeners(h),updateProvider:h=>{r=h,T=S(u,T,h)},destroy:()=>{T.disconnect(),C.stopListeningIframe(),l.forEach(h=>window.removeEventListener("message",h)),j.stopListening(),e.removeChild(d)}}}function S(e,n,t){n&&n.disconnect();const i=n||new y(e);return t&&i.onConnect(t),i}function q(e){const{width:n=Q,height:t=I}=e,i=document.createElement("iframe");return i.src=N(e),i.width=n,i.height=t,i.style.border="0",i}function v(e,n,t){const i=!!t,s=A(n),r=W(n).toString(),{theme:c,...d}=n;f(e,E.UPDATE_PARAMS,{urlParams:{pathname:s,search:r},appParams:d,hasProvider:i})}function K(e,n){const t=p(window,a.ACTIVATE,()=>{w(window,t),f(e,E.UPDATE_APP_DATA,{metaData:n?{appCode:n}:void 0})});return t}function Y(e,n=I){return[p(window,a.UPDATE_HEIGHT,t=>{e.style.height=t.height?`${t.height+$}px`:n}),p(window,a.SET_FULL_HEIGHT,({isUpToSmall:t})=>{e.style.height=t?n:`${document.body.offsetHeight}px`})]}return o.TradeType=m,o.WIDGET_PALETTE_COLORS=O,o.WidgetMethodsEmit=a,o.WidgetMethodsListen=E,o.createCowSwapWidget=B,o.isCowSwapWidgetPalette=R,o.listenToMessageFromWindow=p,o.postMessageToWindow=f,o.stopListeningToMessageFromWindow=g,o.stopListeningWindowListener=w,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"}),o}({}); |
@@ -1,1 +0,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; | ||
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class L{constructor(){this.subscriptions={}}on(n){const{event:t,handler:i}=n;this.subscriptions[t]||(this.subscriptions[t]=[]),this.subscriptions[t].push(i)}off(n){const{event:t,handler:i}=n;this.subscriptions[t]&&(this.subscriptions[t]=this.subscriptions[t].filter(s=>s!==i))}emit(n,t){this.subscriptions[n]&&this.subscriptions[n].forEach(i=>i(t))}}const _="cowSwapWidget";function E(e,n,t){const s={key:_,method:n,...typeof t=="object"?t:{}};e.postMessage(s,"*")}function p(e,n,t){const i=s=>{!D(s.data)||s.data.key!==_||s.data.method!==n||t(s.data)};return e.addEventListener("message",i),i}function D(e){return typeof e=="object"&&e!==null&&"key"in e&&"method"in e&&typeof e.key=="string"&&typeof e.method=="string"}function A(e,n,t){e.removeEventListener("message",t)}function m(e,n){e.removeEventListener("message",n)}var d=(e=>(e.ACTIVATE="ACTIVATE",e.UPDATE_HEIGHT="UPDATE_HEIGHT",e.SET_FULL_HEIGHT="SET_FULL_HEIGHT",e.EMIT_COW_EVENT="EMIT_COW_EVENT",e.PROVIDER_RPC_REQUEST="PROVIDER_RPC_REQUEST",e))(d||{}),u=(e=>(e.UPDATE_PARAMS="UPDATE_PARAMS",e.UPDATE_APP_DATA="UPDATE_APP_DATA",e.PROVIDER_RPC_RESPONSE="PROVIDER_RPC_RESPONSE",e.PROVIDER_ON_EVENT="PROVIDER_ON_EVENT",e))(u||{}),l=(e=>(e.SWAP="swap",e.LIMIT="limit",e.ADVANCED="advanced",e))(l||{});const O=["primary","background","paper","text","danger","warning","alert","info","success"];class U{constructor(n,t=[]){this.contentWindow=n,this.eventEmitter=new L,this.listeners=[],this.updateListeners(t),this.widgetListener=p(this.contentWindow,d.EMIT_COW_EVENT,i=>this.eventEmitter.emit(i.event,i.payload))}stopListeningIframe(){m(this.contentWindow,this.widgetListener)}updateListeners(n){for(const t of this.listeners)this.eventEmitter.off(t);this.listeners=n||[];for(const t of this.listeners)this.eventEmitter.on(t)}}const V=["connect","disconnect","close","chainChanged","accountsChanged"];class y{constructor(n){this.iframeWidow=n,this.ethereumProvider=null,this.requestWaitingForConnection={},this.processRpcCallFromWindow=({rpcRequest:t})=>{if(!this.ethereumProvider){t.id&&(this.requestWaitingForConnection[t.id]=t);return}this.processRpcRequest(t)}}disconnect(){this.ethereumProvider=null,A(window,d.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow)}onConnect(n){this.ethereumProvider?this.disconnect():p(window,d.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),this.ethereumProvider=n,this.processPendingRequests(),V.forEach(t=>{n.on(t,i=>this.onProviderEvent(t,i))})}processPendingRequests(){Object.keys(this.requestWaitingForConnection).forEach(n=>{this.processRpcRequest(this.requestWaitingForConnection[n])}),this.requestWaitingForConnection={}}processRpcRequest(n){const{id:t,jsonrpc:i,method:s}=n;if(!this.ethereumProvider||!t)return;(s==="enable"?this.ethereumProvider.enable():this.ethereumProvider.request({...n,id:t})).then(r=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:i,id:t,result:r}})).catch(r=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:i,id:t,error:r}}))}onProviderEvent(n,t){E(this.iframeWidow,u.PROVIDER_ON_EVENT,{event:n,params:t})}forwardRpcResponseToIframe(n){E(this.iframeWidow,u.PROVIDER_RPC_RESPONSE,n)}}class H{constructor(n,t){this.appWindow=n,this.iframeWidow=t,this.forwardSdkMessage=i=>{M(i.data)&&i.origin!==window.location.origin&&(b(i.data)?this.appWindow.parent.postMessage(i.data,"*"):F(i.data)&&this.iframeWidow.postMessage(i.data,"*"))},this.startListening()}startListening(){this.appWindow.addEventListener("message",this.forwardSdkMessage)}stopListening(){this.appWindow.removeEventListener("message",this.forwardSdkMessage)}}function M(e){return typeof e=="object"&&e!==null&&"id"in e&&typeof e.id=="string"}function b(e){return"method"in e&&typeof e.method=="string"&&"params"in e&&"env"in e&&typeof e.env=="object"&&e.env!==null&&"sdkVersion"in e.env}function F(e){return"success"in e&&typeof e.success=="boolean"&&"version"in e&&typeof e.version=="string"}function I(e){return!!(e&&typeof e=="object")}const P="_";function N(e){const n=typeof e.baseUrl=="string"?e.baseUrl:"https://swap.cow.fi",t=W(e);return n+"/#"+t+"?"+S(e)}function W(e){const{chainId:n=1,sell:t,buy:i,tradeType:s=l.SWAP}=e,o=[(t==null?void 0:t.asset)||P,(i==null?void 0:i.asset)||P].map(encodeURIComponent).join("/");return`/${n}/widget/${s}/${o}`}function S(e){const n=new URLSearchParams;return G(k(n,e),e)}function k(e,n){const{sell:t,buy:i}=n;return t!=null&&t.amount&&e.append("sellAmount",t.amount),i!=null&&i.amount&&e.append("buyAmount",i.amount),e}function G(e,n){const t=n.theme;return t?(I(t)?(e.append("palette",encodeURIComponent(JSON.stringify(t))),e.append("theme",t.baseTheme)):(e.append("palette","null"),e.append("theme",t)),e):(e.append("palette","null"),e)}const C="640px",Q="450px",x=20;function $(e,n){const{params:t,provider:i,listeners:s}=n;let o=i,r=t;const a=B(t);e.innerHTML="",e.appendChild(a);const{contentWindow:h}=a;if(!h)throw console.error("Iframe does not contain a window",a),new Error("Iframe does not contain a window!");const f=[];f.push(q(h,t.appCode)),f.push(...K(a,t.height));const T=new U(window,s);let w=g(h,null,o);a.addEventListener("load",()=>R(h,r,o));const v=new H(window,h);return{updateParams:c=>{r=c,R(h,r,o)},updateListeners:c=>T.updateListeners(c),updateProvider:c=>{o=c,w=g(h,w,c)},destroy:()=>{w.disconnect(),T.stopListeningIframe(),f.forEach(c=>window.removeEventListener("message",c)),v.stopListening(),e.removeChild(a)}}}function g(e,n,t){n&&n.disconnect();const i=n||new y(e);return t&&i.onConnect(t),i}function B(e){const{width:n=Q,height:t=C}=e,i=document.createElement("iframe");return i.src=N(e),i.width=n,i.height=t,i.style.border="0",i}function R(e,n,t){const i=!!t,s=W(n),o=S(n).toString(),{theme:r,...a}=n;E(e,u.UPDATE_PARAMS,{urlParams:{pathname:s,search:o},appParams:a,hasProvider:i})}function q(e,n){const t=p(window,d.ACTIVATE,()=>{m(window,t),E(e,u.UPDATE_APP_DATA,{metaData:n?{appCode:n}:void 0})});return t}function K(e,n=C){return[p(window,d.UPDATE_HEIGHT,t=>{e.style.height=t.height?`${t.height+x}px`:n}),p(window,d.SET_FULL_HEIGHT,({isUpToSmall:t})=>{e.style.height=t?n:`${document.body.offsetHeight}px`})]}exports.TradeType=l;exports.WIDGET_PALETTE_COLORS=O;exports.WidgetMethodsEmit=d;exports.WidgetMethodsListen=u;exports.createCowSwapWidget=$;exports.isCowSwapWidgetPalette=I;exports.listenToMessageFromWindow=p;exports.postMessageToWindow=E;exports.stopListeningToMessageFromWindow=A;exports.stopListeningWindowListener=m; |
import { WidgetMethodsEmit, WidgetMethodsEmitPayloadMap, WidgetMethodsListen, WidgetMethodsListenPayloadMap } from './types'; | ||
export declare function postMessageToWindow<T extends WidgetMethodsEmit>(contentWindow: Window, method: T, payload: WidgetMethodsEmitPayloadMap[T]): void; | ||
@@ -3,0 +4,0 @@ export declare function postMessageToWindow<T extends WidgetMethodsListen>(contentWindow: Window, method: T, payload: WidgetMethodsListenPayloadMap[T]): void; |
{ | ||
"name": "@cowprotocol/widget-lib", | ||
"version": "0.11.0", | ||
"version": "0.11.1", | ||
"type": "commonjs", | ||
@@ -24,4 +24,4 @@ "description": "CoW Swap Widget Library. Allows you to easily embed a CoW Swap widget on your website.", | ||
"dependencies": { | ||
"@cowprotocol/events": "^1.2.1" | ||
"@cowprotocol/events": "^1.3.0" | ||
} | ||
} |
import { CowSwapTheme, CowSwapWidgetPalette } from './types'; | ||
export declare function isCowSwapWidgetPalette(palette: CowSwapTheme | CowSwapWidgetPalette | undefined): palette is CowSwapWidgetPalette; |
@@ -1,3 +0,4 @@ | ||
import type { SupportedChainId } from '@cowprotocol/cow-sdk'; | ||
import { CowEventListeners, CowEventPayloadMap, CowEvents } from '@cowprotocol/events'; | ||
import { CowEventListeners, CowEventPayloadMap, CowEvents } from '../../events/src/index.ts'; | ||
import { SupportedChainId } from '@cowprotocol/cow-sdk'; | ||
export type { SupportedChainId } from '@cowprotocol/cow-sdk'; | ||
@@ -7,2 +8,3 @@ export declare enum WidgetMethodsEmit { | ||
UPDATE_HEIGHT = "UPDATE_HEIGHT", | ||
SET_FULL_HEIGHT = "SET_FULL_HEIGHT", | ||
EMIT_COW_EVENT = "EMIT_COW_EVENT", | ||
@@ -84,3 +86,3 @@ PROVIDER_RPC_REQUEST = "PROVIDER_RPC_REQUEST" | ||
*/ | ||
recipient: string; | ||
recipient: string | Record<SupportedChainId, string>; | ||
} | ||
@@ -253,2 +255,3 @@ /** | ||
[WidgetMethodsEmit.UPDATE_HEIGHT]: UpdateWidgetHeightPayload; | ||
[WidgetMethodsEmit.SET_FULL_HEIGHT]: SetWidgetFullHeightPayload; | ||
[WidgetMethodsEmit.PROVIDER_RPC_REQUEST]: ProviderRpcRequestPayload; | ||
@@ -281,2 +284,5 @@ } | ||
} | ||
export interface SetWidgetFullHeightPayload { | ||
isUpToSmall?: boolean; | ||
} | ||
export interface EmitCowEventPayload<T extends CowEvents> { | ||
@@ -283,0 +289,0 @@ event: T; |
import { CowSwapWidgetParams } from './types'; | ||
export declare function buildWidgetUrl(params: Partial<CowSwapWidgetParams>): string; | ||
export declare function buildWidgetPath(params: Partial<CowSwapWidgetParams>): string; | ||
export declare function buildWidgetUrlQuery(params: Partial<CowSwapWidgetParams>): URLSearchParams; |
Sorry, the diff of this file is not supported yet
40378
1.84%740
1.51%Updated