@cowprotocol/widget-lib 0.22.1 → 0.22.3
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 +1 -1
- package/index.js +1 -1
- package/index.mjs +68 -68
- package/package.json +2 -2
package/index.iife.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var cowSwapWidget=function(w){"use strict";class D{key;constructor(t){this.key=t}postMessageToWindow(t,n,s){const p=typeof s=="object"?s:{},l={key:this.key,method:n,...p};t.postMessage(l,"*")}listenToMessageFromWindow(t,n,s){const p=l=>{!x(l.data)||l.data.key!==this.key||l.data.method!==n||s(l.data)};return t.addEventListener("message",p),p}stopListeningToMessageFromWindow(t,n,s){t.removeEventListener("message",s)}stopListeningWindowListener(t,n){t.removeEventListener("message",n)}}function x(e){return typeof e=="object"&&e!==null&&"key"in e&&"method"in e&&typeof e.key=="string"&&typeof e.method=="string"}var m=function(e){return e.PROVIDER_RPC_REQUEST="PROVIDER_RPC_REQUEST",e.PROVIDER_RPC_RESPONSE="PROVIDER_RPC_RESPONSE",e.PROVIDER_ON_EVENT="PROVIDER_ON_EVENT",e.SEND_PROVIDER_META_INFO="SEND_PROVIDER_META_INFO",e.REQUEST_PROVIDER_META_INFO="REQUEST_PROVIDER_META_INFO",e}({});const R=new D("cowSwapIframeRpcProviderTransport");function Q(e){if(e.isWalletConnect)return e?.session?.peer?.metadata}function j(e,t){return t.find(n=>n.provider===e)?.info}const G=["connect","disconnect","close","chainChanged","accountsChanged"],L=[];window.addEventListener("eip6963:announceProvider",e=>{const t=e;L.push(t.detail)});class q{iframeWidow;ethereumProvider=null;requestWaitingForConnection={};constructor(t){this.iframeWidow=t}disconnect(){this.ethereumProvider=null,R.stopListeningToMessageFromWindow(window,m.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),R.stopListeningToMessageFromWindow(window,m.REQUEST_PROVIDER_META_INFO,this.processProviderMetaInfoRequest)}onConnect(t){this.ethereumProvider?this.disconnect():R.listenToMessageFromWindow(window,m.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),this.ethereumProvider=t,this.processPendingRequests(),G.forEach(n=>{t.on(n,s=>this.onProviderEvent(n,s))}),R.listenToMessageFromWindow(window,m.REQUEST_PROVIDER_META_INFO,this.processProviderMetaInfoRequest)}processPendingRequests(){Object.keys(this.requestWaitingForConnection).forEach(t=>{this.processRpcRequest(this.requestWaitingForConnection[t])}),this.requestWaitingForConnection={}}processRpcRequest(t){const{id:n,jsonrpc:s,method:p}=t;if(!this.ethereumProvider||!n)return;(p==="enable"?this.ethereumProvider.request({method:"eth_requestAccounts",id:n}):this.ethereumProvider.request({...t,id:n})).then(_=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:s,id:n,result:_}})).catch(_=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:s,id:n,error:_}}))}processRpcCallFromWindow=({rpcRequest:t})=>{if(!this.ethereumProvider){t.id&&(this.requestWaitingForConnection[t.id]=t);return}this.processRpcRequest(t)};processProviderMetaInfoRequest=()=>{if(!this.ethereumProvider)return;const t=j(this.ethereumProvider,L),n=Q(this.ethereumProvider);R.postMessageToWindow(this.iframeWidow,m.SEND_PROVIDER_META_INFO,{providerEip6963Info:t,providerWcMetadata:n})};onProviderEvent(t,n){R.postMessageToWindow(this.iframeWidow,m.PROVIDER_ON_EVENT,{event:t,params:n})}forwardRpcResponseToIframe(t){R.postMessageToWindow(this.iframeWidow,m.PROVIDER_RPC_RESPONSE,t)}}var y={exports:{}},b;function $(){return b||(b=1,function(e){var t=Object.prototype.hasOwnProperty,n="~";function s(){}Object.create&&(s.prototype=Object.create(null),new s().__proto__||(n=!1));function p(c,r,i){this.fn=c,this.context=r,this.once=i||!1}function l(c,r,i,o,E){if(typeof i!="function")throw new TypeError("The listener must be a function");var d=new p(i,o||c,E),f=n?n+r:r;return c._events[f]?c._events[f].fn?c._events[f]=[c._events[f],d]:c._events[f].push(d):(c._events[f]=d,c._eventsCount++),c}function _(c,r){--c._eventsCount===0?c._events=new s:delete c._events[r]}function u(){this._events=new s,this._eventsCount=0}u.prototype.eventNames=function(){var r=[],i,o;if(this._eventsCount===0)return r;for(o in i=this._events)t.call(i,o)&&r.push(n?o.slice(1):o);return Object.getOwnPropertySymbols?r.concat(Object.getOwnPropertySymbols(i)):r},u.prototype.listeners=function(r){var i=n?n+r:r,o=this._events[i];if(!o)return[];if(o.fn)return[o.fn];for(var E=0,d=o.length,f=new Array(d);E<d;E++)f[E]=o[E].fn;return f},u.prototype.listenerCount=function(r){var i=n?n+r:r,o=this._events[i];return o?o.fn?1:o.length:0},u.prototype.emit=function(r,i,o,E,d,f){var v=n?n+r:r;if(!this._events[v])return!1;var a=this._events[v],P=arguments.length,g,h;if(a.fn){switch(a.once&&this.removeListener(r,a.fn,void 0,!0),P){case 1:return a.fn.call(a.context),!0;case 2:return a.fn.call(a.context,i),!0;case 3:return a.fn.call(a.context,i,o),!0;case 4:return a.fn.call(a.context,i,o,E),!0;case 5:return a.fn.call(a.context,i,o,E,d),!0;case 6:return a.fn.call(a.context,i,o,E,d,f),!0}for(h=1,g=new Array(P-1);h<P;h++)g[h-1]=arguments[h];a.fn.apply(a.context,g)}else{var Ee=a.length,W;for(h=0;h<Ee;h++)switch(a[h].once&&this.removeListener(r,a[h].fn,void 0,!0),P){case 1:a[h].fn.call(a[h].context);break;case 2:a[h].fn.call(a[h].context,i);break;case 3:a[h].fn.call(a[h].context,i,o);break;case 4:a[h].fn.call(a[h].context,i,o,E);break;default:if(!g)for(W=1,g=new Array(P-1);W<P;W++)g[W-1]=arguments[W];a[h].fn.apply(a[h].context,g)}}return!0},u.prototype.on=function(r,i,o){return l(this,r,i,o,!1)},u.prototype.once=function(r,i,o){return l(this,r,i,o,!0)},u.prototype.removeListener=function(r,i,o,E){var d=n?n+r:r;if(!this._events[d])return this;if(!i)return _(this,d),this;var f=this._events[d];if(f.fn)f.fn===i&&(!E||f.once)&&(!o||f.context===o)&&_(this,d);else{for(var v=0,a=[],P=f.length;v<P;v++)(f[v].fn!==i||E&&!f[v].once||o&&f[v].context!==o)&&a.push(f[v]);a.length?this._events[d]=a.length===1?a[0]:a:_(this,d)}return this},u.prototype.removeAllListeners=function(r){var i;return r?(i=n?n+r:r,this._events[i]&&_(this,i)):(this._events=new s,this._eventsCount=0),this},u.prototype.off=u.prototype.removeListener,u.prototype.addListener=u.prototype.on,u.prefixed=n,u.EventEmitter=u,e.exports=u}(y)),y.exports}$();class B{subscriptions={};on(t){const{event:n,handler:s}=t;return this.subscriptions[n]||(this.subscriptions[n]=[]),this.subscriptions[n].push(s),t}off(t){const{event:n,handler:s}=t;return this.subscriptions[n]&&(this.subscriptions[n]=this.subscriptions[n].filter(p=>p!==s)),t}emit(t,n){this.subscriptions[t]&&this.subscriptions[t].forEach(s=>s(n))}}var I=function(e){return 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.INTERCEPT_WINDOW_OPEN="INTERCEPT_WINDOW_OPEN",e}({}),S=function(e){return 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}({}),A=function(e){return e.SWAP="swap",e.LIMIT="limit",e.ADVANCED="advanced",e.YIELD="yield",e}({});const Y=["primary","background","paper","text","danger","warning","alert","info","success"],T=new D("cowSwapWidget");class z{contentWindow;eventEmitter=new B;listeners=[];widgetListener;constructor(t,n=[]){this.contentWindow=t,this.updateListeners(n),this.widgetListener=T.listenToMessageFromWindow(this.contentWindow,I.EMIT_COW_EVENT,s=>this.eventEmitter.emit(s.event,s.payload))}stopListeningIframe(){T.stopListeningWindowListener(this.contentWindow,this.widgetListener)}updateListeners(t){for(const n of this.listeners)this.eventEmitter.off(n);this.listeners=t||[];for(const n of this.listeners)this.eventEmitter.on(n)}}class J{appWindow;iframeWidow;forwardSdkMessage;constructor(t,n){this.appWindow=t,this.iframeWidow=n,this.forwardSdkMessage=s=>{K(s.data)&&s.origin!==window.location.origin&&(X(s.data)?this.appWindow.parent.postMessage(s.data,"*"):Z(s.data)&&this.iframeWidow.postMessage(s.data,"*"))},this.startListening()}startListening(){this.appWindow.addEventListener("message",this.forwardSdkMessage)}stopListening(){this.appWindow.removeEventListener("message",this.forwardSdkMessage)}}function K(e){return typeof e=="object"&&e!==null&&"id"in e&&typeof e.id=="string"}function X(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 Z(e){return"success"in e&&typeof e.success=="boolean"&&"version"in e&&typeof e.version=="string"}function M(e){return!!(e&&typeof e=="object")}const N="_";function ee(e){const t=typeof e.baseUrl=="string"?e.baseUrl:"https://swap.cow.fi",n=V(e);return t+"/#"+n+"?"+F(e)}function V(e){const{chainId:t=1,sell:n,buy:s,tradeType:p=A.SWAP}=e,l=[n?.asset||N,s?.asset||N].map(encodeURIComponent).join("/");return`/${t}/widget/${p}/${l}`}function F(e){const t=new URLSearchParams;return se(ne(te(t,e),e),e)}function te(e,t){const{sell:n,buy:s}=t;return n?.amount&&e.append("sellAmount",n.amount),s?.amount&&e.append("buyAmount",s.amount),e}function ne(e,t){const n=t.theme;return n?(M(n)?(e.append("palette",encodeURIComponent(JSON.stringify(n))),e.append("theme",n.baseTheme)):(e.append("palette","null"),e.append("theme",n)),e):(e.append("palette","null"),e)}function se(e,t){return t.targetChainId&&e.append("targetChainId",t.targetChainId.toString()),e}const U="640px",re="450px",ie=20;function oe(e,t){const{params:n,provider:s,listeners:p}=t;let l=s,_=n;const u=ae(n);e.innerHTML="",e.appendChild(u);const{contentWindow:c}=u;if(!c)throw console.error("Iframe does not contain a window",u),new Error("Iframe does not contain a window!");const r=[];r.push(ce(c,n.appCode)),r.push(...de(u,n.height,n.maxHeight)),r.push(ue());const i=new z(window,p);let o=k(c,null,l);u.addEventListener("load",()=>H(c,_,l));const E=new J(window,c);return{updateParams:d=>{_=d,H(c,_,l)},updateListeners:d=>i.updateListeners(d),updateProvider:d=>{l=d,o=k(c,o,d)},destroy:()=>{o.disconnect(),i.stopListeningIframe(),r.forEach(d=>window.removeEventListener("message",d)),E.stopListening(),e.removeChild(u)}}}function k(e,t,n){t&&t.disconnect();const s=t||new q(e);return n&&s.onConnect(n),s}function ae(e){const{width:t=re,height:n=U}=e,s=document.createElement("iframe");return s.src=ee(e),s.width=t,s.height=n,s.style.border="0",s.allow="clipboard-read; clipboard-write",s}function H(e,t,n){const s=!!n,p=V(t),l=F(t).toString(),{theme:_,...u}=t;T.postMessageToWindow(e,S.UPDATE_PARAMS,{urlParams:{pathname:p,search:l},appParams:u,hasProvider:s})}function ce(e,t){return T.listenToMessageFromWindow(window,I.ACTIVATE,()=>{T.postMessageToWindow(e,S.UPDATE_APP_DATA,{metaData:t?{appCode:t}:void 0})})}function ue(){return T.listenToMessageFromWindow(window,I.INTERCEPT_WINDOW_OPEN,({href:e,rel:t,target:n})=>{const s=e.toString();if(!s.startsWith("http")&&s.match(/^[a-zA-Z0-9]+:\/\//)){window.open(s,n,t);return}})}function de(e,t=U,n){return[T.listenToMessageFromWindow(window,I.UPDATE_HEIGHT,s=>{const p=s.height?s.height+ie:void 0;e.style.height=p?`${n?Math.min(p,n):p}px`:t}),T.listenToMessageFromWindow(window,I.SET_FULL_HEIGHT,({isUpToSmall:s})=>{e.style.height=s?t:`${n||document.body.offsetHeight}px`})]}const fe=Object.values(A);function pe(e,t,n){if(O(e)){const s=e[n];return C(s)?s[t]:s}if(C(e)){const s=e[t];return O(s)?s[n]:s}return e}function O(e){return typeof e!="object"?!1:Object.keys(e).every(t=>fe.includes(t))}const he=/^\d+$/;function C(e){return typeof e!="object"?!1:Object.keys(e).every(t=>he.test(t))}function le(e){return O(e)?Object.values(e).flatMap(t=>C(t)?Object.values(t):[t]):C(e)?Object.values(e).flatMap(t=>O(t)?Object.values(t):[t]):[e]}return w.TradeType=A,w.WIDGET_PALETTE_COLORS=Y,w.WidgetMethodsEmit=I,w.WidgetMethodsListen=S,w.createCowSwapWidget=oe,w.isCowSwapWidgetPalette=M,w.isPerNetworkConfig=C,w.isPerTradeTypeConfig=O,w.resolveFlexibleConfig=pe,w.resolveFlexibleConfigValues=le,w.widgetIframeTransport=T,Object.defineProperty(w,Symbol.toStringTag,{value:"Module"}),w}({});
|
|
1
|
+
var cowSwapWidget=function(w){"use strict";class D{key;constructor(t){this.key=t}postMessageToWindow(t,n,r){const p=typeof r=="object"?r:{},l={key:this.key,method:n,...p};t.postMessage(l,"*")}listenToMessageFromWindow(t,n,r){const p=l=>{!x(l.data)||l.data.key!==this.key||l.data.method!==n||r(l.data)};return t.addEventListener("message",p),p}stopListeningToMessageFromWindow(t,n,r){t.removeEventListener("message",r)}stopListeningWindowListener(t,n){t.removeEventListener("message",n)}}function x(e){return typeof e=="object"&&e!==null&&"key"in e&&"method"in e&&typeof e.key=="string"&&typeof e.method=="string"}var m=function(e){return e.PROVIDER_RPC_REQUEST="PROVIDER_RPC_REQUEST",e.PROVIDER_RPC_RESPONSE="PROVIDER_RPC_RESPONSE",e.PROVIDER_ON_EVENT="PROVIDER_ON_EVENT",e.SEND_PROVIDER_META_INFO="SEND_PROVIDER_META_INFO",e.REQUEST_PROVIDER_META_INFO="REQUEST_PROVIDER_META_INFO",e}({});const R=new D("cowSwapIframeRpcProviderTransport");function Q(e){if(e.isWalletConnect)return e?.session?.peer?.metadata}function j(e,t){return t.find(n=>n.provider===e)?.info}const G=["connect","disconnect","close","chainChanged","accountsChanged"],L=[];typeof window<"u"&&window.addEventListener("eip6963:announceProvider",e=>{const t=e;L.push(t.detail)});class q{iframeWidow;ethereumProvider=null;requestWaitingForConnection={};constructor(t){this.iframeWidow=t}disconnect(){typeof window>"u"||(this.ethereumProvider=null,R.stopListeningToMessageFromWindow(window,m.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),R.stopListeningToMessageFromWindow(window,m.REQUEST_PROVIDER_META_INFO,this.processProviderMetaInfoRequest))}onConnect(t){typeof window>"u"||(this.ethereumProvider?this.disconnect():R.listenToMessageFromWindow(window,m.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),this.ethereumProvider=t,this.processPendingRequests(),G.forEach(n=>{t.on(n,r=>this.onProviderEvent(n,r))}),R.listenToMessageFromWindow(window,m.REQUEST_PROVIDER_META_INFO,this.processProviderMetaInfoRequest))}processPendingRequests(){Object.keys(this.requestWaitingForConnection).forEach(t=>{this.processRpcRequest(this.requestWaitingForConnection[t])}),this.requestWaitingForConnection={}}processRpcRequest(t){const{id:n,jsonrpc:r,method:p}=t;if(!this.ethereumProvider||!n)return;(p==="enable"?this.ethereumProvider.request({method:"eth_requestAccounts",id:n}):this.ethereumProvider.request({...t,id:n})).then(_=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:r,id:n,result:_}})).catch(_=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:r,id:n,error:_}}))}processRpcCallFromWindow=({rpcRequest:t})=>{if(!this.ethereumProvider){t.id&&(this.requestWaitingForConnection[t.id]=t);return}this.processRpcRequest(t)};processProviderMetaInfoRequest=()=>{if(!this.ethereumProvider)return;const t=j(this.ethereumProvider,L),n=Q(this.ethereumProvider);R.postMessageToWindow(this.iframeWidow,m.SEND_PROVIDER_META_INFO,{providerEip6963Info:t,providerWcMetadata:n})};onProviderEvent(t,n){R.postMessageToWindow(this.iframeWidow,m.PROVIDER_ON_EVENT,{event:t,params:n})}forwardRpcResponseToIframe(t){R.postMessageToWindow(this.iframeWidow,m.PROVIDER_RPC_RESPONSE,t)}}var W={exports:{}},b;function $(){return b||(b=1,function(e){var t=Object.prototype.hasOwnProperty,n="~";function r(){}Object.create&&(r.prototype=Object.create(null),new r().__proto__||(n=!1));function p(c,i,s){this.fn=c,this.context=i,this.once=s||!1}function l(c,i,s,o,E){if(typeof s!="function")throw new TypeError("The listener must be a function");var u=new p(s,o||c,E),f=n?n+i:i;return c._events[f]?c._events[f].fn?c._events[f]=[c._events[f],u]:c._events[f].push(u):(c._events[f]=u,c._eventsCount++),c}function _(c,i){--c._eventsCount===0?c._events=new r:delete c._events[i]}function d(){this._events=new r,this._eventsCount=0}d.prototype.eventNames=function(){var i=[],s,o;if(this._eventsCount===0)return i;for(o in s=this._events)t.call(s,o)&&i.push(n?o.slice(1):o);return Object.getOwnPropertySymbols?i.concat(Object.getOwnPropertySymbols(s)):i},d.prototype.listeners=function(i){var s=n?n+i:i,o=this._events[s];if(!o)return[];if(o.fn)return[o.fn];for(var E=0,u=o.length,f=new Array(u);E<u;E++)f[E]=o[E].fn;return f},d.prototype.listenerCount=function(i){var s=n?n+i:i,o=this._events[s];return o?o.fn?1:o.length:0},d.prototype.emit=function(i,s,o,E,u,f){var v=n?n+i:i;if(!this._events[v])return!1;var a=this._events[v],P=arguments.length,g,h;if(a.fn){switch(a.once&&this.removeListener(i,a.fn,void 0,!0),P){case 1:return a.fn.call(a.context),!0;case 2:return a.fn.call(a.context,s),!0;case 3:return a.fn.call(a.context,s,o),!0;case 4:return a.fn.call(a.context,s,o,E),!0;case 5:return a.fn.call(a.context,s,o,E,u),!0;case 6:return a.fn.call(a.context,s,o,E,u,f),!0}for(h=1,g=new Array(P-1);h<P;h++)g[h-1]=arguments[h];a.fn.apply(a.context,g)}else{var we=a.length,C;for(h=0;h<we;h++)switch(a[h].once&&this.removeListener(i,a[h].fn,void 0,!0),P){case 1:a[h].fn.call(a[h].context);break;case 2:a[h].fn.call(a[h].context,s);break;case 3:a[h].fn.call(a[h].context,s,o);break;case 4:a[h].fn.call(a[h].context,s,o,E);break;default:if(!g)for(C=1,g=new Array(P-1);C<P;C++)g[C-1]=arguments[C];a[h].fn.apply(a[h].context,g)}}return!0},d.prototype.on=function(i,s,o){return l(this,i,s,o,!1)},d.prototype.once=function(i,s,o){return l(this,i,s,o,!0)},d.prototype.removeListener=function(i,s,o,E){var u=n?n+i:i;if(!this._events[u])return this;if(!s)return _(this,u),this;var f=this._events[u];if(f.fn)f.fn===s&&(!E||f.once)&&(!o||f.context===o)&&_(this,u);else{for(var v=0,a=[],P=f.length;v<P;v++)(f[v].fn!==s||E&&!f[v].once||o&&f[v].context!==o)&&a.push(f[v]);a.length?this._events[u]=a.length===1?a[0]:a:_(this,u)}return this},d.prototype.removeAllListeners=function(i){var s;return i?(s=n?n+i:i,this._events[s]&&_(this,s)):(this._events=new r,this._eventsCount=0),this},d.prototype.off=d.prototype.removeListener,d.prototype.addListener=d.prototype.on,d.prefixed=n,d.EventEmitter=d,e.exports=d}(W)),W.exports}$();class B{subscriptions={};on(t){const{event:n,handler:r}=t;return this.subscriptions[n]||(this.subscriptions[n]=[]),this.subscriptions[n].push(r),t}off(t){const{event:n,handler:r}=t;return this.subscriptions[n]&&(this.subscriptions[n]=this.subscriptions[n].filter(p=>p!==r)),t}emit(t,n){this.subscriptions[t]&&this.subscriptions[t].forEach(r=>r(n))}}var I=function(e){return 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.INTERCEPT_WINDOW_OPEN="INTERCEPT_WINDOW_OPEN",e}({}),S=function(e){return 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}({}),A=function(e){return e.SWAP="swap",e.LIMIT="limit",e.ADVANCED="advanced",e.YIELD="yield",e}({});const Y=["primary","background","paper","text","danger","warning","alert","info","success"],T=new D("cowSwapWidget");class z{contentWindow;eventEmitter=new B;listeners=[];widgetListener;constructor(t,n=[]){this.contentWindow=t,this.updateListeners(n),this.widgetListener=T.listenToMessageFromWindow(this.contentWindow,I.EMIT_COW_EVENT,r=>this.eventEmitter.emit(r.event,r.payload))}stopListeningIframe(){T.stopListeningWindowListener(this.contentWindow,this.widgetListener)}updateListeners(t){for(const n of this.listeners)this.eventEmitter.off(n);this.listeners=t||[];for(const n of this.listeners)this.eventEmitter.on(n)}}class J{appWindow;iframeWidow;forwardSdkMessage;constructor(t,n){this.appWindow=t,this.iframeWidow=n,this.forwardSdkMessage=r=>{K(r.data)&&(typeof window<"u"&&r.origin===window.location.origin||(X(r.data)?this.appWindow.parent.postMessage(r.data,"*"):Z(r.data)&&this.iframeWidow.postMessage(r.data,"*")))},this.startListening()}startListening(){this.appWindow.addEventListener("message",this.forwardSdkMessage)}stopListening(){this.appWindow.removeEventListener("message",this.forwardSdkMessage)}}function K(e){return typeof e=="object"&&e!==null&&"id"in e&&typeof e.id=="string"}function X(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 Z(e){return"success"in e&&typeof e.success=="boolean"&&"version"in e&&typeof e.version=="string"}function M(e){return!!(e&&typeof e=="object")}const N="_";function ee(e){const t=typeof e.baseUrl=="string"?e.baseUrl:"https://swap.cow.fi",n=V(e);return t+"/#"+n+"?"+F(e)}function V(e){const{chainId:t=1,sell:n,buy:r,tradeType:p=A.SWAP}=e,l=[n?.asset||N,r?.asset||N].map(encodeURIComponent).join("/");return`/${t}/widget/${p}/${l}`}function F(e){const t=new URLSearchParams;return re(ne(te(t,e),e),e)}function te(e,t){const{sell:n,buy:r}=t;return n?.amount&&e.append("sellAmount",n.amount),r?.amount&&e.append("buyAmount",r.amount),e}function ne(e,t){const n=t.theme;return n?(M(n)?(e.append("palette",encodeURIComponent(JSON.stringify(n))),e.append("theme",n.baseTheme)):(e.append("palette","null"),e.append("theme",n)),e):(e.append("palette","null"),e)}function re(e,t){return t.targetChainId&&e.append("targetChainId",t.targetChainId.toString()),e}const U="640px",ie="450px",se=20,oe={updateParams:()=>{},updateListeners:()=>{},updateProvider:()=>{},destroy:()=>{}};function ae(e,t){const{params:n,provider:r,listeners:p}=t;let l=r,_=n;if(typeof window>"u")return oe;const d=ce(n);e.innerHTML="",e.appendChild(d);const{contentWindow:c}=d;if(!c)throw console.error("Iframe does not contain a window",d),new Error("Iframe does not contain a window!");const i=[];i.push(de(c,n.appCode)),i.push(...fe(d,n.height,n.maxHeight)),i.push(ue());const s=new z(window,p);let o=k(c,null,l);d.addEventListener("load",()=>H(c,_,l));const E=new J(window,c);return{updateParams:u=>{_=u,H(c,_,l)},updateListeners:u=>s.updateListeners(u),updateProvider:u=>{l=u,o=k(c,o,u)},destroy:()=>{o.disconnect(),s.stopListeningIframe(),i.forEach(u=>window.removeEventListener("message",u)),E.stopListening(),e.removeChild(d)}}}function k(e,t,n){t&&t.disconnect();const r=t||new q(e);return n&&r.onConnect(n),r}function ce(e){const{width:t=ie,height:n=U}=e,r=document.createElement("iframe");return r.src=ee(e),r.width=t,r.height=n,r.style.border="0",r.allow="clipboard-read; clipboard-write",r}function H(e,t,n){const r=!!n,p=V(t),l=F(t).toString(),{theme:_,...d}=t;T.postMessageToWindow(e,S.UPDATE_PARAMS,{urlParams:{pathname:p,search:l},appParams:d,hasProvider:r})}function de(e,t){return T.listenToMessageFromWindow(window,I.ACTIVATE,()=>{T.postMessageToWindow(e,S.UPDATE_APP_DATA,{metaData:t?{appCode:t}:void 0})})}function ue(){return T.listenToMessageFromWindow(window,I.INTERCEPT_WINDOW_OPEN,({href:e,rel:t,target:n})=>{const r=e.toString();if(!r.startsWith("http")&&r.match(/^[a-zA-Z0-9]+:\/\//)){window.open(r,n,t);return}})}function fe(e,t=U,n){return[T.listenToMessageFromWindow(window,I.UPDATE_HEIGHT,r=>{const p=r.height?r.height+se:void 0;e.style.height=p?`${n?Math.min(p,n):p}px`:t}),T.listenToMessageFromWindow(window,I.SET_FULL_HEIGHT,({isUpToSmall:r})=>{e.style.height=r?t:`${n||document.body.offsetHeight}px`})]}const pe=Object.values(A);function he(e,t,n){if(O(e)){const r=e[n];return y(r)?r[t]:r}if(y(e)){const r=e[t];return O(r)?r[n]:r}return e}function O(e){return typeof e!="object"?!1:Object.keys(e).every(t=>pe.includes(t))}const le=/^\d+$/;function y(e){return typeof e!="object"?!1:Object.keys(e).every(t=>le.test(t))}function Ee(e){return O(e)?Object.values(e).flatMap(t=>y(t)?Object.values(t):[t]):y(e)?Object.values(e).flatMap(t=>O(t)?Object.values(t):[t]):[e]}return w.TradeType=A,w.WIDGET_PALETTE_COLORS=Y,w.WidgetMethodsEmit=I,w.WidgetMethodsListen=S,w.createCowSwapWidget=ae,w.isCowSwapWidgetPalette=M,w.isPerNetworkConfig=y,w.isPerTradeTypeConfig=O,w.resolveFlexibleConfig=he,w.resolveFlexibleConfigValues=Ee,w.widgetIframeTransport=T,Object.defineProperty(w,Symbol.toStringTag,{value:"Module"}),w}({});
|
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class M{key;constructor(t){this.key=t}postMessageToWindow(t,n,s){const p=typeof s=="object"?s:{},l={key:this.key,method:n,...p};t.postMessage(l,"*")}listenToMessageFromWindow(t,n,s){const p=l=>{!H(l.data)||l.data.key!==this.key||l.data.method!==n||s(l.data)};return t.addEventListener("message",p),p}stopListeningToMessageFromWindow(t,n,s){t.removeEventListener("message",s)}stopListeningWindowListener(t,n){t.removeEventListener("message",n)}}function H(e){return typeof e=="object"&&e!==null&&"key"in e&&"method"in e&&typeof e.key=="string"&&typeof e.method=="string"}var m=function(e){return e.PROVIDER_RPC_REQUEST="PROVIDER_RPC_REQUEST",e.PROVIDER_RPC_RESPONSE="PROVIDER_RPC_RESPONSE",e.PROVIDER_ON_EVENT="PROVIDER_ON_EVENT",e.SEND_PROVIDER_META_INFO="SEND_PROVIDER_META_INFO",e.REQUEST_PROVIDER_META_INFO="REQUEST_PROVIDER_META_INFO",e}({});const R=new M("cowSwapIframeRpcProviderTransport");function Q(e){if(e.isWalletConnect)return e?.session?.peer?.metadata}function j(e,t){return t.find(n=>n.provider===e)?.info}const G=["connect","disconnect","close","chainChanged","accountsChanged"],N=[];window.addEventListener("eip6963:announceProvider",e=>{const t=e;N.push(t.detail)});class q{iframeWidow;ethereumProvider=null;requestWaitingForConnection={};constructor(t){this.iframeWidow=t}disconnect(){this.ethereumProvider=null,R.stopListeningToMessageFromWindow(window,m.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),R.stopListeningToMessageFromWindow(window,m.REQUEST_PROVIDER_META_INFO,this.processProviderMetaInfoRequest)}onConnect(t){this.ethereumProvider?this.disconnect():R.listenToMessageFromWindow(window,m.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),this.ethereumProvider=t,this.processPendingRequests(),G.forEach(n=>{t.on(n,s=>this.onProviderEvent(n,s))}),R.listenToMessageFromWindow(window,m.REQUEST_PROVIDER_META_INFO,this.processProviderMetaInfoRequest)}processPendingRequests(){Object.keys(this.requestWaitingForConnection).forEach(t=>{this.processRpcRequest(this.requestWaitingForConnection[t])}),this.requestWaitingForConnection={}}processRpcRequest(t){const{id:n,jsonrpc:s,method:p}=t;if(!this.ethereumProvider||!n)return;(p==="enable"?this.ethereumProvider.request({method:"eth_requestAccounts",id:n}):this.ethereumProvider.request({...t,id:n})).then(w=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:s,id:n,result:w}})).catch(w=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:s,id:n,error:w}}))}processRpcCallFromWindow=({rpcRequest:t})=>{if(!this.ethereumProvider){t.id&&(this.requestWaitingForConnection[t.id]=t);return}this.processRpcRequest(t)};processProviderMetaInfoRequest=()=>{if(!this.ethereumProvider)return;const t=j(this.ethereumProvider,N),n=Q(this.ethereumProvider);R.postMessageToWindow(this.iframeWidow,m.SEND_PROVIDER_META_INFO,{providerEip6963Info:t,providerWcMetadata:n})};onProviderEvent(t,n){R.postMessageToWindow(this.iframeWidow,m.PROVIDER_ON_EVENT,{event:t,params:n})}forwardRpcResponseToIframe(t){R.postMessageToWindow(this.iframeWidow,m.PROVIDER_RPC_RESPONSE,t)}}var W={exports:{}},A;function $(){return A||(A=1,function(e){var t=Object.prototype.hasOwnProperty,n="~";function s(){}Object.create&&(s.prototype=Object.create(null),new s().__proto__||(n=!1));function p(c,r,i){this.fn=c,this.context=r,this.once=i||!1}function l(c,r,i,o,E){if(typeof i!="function")throw new TypeError("The listener must be a function");var d=new p(i,o||c,E),f=n?n+r:r;return c._events[f]?c._events[f].fn?c._events[f]=[c._events[f],d]:c._events[f].push(d):(c._events[f]=d,c._eventsCount++),c}function w(c,r){--c._eventsCount===0?c._events=new s:delete c._events[r]}function u(){this._events=new s,this._eventsCount=0}u.prototype.eventNames=function(){var r=[],i,o;if(this._eventsCount===0)return r;for(o in i=this._events)t.call(i,o)&&r.push(n?o.slice(1):o);return Object.getOwnPropertySymbols?r.concat(Object.getOwnPropertySymbols(i)):r},u.prototype.listeners=function(r){var i=n?n+r:r,o=this._events[i];if(!o)return[];if(o.fn)return[o.fn];for(var E=0,d=o.length,f=new Array(d);E<d;E++)f[E]=o[E].fn;return f},u.prototype.listenerCount=function(r){var i=n?n+r:r,o=this._events[i];return o?o.fn?1:o.length:0},u.prototype.emit=function(r,i,o,E,d,f){var _=n?n+r:r;if(!this._events[_])return!1;var a=this._events[_],v=arguments.length,P,h;if(a.fn){switch(a.once&&this.removeListener(r,a.fn,void 0,!0),v){case 1:return a.fn.call(a.context),!0;case 2:return a.fn.call(a.context,i),!0;case 3:return a.fn.call(a.context,i,o),!0;case 4:return a.fn.call(a.context,i,o,E),!0;case 5:return a.fn.call(a.context,i,o,E,d),!0;case 6:return a.fn.call(a.context,i,o,E,d,f),!0}for(h=1,P=new Array(v-1);h<v;h++)P[h-1]=arguments[h];a.fn.apply(a.context,P)}else{var k=a.length,I;for(h=0;h<k;h++)switch(a[h].once&&this.removeListener(r,a[h].fn,void 0,!0),v){case 1:a[h].fn.call(a[h].context);break;case 2:a[h].fn.call(a[h].context,i);break;case 3:a[h].fn.call(a[h].context,i,o);break;case 4:a[h].fn.call(a[h].context,i,o,E);break;default:if(!P)for(I=1,P=new Array(v-1);I<v;I++)P[I-1]=arguments[I];a[h].fn.apply(a[h].context,P)}}return!0},u.prototype.on=function(r,i,o){return l(this,r,i,o,!1)},u.prototype.once=function(r,i,o){return l(this,r,i,o,!0)},u.prototype.removeListener=function(r,i,o,E){var d=n?n+r:r;if(!this._events[d])return this;if(!i)return w(this,d),this;var f=this._events[d];if(f.fn)f.fn===i&&(!E||f.once)&&(!o||f.context===o)&&w(this,d);else{for(var _=0,a=[],v=f.length;_<v;_++)(f[_].fn!==i||E&&!f[_].once||o&&f[_].context!==o)&&a.push(f[_]);a.length?this._events[d]=a.length===1?a[0]:a:w(this,d)}return this},u.prototype.removeAllListeners=function(r){var i;return r?(i=n?n+r:r,this._events[i]&&w(this,i)):(this._events=new s,this._eventsCount=0),this},u.prototype.off=u.prototype.removeListener,u.prototype.addListener=u.prototype.on,u.prefixed=n,u.EventEmitter=u,e.exports=u}(W)),W.exports}$();class B{subscriptions={};on(t){const{event:n,handler:s}=t;return this.subscriptions[n]||(this.subscriptions[n]=[]),this.subscriptions[n].push(s),t}off(t){const{event:n,handler:s}=t;return this.subscriptions[n]&&(this.subscriptions[n]=this.subscriptions[n].filter(p=>p!==s)),t}emit(t,n){this.subscriptions[t]&&this.subscriptions[t].forEach(s=>s(n))}}var g=function(e){return 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.INTERCEPT_WINDOW_OPEN="INTERCEPT_WINDOW_OPEN",e}({}),y=function(e){return 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}({}),S=function(e){return e.SWAP="swap",e.LIMIT="limit",e.ADVANCED="advanced",e.YIELD="yield",e}({});const Y=["primary","background","paper","text","danger","warning","alert","info","success"],T=new M("cowSwapWidget");class z{contentWindow;eventEmitter=new B;listeners=[];widgetListener;constructor(t,n=[]){this.contentWindow=t,this.updateListeners(n),this.widgetListener=T.listenToMessageFromWindow(this.contentWindow,g.EMIT_COW_EVENT,s=>this.eventEmitter.emit(s.event,s.payload))}stopListeningIframe(){T.stopListeningWindowListener(this.contentWindow,this.widgetListener)}updateListeners(t){for(const n of this.listeners)this.eventEmitter.off(n);this.listeners=t||[];for(const n of this.listeners)this.eventEmitter.on(n)}}class J{appWindow;iframeWidow;forwardSdkMessage;constructor(t,n){this.appWindow=t,this.iframeWidow=n,this.forwardSdkMessage=s=>{K(s.data)&&s.origin!==window.location.origin&&(X(s.data)?this.appWindow.parent.postMessage(s.data,"*"):Z(s.data)&&this.iframeWidow.postMessage(s.data,"*"))},this.startListening()}startListening(){this.appWindow.addEventListener("message",this.forwardSdkMessage)}stopListening(){this.appWindow.removeEventListener("message",this.forwardSdkMessage)}}function K(e){return typeof e=="object"&&e!==null&&"id"in e&&typeof e.id=="string"}function X(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 Z(e){return"success"in e&&typeof e.success=="boolean"&&"version"in e&&typeof e.version=="string"}function V(e){return!!(e&&typeof e=="object")}const D="_";function ee(e){const t=typeof e.baseUrl=="string"?e.baseUrl:"https://swap.cow.fi",n=F(e);return t+"/#"+n+"?"+U(e)}function F(e){const{chainId:t=1,sell:n,buy:s,tradeType:p=S.SWAP}=e,l=[n?.asset||D,s?.asset||D].map(encodeURIComponent).join("/");return`/${t}/widget/${p}/${l}`}function U(e){const t=new URLSearchParams;return se(ne(te(t,e),e),e)}function te(e,t){const{sell:n,buy:s}=t;return n?.amount&&e.append("sellAmount",n.amount),s?.amount&&e.append("buyAmount",s.amount),e}function ne(e,t){const n=t.theme;return n?(V(n)?(e.append("palette",encodeURIComponent(JSON.stringify(n))),e.append("theme",n.baseTheme)):(e.append("palette","null"),e.append("theme",n)),e):(e.append("palette","null"),e)}function se(e,t){return t.targetChainId&&e.append("targetChainId",t.targetChainId.toString()),e}const x="640px",re="450px",ie=20;function oe(e,t){const{params:n,provider:s,listeners:p}=t;let l=s,w=n;const u=ae(n);e.innerHTML="",e.appendChild(u);const{contentWindow:c}=u;if(!c)throw console.error("Iframe does not contain a window",u),new Error("Iframe does not contain a window!");const r=[];r.push(ce(c,n.appCode)),r.push(...de(u,n.height,n.maxHeight)),r.push(ue());const i=new z(window,p);let o=L(c,null,l);u.addEventListener("load",()=>b(c,w,l));const E=new J(window,c);return{updateParams:d=>{w=d,b(c,w,l)},updateListeners:d=>i.updateListeners(d),updateProvider:d=>{l=d,o=L(c,o,d)},destroy:()=>{o.disconnect(),i.stopListeningIframe(),r.forEach(d=>window.removeEventListener("message",d)),E.stopListening(),e.removeChild(u)}}}function L(e,t,n){t&&t.disconnect();const s=t||new q(e);return n&&s.onConnect(n),s}function ae(e){const{width:t=re,height:n=x}=e,s=document.createElement("iframe");return s.src=ee(e),s.width=t,s.height=n,s.style.border="0",s.allow="clipboard-read; clipboard-write",s}function b(e,t,n){const s=!!n,p=F(t),l=U(t).toString(),{theme:w,...u}=t;T.postMessageToWindow(e,y.UPDATE_PARAMS,{urlParams:{pathname:p,search:l},appParams:u,hasProvider:s})}function ce(e,t){return T.listenToMessageFromWindow(window,g.ACTIVATE,()=>{T.postMessageToWindow(e,y.UPDATE_APP_DATA,{metaData:t?{appCode:t}:void 0})})}function ue(){return T.listenToMessageFromWindow(window,g.INTERCEPT_WINDOW_OPEN,({href:e,rel:t,target:n})=>{const s=e.toString();if(!s.startsWith("http")&&s.match(/^[a-zA-Z0-9]+:\/\//)){window.open(s,n,t);return}})}function de(e,t=x,n){return[T.listenToMessageFromWindow(window,g.UPDATE_HEIGHT,s=>{const p=s.height?s.height+ie:void 0;e.style.height=p?`${n?Math.min(p,n):p}px`:t}),T.listenToMessageFromWindow(window,g.SET_FULL_HEIGHT,({isUpToSmall:s})=>{e.style.height=s?t:`${n||document.body.offsetHeight}px`})]}const fe=Object.values(S);function pe(e,t,n){if(O(e)){const s=e[n];return C(s)?s[t]:s}if(C(e)){const s=e[t];return O(s)?s[n]:s}return e}function O(e){return typeof e!="object"?!1:Object.keys(e).every(t=>fe.includes(t))}const he=/^\d+$/;function C(e){return typeof e!="object"?!1:Object.keys(e).every(t=>he.test(t))}function le(e){return O(e)?Object.values(e).flatMap(t=>C(t)?Object.values(t):[t]):C(e)?Object.values(e).flatMap(t=>O(t)?Object.values(t):[t]):[e]}exports.TradeType=S;exports.WIDGET_PALETTE_COLORS=Y;exports.WidgetMethodsEmit=g;exports.WidgetMethodsListen=y;exports.createCowSwapWidget=oe;exports.isCowSwapWidgetPalette=V;exports.isPerNetworkConfig=C;exports.isPerTradeTypeConfig=O;exports.resolveFlexibleConfig=pe;exports.resolveFlexibleConfigValues=le;exports.widgetIframeTransport=T;
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class M{key;constructor(t){this.key=t}postMessageToWindow(t,n,s){const p=typeof s=="object"?s:{},l={key:this.key,method:n,...p};t.postMessage(l,"*")}listenToMessageFromWindow(t,n,s){const p=l=>{!H(l.data)||l.data.key!==this.key||l.data.method!==n||s(l.data)};return t.addEventListener("message",p),p}stopListeningToMessageFromWindow(t,n,s){t.removeEventListener("message",s)}stopListeningWindowListener(t,n){t.removeEventListener("message",n)}}function H(e){return typeof e=="object"&&e!==null&&"key"in e&&"method"in e&&typeof e.key=="string"&&typeof e.method=="string"}var m=function(e){return e.PROVIDER_RPC_REQUEST="PROVIDER_RPC_REQUEST",e.PROVIDER_RPC_RESPONSE="PROVIDER_RPC_RESPONSE",e.PROVIDER_ON_EVENT="PROVIDER_ON_EVENT",e.SEND_PROVIDER_META_INFO="SEND_PROVIDER_META_INFO",e.REQUEST_PROVIDER_META_INFO="REQUEST_PROVIDER_META_INFO",e}({});const R=new M("cowSwapIframeRpcProviderTransport");function Q(e){if(e.isWalletConnect)return e?.session?.peer?.metadata}function j(e,t){return t.find(n=>n.provider===e)?.info}const G=["connect","disconnect","close","chainChanged","accountsChanged"],N=[];typeof window<"u"&&window.addEventListener("eip6963:announceProvider",e=>{const t=e;N.push(t.detail)});class q{iframeWidow;ethereumProvider=null;requestWaitingForConnection={};constructor(t){this.iframeWidow=t}disconnect(){typeof window>"u"||(this.ethereumProvider=null,R.stopListeningToMessageFromWindow(window,m.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),R.stopListeningToMessageFromWindow(window,m.REQUEST_PROVIDER_META_INFO,this.processProviderMetaInfoRequest))}onConnect(t){typeof window>"u"||(this.ethereumProvider?this.disconnect():R.listenToMessageFromWindow(window,m.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),this.ethereumProvider=t,this.processPendingRequests(),G.forEach(n=>{t.on(n,s=>this.onProviderEvent(n,s))}),R.listenToMessageFromWindow(window,m.REQUEST_PROVIDER_META_INFO,this.processProviderMetaInfoRequest))}processPendingRequests(){Object.keys(this.requestWaitingForConnection).forEach(t=>{this.processRpcRequest(this.requestWaitingForConnection[t])}),this.requestWaitingForConnection={}}processRpcRequest(t){const{id:n,jsonrpc:s,method:p}=t;if(!this.ethereumProvider||!n)return;(p==="enable"?this.ethereumProvider.request({method:"eth_requestAccounts",id:n}):this.ethereumProvider.request({...t,id:n})).then(w=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:s,id:n,result:w}})).catch(w=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:s,id:n,error:w}}))}processRpcCallFromWindow=({rpcRequest:t})=>{if(!this.ethereumProvider){t.id&&(this.requestWaitingForConnection[t.id]=t);return}this.processRpcRequest(t)};processProviderMetaInfoRequest=()=>{if(!this.ethereumProvider)return;const t=j(this.ethereumProvider,N),n=Q(this.ethereumProvider);R.postMessageToWindow(this.iframeWidow,m.SEND_PROVIDER_META_INFO,{providerEip6963Info:t,providerWcMetadata:n})};onProviderEvent(t,n){R.postMessageToWindow(this.iframeWidow,m.PROVIDER_ON_EVENT,{event:t,params:n})}forwardRpcResponseToIframe(t){R.postMessageToWindow(this.iframeWidow,m.PROVIDER_RPC_RESPONSE,t)}}var C={exports:{}},A;function $(){return A||(A=1,function(e){var t=Object.prototype.hasOwnProperty,n="~";function s(){}Object.create&&(s.prototype=Object.create(null),new s().__proto__||(n=!1));function p(c,r,i){this.fn=c,this.context=r,this.once=i||!1}function l(c,r,i,o,E){if(typeof i!="function")throw new TypeError("The listener must be a function");var u=new p(i,o||c,E),f=n?n+r:r;return c._events[f]?c._events[f].fn?c._events[f]=[c._events[f],u]:c._events[f].push(u):(c._events[f]=u,c._eventsCount++),c}function w(c,r){--c._eventsCount===0?c._events=new s:delete c._events[r]}function d(){this._events=new s,this._eventsCount=0}d.prototype.eventNames=function(){var r=[],i,o;if(this._eventsCount===0)return r;for(o in i=this._events)t.call(i,o)&&r.push(n?o.slice(1):o);return Object.getOwnPropertySymbols?r.concat(Object.getOwnPropertySymbols(i)):r},d.prototype.listeners=function(r){var i=n?n+r:r,o=this._events[i];if(!o)return[];if(o.fn)return[o.fn];for(var E=0,u=o.length,f=new Array(u);E<u;E++)f[E]=o[E].fn;return f},d.prototype.listenerCount=function(r){var i=n?n+r:r,o=this._events[i];return o?o.fn?1:o.length:0},d.prototype.emit=function(r,i,o,E,u,f){var _=n?n+r:r;if(!this._events[_])return!1;var a=this._events[_],v=arguments.length,P,h;if(a.fn){switch(a.once&&this.removeListener(r,a.fn,void 0,!0),v){case 1:return a.fn.call(a.context),!0;case 2:return a.fn.call(a.context,i),!0;case 3:return a.fn.call(a.context,i,o),!0;case 4:return a.fn.call(a.context,i,o,E),!0;case 5:return a.fn.call(a.context,i,o,E,u),!0;case 6:return a.fn.call(a.context,i,o,E,u,f),!0}for(h=1,P=new Array(v-1);h<v;h++)P[h-1]=arguments[h];a.fn.apply(a.context,P)}else{var k=a.length,I;for(h=0;h<k;h++)switch(a[h].once&&this.removeListener(r,a[h].fn,void 0,!0),v){case 1:a[h].fn.call(a[h].context);break;case 2:a[h].fn.call(a[h].context,i);break;case 3:a[h].fn.call(a[h].context,i,o);break;case 4:a[h].fn.call(a[h].context,i,o,E);break;default:if(!P)for(I=1,P=new Array(v-1);I<v;I++)P[I-1]=arguments[I];a[h].fn.apply(a[h].context,P)}}return!0},d.prototype.on=function(r,i,o){return l(this,r,i,o,!1)},d.prototype.once=function(r,i,o){return l(this,r,i,o,!0)},d.prototype.removeListener=function(r,i,o,E){var u=n?n+r:r;if(!this._events[u])return this;if(!i)return w(this,u),this;var f=this._events[u];if(f.fn)f.fn===i&&(!E||f.once)&&(!o||f.context===o)&&w(this,u);else{for(var _=0,a=[],v=f.length;_<v;_++)(f[_].fn!==i||E&&!f[_].once||o&&f[_].context!==o)&&a.push(f[_]);a.length?this._events[u]=a.length===1?a[0]:a:w(this,u)}return this},d.prototype.removeAllListeners=function(r){var i;return r?(i=n?n+r:r,this._events[i]&&w(this,i)):(this._events=new s,this._eventsCount=0),this},d.prototype.off=d.prototype.removeListener,d.prototype.addListener=d.prototype.on,d.prefixed=n,d.EventEmitter=d,e.exports=d}(C)),C.exports}$();class B{subscriptions={};on(t){const{event:n,handler:s}=t;return this.subscriptions[n]||(this.subscriptions[n]=[]),this.subscriptions[n].push(s),t}off(t){const{event:n,handler:s}=t;return this.subscriptions[n]&&(this.subscriptions[n]=this.subscriptions[n].filter(p=>p!==s)),t}emit(t,n){this.subscriptions[t]&&this.subscriptions[t].forEach(s=>s(n))}}var g=function(e){return 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.INTERCEPT_WINDOW_OPEN="INTERCEPT_WINDOW_OPEN",e}({}),W=function(e){return 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}({}),S=function(e){return e.SWAP="swap",e.LIMIT="limit",e.ADVANCED="advanced",e.YIELD="yield",e}({});const Y=["primary","background","paper","text","danger","warning","alert","info","success"],T=new M("cowSwapWidget");class z{contentWindow;eventEmitter=new B;listeners=[];widgetListener;constructor(t,n=[]){this.contentWindow=t,this.updateListeners(n),this.widgetListener=T.listenToMessageFromWindow(this.contentWindow,g.EMIT_COW_EVENT,s=>this.eventEmitter.emit(s.event,s.payload))}stopListeningIframe(){T.stopListeningWindowListener(this.contentWindow,this.widgetListener)}updateListeners(t){for(const n of this.listeners)this.eventEmitter.off(n);this.listeners=t||[];for(const n of this.listeners)this.eventEmitter.on(n)}}class J{appWindow;iframeWidow;forwardSdkMessage;constructor(t,n){this.appWindow=t,this.iframeWidow=n,this.forwardSdkMessage=s=>{K(s.data)&&(typeof window<"u"&&s.origin===window.location.origin||(X(s.data)?this.appWindow.parent.postMessage(s.data,"*"):Z(s.data)&&this.iframeWidow.postMessage(s.data,"*")))},this.startListening()}startListening(){this.appWindow.addEventListener("message",this.forwardSdkMessage)}stopListening(){this.appWindow.removeEventListener("message",this.forwardSdkMessage)}}function K(e){return typeof e=="object"&&e!==null&&"id"in e&&typeof e.id=="string"}function X(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 Z(e){return"success"in e&&typeof e.success=="boolean"&&"version"in e&&typeof e.version=="string"}function V(e){return!!(e&&typeof e=="object")}const D="_";function ee(e){const t=typeof e.baseUrl=="string"?e.baseUrl:"https://swap.cow.fi",n=F(e);return t+"/#"+n+"?"+U(e)}function F(e){const{chainId:t=1,sell:n,buy:s,tradeType:p=S.SWAP}=e,l=[n?.asset||D,s?.asset||D].map(encodeURIComponent).join("/");return`/${t}/widget/${p}/${l}`}function U(e){const t=new URLSearchParams;return se(ne(te(t,e),e),e)}function te(e,t){const{sell:n,buy:s}=t;return n?.amount&&e.append("sellAmount",n.amount),s?.amount&&e.append("buyAmount",s.amount),e}function ne(e,t){const n=t.theme;return n?(V(n)?(e.append("palette",encodeURIComponent(JSON.stringify(n))),e.append("theme",n.baseTheme)):(e.append("palette","null"),e.append("theme",n)),e):(e.append("palette","null"),e)}function se(e,t){return t.targetChainId&&e.append("targetChainId",t.targetChainId.toString()),e}const x="640px",re="450px",ie=20,oe={updateParams:()=>{},updateListeners:()=>{},updateProvider:()=>{},destroy:()=>{}};function ae(e,t){const{params:n,provider:s,listeners:p}=t;let l=s,w=n;if(typeof window>"u")return oe;const d=ce(n);e.innerHTML="",e.appendChild(d);const{contentWindow:c}=d;if(!c)throw console.error("Iframe does not contain a window",d),new Error("Iframe does not contain a window!");const r=[];r.push(de(c,n.appCode)),r.push(...fe(d,n.height,n.maxHeight)),r.push(ue());const i=new z(window,p);let o=L(c,null,l);d.addEventListener("load",()=>b(c,w,l));const E=new J(window,c);return{updateParams:u=>{w=u,b(c,w,l)},updateListeners:u=>i.updateListeners(u),updateProvider:u=>{l=u,o=L(c,o,u)},destroy:()=>{o.disconnect(),i.stopListeningIframe(),r.forEach(u=>window.removeEventListener("message",u)),E.stopListening(),e.removeChild(d)}}}function L(e,t,n){t&&t.disconnect();const s=t||new q(e);return n&&s.onConnect(n),s}function ce(e){const{width:t=re,height:n=x}=e,s=document.createElement("iframe");return s.src=ee(e),s.width=t,s.height=n,s.style.border="0",s.allow="clipboard-read; clipboard-write",s}function b(e,t,n){const s=!!n,p=F(t),l=U(t).toString(),{theme:w,...d}=t;T.postMessageToWindow(e,W.UPDATE_PARAMS,{urlParams:{pathname:p,search:l},appParams:d,hasProvider:s})}function de(e,t){return T.listenToMessageFromWindow(window,g.ACTIVATE,()=>{T.postMessageToWindow(e,W.UPDATE_APP_DATA,{metaData:t?{appCode:t}:void 0})})}function ue(){return T.listenToMessageFromWindow(window,g.INTERCEPT_WINDOW_OPEN,({href:e,rel:t,target:n})=>{const s=e.toString();if(!s.startsWith("http")&&s.match(/^[a-zA-Z0-9]+:\/\//)){window.open(s,n,t);return}})}function fe(e,t=x,n){return[T.listenToMessageFromWindow(window,g.UPDATE_HEIGHT,s=>{const p=s.height?s.height+ie:void 0;e.style.height=p?`${n?Math.min(p,n):p}px`:t}),T.listenToMessageFromWindow(window,g.SET_FULL_HEIGHT,({isUpToSmall:s})=>{e.style.height=s?t:`${n||document.body.offsetHeight}px`})]}const pe=Object.values(S);function he(e,t,n){if(O(e)){const s=e[n];return y(s)?s[t]:s}if(y(e)){const s=e[t];return O(s)?s[n]:s}return e}function O(e){return typeof e!="object"?!1:Object.keys(e).every(t=>pe.includes(t))}const le=/^\d+$/;function y(e){return typeof e!="object"?!1:Object.keys(e).every(t=>le.test(t))}function Ee(e){return O(e)?Object.values(e).flatMap(t=>y(t)?Object.values(t):[t]):y(e)?Object.values(e).flatMap(t=>O(t)?Object.values(t):[t]):[e]}exports.TradeType=S;exports.WIDGET_PALETTE_COLORS=Y;exports.WidgetMethodsEmit=g;exports.WidgetMethodsListen=W;exports.createCowSwapWidget=ae;exports.isCowSwapWidgetPalette=V;exports.isPerNetworkConfig=y;exports.isPerTradeTypeConfig=O;exports.resolveFlexibleConfig=he;exports.resolveFlexibleConfigValues=Ee;exports.widgetIframeTransport=T;
|
package/index.mjs
CHANGED
|
@@ -3,8 +3,6 @@ class L {
|
|
|
3
3
|
constructor(t) {
|
|
4
4
|
this.key = t;
|
|
5
5
|
}
|
|
6
|
-
// TODO: Add proper return type annotation
|
|
7
|
-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
8
6
|
postMessageToWindow(t, n, s) {
|
|
9
7
|
const p = typeof s == "object" ? s : {}, l = {
|
|
10
8
|
key: this.key,
|
|
@@ -19,13 +17,9 @@ class L {
|
|
|
19
17
|
};
|
|
20
18
|
return t.addEventListener("message", p), p;
|
|
21
19
|
}
|
|
22
|
-
// TODO: Add proper return type annotation
|
|
23
|
-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
24
20
|
stopListeningToMessageFromWindow(t, n, s) {
|
|
25
21
|
t.removeEventListener("message", s);
|
|
26
22
|
}
|
|
27
|
-
// TODO: Add proper return type annotation
|
|
28
|
-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
29
23
|
stopListeningWindowListener(t, n) {
|
|
30
24
|
t.removeEventListener("message", n);
|
|
31
25
|
}
|
|
@@ -33,10 +27,10 @@ class L {
|
|
|
33
27
|
function k(e) {
|
|
34
28
|
return typeof e == "object" && e !== null && "key" in e && "method" in e && typeof e.key == "string" && typeof e.method == "string";
|
|
35
29
|
}
|
|
36
|
-
var
|
|
30
|
+
var R = /* @__PURE__ */ function(e) {
|
|
37
31
|
return e.PROVIDER_RPC_REQUEST = "PROVIDER_RPC_REQUEST", e.PROVIDER_RPC_RESPONSE = "PROVIDER_RPC_RESPONSE", e.PROVIDER_ON_EVENT = "PROVIDER_ON_EVENT", e.SEND_PROVIDER_META_INFO = "SEND_PROVIDER_META_INFO", e.REQUEST_PROVIDER_META_INFO = "REQUEST_PROVIDER_META_INFO", e;
|
|
38
32
|
}({});
|
|
39
|
-
const
|
|
33
|
+
const m = new L("cowSwapIframeRpcProviderTransport");
|
|
40
34
|
function H(e) {
|
|
41
35
|
if (e.isWalletConnect)
|
|
42
36
|
return e?.session?.peer?.metadata;
|
|
@@ -51,7 +45,7 @@ const j = [
|
|
|
51
45
|
"chainChanged",
|
|
52
46
|
"accountsChanged"
|
|
53
47
|
], b = [];
|
|
54
|
-
window.addEventListener("eip6963:announceProvider", (e) => {
|
|
48
|
+
typeof window < "u" && window.addEventListener("eip6963:announceProvider", (e) => {
|
|
55
49
|
const t = e;
|
|
56
50
|
b.push(t.detail);
|
|
57
51
|
});
|
|
@@ -75,21 +69,17 @@ class G {
|
|
|
75
69
|
/**
|
|
76
70
|
* Disconnects the JSON-RPC bridge from the Ethereum provider.
|
|
77
71
|
*/
|
|
78
|
-
// TODO: Add proper return type annotation
|
|
79
|
-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
80
72
|
disconnect() {
|
|
81
|
-
this.ethereumProvider = null,
|
|
73
|
+
typeof window > "u" || (this.ethereumProvider = null, m.stopListeningToMessageFromWindow(window, R.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow), m.stopListeningToMessageFromWindow(window, R.REQUEST_PROVIDER_META_INFO, this.processProviderMetaInfoRequest));
|
|
82
74
|
}
|
|
83
75
|
/**
|
|
84
76
|
* Handles the 'connect' event and sets up event listeners for Ethereum provider events.
|
|
85
77
|
* @param newProvider - The Ethereum provider to connect.
|
|
86
78
|
*/
|
|
87
|
-
// TODO: Add proper return type annotation
|
|
88
|
-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
89
79
|
onConnect(t) {
|
|
90
|
-
this.ethereumProvider ? this.disconnect() :
|
|
80
|
+
typeof window > "u" || (this.ethereumProvider ? this.disconnect() : m.listenToMessageFromWindow(window, R.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow), this.ethereumProvider = t, this.processPendingRequests(), j.forEach((n) => {
|
|
91
81
|
t.on(n, (s) => this.onProviderEvent(n, s));
|
|
92
|
-
}),
|
|
82
|
+
}), m.listenToMessageFromWindow(window, R.REQUEST_PROVIDER_META_INFO, this.processProviderMetaInfoRequest));
|
|
93
83
|
}
|
|
94
84
|
// TODO: Add proper return type annotation
|
|
95
85
|
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
@@ -139,13 +129,13 @@ class G {
|
|
|
139
129
|
processProviderMetaInfoRequest = () => {
|
|
140
130
|
if (!this.ethereumProvider) return;
|
|
141
131
|
const t = Q(this.ethereumProvider, b), n = H(this.ethereumProvider);
|
|
142
|
-
|
|
132
|
+
m.postMessageToWindow(this.iframeWidow, R.SEND_PROVIDER_META_INFO, {
|
|
143
133
|
providerEip6963Info: t,
|
|
144
134
|
providerWcMetadata: n
|
|
145
135
|
});
|
|
146
136
|
};
|
|
147
137
|
onProviderEvent(t, n) {
|
|
148
|
-
|
|
138
|
+
m.postMessageToWindow(this.iframeWidow, R.PROVIDER_ON_EVENT, {
|
|
149
139
|
event: t,
|
|
150
140
|
params: n
|
|
151
141
|
});
|
|
@@ -156,12 +146,12 @@ class G {
|
|
|
156
146
|
// TODO: Add proper return type annotation
|
|
157
147
|
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
158
148
|
forwardRpcResponseToIframe(t) {
|
|
159
|
-
|
|
149
|
+
m.postMessageToWindow(this.iframeWidow, R.PROVIDER_RPC_RESPONSE, t);
|
|
160
150
|
}
|
|
161
151
|
}
|
|
162
|
-
var
|
|
152
|
+
var W = { exports: {} }, C;
|
|
163
153
|
function q() {
|
|
164
|
-
return
|
|
154
|
+
return C || (C = 1, function(e) {
|
|
165
155
|
var t = Object.prototype.hasOwnProperty, n = "~";
|
|
166
156
|
function s() {
|
|
167
157
|
}
|
|
@@ -200,9 +190,9 @@ function q() {
|
|
|
200
190
|
}, u.prototype.emit = function(r, o, i, E, d, f) {
|
|
201
191
|
var _ = n ? n + r : r;
|
|
202
192
|
if (!this._events[_]) return !1;
|
|
203
|
-
var a = this._events[_],
|
|
193
|
+
var a = this._events[_], v = arguments.length, P, h;
|
|
204
194
|
if (a.fn) {
|
|
205
|
-
switch (a.once && this.removeListener(r, a.fn, void 0, !0),
|
|
195
|
+
switch (a.once && this.removeListener(r, a.fn, void 0, !0), v) {
|
|
206
196
|
case 1:
|
|
207
197
|
return a.fn.call(a.context), !0;
|
|
208
198
|
case 2:
|
|
@@ -216,13 +206,13 @@ function q() {
|
|
|
216
206
|
case 6:
|
|
217
207
|
return a.fn.call(a.context, o, i, E, d, f), !0;
|
|
218
208
|
}
|
|
219
|
-
for (h = 1,
|
|
220
|
-
|
|
221
|
-
a.fn.apply(a.context,
|
|
209
|
+
for (h = 1, P = new Array(v - 1); h < v; h++)
|
|
210
|
+
P[h - 1] = arguments[h];
|
|
211
|
+
a.fn.apply(a.context, P);
|
|
222
212
|
} else {
|
|
223
213
|
var x = a.length, g;
|
|
224
214
|
for (h = 0; h < x; h++)
|
|
225
|
-
switch (a[h].once && this.removeListener(r, a[h].fn, void 0, !0),
|
|
215
|
+
switch (a[h].once && this.removeListener(r, a[h].fn, void 0, !0), v) {
|
|
226
216
|
case 1:
|
|
227
217
|
a[h].fn.call(a[h].context);
|
|
228
218
|
break;
|
|
@@ -236,9 +226,9 @@ function q() {
|
|
|
236
226
|
a[h].fn.call(a[h].context, o, i, E);
|
|
237
227
|
break;
|
|
238
228
|
default:
|
|
239
|
-
if (!
|
|
240
|
-
|
|
241
|
-
a[h].fn.apply(a[h].context,
|
|
229
|
+
if (!P) for (g = 1, P = new Array(v - 1); g < v; g++)
|
|
230
|
+
P[g - 1] = arguments[g];
|
|
231
|
+
a[h].fn.apply(a[h].context, P);
|
|
242
232
|
}
|
|
243
233
|
}
|
|
244
234
|
return !0;
|
|
@@ -255,7 +245,7 @@ function q() {
|
|
|
255
245
|
if (f.fn)
|
|
256
246
|
f.fn === o && (!E || f.once) && (!i || f.context === i) && w(this, d);
|
|
257
247
|
else {
|
|
258
|
-
for (var _ = 0, a = [],
|
|
248
|
+
for (var _ = 0, a = [], v = f.length; _ < v; _++)
|
|
259
249
|
(f[_].fn !== o || E && !f[_].once || i && f[_].context !== i) && a.push(f[_]);
|
|
260
250
|
a.length ? this._events[d] = a.length === 1 ? a[0] : a : w(this, d);
|
|
261
251
|
}
|
|
@@ -264,7 +254,7 @@ function q() {
|
|
|
264
254
|
var o;
|
|
265
255
|
return r ? (o = n ? n + r : r, this._events[o] && w(this, o)) : (this._events = new s(), this._eventsCount = 0), this;
|
|
266
256
|
}, u.prototype.off = u.prototype.removeListener, u.prototype.addListener = u.prototype.on, u.prefixed = n, u.EventEmitter = u, e.exports = u;
|
|
267
|
-
}(
|
|
257
|
+
}(W)), W.exports;
|
|
268
258
|
}
|
|
269
259
|
q();
|
|
270
260
|
class $ {
|
|
@@ -288,7 +278,7 @@ var I = /* @__PURE__ */ function(e) {
|
|
|
288
278
|
}({}), N = /* @__PURE__ */ function(e) {
|
|
289
279
|
return e.SWAP = "swap", e.LIMIT = "limit", e.ADVANCED = "advanced", e.YIELD = "yield", e;
|
|
290
280
|
}({});
|
|
291
|
-
const
|
|
281
|
+
const pe = [
|
|
292
282
|
"primary",
|
|
293
283
|
"background",
|
|
294
284
|
"paper",
|
|
@@ -298,19 +288,19 @@ const fe = [
|
|
|
298
288
|
"alert",
|
|
299
289
|
"info",
|
|
300
290
|
"success"
|
|
301
|
-
],
|
|
291
|
+
], T = new L("cowSwapWidget");
|
|
302
292
|
class B {
|
|
303
293
|
contentWindow;
|
|
304
294
|
eventEmitter = new $();
|
|
305
295
|
listeners = [];
|
|
306
296
|
widgetListener;
|
|
307
297
|
constructor(t, n = []) {
|
|
308
|
-
this.contentWindow = t, this.updateListeners(n), this.widgetListener =
|
|
298
|
+
this.contentWindow = t, this.updateListeners(n), this.widgetListener = T.listenToMessageFromWindow(this.contentWindow, I.EMIT_COW_EVENT, (s) => this.eventEmitter.emit(s.event, s.payload));
|
|
309
299
|
}
|
|
310
300
|
// TODO: Add proper return type annotation
|
|
311
301
|
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
312
302
|
stopListeningIframe() {
|
|
313
|
-
|
|
303
|
+
T.stopListeningWindowListener(this.contentWindow, this.widgetListener);
|
|
314
304
|
}
|
|
315
305
|
updateListeners(t) {
|
|
316
306
|
for (const n of this.listeners)
|
|
@@ -326,7 +316,7 @@ class Y {
|
|
|
326
316
|
forwardSdkMessage;
|
|
327
317
|
constructor(t, n) {
|
|
328
318
|
this.appWindow = t, this.iframeWidow = n, this.forwardSdkMessage = (s) => {
|
|
329
|
-
z(s.data) && s.origin
|
|
319
|
+
z(s.data) && (typeof window < "u" && s.origin === window.location.origin || (J(s.data) ? this.appWindow.parent.postMessage(s.data, "*") : K(s.data) && this.iframeWidow.postMessage(s.data, "*")));
|
|
330
320
|
}, this.startListening();
|
|
331
321
|
}
|
|
332
322
|
// TODO: Add proper return type annotation
|
|
@@ -379,17 +369,27 @@ function te(e, t) {
|
|
|
379
369
|
function ne(e, t) {
|
|
380
370
|
return t.targetChainId && e.append("targetChainId", t.targetChainId.toString()), e;
|
|
381
371
|
}
|
|
382
|
-
const U = "640px", se = "450px", re = 20
|
|
383
|
-
|
|
372
|
+
const U = "640px", se = "450px", re = 20, oe = {
|
|
373
|
+
updateParams: () => {
|
|
374
|
+
},
|
|
375
|
+
updateListeners: () => {
|
|
376
|
+
},
|
|
377
|
+
updateProvider: () => {
|
|
378
|
+
},
|
|
379
|
+
destroy: () => {
|
|
380
|
+
}
|
|
381
|
+
};
|
|
382
|
+
function he(e, t) {
|
|
384
383
|
const { params: n, provider: s, listeners: p } = t;
|
|
385
384
|
let l = s, w = n;
|
|
386
|
-
|
|
385
|
+
if (typeof window > "u") return oe;
|
|
386
|
+
const u = ie(n);
|
|
387
387
|
e.innerHTML = "", e.appendChild(u);
|
|
388
388
|
const { contentWindow: c } = u;
|
|
389
389
|
if (!c)
|
|
390
390
|
throw console.error("Iframe does not contain a window", u), new Error("Iframe does not contain a window!");
|
|
391
391
|
const r = [];
|
|
392
|
-
r.push(
|
|
392
|
+
r.push(ae(c, n.appCode)), r.push(...ue(u, n.height, n.maxHeight)), r.push(ce());
|
|
393
393
|
const o = new B(window, p);
|
|
394
394
|
let i = S(c, null, l);
|
|
395
395
|
u.addEventListener("load", () => D(c, w, l));
|
|
@@ -412,13 +412,13 @@ function S(e, t, n) {
|
|
|
412
412
|
const s = t || new G(e);
|
|
413
413
|
return n && s.onConnect(n), s;
|
|
414
414
|
}
|
|
415
|
-
function
|
|
415
|
+
function ie(e) {
|
|
416
416
|
const { width: t = se, height: n = U } = e, s = document.createElement("iframe");
|
|
417
417
|
return s.src = Z(e), s.width = t, s.height = n, s.style.border = "0", s.allow = "clipboard-read; clipboard-write", s;
|
|
418
418
|
}
|
|
419
419
|
function D(e, t, n) {
|
|
420
420
|
const s = !!n, p = V(t), l = F(t).toString(), { theme: w, ...u } = t;
|
|
421
|
-
|
|
421
|
+
T.postMessageToWindow(e, M.UPDATE_PARAMS, {
|
|
422
422
|
urlParams: {
|
|
423
423
|
pathname: p,
|
|
424
424
|
search: l
|
|
@@ -427,17 +427,17 @@ function D(e, t, n) {
|
|
|
427
427
|
hasProvider: s
|
|
428
428
|
});
|
|
429
429
|
}
|
|
430
|
-
function
|
|
431
|
-
return
|
|
432
|
-
|
|
430
|
+
function ae(e, t) {
|
|
431
|
+
return T.listenToMessageFromWindow(window, I.ACTIVATE, () => {
|
|
432
|
+
T.postMessageToWindow(e, M.UPDATE_APP_DATA, {
|
|
433
433
|
metaData: t ? {
|
|
434
434
|
appCode: t
|
|
435
435
|
} : void 0
|
|
436
436
|
});
|
|
437
437
|
});
|
|
438
438
|
}
|
|
439
|
-
function
|
|
440
|
-
return
|
|
439
|
+
function ce() {
|
|
440
|
+
return T.listenToMessageFromWindow(window, I.INTERCEPT_WINDOW_OPEN, ({ href: e, rel: t, target: n }) => {
|
|
441
441
|
const s = e.toString();
|
|
442
442
|
if (!s.startsWith("http") && s.match(/^[a-zA-Z0-9]+:\/\//)) {
|
|
443
443
|
window.open(s, n, t);
|
|
@@ -445,40 +445,40 @@ function ae() {
|
|
|
445
445
|
}
|
|
446
446
|
});
|
|
447
447
|
}
|
|
448
|
-
function
|
|
448
|
+
function ue(e, t = U, n) {
|
|
449
449
|
return [
|
|
450
|
-
|
|
450
|
+
T.listenToMessageFromWindow(window, I.UPDATE_HEIGHT, (s) => {
|
|
451
451
|
const p = s.height ? s.height + re : void 0;
|
|
452
452
|
e.style.height = p ? `${n ? Math.min(p, n) : p}px` : t;
|
|
453
453
|
}),
|
|
454
|
-
|
|
454
|
+
T.listenToMessageFromWindow(window, I.SET_FULL_HEIGHT, ({ isUpToSmall: s }) => {
|
|
455
455
|
e.style.height = s ? t : `${n || document.body.offsetHeight}px`;
|
|
456
456
|
})
|
|
457
457
|
];
|
|
458
458
|
}
|
|
459
|
-
const
|
|
460
|
-
function
|
|
459
|
+
const de = Object.values(N);
|
|
460
|
+
function le(e, t, n) {
|
|
461
461
|
if (O(e)) {
|
|
462
462
|
const s = e[n];
|
|
463
|
-
return
|
|
463
|
+
return y(s) ? s[t] : s;
|
|
464
464
|
}
|
|
465
|
-
if (
|
|
465
|
+
if (y(e)) {
|
|
466
466
|
const s = e[t];
|
|
467
467
|
return O(s) ? s[n] : s;
|
|
468
468
|
}
|
|
469
469
|
return e;
|
|
470
470
|
}
|
|
471
471
|
function O(e) {
|
|
472
|
-
return typeof e != "object" ? !1 : Object.keys(e).every((t) =>
|
|
472
|
+
return typeof e != "object" ? !1 : Object.keys(e).every((t) => de.includes(t));
|
|
473
473
|
}
|
|
474
|
-
const
|
|
475
|
-
function
|
|
476
|
-
return typeof e != "object" ? !1 : Object.keys(e).every((t) =>
|
|
474
|
+
const fe = /^\d+$/;
|
|
475
|
+
function y(e) {
|
|
476
|
+
return typeof e != "object" ? !1 : Object.keys(e).every((t) => fe.test(t));
|
|
477
477
|
}
|
|
478
|
-
function
|
|
479
|
-
return O(e) ? Object.values(e).flatMap((t) =>
|
|
478
|
+
function Ee(e) {
|
|
479
|
+
return O(e) ? Object.values(e).flatMap((t) => y(t) ? Object.values(t) : [
|
|
480
480
|
t
|
|
481
|
-
]) :
|
|
481
|
+
]) : y(e) ? Object.values(e).flatMap((t) => O(t) ? Object.values(t) : [
|
|
482
482
|
t
|
|
483
483
|
]) : [
|
|
484
484
|
e
|
|
@@ -486,14 +486,14 @@ function le(e) {
|
|
|
486
486
|
}
|
|
487
487
|
export {
|
|
488
488
|
N as TradeType,
|
|
489
|
-
|
|
489
|
+
pe as WIDGET_PALETTE_COLORS,
|
|
490
490
|
I as WidgetMethodsEmit,
|
|
491
491
|
M as WidgetMethodsListen,
|
|
492
|
-
|
|
492
|
+
he as createCowSwapWidget,
|
|
493
493
|
X as isCowSwapWidgetPalette,
|
|
494
|
-
|
|
494
|
+
y as isPerNetworkConfig,
|
|
495
495
|
O as isPerTradeTypeConfig,
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
496
|
+
le as resolveFlexibleConfig,
|
|
497
|
+
Ee as resolveFlexibleConfigValues,
|
|
498
|
+
T as widgetIframeTransport
|
|
499
499
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cowprotocol/widget-lib",
|
|
3
|
-
"version": "0.22.
|
|
3
|
+
"version": "0.22.3",
|
|
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",
|
|
@@ -22,6 +22,6 @@
|
|
|
22
22
|
],
|
|
23
23
|
"dependencies": {
|
|
24
24
|
"@cowprotocol/events": "^2.1.0",
|
|
25
|
-
"@cowprotocol/iframe-transport": "^1.
|
|
25
|
+
"@cowprotocol/iframe-transport": "^1.2.0"
|
|
26
26
|
}
|
|
27
27
|
}
|