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