@cowprotocol/widget-react 0.12.0 → 0.13.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 +126 -128
- 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 T=(e,n,t)=>(j(e,typeof n!="symbol"?n+"":n,t),t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const I=require("react/jsx-runtime"),g=require("react");class U{constructor(n){this.key=n}postMessageToWindow(n,t,r){const h=typeof r=="object"?r:{},l={key:this.key,method:t,...h};n.postMessage(l,"*")}listenToMessageFromWindow(n,t,r){const h=l=>{!Q(l.data)||l.data.key!==this.key||l.data.method!==t||r(l.data)};return n.addEventListener("message",h),h}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){T(this,"ethereumProvider",null);T(this,"requestWaitingForConnection",{});T(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:h}=n;if(!this.ethereumProvider||!t)return;(h==="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 h(c,o,s){this.fn=c,this.context=o,this.once=s||!1}function l(c,o,s,i,d){if(typeof s!="function")throw new TypeError("The listener must be a function");var u=new h(s,i||c,d),p=t?t+o:o;return c._events[p]?c._events[p].fn?c._events[p]=[c._events[p],u]:c._events[p].push(u):(c._events[p]=u,c._eventsCount++),c}function w(c,o){--c._eventsCount===0?c._events=new r:delete c._events[o]}function f(){this._events=new r,this._eventsCount=0}f.prototype.eventNames=function(){var o=[],s,i;if(this._eventsCount===0)return o;for(i in s=this._events)n.call(s,i)&&o.push(t?i.slice(1):i);return Object.getOwnPropertySymbols?o.concat(Object.getOwnPropertySymbols(s)):o},f.prototype.listeners=function(o){var s=t?t+o:o,i=this._events[s];if(!i)return[];if(i.fn)return[i.fn];for(var d=0,u=i.length,p=new Array(u);d<u;d++)p[d]=i[d].fn;return p},f.prototype.listenerCount=function(o){var s=t?t+o:o,i=this._events[s];return i?i.fn?1:i.length:0},f.prototype.emit=function(o,s,i,d,u,p){var m=t?t+o:o;if(!this._events[m])return!1;var a=this._events[m],v=arguments.length,R,E;if(a.fn){switch(a.once&&this.removeListener(o,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,i),!0;case 4:return a.fn.call(a.context,s,i,d),!0;case 5:return a.fn.call(a.context,s,i,d,u),!0;case 6:return a.fn.call(a.context,s,i,d,u,p),!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(o,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,i);break;case 4:a[E].fn.call(a[E].context,s,i,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(o,s,i){return l(this,o,s,i,!1)},f.prototype.once=function(o,s,i){return l(this,o,s,i,!0)},f.prototype.removeListener=function(o,s,i,d){var u=t?t+o:o;if(!this._events[u])return this;if(!s)return w(this,u),this;var p=this._events[u];if(p.fn)p.fn===s&&(!d||p.once)&&(!i||p.context===i)&&w(this,u);else{for(var m=0,a=[],v=p.length;m<v;m++)(p[m].fn!==s||d&&!p[m].once||i&&p[m].context!==i)&&a.push(p[m]);a.length?this._events[u]=a.length===1?a[0]:a:w(this,u)}return this},f.prototype.removeAllListeners=function(o){var s;return o?(s=t?t+o:o,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(){T(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(h=>h!==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||{}),W=(e=>(e.SWAP="swap",e.LIMIT="limit",e.ADVANCED="advanced",e))(W||{});const z=["primary","background","paper","text","danger","warning","alert","info","success"],_=new U("cowSwapWidget");class K{constructor(n,t=[]){T(this,"eventEmitter",new J);T(this,"listeners",[]);T(this,"widgetListener");this.contentWindow=n,this.updateListeners(t),this.widgetListener=_.listenToMessageFromWindow(this.contentWindow,P.EMIT_COW_EVENT,r=>this.eventEmitter.emit(r.event,r.payload))}stopListeningIframe(){_.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 X{constructor(n,t){T(this,"forwardSdkMessage");this.appWindow=n,this.iframeWidow=t,this.forwardSdkMessage=r=>{Y(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 Y(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:h=W.SWAP}=e,l=[(t==null?void 0:t.asset)||b,(r==null?void 0:r.asset)||b].map(encodeURIComponent).join("/");return`/${n}/widget/${h}/${l}`}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:h}=n;let l=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 o=[];o.push(ae(c,t.appCode)),o.push(...ue(f,t.height)),o.push(ce());const s=new K(window,h);let i=N(c,null,l);f.addEventListener("load",()=>M(c,w,l));const d=new X(window,c);return{updateParams:u=>{w=u,M(c,w,l)},updateListeners:u=>s.updateListeners(u),updateProvider:u=>{l=u,i=N(c,i,u)},destroy:()=>{i.disconnect(),s.stopListeningIframe(),o.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,h=x(n),l=F(n).toString(),{theme:w,...f}=n;_.postMessageToWindow(e,S.UPDATE_PARAMS,{urlParams:{pathname:h,search:l},appParams:f,hasProvider:r})}function ae(e,n){return _.listenToMessageFromWindow(window,P.ACTIVATE,()=>{_.postMessageToWindow(e,S.UPDATE_APP_DATA,{metaData:n?{appCode:n}:void 0})})}function ce(){return _.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){return[_.listenToMessageFromWindow(window,P.UPDATE_HEIGHT,t=>{e.style.height=t.height?`${t.height+oe}px`:n}),_.listenToMessageFromWindow(window,P.SET_FULL_HEIGHT,({isUpToSmall:t})=>{e.style.height=t?n:`${document.body.offsetHeight}px`})]}const fe=Object.values(W);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,[h,l]=g.useState(null),w=g.useRef(null),f=g.useRef(t),c=g.useRef(r),o=g.useRef(null),s=g.useRef(null),i=g.useCallback((d,u)=>{try{console.log(`[WIDGET] ${d}`),u()}catch(p){const m=`Error ${d.toLowerCase()}`;console.error(`[WIDGET] ${m}`,p),l({message:m,error:p})}},[l]);return g.useEffect(()=>()=>{w.current=null,f.current=void 0,c.current=void 0;const d=s.current;d&&(i("💥 Destroy widget",()=>d.destroy()),s.current=null)},[]),g.useEffect(()=>{if(!o.current||JSON.stringify(w.current)===JSON.stringify(n))return;const d=o.current,u=s.current;w.current=n,u===null?i("Creating a new widget",()=>{s.current=L(d,{params:n,provider:f.current,listeners:r}),c.current=r}):i("Updating the widget",()=>u.updateParams(n))},[n]),g.useEffect(()=>{if(!s.current||f.current===t)return;f.current=t;const d=o.current;d&&i("Updating the provider",()=>{var u;(u=s.current)==null||u.destroy(),s.current=L(d,{params:n,provider:f.current,listeners:r})})},[t]),g.useEffect(()=>{if(!s.current||c.current===r)return;const d=s.current;i("Updating the listeners",()=>d.updateListeners(r))},[r]),h?I.jsxs("div",{style:{color:"#ff3a3a"},children:[h.message,h.error.message&&I.jsx("pre",{style:{whiteSpace:"pre-wrap",fontSize:"0.75em"},children:h.error.message})]}):I.jsx("div",{ref:o,style:{width:"100%"}})}exports.CowSwapWidget=le;exports.TradeType=W;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=_;
|
|
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;
|
package/index.mjs
CHANGED
|
@@ -8,22 +8,22 @@ class M {
|
|
|
8
8
|
this.key = n;
|
|
9
9
|
}
|
|
10
10
|
postMessageToWindow(n, t, r) {
|
|
11
|
-
const
|
|
11
|
+
const p = typeof r == "object" ? r : {}, h = {
|
|
12
12
|
key: this.key,
|
|
13
13
|
method: t,
|
|
14
|
-
...
|
|
14
|
+
...p
|
|
15
15
|
};
|
|
16
16
|
n.postMessage(
|
|
17
|
-
|
|
17
|
+
h,
|
|
18
18
|
"*"
|
|
19
19
|
// TODO: Change to CoW specific origin in production. https://github.com/cowprotocol/cowswap/issues/3828
|
|
20
20
|
);
|
|
21
21
|
}
|
|
22
22
|
listenToMessageFromWindow(n, t, r) {
|
|
23
|
-
const
|
|
24
|
-
!q(
|
|
23
|
+
const p = (h) => {
|
|
24
|
+
!q(h.data) || h.data.key !== this.key || h.data.method !== t || r(h.data);
|
|
25
25
|
};
|
|
26
|
-
return n.addEventListener("message",
|
|
26
|
+
return n.addEventListener("message", p), p;
|
|
27
27
|
}
|
|
28
28
|
stopListeningToMessageFromWindow(n, t, r) {
|
|
29
29
|
n.removeEventListener("message", r);
|
|
@@ -95,10 +95,10 @@ class z {
|
|
|
95
95
|
* @param request - The JSON-RPC request to be processed.
|
|
96
96
|
*/
|
|
97
97
|
processRpcRequest(n) {
|
|
98
|
-
const { id: t, jsonrpc: r, method:
|
|
98
|
+
const { id: t, jsonrpc: r, method: p } = n;
|
|
99
99
|
if (!this.ethereumProvider || !t)
|
|
100
100
|
return;
|
|
101
|
-
(
|
|
101
|
+
(p === "enable" ? this.ethereumProvider.enable() : this.ethereumProvider.request({ ...n, id: t })).then(
|
|
102
102
|
(w) => this.forwardRpcResponseToIframe({
|
|
103
103
|
rpcResponse: { jsonrpc: r, id: t, result: w }
|
|
104
104
|
})
|
|
@@ -125,73 +125,73 @@ class z {
|
|
|
125
125
|
);
|
|
126
126
|
}
|
|
127
127
|
}
|
|
128
|
-
var
|
|
128
|
+
var Y = { exports: {} };
|
|
129
129
|
(function(e) {
|
|
130
130
|
var n = Object.prototype.hasOwnProperty, t = "~";
|
|
131
131
|
function r() {
|
|
132
132
|
}
|
|
133
133
|
Object.create && (r.prototype = /* @__PURE__ */ Object.create(null), new r().__proto__ || (t = !1));
|
|
134
|
-
function
|
|
135
|
-
this.fn = c, this.context =
|
|
134
|
+
function p(c, i, s) {
|
|
135
|
+
this.fn = c, this.context = i, this.once = s || !1;
|
|
136
136
|
}
|
|
137
|
-
function
|
|
137
|
+
function h(c, i, s, o, d) {
|
|
138
138
|
if (typeof s != "function")
|
|
139
139
|
throw new TypeError("The listener must be a function");
|
|
140
|
-
var u = new
|
|
141
|
-
return c._events[
|
|
140
|
+
var u = new p(s, o || c, d), l = t ? t + i : i;
|
|
141
|
+
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;
|
|
142
142
|
}
|
|
143
|
-
function w(c,
|
|
144
|
-
--c._eventsCount === 0 ? c._events = new r() : delete c._events[
|
|
143
|
+
function w(c, i) {
|
|
144
|
+
--c._eventsCount === 0 ? c._events = new r() : delete c._events[i];
|
|
145
145
|
}
|
|
146
146
|
function f() {
|
|
147
147
|
this._events = new r(), this._eventsCount = 0;
|
|
148
148
|
}
|
|
149
149
|
f.prototype.eventNames = function() {
|
|
150
|
-
var
|
|
150
|
+
var i = [], s, o;
|
|
151
151
|
if (this._eventsCount === 0)
|
|
152
|
-
return
|
|
153
|
-
for (
|
|
154
|
-
n.call(s,
|
|
155
|
-
return Object.getOwnPropertySymbols ?
|
|
156
|
-
}, f.prototype.listeners = function(
|
|
157
|
-
var s = t ? t +
|
|
158
|
-
if (!
|
|
152
|
+
return i;
|
|
153
|
+
for (o in s = this._events)
|
|
154
|
+
n.call(s, o) && i.push(t ? o.slice(1) : o);
|
|
155
|
+
return Object.getOwnPropertySymbols ? i.concat(Object.getOwnPropertySymbols(s)) : i;
|
|
156
|
+
}, f.prototype.listeners = function(i) {
|
|
157
|
+
var s = t ? t + i : i, o = this._events[s];
|
|
158
|
+
if (!o)
|
|
159
159
|
return [];
|
|
160
|
-
if (
|
|
161
|
-
return [
|
|
162
|
-
for (var d = 0, u =
|
|
163
|
-
|
|
164
|
-
return
|
|
165
|
-
}, f.prototype.listenerCount = function(
|
|
166
|
-
var s = t ? t +
|
|
167
|
-
return
|
|
168
|
-
}, f.prototype.emit = function(
|
|
169
|
-
var m = t ? t +
|
|
160
|
+
if (o.fn)
|
|
161
|
+
return [o.fn];
|
|
162
|
+
for (var d = 0, u = o.length, l = new Array(u); d < u; d++)
|
|
163
|
+
l[d] = o[d].fn;
|
|
164
|
+
return l;
|
|
165
|
+
}, f.prototype.listenerCount = function(i) {
|
|
166
|
+
var s = t ? t + i : i, o = this._events[s];
|
|
167
|
+
return o ? o.fn ? 1 : o.length : 0;
|
|
168
|
+
}, f.prototype.emit = function(i, s, o, d, u, l) {
|
|
169
|
+
var m = t ? t + i : i;
|
|
170
170
|
if (!this._events[m])
|
|
171
171
|
return !1;
|
|
172
|
-
var a = this._events[m], v = arguments.length,
|
|
172
|
+
var a = this._events[m], v = arguments.length, T, E;
|
|
173
173
|
if (a.fn) {
|
|
174
|
-
switch (a.once && this.removeListener(
|
|
174
|
+
switch (a.once && this.removeListener(i, a.fn, void 0, !0), v) {
|
|
175
175
|
case 1:
|
|
176
176
|
return a.fn.call(a.context), !0;
|
|
177
177
|
case 2:
|
|
178
178
|
return a.fn.call(a.context, s), !0;
|
|
179
179
|
case 3:
|
|
180
|
-
return a.fn.call(a.context, s,
|
|
180
|
+
return a.fn.call(a.context, s, o), !0;
|
|
181
181
|
case 4:
|
|
182
|
-
return a.fn.call(a.context, s,
|
|
182
|
+
return a.fn.call(a.context, s, o, d), !0;
|
|
183
183
|
case 5:
|
|
184
|
-
return a.fn.call(a.context, s,
|
|
184
|
+
return a.fn.call(a.context, s, o, d, u), !0;
|
|
185
185
|
case 6:
|
|
186
|
-
return a.fn.call(a.context, s,
|
|
186
|
+
return a.fn.call(a.context, s, o, d, u, l), !0;
|
|
187
187
|
}
|
|
188
|
-
for (E = 1,
|
|
189
|
-
|
|
190
|
-
a.fn.apply(a.context,
|
|
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
191
|
} else {
|
|
192
192
|
var H = a.length, P;
|
|
193
193
|
for (E = 0; E < H; E++)
|
|
194
|
-
switch (a[E].once && this.removeListener(
|
|
194
|
+
switch (a[E].once && this.removeListener(i, a[E].fn, void 0, !0), v) {
|
|
195
195
|
case 1:
|
|
196
196
|
a[E].fn.call(a[E].context);
|
|
197
197
|
break;
|
|
@@ -199,44 +199,44 @@ var K = { exports: {} };
|
|
|
199
199
|
a[E].fn.call(a[E].context, s);
|
|
200
200
|
break;
|
|
201
201
|
case 3:
|
|
202
|
-
a[E].fn.call(a[E].context, s,
|
|
202
|
+
a[E].fn.call(a[E].context, s, o);
|
|
203
203
|
break;
|
|
204
204
|
case 4:
|
|
205
|
-
a[E].fn.call(a[E].context, s,
|
|
205
|
+
a[E].fn.call(a[E].context, s, o, d);
|
|
206
206
|
break;
|
|
207
207
|
default:
|
|
208
|
-
if (!
|
|
209
|
-
for (P = 1,
|
|
210
|
-
|
|
211
|
-
a[E].fn.apply(a[E].context,
|
|
208
|
+
if (!T)
|
|
209
|
+
for (P = 1, T = new Array(v - 1); P < v; P++)
|
|
210
|
+
T[P - 1] = arguments[P];
|
|
211
|
+
a[E].fn.apply(a[E].context, T);
|
|
212
212
|
}
|
|
213
213
|
}
|
|
214
214
|
return !0;
|
|
215
|
-
}, f.prototype.on = function(
|
|
216
|
-
return
|
|
217
|
-
}, f.prototype.once = function(
|
|
218
|
-
return
|
|
219
|
-
}, f.prototype.removeListener = function(
|
|
220
|
-
var u = t ? t +
|
|
215
|
+
}, f.prototype.on = function(i, s, o) {
|
|
216
|
+
return h(this, i, s, o, !1);
|
|
217
|
+
}, f.prototype.once = function(i, s, o) {
|
|
218
|
+
return h(this, i, s, o, !0);
|
|
219
|
+
}, f.prototype.removeListener = function(i, s, o, d) {
|
|
220
|
+
var u = t ? t + i : i;
|
|
221
221
|
if (!this._events[u])
|
|
222
222
|
return this;
|
|
223
223
|
if (!s)
|
|
224
224
|
return w(this, u), this;
|
|
225
|
-
var
|
|
226
|
-
if (
|
|
227
|
-
|
|
225
|
+
var l = this._events[u];
|
|
226
|
+
if (l.fn)
|
|
227
|
+
l.fn === s && (!d || l.once) && (!o || l.context === o) && w(this, u);
|
|
228
228
|
else {
|
|
229
|
-
for (var m = 0, a = [], v =
|
|
230
|
-
(
|
|
229
|
+
for (var m = 0, a = [], v = l.length; m < v; m++)
|
|
230
|
+
(l[m].fn !== s || d && !l[m].once || o && l[m].context !== o) && a.push(l[m]);
|
|
231
231
|
a.length ? this._events[u] = a.length === 1 ? a[0] : a : w(this, u);
|
|
232
232
|
}
|
|
233
233
|
return this;
|
|
234
|
-
}, f.prototype.removeAllListeners = function(
|
|
234
|
+
}, f.prototype.removeAllListeners = function(i) {
|
|
235
235
|
var s;
|
|
236
|
-
return
|
|
236
|
+
return i ? (s = t ? t + i : i, this._events[s] && w(this, s)) : (this._events = new r(), this._eventsCount = 0), this;
|
|
237
237
|
}, f.prototype.off = f.prototype.removeListener, f.prototype.addListener = f.prototype.on, f.prefixed = t, f.EventEmitter = f, e.exports = f;
|
|
238
|
-
})(
|
|
239
|
-
class
|
|
238
|
+
})(Y);
|
|
239
|
+
class K {
|
|
240
240
|
constructor() {
|
|
241
241
|
_(this, "subscriptions", {});
|
|
242
242
|
}
|
|
@@ -246,13 +246,13 @@ class X {
|
|
|
246
246
|
}
|
|
247
247
|
off(n) {
|
|
248
248
|
const { event: t, handler: r } = n;
|
|
249
|
-
return this.subscriptions[t] && (this.subscriptions[t] = this.subscriptions[t].filter((
|
|
249
|
+
return this.subscriptions[t] && (this.subscriptions[t] = this.subscriptions[t].filter((p) => p !== r)), n;
|
|
250
250
|
}
|
|
251
251
|
emit(n, t) {
|
|
252
252
|
this.subscriptions[n] && this.subscriptions[n].forEach((r) => r(t));
|
|
253
253
|
}
|
|
254
254
|
}
|
|
255
|
-
var R = /* @__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))(R || {}), S = /* @__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))(S || {}), W = /* @__PURE__ */ ((e) => (e.SWAP = "swap", e.LIMIT = "limit", e.ADVANCED = "advanced", e))(W || {});
|
|
255
|
+
var R = /* @__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))(R || {}), S = /* @__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))(S || {}), W = /* @__PURE__ */ ((e) => (e.SWAP = "swap", e.LIMIT = "limit", e.ADVANCED = "advanced", e.YIELD = "yield", e))(W || {});
|
|
256
256
|
const _e = [
|
|
257
257
|
"primary",
|
|
258
258
|
"background",
|
|
@@ -263,20 +263,20 @@ const _e = [
|
|
|
263
263
|
"alert",
|
|
264
264
|
"info",
|
|
265
265
|
"success"
|
|
266
|
-
],
|
|
267
|
-
class
|
|
266
|
+
], g = new M("cowSwapWidget");
|
|
267
|
+
class X {
|
|
268
268
|
constructor(n, t = []) {
|
|
269
|
-
_(this, "eventEmitter", new
|
|
269
|
+
_(this, "eventEmitter", new K());
|
|
270
270
|
_(this, "listeners", []);
|
|
271
271
|
_(this, "widgetListener");
|
|
272
|
-
this.contentWindow = n, this.updateListeners(t), this.widgetListener =
|
|
272
|
+
this.contentWindow = n, this.updateListeners(t), this.widgetListener = g.listenToMessageFromWindow(
|
|
273
273
|
this.contentWindow,
|
|
274
274
|
R.EMIT_COW_EVENT,
|
|
275
275
|
(r) => this.eventEmitter.emit(r.event, r.payload)
|
|
276
276
|
);
|
|
277
277
|
}
|
|
278
278
|
stopListeningIframe() {
|
|
279
|
-
|
|
279
|
+
g.stopListeningWindowListener(this.contentWindow, this.widgetListener);
|
|
280
280
|
}
|
|
281
281
|
updateListeners(n) {
|
|
282
282
|
for (const t of this.listeners)
|
|
@@ -314,14 +314,14 @@ function re(e) {
|
|
|
314
314
|
}
|
|
315
315
|
const L = "_";
|
|
316
316
|
function se(e) {
|
|
317
|
-
const n = typeof e.baseUrl == "string" ? e.baseUrl : "https://swap.cow.fi", t =
|
|
318
|
-
return n + "/#" + t + "?" +
|
|
319
|
-
}
|
|
320
|
-
function x(e) {
|
|
321
|
-
const { chainId: n = 1, sell: t, buy: r, tradeType: h = W.SWAP } = e, l = [(t == null ? void 0 : t.asset) || L, (r == null ? void 0 : r.asset) || L].map(encodeURIComponent).join("/");
|
|
322
|
-
return `/${n}/widget/${h}/${l}`;
|
|
317
|
+
const n = typeof e.baseUrl == "string" ? e.baseUrl : "https://swap.cow.fi", t = F(e);
|
|
318
|
+
return n + "/#" + t + "?" + x(e);
|
|
323
319
|
}
|
|
324
320
|
function F(e) {
|
|
321
|
+
const { chainId: n = 1, sell: t, buy: r, tradeType: p = W.SWAP } = e, h = [(t == null ? void 0 : t.asset) || L, (r == null ? void 0 : r.asset) || L].map(encodeURIComponent).join("/");
|
|
322
|
+
return `/${n}/widget/${p}/${h}`;
|
|
323
|
+
}
|
|
324
|
+
function x(e) {
|
|
325
325
|
const n = new URLSearchParams();
|
|
326
326
|
return ie(oe(n, e), e);
|
|
327
327
|
}
|
|
@@ -335,29 +335,29 @@ function ie(e, n) {
|
|
|
335
335
|
}
|
|
336
336
|
const k = "640px", ae = "450px", ce = 20;
|
|
337
337
|
function D(e, n) {
|
|
338
|
-
const { params: t, provider: r, listeners:
|
|
339
|
-
let
|
|
338
|
+
const { params: t, provider: r, listeners: p } = n;
|
|
339
|
+
let h = r, w = t;
|
|
340
340
|
const f = ue(t);
|
|
341
341
|
e.innerHTML = "", e.appendChild(f);
|
|
342
342
|
const { contentWindow: c } = f;
|
|
343
343
|
if (!c)
|
|
344
344
|
throw console.error("Iframe does not contain a window", f), new Error("Iframe does not contain a window!");
|
|
345
|
-
const
|
|
346
|
-
|
|
347
|
-
const s = new
|
|
348
|
-
let
|
|
349
|
-
f.addEventListener("load", () => N(c, w,
|
|
345
|
+
const i = [];
|
|
346
|
+
i.push(fe(c, t.appCode)), i.push(...pe(f, t.height, t.maxHeight)), i.push(de());
|
|
347
|
+
const s = new X(window, p);
|
|
348
|
+
let o = b(c, null, h);
|
|
349
|
+
f.addEventListener("load", () => N(c, w, h));
|
|
350
350
|
const d = new Z(window, c);
|
|
351
351
|
return {
|
|
352
352
|
updateParams: (u) => {
|
|
353
|
-
w = u, N(c, w,
|
|
353
|
+
w = u, N(c, w, h);
|
|
354
354
|
},
|
|
355
355
|
updateListeners: (u) => s.updateListeners(u),
|
|
356
356
|
updateProvider: (u) => {
|
|
357
|
-
|
|
357
|
+
h = u, o = b(c, o, u);
|
|
358
358
|
},
|
|
359
359
|
destroy: () => {
|
|
360
|
-
|
|
360
|
+
o.disconnect(), s.stopListeningIframe(), i.forEach((u) => window.removeEventListener("message", u)), d.stopListening(), e.removeChild(f);
|
|
361
361
|
}
|
|
362
362
|
};
|
|
363
363
|
}
|
|
@@ -371,25 +371,25 @@ function ue(e) {
|
|
|
371
371
|
return r.src = se(e), r.width = n, r.height = t, r.style.border = "0", r.allow = "clipboard-read; clipboard-write", r;
|
|
372
372
|
}
|
|
373
373
|
function N(e, n, t) {
|
|
374
|
-
const r = !!t,
|
|
375
|
-
|
|
374
|
+
const r = !!t, p = F(n), h = x(n).toString(), { theme: w, ...f } = n;
|
|
375
|
+
g.postMessageToWindow(e, S.UPDATE_PARAMS, {
|
|
376
376
|
urlParams: {
|
|
377
|
-
pathname:
|
|
378
|
-
search:
|
|
377
|
+
pathname: p,
|
|
378
|
+
search: h
|
|
379
379
|
},
|
|
380
380
|
appParams: f,
|
|
381
381
|
hasProvider: r
|
|
382
382
|
});
|
|
383
383
|
}
|
|
384
384
|
function fe(e, n) {
|
|
385
|
-
return
|
|
386
|
-
|
|
385
|
+
return g.listenToMessageFromWindow(window, R.ACTIVATE, () => {
|
|
386
|
+
g.postMessageToWindow(e, S.UPDATE_APP_DATA, {
|
|
387
387
|
metaData: n ? { appCode: n } : void 0
|
|
388
388
|
});
|
|
389
389
|
});
|
|
390
390
|
}
|
|
391
391
|
function de() {
|
|
392
|
-
return
|
|
392
|
+
return g.listenToMessageFromWindow(
|
|
393
393
|
window,
|
|
394
394
|
R.INTERCEPT_WINDOW_OPEN,
|
|
395
395
|
({ href: e, rel: n, target: t }) => {
|
|
@@ -401,13 +401,14 @@ function de() {
|
|
|
401
401
|
}
|
|
402
402
|
);
|
|
403
403
|
}
|
|
404
|
-
function pe(e, n = k) {
|
|
404
|
+
function pe(e, n = k, t) {
|
|
405
405
|
return [
|
|
406
|
-
|
|
407
|
-
|
|
406
|
+
g.listenToMessageFromWindow(window, R.UPDATE_HEIGHT, (r) => {
|
|
407
|
+
const p = r.height ? r.height + ce : void 0;
|
|
408
|
+
e.style.height = p ? `${t ? Math.min(p, t) : p}px` : n;
|
|
408
409
|
}),
|
|
409
|
-
|
|
410
|
-
e.style.height =
|
|
410
|
+
g.listenToMessageFromWindow(window, R.SET_FULL_HEIGHT, ({ isUpToSmall: r }) => {
|
|
411
|
+
e.style.height = r ? n : `${t || document.body.offsetHeight}px`;
|
|
411
412
|
})
|
|
412
413
|
];
|
|
413
414
|
}
|
|
@@ -430,50 +431,47 @@ const he = /^\d+$/;
|
|
|
430
431
|
function V(e) {
|
|
431
432
|
return typeof e != "object" ? !1 : Object.keys(e).every((n) => typeof n == "number" || he.test(n));
|
|
432
433
|
}
|
|
433
|
-
function
|
|
434
|
-
const { params: n, provider: t, listeners: r } = e, [
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
},
|
|
443
|
-
[l]
|
|
444
|
-
);
|
|
434
|
+
function ge(e) {
|
|
435
|
+
const { params: n, provider: t, listeners: r } = e, [p, h] = j(null), w = y(null), f = y(t), c = y(r), i = y(null), s = y(null), o = B((d, u) => {
|
|
436
|
+
try {
|
|
437
|
+
console.log(`[WIDGET] ${d}`), u();
|
|
438
|
+
} catch (l) {
|
|
439
|
+
const m = `Error ${d.toLowerCase()}`;
|
|
440
|
+
console.error(`[WIDGET] ${m}`, l), h({ message: m, error: l });
|
|
441
|
+
}
|
|
442
|
+
}, []);
|
|
445
443
|
return O(() => () => {
|
|
446
444
|
w.current = null, f.current = void 0, c.current = void 0;
|
|
447
445
|
const d = s.current;
|
|
448
|
-
d && (
|
|
446
|
+
d && (o("💥 Destroy widget", () => d.destroy()), s.current = null);
|
|
449
447
|
}, []), O(() => {
|
|
450
|
-
if (!
|
|
448
|
+
if (!i.current || JSON.stringify(w.current) === JSON.stringify(n))
|
|
451
449
|
return;
|
|
452
|
-
const d =
|
|
453
|
-
w.current = n, u === null ?
|
|
450
|
+
const d = i.current, u = s.current;
|
|
451
|
+
w.current = n, u === null ? o("Creating a new widget", () => {
|
|
454
452
|
s.current = D(d, { params: n, provider: f.current, listeners: r }), c.current = r;
|
|
455
|
-
}) :
|
|
456
|
-
}, [n]), O(() => {
|
|
453
|
+
}) : o("Updating the widget", () => u.updateParams(n));
|
|
454
|
+
}, [n, o]), O(() => {
|
|
457
455
|
if (!s.current || f.current === t)
|
|
458
456
|
return;
|
|
459
457
|
f.current = t;
|
|
460
|
-
const d =
|
|
461
|
-
d &&
|
|
458
|
+
const d = i.current;
|
|
459
|
+
d && o("Updating the provider", () => {
|
|
462
460
|
var u;
|
|
463
461
|
(u = s.current) == null || u.destroy(), s.current = D(d, { params: n, provider: f.current, listeners: r });
|
|
464
462
|
});
|
|
465
|
-
}, [t]), O(() => {
|
|
463
|
+
}, [t, o]), O(() => {
|
|
466
464
|
if (!s.current || c.current === r)
|
|
467
465
|
return;
|
|
468
466
|
const d = s.current;
|
|
469
|
-
|
|
470
|
-
}, [r]),
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
] }) : /* @__PURE__ */ A("div", { ref:
|
|
467
|
+
o("Updating the listeners", () => d.updateListeners(r));
|
|
468
|
+
}, [r, o]), p ? /* @__PURE__ */ $("div", { style: { color: "#ff3a3a" }, children: [
|
|
469
|
+
p.message,
|
|
470
|
+
p.error.message && /* @__PURE__ */ A("pre", { style: { whiteSpace: "pre-wrap", fontSize: "0.75em" }, children: p.error.message })
|
|
471
|
+
] }) : /* @__PURE__ */ A("div", { ref: i, style: { width: "100%" } });
|
|
474
472
|
}
|
|
475
473
|
export {
|
|
476
|
-
|
|
474
|
+
ge as CowSwapWidget,
|
|
477
475
|
W as TradeType,
|
|
478
476
|
_e as WIDGET_PALETTE_COLORS,
|
|
479
477
|
R as WidgetMethodsEmit,
|
|
@@ -483,5 +481,5 @@ export {
|
|
|
483
481
|
V as isPerNetworkConfig,
|
|
484
482
|
U as isPerTradeTypeConfig,
|
|
485
483
|
ve as resolveFlexibleConfig,
|
|
486
|
-
|
|
484
|
+
g as widgetIframeTransport
|
|
487
485
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cowprotocol/widget-react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.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.18.0"
|
|
26
26
|
}
|
|
27
27
|
}
|