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