@cowprotocol/widget-lib 0.20.0 → 0.22.1
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/flexibleConfig.d.ts +1 -1
- package/index.d.ts +1 -0
- package/index.iife.js +1 -1
- package/index.js +1 -1
- package/index.mjs +217 -213
- package/package.json +2 -2
- package/resolveFlexibleConfigValues.d.ts +2 -0
- package/types.d.ts +18 -5
package/flexibleConfig.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { SupportedChainId } from '@cowprotocol/cow-sdk';
|
|
2
2
|
import { FlexibleConfig, PerNetworkConfig, PerTradeTypeConfig, TradeType } from './types';
|
|
3
|
-
export declare function resolveFlexibleConfig<T
|
|
3
|
+
export declare function resolveFlexibleConfig<T>(config: FlexibleConfig<T>, chainId: SupportedChainId, tradeType: TradeType): T | undefined;
|
|
4
4
|
export declare function isPerTradeTypeConfig<T>(config: FlexibleConfig<T>): config is PerTradeTypeConfig<T>;
|
|
5
5
|
export declare function isPerNetworkConfig<T>(config: FlexibleConfig<T>): config is PerNetworkConfig<T>;
|
package/index.d.ts
CHANGED
package/index.iife.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var cowSwapWidget=function(E){"use strict";var we=Object.defineProperty;var _e=(E,T,W)=>T in E?we(E,T,{enumerable:!0,configurable:!0,writable:!0,value:W}):E[T]=W;var P=(E,T,W)=>_e(E,typeof T!="symbol"?T+"":T,W);class T{constructor(n){this.key=n}postMessageToWindow(n,t,s){const p=typeof s=="object"?s:{},l={key:this.key,method:t,...p};n.postMessage(l,"*")}listenToMessageFromWindow(n,t,s){const p=l=>{!W(l.data)||l.data.key!==this.key||l.data.method!==t||s(l.data)};return n.addEventListener("message",p),p}stopListeningToMessageFromWindow(n,t,s){n.removeEventListener("message",s)}stopListeningWindowListener(n,t){n.removeEventListener("message",t)}}function W(e){return typeof e=="object"&&e!==null&&"key"in e&&"method"in e&&typeof e.key=="string"&&typeof e.method=="string"}var v=(e=>(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))(v||{});const I=new T("cowSwapIframeRpcProviderTransport");function G(e){var n,t;if(e.isWalletConnect)return(t=(n=e==null?void 0:e.session)==null?void 0:n.peer)==null?void 0:t.metadata}function j(e,n){var t;return(t=n.find(s=>s.provider===e))==null?void 0:t.info}const q=["connect","disconnect","close","chainChanged","accountsChanged"],N=[];window.addEventListener("eip6963:announceProvider",e=>{const n=e;N.push(n.detail)});class ${constructor(n){P(this,"ethereumProvider",null);P(this,"requestWaitingForConnection",{});P(this,"processRpcCallFromWindow",({rpcRequest:n})=>{if(!this.ethereumProvider){n.id&&(this.requestWaitingForConnection[n.id]=n);return}this.processRpcRequest(n)});P(this,"processProviderMetaInfoRequest",()=>{if(!this.ethereumProvider)return;const n=j(this.ethereumProvider,N),t=G(this.ethereumProvider);I.postMessageToWindow(this.iframeWidow,v.SEND_PROVIDER_META_INFO,{providerEip6963Info:n,providerWcMetadata:t})});this.iframeWidow=n}disconnect(){this.ethereumProvider=null,I.stopListeningToMessageFromWindow(window,v.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),I.stopListeningToMessageFromWindow(window,v.REQUEST_PROVIDER_META_INFO,this.processProviderMetaInfoRequest)}onConnect(n){this.ethereumProvider?this.disconnect():I.listenToMessageFromWindow(window,v.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),this.ethereumProvider=n,this.processPendingRequests(),q.forEach(t=>{n.on(t,s=>this.onProviderEvent(t,s))}),I.listenToMessageFromWindow(window,v.REQUEST_PROVIDER_META_INFO,this.processProviderMetaInfoRequest)}processPendingRequests(){Object.keys(this.requestWaitingForConnection).forEach(n=>{this.processRpcRequest(this.requestWaitingForConnection[n])}),this.requestWaitingForConnection={}}processRpcRequest(n){const{id:t,jsonrpc:s,method:p}=n;if(!this.ethereumProvider||!t)return;(p==="enable"?this.ethereumProvider.enable():this.ethereumProvider.request({...n,id:t})).then(_=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:s,id:t,result:_}})).catch(_=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:s,id:t,error:_}}))}onProviderEvent(n,t){I.postMessageToWindow(this.iframeWidow,v.PROVIDER_ON_EVENT,{event:n,params:t})}forwardRpcResponseToIframe(n){I.postMessageToWindow(this.iframeWidow,v.PROVIDER_RPC_RESPONSE,n)}}var D={exports:{}},b;function B(){return b||(b=1,function(e){var n=Object.prototype.hasOwnProperty,t="~";function s(){}Object.create&&(s.prototype=Object.create(null),new s().__proto__||(t=!1));function p(c,r,i){this.fn=c,this.context=r,this.once=i||!1}function l(c,r,i,o,w){if(typeof i!="function")throw new TypeError("The listener must be a function");var d=new p(i,o||c,w),f=t?t+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)n.call(i,o)&&r.push(t?o.slice(1):o);return Object.getOwnPropertySymbols?r.concat(Object.getOwnPropertySymbols(i)):r},u.prototype.listeners=function(r){var i=t?t+r:r,o=this._events[i];if(!o)return[];if(o.fn)return[o.fn];for(var w=0,d=o.length,f=new Array(d);w<d;w++)f[w]=o[w].fn;return f},u.prototype.listenerCount=function(r){var i=t?t+r:r,o=this._events[i];return o?o.fn?1:o.length:0},u.prototype.emit=function(r,i,o,w,d,f){var m=t?t+r:r;if(!this._events[m])return!1;var a=this._events[m],g=arguments.length,C,h;if(a.fn){switch(a.once&&this.removeListener(r,a.fn,void 0,!0),g){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,w),!0;case 5:return a.fn.call(a.context,i,o,w,d),!0;case 6:return a.fn.call(a.context,i,o,w,d,f),!0}for(h=1,C=new Array(g-1);h<g;h++)C[h-1]=arguments[h];a.fn.apply(a.context,C)}else{var Ee=a.length,S;for(h=0;h<Ee;h++)switch(a[h].once&&this.removeListener(r,a[h].fn,void 0,!0),g){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,w);break;default:if(!C)for(S=1,C=new Array(g-1);S<g;S++)C[S-1]=arguments[S];a[h].fn.apply(a[h].context,C)}}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,w){var d=t?t+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&&(!w||f.once)&&(!o||f.context===o)&&_(this,d);else{for(var m=0,a=[],g=f.length;m<g;m++)(f[m].fn!==i||w&&!f[m].once||o&&f[m].context!==o)&&a.push(f[m]);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=t?t+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=t,u.EventEmitter=u,e.exports=u}(D)),D.exports}B();class Y{constructor(){P(this,"subscriptions",{})}on(n){const{event:t,handler:s}=n;return this.subscriptions[t]||(this.subscriptions[t]=[]),this.subscriptions[t].push(s),n}off(n){const{event:t,handler:s}=n;return this.subscriptions[t]&&(this.subscriptions[t]=this.subscriptions[t].filter(p=>p!==s)),n}emit(n,t){this.subscriptions[n]&&this.subscriptions[n].forEach(s=>s(t))}}var O=(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.INTERCEPT_WINDOW_OPEN="INTERCEPT_WINDOW_OPEN",e))(O||{}),y=(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))(y||{}),A=(e=>(e.SWAP="swap",e.LIMIT="limit",e.ADVANCED="advanced",e.YIELD="yield",e))(A||{});const z=["primary","background","paper","text","danger","warning","alert","info","success"],R=new T("cowSwapWidget");class J{constructor(n,t=[]){P(this,"eventEmitter",new Y);P(this,"listeners",[]);P(this,"widgetListener");this.contentWindow=n,this.updateListeners(t),this.widgetListener=R.listenToMessageFromWindow(this.contentWindow,O.EMIT_COW_EVENT,s=>this.eventEmitter.emit(s.event,s.payload))}stopListeningIframe(){R.stopListeningWindowListener(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 K{constructor(n,t){P(this,"forwardSdkMessage");this.appWindow=n,this.iframeWidow=t,this.forwardSdkMessage=s=>{X(s.data)&&s.origin!==window.location.origin&&(Z(s.data)?this.appWindow.parent.postMessage(s.data,"*"):ee(s.data)&&this.iframeWidow.postMessage(s.data,"*"))},this.startListening()}startListening(){this.appWindow.addEventListener("message",this.forwardSdkMessage)}stopListening(){this.appWindow.removeEventListener("message",this.forwardSdkMessage)}}function X(e){return typeof e=="object"&&e!==null&&"id"in e&&typeof e.id=="string"}function Z(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 ee(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 F="_";function te(e){const n=typeof e.baseUrl=="string"?e.baseUrl:"https://swap.cow.fi",t=U(e);return n+"/#"+t+"?"+k(e)}function U(e){const{chainId:n=1,sell:t,buy:s,tradeType:p=A.SWAP}=e,l=[(t==null?void 0:t.asset)||F,(s==null?void 0:s.asset)||F].map(encodeURIComponent).join("/");return`/${n}/widget/${p}/${l}`}function k(e){const n=new URLSearchParams;return re(se(ne(n,e),e),e)}function ne(e,n){const{sell:t,buy:s}=n;return t!=null&&t.amount&&e.append("sellAmount",t.amount),s!=null&&s.amount&&e.append("buyAmount",s.amount),e}function se(e,n){const t=n.theme;return t?(V(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)}function re(e,n){return n.targetChainId&&e.append("targetChainId",n.targetChainId.toString()),e}const H="640px",ie="450px",oe=20;function ae(e,n){const{params:t,provider:s,listeners:p}=n;let l=s,_=t;const u=ce(t);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(ue(c,t.appCode)),r.push(...fe(u,t.height,t.maxHeight)),r.push(de());const i=new J(window,p);let o=x(c,null,l);u.addEventListener("load",()=>Q(c,_,l));const w=new K(window,c);return{updateParams:d=>{_=d,Q(c,_,l)},updateListeners:d=>i.updateListeners(d),updateProvider:d=>{l=d,o=x(c,o,d)},destroy:()=>{o.disconnect(),i.stopListeningIframe(),r.forEach(d=>window.removeEventListener("message",d)),w.stopListening(),e.removeChild(u)}}}function x(e,n,t){n&&n.disconnect();const s=n||new $(e);return t&&s.onConnect(t),s}function ce(e){const{width:n=ie,height:t=H}=e,s=document.createElement("iframe");return s.src=te(e),s.width=n,s.height=t,s.style.border="0",s.allow="clipboard-read; clipboard-write",s}function Q(e,n,t){const s=!!t,p=U(n),l=k(n).toString(),{theme:_,...u}=n;R.postMessageToWindow(e,y.UPDATE_PARAMS,{urlParams:{pathname:p,search:l},appParams:u,hasProvider:s})}function ue(e,n){return R.listenToMessageFromWindow(window,O.ACTIVATE,()=>{R.postMessageToWindow(e,y.UPDATE_APP_DATA,{metaData:n?{appCode:n}:void 0})})}function de(){return R.listenToMessageFromWindow(window,O.INTERCEPT_WINDOW_OPEN,({href:e,rel:n,target:t})=>{const s=e.toString();if(!s.startsWith("http")&&s.match(/^[a-zA-Z0-9]+:\/\//)){window.open(s,t,n);return}})}function fe(e,n=H,t){return[R.listenToMessageFromWindow(window,O.UPDATE_HEIGHT,s=>{const p=s.height?s.height+oe:void 0;e.style.height=p?`${t?Math.min(p,t):p}px`:n}),R.listenToMessageFromWindow(window,O.SET_FULL_HEIGHT,({isUpToSmall:s})=>{e.style.height=s?n:`${t||document.body.offsetHeight}px`})]}const pe=Object.values(A);function he(e,n,t){if(L(e)){const s=e[t];return M(s)?s[n]:s}if(M(e)){const s=e[n];return L(s)?s[t]:s}return e}function L(e){return typeof e!="object"?!1:Object.keys(e).every(n=>pe.includes(n))}const le=/^\d+$/;function M(e){return typeof e!="object"?!1:Object.keys(e).every(n=>typeof n=="number"||le.test(n))}return E.TradeType=A,E.WIDGET_PALETTE_COLORS=z,E.WidgetMethodsEmit=O,E.WidgetMethodsListen=y,E.createCowSwapWidget=ae,E.isCowSwapWidgetPalette=V,E.isPerNetworkConfig=M,E.isPerTradeTypeConfig=L,E.resolveFlexibleConfig=he,E.widgetIframeTransport=R,Object.defineProperty(E,Symbol.toStringTag,{value:"Module"}),E}({});
|
|
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}({});
|
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var Q=Object.defineProperty;var G=(e,t,n)=>t in e?Q(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n;var _=(e,t,n)=>G(e,typeof t!="symbol"?t+"":t,n);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class b{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=>{!j(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 j(e){return typeof e=="object"&&e!==null&&"key"in e&&"method"in e&&typeof e.key=="string"&&typeof e.method=="string"}var m=(e=>(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))(m||{});const g=new b("cowSwapIframeRpcProviderTransport");function q(e){var t,n;if(e.isWalletConnect)return(n=(t=e==null?void 0:e.session)==null?void 0:t.peer)==null?void 0:n.metadata}function $(e,t){var n;return(n=t.find(s=>s.provider===e))==null?void 0:n.info}const B=["connect","disconnect","close","chainChanged","accountsChanged"],V=[];window.addEventListener("eip6963:announceProvider",e=>{const t=e;V.push(t.detail)});class Y{constructor(t){_(this,"ethereumProvider",null);_(this,"requestWaitingForConnection",{});_(this,"processRpcCallFromWindow",({rpcRequest:t})=>{if(!this.ethereumProvider){t.id&&(this.requestWaitingForConnection[t.id]=t);return}this.processRpcRequest(t)});_(this,"processProviderMetaInfoRequest",()=>{if(!this.ethereumProvider)return;const t=$(this.ethereumProvider,V),n=q(this.ethereumProvider);g.postMessageToWindow(this.iframeWidow,m.SEND_PROVIDER_META_INFO,{providerEip6963Info:t,providerWcMetadata:n})});this.iframeWidow=t}disconnect(){this.ethereumProvider=null,g.stopListeningToMessageFromWindow(window,m.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),g.stopListeningToMessageFromWindow(window,m.REQUEST_PROVIDER_META_INFO,this.processProviderMetaInfoRequest)}onConnect(t){this.ethereumProvider?this.disconnect():g.listenToMessageFromWindow(window,m.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),this.ethereumProvider=t,this.processPendingRequests(),B.forEach(n=>{t.on(n,s=>this.onProviderEvent(n,s))}),g.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.enable():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}}))}onProviderEvent(t,n){g.postMessageToWindow(this.iframeWidow,m.PROVIDER_ON_EVENT,{event:t,params:n})}forwardRpcResponseToIframe(t){g.postMessageToWindow(this.iframeWidow,m.PROVIDER_RPC_RESPONSE,t)}}var S={exports:{}},D;function z(){return D||(D=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,o){this.fn=c,this.context=r,this.once=o||!1}function l(c,r,o,i,E){if(typeof o!="function")throw new TypeError("The listener must be a function");var d=new p(o,i||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=[],o,i;if(this._eventsCount===0)return r;for(i in o=this._events)t.call(o,i)&&r.push(n?i.slice(1):i);return Object.getOwnPropertySymbols?r.concat(Object.getOwnPropertySymbols(o)):r},u.prototype.listeners=function(r){var o=n?n+r:r,i=this._events[o];if(!i)return[];if(i.fn)return[i.fn];for(var E=0,d=i.length,f=new Array(d);E<d;E++)f[E]=i[E].fn;return f},u.prototype.listenerCount=function(r){var o=n?n+r:r,i=this._events[o];return i?i.fn?1:i.length:0},u.prototype.emit=function(r,o,i,E,d,f){var T=n?n+r:r;if(!this._events[T])return!1;var a=this._events[T],R=arguments.length,v,h;if(a.fn){switch(a.once&&this.removeListener(r,a.fn,void 0,!0),R){case 1:return a.fn.call(a.context),!0;case 2:return a.fn.call(a.context,o),!0;case 3:return a.fn.call(a.context,o,i),!0;case 4:return a.fn.call(a.context,o,i,E),!0;case 5:return a.fn.call(a.context,o,i,E,d),!0;case 6:return a.fn.call(a.context,o,i,E,d,f),!0}for(h=1,v=new Array(R-1);h<R;h++)v[h-1]=arguments[h];a.fn.apply(a.context,v)}else{var H=a.length,O;for(h=0;h<H;h++)switch(a[h].once&&this.removeListener(r,a[h].fn,void 0,!0),R){case 1:a[h].fn.call(a[h].context);break;case 2:a[h].fn.call(a[h].context,o);break;case 3:a[h].fn.call(a[h].context,o,i);break;case 4:a[h].fn.call(a[h].context,o,i,E);break;default:if(!v)for(O=1,v=new Array(R-1);O<R;O++)v[O-1]=arguments[O];a[h].fn.apply(a[h].context,v)}}return!0},u.prototype.on=function(r,o,i){return l(this,r,o,i,!1)},u.prototype.once=function(r,o,i){return l(this,r,o,i,!0)},u.prototype.removeListener=function(r,o,i,E){var d=n?n+r:r;if(!this._events[d])return this;if(!o)return w(this,d),this;var f=this._events[d];if(f.fn)f.fn===o&&(!E||f.once)&&(!i||f.context===i)&&w(this,d);else{for(var T=0,a=[],R=f.length;T<R;T++)(f[T].fn!==o||E&&!f[T].once||i&&f[T].context!==i)&&a.push(f[T]);a.length?this._events[d]=a.length===1?a[0]:a:w(this,d)}return this},u.prototype.removeAllListeners=function(r){var o;return r?(o=n?n+r:r,this._events[o]&&w(this,o)):(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}(S)),S.exports}z();class J{constructor(){_(this,"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=(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.INTERCEPT_WINDOW_OPEN="INTERCEPT_WINDOW_OPEN",e))(I||{}),C=(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))(C||{}),W=(e=>(e.SWAP="swap",e.LIMIT="limit",e.ADVANCED="advanced",e.YIELD="yield",e))(W||{});const K=["primary","background","paper","text","danger","warning","alert","info","success"],P=new b("cowSwapWidget");class X{constructor(t,n=[]){_(this,"eventEmitter",new J);_(this,"listeners",[]);_(this,"widgetListener");this.contentWindow=t,this.updateListeners(n),this.widgetListener=P.listenToMessageFromWindow(this.contentWindow,I.EMIT_COW_EVENT,s=>this.eventEmitter.emit(s.event,s.payload))}stopListeningIframe(){P.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 Z{constructor(t,n){_(this,"forwardSdkMessage");this.appWindow=t,this.iframeWidow=n,this.forwardSdkMessage=s=>{ee(s.data)&&s.origin!==window.location.origin&&(te(s.data)?this.appWindow.parent.postMessage(s.data,"*"):ne(s.data)&&this.iframeWidow.postMessage(s.data,"*"))},this.startListening()}startListening(){this.appWindow.addEventListener("message",this.forwardSdkMessage)}stopListening(){this.appWindow.removeEventListener("message",this.forwardSdkMessage)}}function ee(e){return typeof e=="object"&&e!==null&&"id"in e&&typeof e.id=="string"}function te(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 ne(e){return"success"in e&&typeof e.success=="boolean"&&"version"in e&&typeof e.version=="string"}function F(e){return!!(e&&typeof e=="object")}const L="_";function se(e){const t=typeof e.baseUrl=="string"?e.baseUrl:"https://swap.cow.fi",n=U(e);return t+"/#"+n+"?"+x(e)}function U(e){const{chainId:t=1,sell:n,buy:s,tradeType:p=W.SWAP}=e,l=[(n==null?void 0:n.asset)||L,(s==null?void 0:s.asset)||L].map(encodeURIComponent).join("/");return`/${t}/widget/${p}/${l}`}function x(e){const t=new URLSearchParams;return ie(oe(re(t,e),e),e)}function re(e,t){const{sell:n,buy:s}=t;return n!=null&&n.amount&&e.append("sellAmount",n.amount),s!=null&&s.amount&&e.append("buyAmount",s.amount),e}function oe(e,t){const n=t.theme;return n?(F(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 ie(e,t){return t.targetChainId&&e.append("targetChainId",t.targetChainId.toString()),e}const k="640px",ae="450px",ce=20;function ue(e,t){const{params:n,provider:s,listeners:p}=t;let l=s,w=n;const u=de(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(fe(c,n.appCode)),r.push(...he(u,n.height,n.maxHeight)),r.push(pe());const o=new X(window,p);let i=M(c,null,l);u.addEventListener("load",()=>N(c,w,l));const E=new Z(window,c);return{updateParams:d=>{w=d,N(c,w,l)},updateListeners:d=>o.updateListeners(d),updateProvider:d=>{l=d,i=M(c,i,d)},destroy:()=>{i.disconnect(),o.stopListeningIframe(),r.forEach(d=>window.removeEventListener("message",d)),E.stopListening(),e.removeChild(u)}}}function M(e,t,n){t&&t.disconnect();const s=t||new Y(e);return n&&s.onConnect(n),s}function de(e){const{width:t=ae,height:n=k}=e,s=document.createElement("iframe");return s.src=se(e),s.width=t,s.height=n,s.style.border="0",s.allow="clipboard-read; clipboard-write",s}function N(e,t,n){const s=!!n,p=U(t),l=x(t).toString(),{theme:w,...u}=t;P.postMessageToWindow(e,C.UPDATE_PARAMS,{urlParams:{pathname:p,search:l},appParams:u,hasProvider:s})}function fe(e,t){return P.listenToMessageFromWindow(window,I.ACTIVATE,()=>{P.postMessageToWindow(e,C.UPDATE_APP_DATA,{metaData:t?{appCode:t}:void 0})})}function pe(){return P.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 he(e,t=k,n){return[P.listenToMessageFromWindow(window,I.UPDATE_HEIGHT,s=>{const p=s.height?s.height+ce:void 0;e.style.height=p?`${n?Math.min(p,n):p}px`:t}),P.listenToMessageFromWindow(window,I.SET_FULL_HEIGHT,({isUpToSmall:s})=>{e.style.height=s?t:`${n||document.body.offsetHeight}px`})]}const le=Object.values(W);function Ee(e,t,n){if(y(e)){const s=e[n];return A(s)?s[t]:s}if(A(e)){const s=e[t];return y(s)?s[n]:s}return e}function y(e){return typeof e!="object"?!1:Object.keys(e).every(t=>le.includes(t))}const we=/^\d+$/;function A(e){return typeof e!="object"?!1:Object.keys(e).every(t=>typeof t=="number"||we.test(t))}exports.TradeType=W;exports.WIDGET_PALETTE_COLORS=K;exports.WidgetMethodsEmit=I;exports.WidgetMethodsListen=C;exports.createCowSwapWidget=ue;exports.isCowSwapWidgetPalette=F;exports.isPerNetworkConfig=A;exports.isPerTradeTypeConfig=y;exports.resolveFlexibleConfig=Ee;exports.widgetIframeTransport=P;
|
|
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;
|
package/index.mjs
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var w = (e, t, n) => Q(e, typeof t != "symbol" ? t + "" : t, n);
|
|
4
|
-
class b {
|
|
1
|
+
class L {
|
|
2
|
+
key;
|
|
5
3
|
constructor(t) {
|
|
6
4
|
this.key = t;
|
|
7
5
|
}
|
|
@@ -13,15 +11,11 @@ class b {
|
|
|
13
11
|
method: n,
|
|
14
12
|
...p
|
|
15
13
|
};
|
|
16
|
-
t.postMessage(
|
|
17
|
-
l,
|
|
18
|
-
"*"
|
|
19
|
-
// TODO: Change to CoW specific origin in production. https://github.com/cowprotocol/cowswap/issues/3828
|
|
20
|
-
);
|
|
14
|
+
t.postMessage(l, "*");
|
|
21
15
|
}
|
|
22
16
|
listenToMessageFromWindow(t, n, s) {
|
|
23
17
|
const p = (l) => {
|
|
24
|
-
!
|
|
18
|
+
!k(l.data) || l.data.key !== this.key || l.data.method !== n || s(l.data);
|
|
25
19
|
};
|
|
26
20
|
return t.addEventListener("message", p), p;
|
|
27
21
|
}
|
|
@@ -36,92 +30,66 @@ class b {
|
|
|
36
30
|
t.removeEventListener("message", n);
|
|
37
31
|
}
|
|
38
32
|
}
|
|
39
|
-
function
|
|
33
|
+
function k(e) {
|
|
40
34
|
return typeof e == "object" && e !== null && "key" in e && "method" in e && typeof e.key == "string" && typeof e.method == "string";
|
|
41
35
|
}
|
|
42
|
-
var
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
);
|
|
46
|
-
function
|
|
47
|
-
var t, n;
|
|
36
|
+
var m = /* @__PURE__ */ function(e) {
|
|
37
|
+
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
|
+
}({});
|
|
39
|
+
const P = new L("cowSwapIframeRpcProviderTransport");
|
|
40
|
+
function H(e) {
|
|
48
41
|
if (e.isWalletConnect)
|
|
49
|
-
return
|
|
42
|
+
return e?.session?.peer?.metadata;
|
|
50
43
|
}
|
|
51
|
-
function
|
|
52
|
-
|
|
53
|
-
return (n = t.find((s) => s.provider === e)) == null ? void 0 : n.info;
|
|
44
|
+
function Q(e, t) {
|
|
45
|
+
return t.find((n) => n.provider === e)?.info;
|
|
54
46
|
}
|
|
55
|
-
const
|
|
47
|
+
const j = [
|
|
48
|
+
"connect",
|
|
49
|
+
"disconnect",
|
|
50
|
+
"close",
|
|
51
|
+
"chainChanged",
|
|
52
|
+
"accountsChanged"
|
|
53
|
+
], b = [];
|
|
56
54
|
window.addEventListener("eip6963:announceProvider", (e) => {
|
|
57
55
|
const t = e;
|
|
58
|
-
|
|
56
|
+
b.push(t.detail);
|
|
59
57
|
});
|
|
60
|
-
class
|
|
58
|
+
class G {
|
|
59
|
+
iframeWidow;
|
|
60
|
+
/**
|
|
61
|
+
* The Ethereum provider instance.
|
|
62
|
+
* When is null the JSON-RPC bridge is disconnected from the Ethereum provider.
|
|
63
|
+
* */
|
|
64
|
+
ethereumProvider = null;
|
|
65
|
+
/** Stored JSON-RPC requests, to queue them when disconnected. */
|
|
66
|
+
requestWaitingForConnection = {};
|
|
61
67
|
/**
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
68
|
+
* Creates an instance of IframeRpcProviderBridge.
|
|
69
|
+
* @param iframeWidow - The iFrame window that will post up general RPC messages and to which the IframeRpcProviderBridge will forward the RPC result.
|
|
70
|
+
* Also it will receive some special RPC events coming from the wallet, like connect/chainChanged,accountChanged
|
|
71
|
+
*/
|
|
66
72
|
constructor(t) {
|
|
67
|
-
/**
|
|
68
|
-
* The Ethereum provider instance.
|
|
69
|
-
* When is null the JSON-RPC bridge is disconnected from the Ethereum provider.
|
|
70
|
-
* */
|
|
71
|
-
w(this, "ethereumProvider", null);
|
|
72
|
-
/** Stored JSON-RPC requests, to queue them when disconnected. */
|
|
73
|
-
w(this, "requestWaitingForConnection", {});
|
|
74
|
-
w(this, "processRpcCallFromWindow", ({ rpcRequest: t }) => {
|
|
75
|
-
if (!this.ethereumProvider) {
|
|
76
|
-
t.id && (this.requestWaitingForConnection[t.id] = t);
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
this.processRpcRequest(t);
|
|
80
|
-
});
|
|
81
|
-
w(this, "processProviderMetaInfoRequest", () => {
|
|
82
|
-
if (!this.ethereumProvider) return;
|
|
83
|
-
const t = j(this.ethereumProvider, V), n = q(this.ethereumProvider);
|
|
84
|
-
g.postMessageToWindow(this.iframeWidow, P.SEND_PROVIDER_META_INFO, {
|
|
85
|
-
providerEip6963Info: t,
|
|
86
|
-
providerWcMetadata: n
|
|
87
|
-
});
|
|
88
|
-
});
|
|
89
73
|
this.iframeWidow = t;
|
|
90
74
|
}
|
|
91
75
|
/**
|
|
92
|
-
|
|
93
|
-
|
|
76
|
+
* Disconnects the JSON-RPC bridge from the Ethereum provider.
|
|
77
|
+
*/
|
|
94
78
|
// TODO: Add proper return type annotation
|
|
95
79
|
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
96
80
|
disconnect() {
|
|
97
|
-
this.ethereumProvider = null,
|
|
98
|
-
window,
|
|
99
|
-
P.PROVIDER_RPC_REQUEST,
|
|
100
|
-
this.processRpcCallFromWindow
|
|
101
|
-
), g.stopListeningToMessageFromWindow(
|
|
102
|
-
window,
|
|
103
|
-
P.REQUEST_PROVIDER_META_INFO,
|
|
104
|
-
this.processProviderMetaInfoRequest
|
|
105
|
-
);
|
|
81
|
+
this.ethereumProvider = null, P.stopListeningToMessageFromWindow(window, m.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow), P.stopListeningToMessageFromWindow(window, m.REQUEST_PROVIDER_META_INFO, this.processProviderMetaInfoRequest);
|
|
106
82
|
}
|
|
107
83
|
/**
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
84
|
+
* Handles the 'connect' event and sets up event listeners for Ethereum provider events.
|
|
85
|
+
* @param newProvider - The Ethereum provider to connect.
|
|
86
|
+
*/
|
|
111
87
|
// TODO: Add proper return type annotation
|
|
112
88
|
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
113
89
|
onConnect(t) {
|
|
114
|
-
this.ethereumProvider ? this.disconnect() :
|
|
115
|
-
window,
|
|
116
|
-
P.PROVIDER_RPC_REQUEST,
|
|
117
|
-
this.processRpcCallFromWindow
|
|
118
|
-
), this.ethereumProvider = t, this.processPendingRequests(), $.forEach((n) => {
|
|
90
|
+
this.ethereumProvider ? this.disconnect() : P.listenToMessageFromWindow(window, m.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow), this.ethereumProvider = t, this.processPendingRequests(), j.forEach((n) => {
|
|
119
91
|
t.on(n, (s) => this.onProviderEvent(n, s));
|
|
120
|
-
}),
|
|
121
|
-
window,
|
|
122
|
-
P.REQUEST_PROVIDER_META_INFO,
|
|
123
|
-
this.processProviderMetaInfoRequest
|
|
124
|
-
);
|
|
92
|
+
}), P.listenToMessageFromWindow(window, m.REQUEST_PROVIDER_META_INFO, this.processProviderMetaInfoRequest);
|
|
125
93
|
}
|
|
126
94
|
// TODO: Add proper return type annotation
|
|
127
95
|
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
@@ -131,47 +99,69 @@ class B {
|
|
|
131
99
|
}), this.requestWaitingForConnection = {};
|
|
132
100
|
}
|
|
133
101
|
/**
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
102
|
+
* Processes a JSON-RPC request and sends appropriate response or error via the content window.
|
|
103
|
+
* @param request - The JSON-RPC request to be processed.
|
|
104
|
+
*/
|
|
137
105
|
// TODO: Add proper return type annotation
|
|
138
106
|
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
139
107
|
processRpcRequest(t) {
|
|
140
108
|
const { id: n, jsonrpc: s, method: p } = t;
|
|
141
109
|
if (!this.ethereumProvider || !n)
|
|
142
110
|
return;
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
).
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
111
|
+
// Keep the legacy "enable" method for backward compatibility
|
|
112
|
+
(p === "enable" ? this.ethereumProvider.request({
|
|
113
|
+
method: "eth_requestAccounts",
|
|
114
|
+
id: n
|
|
115
|
+
}) : this.ethereumProvider.request({
|
|
116
|
+
...t,
|
|
117
|
+
id: n
|
|
118
|
+
})).then((w) => this.forwardRpcResponseToIframe({
|
|
119
|
+
rpcResponse: {
|
|
120
|
+
jsonrpc: s,
|
|
121
|
+
id: n,
|
|
122
|
+
result: w
|
|
123
|
+
}
|
|
124
|
+
})).catch((w) => this.forwardRpcResponseToIframe({
|
|
125
|
+
rpcResponse: {
|
|
126
|
+
jsonrpc: s,
|
|
127
|
+
id: n,
|
|
128
|
+
error: w
|
|
129
|
+
}
|
|
130
|
+
}));
|
|
152
131
|
}
|
|
132
|
+
processRpcCallFromWindow = ({ rpcRequest: t }) => {
|
|
133
|
+
if (!this.ethereumProvider) {
|
|
134
|
+
t.id && (this.requestWaitingForConnection[t.id] = t);
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
this.processRpcRequest(t);
|
|
138
|
+
};
|
|
139
|
+
processProviderMetaInfoRequest = () => {
|
|
140
|
+
if (!this.ethereumProvider) return;
|
|
141
|
+
const t = Q(this.ethereumProvider, b), n = H(this.ethereumProvider);
|
|
142
|
+
P.postMessageToWindow(this.iframeWidow, m.SEND_PROVIDER_META_INFO, {
|
|
143
|
+
providerEip6963Info: t,
|
|
144
|
+
providerWcMetadata: n
|
|
145
|
+
});
|
|
146
|
+
};
|
|
153
147
|
onProviderEvent(t, n) {
|
|
154
|
-
|
|
148
|
+
P.postMessageToWindow(this.iframeWidow, m.PROVIDER_ON_EVENT, {
|
|
155
149
|
event: t,
|
|
156
150
|
params: n
|
|
157
151
|
});
|
|
158
152
|
}
|
|
159
153
|
/**
|
|
160
|
-
|
|
161
|
-
|
|
154
|
+
* Forward a JSON-RPC message to the content window.
|
|
155
|
+
*/
|
|
162
156
|
// TODO: Add proper return type annotation
|
|
163
157
|
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
164
158
|
forwardRpcResponseToIframe(t) {
|
|
165
|
-
|
|
166
|
-
this.iframeWidow,
|
|
167
|
-
P.PROVIDER_RPC_RESPONSE,
|
|
168
|
-
t
|
|
169
|
-
);
|
|
159
|
+
P.postMessageToWindow(this.iframeWidow, m.PROVIDER_RPC_RESPONSE, t);
|
|
170
160
|
}
|
|
171
161
|
}
|
|
172
|
-
var C = { exports: {} },
|
|
173
|
-
function
|
|
174
|
-
return
|
|
162
|
+
var C = { exports: {} }, y;
|
|
163
|
+
function q() {
|
|
164
|
+
return y || (y = 1, function(e) {
|
|
175
165
|
var t = Object.prototype.hasOwnProperty, n = "~";
|
|
176
166
|
function s() {
|
|
177
167
|
}
|
|
@@ -185,7 +175,7 @@ function Y() {
|
|
|
185
175
|
var d = new p(o, i || c, E), f = n ? n + r : r;
|
|
186
176
|
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;
|
|
187
177
|
}
|
|
188
|
-
function
|
|
178
|
+
function w(c, r) {
|
|
189
179
|
--c._eventsCount === 0 ? c._events = new s() : delete c._events[r];
|
|
190
180
|
}
|
|
191
181
|
function u() {
|
|
@@ -208,11 +198,11 @@ function Y() {
|
|
|
208
198
|
var o = n ? n + r : r, i = this._events[o];
|
|
209
199
|
return i ? i.fn ? 1 : i.length : 0;
|
|
210
200
|
}, u.prototype.emit = function(r, o, i, E, d, f) {
|
|
211
|
-
var
|
|
212
|
-
if (!this._events[
|
|
213
|
-
var a = this._events[
|
|
201
|
+
var _ = n ? n + r : r;
|
|
202
|
+
if (!this._events[_]) return !1;
|
|
203
|
+
var a = this._events[_], T = arguments.length, R, h;
|
|
214
204
|
if (a.fn) {
|
|
215
|
-
switch (a.once && this.removeListener(r, a.fn, void 0, !0),
|
|
205
|
+
switch (a.once && this.removeListener(r, a.fn, void 0, !0), T) {
|
|
216
206
|
case 1:
|
|
217
207
|
return a.fn.call(a.context), !0;
|
|
218
208
|
case 2:
|
|
@@ -226,13 +216,13 @@ function Y() {
|
|
|
226
216
|
case 6:
|
|
227
217
|
return a.fn.call(a.context, o, i, E, d, f), !0;
|
|
228
218
|
}
|
|
229
|
-
for (h = 1,
|
|
230
|
-
|
|
231
|
-
a.fn.apply(a.context,
|
|
219
|
+
for (h = 1, R = new Array(T - 1); h < T; h++)
|
|
220
|
+
R[h - 1] = arguments[h];
|
|
221
|
+
a.fn.apply(a.context, R);
|
|
232
222
|
} else {
|
|
233
|
-
var
|
|
234
|
-
for (h = 0; h <
|
|
235
|
-
switch (a[h].once && this.removeListener(r, a[h].fn, void 0, !0),
|
|
223
|
+
var x = a.length, g;
|
|
224
|
+
for (h = 0; h < x; h++)
|
|
225
|
+
switch (a[h].once && this.removeListener(r, a[h].fn, void 0, !0), T) {
|
|
236
226
|
case 1:
|
|
237
227
|
a[h].fn.call(a[h].context);
|
|
238
228
|
break;
|
|
@@ -246,9 +236,9 @@ function Y() {
|
|
|
246
236
|
a[h].fn.call(a[h].context, o, i, E);
|
|
247
237
|
break;
|
|
248
238
|
default:
|
|
249
|
-
if (!
|
|
250
|
-
|
|
251
|
-
a[h].fn.apply(a[h].context,
|
|
239
|
+
if (!R) for (g = 1, R = new Array(T - 1); g < T; g++)
|
|
240
|
+
R[g - 1] = arguments[g];
|
|
241
|
+
a[h].fn.apply(a[h].context, R);
|
|
252
242
|
}
|
|
253
243
|
}
|
|
254
244
|
return !0;
|
|
@@ -260,27 +250,25 @@ function Y() {
|
|
|
260
250
|
var d = n ? n + r : r;
|
|
261
251
|
if (!this._events[d]) return this;
|
|
262
252
|
if (!o)
|
|
263
|
-
return
|
|
253
|
+
return w(this, d), this;
|
|
264
254
|
var f = this._events[d];
|
|
265
255
|
if (f.fn)
|
|
266
|
-
f.fn === o && (!E || f.once) && (!i || f.context === i) &&
|
|
256
|
+
f.fn === o && (!E || f.once) && (!i || f.context === i) && w(this, d);
|
|
267
257
|
else {
|
|
268
|
-
for (var
|
|
269
|
-
(f[
|
|
270
|
-
a.length ? this._events[d] = a.length === 1 ? a[0] : a :
|
|
258
|
+
for (var _ = 0, a = [], T = f.length; _ < T; _++)
|
|
259
|
+
(f[_].fn !== o || E && !f[_].once || i && f[_].context !== i) && a.push(f[_]);
|
|
260
|
+
a.length ? this._events[d] = a.length === 1 ? a[0] : a : w(this, d);
|
|
271
261
|
}
|
|
272
262
|
return this;
|
|
273
263
|
}, u.prototype.removeAllListeners = function(r) {
|
|
274
264
|
var o;
|
|
275
|
-
return r ? (o = n ? n + r : r, this._events[o] &&
|
|
265
|
+
return r ? (o = n ? n + r : r, this._events[o] && w(this, o)) : (this._events = new s(), this._eventsCount = 0), this;
|
|
276
266
|
}, u.prototype.off = u.prototype.removeListener, u.prototype.addListener = u.prototype.on, u.prefixed = n, u.EventEmitter = u, e.exports = u;
|
|
277
267
|
}(C)), C.exports;
|
|
278
268
|
}
|
|
279
|
-
|
|
280
|
-
class
|
|
281
|
-
|
|
282
|
-
w(this, "subscriptions", {});
|
|
283
|
-
}
|
|
269
|
+
q();
|
|
270
|
+
class $ {
|
|
271
|
+
subscriptions = {};
|
|
284
272
|
on(t) {
|
|
285
273
|
const { event: n, handler: s } = t;
|
|
286
274
|
return this.subscriptions[n] || (this.subscriptions[n] = []), this.subscriptions[n].push(s), t;
|
|
@@ -293,8 +281,14 @@ class z {
|
|
|
293
281
|
this.subscriptions[t] && this.subscriptions[t].forEach((s) => s(n));
|
|
294
282
|
}
|
|
295
283
|
}
|
|
296
|
-
var I = /* @__PURE__ */ (
|
|
297
|
-
|
|
284
|
+
var I = /* @__PURE__ */ function(e) {
|
|
285
|
+
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;
|
|
286
|
+
}({}), M = /* @__PURE__ */ function(e) {
|
|
287
|
+
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;
|
|
288
|
+
}({}), N = /* @__PURE__ */ function(e) {
|
|
289
|
+
return e.SWAP = "swap", e.LIMIT = "limit", e.ADVANCED = "advanced", e.YIELD = "yield", e;
|
|
290
|
+
}({});
|
|
291
|
+
const fe = [
|
|
298
292
|
"primary",
|
|
299
293
|
"background",
|
|
300
294
|
"paper",
|
|
@@ -304,22 +298,19 @@ const Ee = [
|
|
|
304
298
|
"alert",
|
|
305
299
|
"info",
|
|
306
300
|
"success"
|
|
307
|
-
],
|
|
308
|
-
class
|
|
301
|
+
], v = new L("cowSwapWidget");
|
|
302
|
+
class B {
|
|
303
|
+
contentWindow;
|
|
304
|
+
eventEmitter = new $();
|
|
305
|
+
listeners = [];
|
|
306
|
+
widgetListener;
|
|
309
307
|
constructor(t, n = []) {
|
|
310
|
-
|
|
311
|
-
w(this, "listeners", []);
|
|
312
|
-
w(this, "widgetListener");
|
|
313
|
-
this.contentWindow = t, this.updateListeners(n), this.widgetListener = m.listenToMessageFromWindow(
|
|
314
|
-
this.contentWindow,
|
|
315
|
-
I.EMIT_COW_EVENT,
|
|
316
|
-
(s) => this.eventEmitter.emit(s.event, s.payload)
|
|
317
|
-
);
|
|
308
|
+
this.contentWindow = t, this.updateListeners(n), this.widgetListener = v.listenToMessageFromWindow(this.contentWindow, I.EMIT_COW_EVENT, (s) => this.eventEmitter.emit(s.event, s.payload));
|
|
318
309
|
}
|
|
319
310
|
// TODO: Add proper return type annotation
|
|
320
311
|
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
321
312
|
stopListeningIframe() {
|
|
322
|
-
|
|
313
|
+
v.stopListeningWindowListener(this.contentWindow, this.widgetListener);
|
|
323
314
|
}
|
|
324
315
|
updateListeners(t) {
|
|
325
316
|
for (const n of this.listeners)
|
|
@@ -329,11 +320,13 @@ class J {
|
|
|
329
320
|
this.eventEmitter.on(n);
|
|
330
321
|
}
|
|
331
322
|
}
|
|
332
|
-
class
|
|
323
|
+
class Y {
|
|
324
|
+
appWindow;
|
|
325
|
+
iframeWidow;
|
|
326
|
+
forwardSdkMessage;
|
|
333
327
|
constructor(t, n) {
|
|
334
|
-
w(this, "forwardSdkMessage");
|
|
335
328
|
this.appWindow = t, this.iframeWidow = n, this.forwardSdkMessage = (s) => {
|
|
336
|
-
|
|
329
|
+
z(s.data) && s.origin !== window.location.origin && (J(s.data) ? this.appWindow.parent.postMessage(s.data, "*") : K(s.data) && this.iframeWidow.postMessage(s.data, "*"));
|
|
337
330
|
}, this.startListening();
|
|
338
331
|
}
|
|
339
332
|
// TODO: Add proper return type annotation
|
|
@@ -347,82 +340,85 @@ class K {
|
|
|
347
340
|
this.appWindow.removeEventListener("message", this.forwardSdkMessage);
|
|
348
341
|
}
|
|
349
342
|
}
|
|
350
|
-
function
|
|
343
|
+
function z(e) {
|
|
351
344
|
return typeof e == "object" && e !== null && "id" in e && typeof e.id == "string";
|
|
352
345
|
}
|
|
353
|
-
function
|
|
346
|
+
function J(e) {
|
|
354
347
|
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;
|
|
355
348
|
}
|
|
356
|
-
function
|
|
349
|
+
function K(e) {
|
|
357
350
|
return "success" in e && typeof e.success == "boolean" && "version" in e && typeof e.version == "string";
|
|
358
351
|
}
|
|
359
|
-
function
|
|
352
|
+
function X(e) {
|
|
360
353
|
return !!(e && typeof e == "object");
|
|
361
354
|
}
|
|
362
|
-
const
|
|
363
|
-
function
|
|
364
|
-
const t = typeof e.baseUrl == "string" ? e.baseUrl : "https://swap.cow.fi", n =
|
|
365
|
-
return t + "/#" + n + "?" +
|
|
355
|
+
const A = "_";
|
|
356
|
+
function Z(e) {
|
|
357
|
+
const t = typeof e.baseUrl == "string" ? e.baseUrl : "https://swap.cow.fi", n = V(e);
|
|
358
|
+
return t + "/#" + n + "?" + F(e);
|
|
366
359
|
}
|
|
367
|
-
function
|
|
368
|
-
const { chainId: t = 1, sell: n, buy: s, tradeType: p =
|
|
360
|
+
function V(e) {
|
|
361
|
+
const { chainId: t = 1, sell: n, buy: s, tradeType: p = N.SWAP } = e, l = [
|
|
362
|
+
n?.asset || A,
|
|
363
|
+
s?.asset || A
|
|
364
|
+
].map(encodeURIComponent).join("/");
|
|
369
365
|
return `/${t}/widget/${p}/${l}`;
|
|
370
366
|
}
|
|
371
|
-
function
|
|
367
|
+
function F(e) {
|
|
372
368
|
const t = new URLSearchParams();
|
|
373
|
-
return
|
|
369
|
+
return ne(te(ee(t, e), e), e);
|
|
374
370
|
}
|
|
375
|
-
function
|
|
371
|
+
function ee(e, t) {
|
|
376
372
|
const { sell: n, buy: s } = t;
|
|
377
|
-
return n
|
|
373
|
+
return n?.amount && e.append("sellAmount", n.amount), s?.amount && e.append("buyAmount", s.amount), e;
|
|
378
374
|
}
|
|
379
|
-
function
|
|
375
|
+
function te(e, t) {
|
|
380
376
|
const n = t.theme;
|
|
381
|
-
return n ? (
|
|
377
|
+
return n ? (X(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);
|
|
382
378
|
}
|
|
383
|
-
function
|
|
379
|
+
function ne(e, t) {
|
|
384
380
|
return t.targetChainId && e.append("targetChainId", t.targetChainId.toString()), e;
|
|
385
381
|
}
|
|
386
|
-
const
|
|
387
|
-
function
|
|
382
|
+
const U = "640px", se = "450px", re = 20;
|
|
383
|
+
function pe(e, t) {
|
|
388
384
|
const { params: n, provider: s, listeners: p } = t;
|
|
389
|
-
let l = s,
|
|
390
|
-
const u =
|
|
385
|
+
let l = s, w = n;
|
|
386
|
+
const u = oe(n);
|
|
391
387
|
e.innerHTML = "", e.appendChild(u);
|
|
392
388
|
const { contentWindow: c } = u;
|
|
393
389
|
if (!c)
|
|
394
390
|
throw console.error("Iframe does not contain a window", u), new Error("Iframe does not contain a window!");
|
|
395
391
|
const r = [];
|
|
396
|
-
r.push(
|
|
397
|
-
const o = new
|
|
398
|
-
let i =
|
|
399
|
-
u.addEventListener("load", () =>
|
|
400
|
-
const E = new
|
|
392
|
+
r.push(ie(c, n.appCode)), r.push(...ce(u, n.height, n.maxHeight)), r.push(ae());
|
|
393
|
+
const o = new B(window, p);
|
|
394
|
+
let i = S(c, null, l);
|
|
395
|
+
u.addEventListener("load", () => D(c, w, l));
|
|
396
|
+
const E = new Y(window, c);
|
|
401
397
|
return {
|
|
402
398
|
updateParams: (d) => {
|
|
403
|
-
|
|
399
|
+
w = d, D(c, w, l);
|
|
404
400
|
},
|
|
405
401
|
updateListeners: (d) => o.updateListeners(d),
|
|
406
402
|
updateProvider: (d) => {
|
|
407
|
-
l = d, i =
|
|
403
|
+
l = d, i = S(c, i, d);
|
|
408
404
|
},
|
|
409
405
|
destroy: () => {
|
|
410
406
|
i.disconnect(), o.stopListeningIframe(), r.forEach((d) => window.removeEventListener("message", d)), E.stopListening(), e.removeChild(u);
|
|
411
407
|
}
|
|
412
408
|
};
|
|
413
409
|
}
|
|
414
|
-
function
|
|
410
|
+
function S(e, t, n) {
|
|
415
411
|
t && t.disconnect();
|
|
416
|
-
const s = t || new
|
|
412
|
+
const s = t || new G(e);
|
|
417
413
|
return n && s.onConnect(n), s;
|
|
418
414
|
}
|
|
419
|
-
function
|
|
420
|
-
const { width: t =
|
|
421
|
-
return s.src =
|
|
415
|
+
function oe(e) {
|
|
416
|
+
const { width: t = se, height: n = U } = e, s = document.createElement("iframe");
|
|
417
|
+
return s.src = Z(e), s.width = t, s.height = n, s.style.border = "0", s.allow = "clipboard-read; clipboard-write", s;
|
|
422
418
|
}
|
|
423
|
-
function
|
|
424
|
-
const s = !!n, p =
|
|
425
|
-
|
|
419
|
+
function D(e, t, n) {
|
|
420
|
+
const s = !!n, p = V(t), l = F(t).toString(), { theme: w, ...u } = t;
|
|
421
|
+
v.postMessageToWindow(e, M.UPDATE_PARAMS, {
|
|
426
422
|
urlParams: {
|
|
427
423
|
pathname: p,
|
|
428
424
|
search: l
|
|
@@ -431,65 +427,73 @@ function L(e, t, n) {
|
|
|
431
427
|
hasProvider: s
|
|
432
428
|
});
|
|
433
429
|
}
|
|
434
|
-
function
|
|
435
|
-
return
|
|
436
|
-
|
|
437
|
-
metaData: t ? {
|
|
430
|
+
function ie(e, t) {
|
|
431
|
+
return v.listenToMessageFromWindow(window, I.ACTIVATE, () => {
|
|
432
|
+
v.postMessageToWindow(e, M.UPDATE_APP_DATA, {
|
|
433
|
+
metaData: t ? {
|
|
434
|
+
appCode: t
|
|
435
|
+
} : void 0
|
|
438
436
|
});
|
|
439
437
|
});
|
|
440
438
|
}
|
|
441
|
-
function
|
|
442
|
-
return
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
if (!s.startsWith("http") && s.match(/^[a-zA-Z0-9]+:\/\//)) {
|
|
448
|
-
window.open(s, n, t);
|
|
449
|
-
return;
|
|
450
|
-
}
|
|
439
|
+
function ae() {
|
|
440
|
+
return v.listenToMessageFromWindow(window, I.INTERCEPT_WINDOW_OPEN, ({ href: e, rel: t, target: n }) => {
|
|
441
|
+
const s = e.toString();
|
|
442
|
+
if (!s.startsWith("http") && s.match(/^[a-zA-Z0-9]+:\/\//)) {
|
|
443
|
+
window.open(s, n, t);
|
|
444
|
+
return;
|
|
451
445
|
}
|
|
452
|
-
);
|
|
446
|
+
});
|
|
453
447
|
}
|
|
454
|
-
function
|
|
448
|
+
function ce(e, t = U, n) {
|
|
455
449
|
return [
|
|
456
|
-
|
|
457
|
-
const p = s.height ? s.height +
|
|
450
|
+
v.listenToMessageFromWindow(window, I.UPDATE_HEIGHT, (s) => {
|
|
451
|
+
const p = s.height ? s.height + re : void 0;
|
|
458
452
|
e.style.height = p ? `${n ? Math.min(p, n) : p}px` : t;
|
|
459
453
|
}),
|
|
460
|
-
|
|
454
|
+
v.listenToMessageFromWindow(window, I.SET_FULL_HEIGHT, ({ isUpToSmall: s }) => {
|
|
461
455
|
e.style.height = s ? t : `${n || document.body.offsetHeight}px`;
|
|
462
456
|
})
|
|
463
457
|
];
|
|
464
458
|
}
|
|
465
|
-
const
|
|
466
|
-
function
|
|
467
|
-
if (
|
|
459
|
+
const ue = Object.values(N);
|
|
460
|
+
function he(e, t, n) {
|
|
461
|
+
if (O(e)) {
|
|
468
462
|
const s = e[n];
|
|
469
|
-
return
|
|
463
|
+
return W(s) ? s[t] : s;
|
|
470
464
|
}
|
|
471
|
-
if (
|
|
465
|
+
if (W(e)) {
|
|
472
466
|
const s = e[t];
|
|
473
|
-
return
|
|
467
|
+
return O(s) ? s[n] : s;
|
|
474
468
|
}
|
|
475
469
|
return e;
|
|
476
470
|
}
|
|
477
|
-
function
|
|
478
|
-
return typeof e != "object" ? !1 : Object.keys(e).every((t) =>
|
|
471
|
+
function O(e) {
|
|
472
|
+
return typeof e != "object" ? !1 : Object.keys(e).every((t) => ue.includes(t));
|
|
473
|
+
}
|
|
474
|
+
const de = /^\d+$/;
|
|
475
|
+
function W(e) {
|
|
476
|
+
return typeof e != "object" ? !1 : Object.keys(e).every((t) => de.test(t));
|
|
479
477
|
}
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
478
|
+
function le(e) {
|
|
479
|
+
return O(e) ? Object.values(e).flatMap((t) => W(t) ? Object.values(t) : [
|
|
480
|
+
t
|
|
481
|
+
]) : W(e) ? Object.values(e).flatMap((t) => O(t) ? Object.values(t) : [
|
|
482
|
+
t
|
|
483
|
+
]) : [
|
|
484
|
+
e
|
|
485
|
+
];
|
|
483
486
|
}
|
|
484
487
|
export {
|
|
485
|
-
|
|
486
|
-
|
|
488
|
+
N as TradeType,
|
|
489
|
+
fe as WIDGET_PALETTE_COLORS,
|
|
487
490
|
I as WidgetMethodsEmit,
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
491
|
+
M as WidgetMethodsListen,
|
|
492
|
+
pe as createCowSwapWidget,
|
|
493
|
+
X as isCowSwapWidgetPalette,
|
|
494
|
+
W as isPerNetworkConfig,
|
|
495
|
+
O as isPerTradeTypeConfig,
|
|
496
|
+
he as resolveFlexibleConfig,
|
|
497
|
+
le as resolveFlexibleConfigValues,
|
|
498
|
+
v as widgetIframeTransport
|
|
495
499
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cowprotocol/widget-lib",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.22.1",
|
|
4
4
|
"type": "commonjs",
|
|
5
5
|
"description": "CoW Swap Widget Library. Allows you to easily embed a CoW Swap widget on your website.",
|
|
6
6
|
"main": "index.js",
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"widget-lib"
|
|
22
22
|
],
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@cowprotocol/events": "^1.
|
|
24
|
+
"@cowprotocol/events": "^2.1.0",
|
|
25
25
|
"@cowprotocol/iframe-transport": "^1.1.1"
|
|
26
26
|
}
|
|
27
27
|
}
|
package/types.d.ts
CHANGED
|
@@ -41,11 +41,6 @@ export interface EthereumProvider {
|
|
|
41
41
|
* @returns A promise that resolves with the response.
|
|
42
42
|
*/
|
|
43
43
|
request<T>(params: JsonRpcRequest): Promise<T>;
|
|
44
|
-
/**
|
|
45
|
-
* Requests permission to connect to the Ethereum provider.
|
|
46
|
-
* @returns A promise that resolves once permission is granted.
|
|
47
|
-
*/
|
|
48
|
-
enable(): Promise<void>;
|
|
49
44
|
}
|
|
50
45
|
export type CowSwapTheme = 'dark' | 'light';
|
|
51
46
|
/**
|
|
@@ -134,6 +129,15 @@ export interface CowSwapWidgetContent {
|
|
|
134
129
|
feeLabel?: string;
|
|
135
130
|
feeTooltipMarkdown?: string;
|
|
136
131
|
}
|
|
132
|
+
export interface SlippageConfig {
|
|
133
|
+
/** Minimum slippage in basis points (e.g., 10 = 0.1%) */
|
|
134
|
+
min?: number;
|
|
135
|
+
/** Maximum slippage in basis points (e.g., 5000 = 50%) - 5000 is max value for dApp */
|
|
136
|
+
max?: number;
|
|
137
|
+
/** Default slippage value in basis points (e.g., 50 = 0.5%) - min <= defaultValue <= max */
|
|
138
|
+
defaultValue?: number;
|
|
139
|
+
}
|
|
140
|
+
export type FlexibleSlippageConfig = FlexibleConfig<SlippageConfig>;
|
|
137
141
|
export interface CowSwapWidgetParams {
|
|
138
142
|
/**
|
|
139
143
|
* The unique identifier of the widget consumer.
|
|
@@ -200,6 +204,11 @@ export interface CowSwapWidgetParams {
|
|
|
200
204
|
* Please contact https://cowprotocol.typeform.com/to/rONXaxHV
|
|
201
205
|
*/
|
|
202
206
|
partnerFee?: PartnerFee;
|
|
207
|
+
/**
|
|
208
|
+
* Disables cross-chain swaps (bridging)
|
|
209
|
+
* Defaults to false.
|
|
210
|
+
*/
|
|
211
|
+
disableCrossChainSwap?: boolean;
|
|
203
212
|
/**
|
|
204
213
|
* Disables showing the confirmation modal you get after posting an order.
|
|
205
214
|
* Defaults to false.
|
|
@@ -265,6 +274,10 @@ export interface CowSwapWidgetParams {
|
|
|
265
274
|
* Customizable labels and content for the widget.
|
|
266
275
|
*/
|
|
267
276
|
content?: CowSwapWidgetContent;
|
|
277
|
+
/**
|
|
278
|
+
* Customizable slippage settings for the widget.
|
|
279
|
+
*/
|
|
280
|
+
slippage?: FlexibleSlippageConfig;
|
|
268
281
|
}
|
|
269
282
|
export interface WidgetMethodsEmitPayloadMap {
|
|
270
283
|
[WidgetMethodsEmit.ACTIVATE]: void;
|