@cowprotocol/widget-lib 0.11.1 → 0.13.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/flexibleConfig.d.ts +6 -0
- package/index.d.ts +1 -0
- package/index.iife.js +1 -1
- package/index.js +1 -1
- package/index.mjs +188 -143
- package/package.json +1 -1
- package/types.d.ts +13 -5
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { FlexibleConfig, PerNetworkConfig, PerTradeTypeConfig, TradeType } from './types';
|
|
2
|
+
import { SupportedChainId } from '@cowprotocol/cow-sdk';
|
|
3
|
+
|
|
4
|
+
export declare function resolveFlexibleConfig<T extends string | number>(config: FlexibleConfig<T>, chainId: SupportedChainId, tradeType: TradeType): T | undefined;
|
|
5
|
+
export declare function isPerTradeTypeConfig<T>(config: FlexibleConfig<T>): config is PerTradeTypeConfig<T>;
|
|
6
|
+
export declare function isPerNetworkConfig<T>(config: FlexibleConfig<T>): config is PerNetworkConfig<T>;
|
package/index.d.ts
CHANGED
package/index.iife.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var cowSwapWidget=function(
|
|
1
|
+
var cowSwapWidget=function(s){"use strict";var ne=Object.defineProperty;var ie=(s,r,u)=>r in s?ne(s,r,{enumerable:!0,configurable:!0,writable:!0,value:u}):s[r]=u;var d=(s,r,u)=>(ie(s,typeof r!="symbol"?r+"":r,u),u);class r{constructor(){d(this,"subscriptions",{})}on(t){const{event:n,handler:i}=t;this.subscriptions[n]||(this.subscriptions[n]=[]),this.subscriptions[n].push(i)}off(t){const{event:n,handler:i}=t;this.subscriptions[n]&&(this.subscriptions[n]=this.subscriptions[n].filter(o=>o!==i))}emit(t,n){this.subscriptions[t]&&this.subscriptions[t].forEach(i=>i(n))}}const u="cowSwapWidget";function T(e,t,n){const o={key:u,method:t,...typeof n=="object"?n:{}};e.postMessage(o,"*")}function E(e,t,n){const i=o=>{!y(o.data)||o.data.key!==u||o.data.method!==t||n(o.data)};return e.addEventListener("message",i),i}function y(e){return typeof e=="object"&&e!==null&&"key"in e&&"method"in e&&typeof e.key=="string"&&typeof e.method=="string"}function W(e,t,n){e.removeEventListener("message",n)}function C(e,t){e.removeEventListener("message",t)}var a=(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))(a||{}),w=(e=>(e.UPDATE_PARAMS="UPDATE_PARAMS",e.UPDATE_APP_DATA="UPDATE_APP_DATA",e.PROVIDER_RPC_RESPONSE="PROVIDER_RPC_RESPONSE",e.PROVIDER_ON_EVENT="PROVIDER_ON_EVENT",e))(w||{}),m=(e=>(e.SWAP="swap",e.LIMIT="limit",e.ADVANCED="advanced",e))(m||{});const N=["primary","background","paper","text","danger","warning","alert","info","success"];class U{constructor(t,n=[]){d(this,"eventEmitter",new r);d(this,"listeners",[]);d(this,"widgetListener");this.contentWindow=t,this.updateListeners(n),this.widgetListener=E(this.contentWindow,a.EMIT_COW_EVENT,i=>this.eventEmitter.emit(i.event,i.payload))}stopListeningIframe(){C(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)}}const V=["connect","disconnect","close","chainChanged","accountsChanged"];class F{constructor(t){d(this,"ethereumProvider",null);d(this,"requestWaitingForConnection",{});d(this,"processRpcCallFromWindow",({rpcRequest:t})=>{if(!this.ethereumProvider){t.id&&(this.requestWaitingForConnection[t.id]=t);return}this.processRpcRequest(t)});this.iframeWidow=t}disconnect(){this.ethereumProvider=null,W(window,a.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow)}onConnect(t){this.ethereumProvider?this.disconnect():E(window,a.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),this.ethereumProvider=t,this.processPendingRequests(),V.forEach(n=>{t.on(n,i=>this.onProviderEvent(n,i))})}processPendingRequests(){Object.keys(this.requestWaitingForConnection).forEach(t=>{this.processRpcRequest(this.requestWaitingForConnection[t])}),this.requestWaitingForConnection={}}processRpcRequest(t){const{id:n,jsonrpc:i,method:o}=t;if(!this.ethereumProvider||!n)return;(o==="enable"?this.ethereumProvider.enable():this.ethereumProvider.request({...t,id:n})).then(p=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:i,id:n,result:p}})).catch(p=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:i,id:n,error:p}}))}onProviderEvent(t,n){T(this.iframeWidow,w.PROVIDER_ON_EVENT,{event:t,params:n})}forwardRpcResponseToIframe(t){T(this.iframeWidow,w.PROVIDER_RPC_RESPONSE,t)}}class H{constructor(t,n){d(this,"forwardSdkMessage");this.appWindow=t,this.iframeWidow=n,this.forwardSdkMessage=i=>{M(i.data)&&i.origin!==window.location.origin&&(k(i.data)?this.appWindow.parent.postMessage(i.data,"*"):G(i.data)&&this.iframeWidow.postMessage(i.data,"*"))},this.startListening()}startListening(){this.appWindow.addEventListener("message",this.forwardSdkMessage)}stopListening(){this.appWindow.removeEventListener("message",this.forwardSdkMessage)}}function M(e){return typeof e=="object"&&e!==null&&"id"in e&&typeof e.id=="string"}function k(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 G(e){return"success"in e&&typeof e.success=="boolean"&&"version"in e&&typeof e.version=="string"}function I(e){return!!(e&&typeof e=="object")}const v="_";function Q(e){const t=typeof e.baseUrl=="string"?e.baseUrl:"https://swap.cow.fi",n=A(e);return t+"/#"+n+"?"+S(e)}function A(e){const{chainId:t=1,sell:n,buy:i,tradeType:o=m.SWAP}=e,c=[(n==null?void 0:n.asset)||v,(i==null?void 0:i.asset)||v].map(encodeURIComponent).join("/");return`/${t}/widget/${o}/${c}`}function S(e){const t=new URLSearchParams;return $(j(t,e),e)}function j(e,t){const{sell:n,buy:i}=t;return n!=null&&n.amount&&e.append("sellAmount",n.amount),i!=null&&i.amount&&e.append("buyAmount",i.amount),e}function $(e,t){const n=t.theme;return n?(I(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)}const L="640px",B="450px",q=20;function K(e,t){const{params:n,provider:i,listeners:o}=t;let c=i,p=n;const h=Y(n);e.innerHTML="",e.appendChild(h);const{contentWindow:l}=h;if(!l)throw console.error("Iframe does not contain a window",h),new Error("Iframe does not contain a window!");const P=[];P.push(z(l,n.appCode)),P.push(...X(h,n.height)),P.push(J());const b=new U(window,o);let g=O(l,null,c);h.addEventListener("load",()=>D(l,p,c));const te=new H(window,l);return{updateParams:f=>{p=f,D(l,p,c)},updateListeners:f=>b.updateListeners(f),updateProvider:f=>{c=f,g=O(l,g,f)},destroy:()=>{g.disconnect(),b.stopListeningIframe(),P.forEach(f=>window.removeEventListener("message",f)),te.stopListening(),e.removeChild(h)}}}function O(e,t,n){t&&t.disconnect();const i=t||new F(e);return n&&i.onConnect(n),i}function Y(e){const{width:t=B,height:n=L}=e,i=document.createElement("iframe");return i.src=Q(e),i.width=t,i.height=n,i.style.border="0",i.allow="clipboard-read; clipboard-write",i}function D(e,t,n){const i=!!n,o=A(t),c=S(t).toString(),{theme:p,...h}=t;T(e,w.UPDATE_PARAMS,{urlParams:{pathname:o,search:c},appParams:h,hasProvider:i})}function z(e,t){return E(window,a.ACTIVATE,()=>{T(e,w.UPDATE_APP_DATA,{metaData:t?{appCode:t}:void 0})})}function J(){return E(window,a.INTERCEPT_WINDOW_OPEN,({href:e,rel:t,target:n})=>{const i=e.toString();if(!i.startsWith("http")&&i.match(/^[a-zA-Z0-9]+:\/\//)){window.open(i,n,t);return}})}function X(e,t=L){return[E(window,a.UPDATE_HEIGHT,n=>{e.style.height=n.height?`${n.height+q}px`:t}),E(window,a.SET_FULL_HEIGHT,({isUpToSmall:n})=>{e.style.height=n?t:`${document.body.offsetHeight}px`})]}const Z=Object.values(m);function x(e,t,n){if(_(e)){const i=e[n];return R(i)?i[t]:i}if(R(e)){const i=e[t];return _(i)?i[n]:i}return e}function _(e){return typeof e!="object"?!1:Object.keys(e).every(t=>Z.includes(t))}const ee=/^\d+$/;function R(e){return typeof e!="object"?!1:Object.keys(e).every(t=>typeof t=="number"||ee.test(t))}return s.TradeType=m,s.WIDGET_PALETTE_COLORS=N,s.WidgetMethodsEmit=a,s.WidgetMethodsListen=w,s.createCowSwapWidget=K,s.isCowSwapWidgetPalette=I,s.isPerNetworkConfig=R,s.isPerTradeTypeConfig=_,s.listenToMessageFromWindow=E,s.postMessageToWindow=T,s.resolveFlexibleConfig=x,s.stopListeningToMessageFromWindow=W,s.stopListeningWindowListener=C,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"}),s}({});
|
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class
|
|
1
|
+
"use strict";var b=Object.defineProperty;var y=(e,t,n)=>t in e?b(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n;var r=(e,t,n)=>(y(e,typeof t!="symbol"?t+"":t,n),n);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class N{constructor(){r(this,"subscriptions",{})}on(t){const{event:n,handler:i}=t;this.subscriptions[n]||(this.subscriptions[n]=[]),this.subscriptions[n].push(i)}off(t){const{event:n,handler:i}=t;this.subscriptions[n]&&(this.subscriptions[n]=this.subscriptions[n].filter(s=>s!==i))}emit(t,n){this.subscriptions[t]&&this.subscriptions[t].forEach(i=>i(n))}}const W="cowSwapWidget";function E(e,t,n){const s={key:W,method:t,...typeof n=="object"?n:{}};e.postMessage(s,"*")}function h(e,t,n){const i=s=>{!U(s.data)||s.data.key!==W||s.data.method!==t||n(s.data)};return e.addEventListener("message",i),i}function U(e){return typeof e=="object"&&e!==null&&"key"in e&&"method"in e&&typeof e.key=="string"&&typeof e.method=="string"}function I(e,t,n){e.removeEventListener("message",n)}function v(e,t){e.removeEventListener("message",t)}var a=(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))(a||{}),f=(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))(f||{}),w=(e=>(e.SWAP="swap",e.LIMIT="limit",e.ADVANCED="advanced",e))(w||{});const V=["primary","background","paper","text","danger","warning","alert","info","success"];class F{constructor(t,n=[]){r(this,"eventEmitter",new N);r(this,"listeners",[]);r(this,"widgetListener");this.contentWindow=t,this.updateListeners(n),this.widgetListener=h(this.contentWindow,a.EMIT_COW_EVENT,i=>this.eventEmitter.emit(i.event,i.payload))}stopListeningIframe(){v(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)}}const H=["connect","disconnect","close","chainChanged","accountsChanged"];class M{constructor(t){r(this,"ethereumProvider",null);r(this,"requestWaitingForConnection",{});r(this,"processRpcCallFromWindow",({rpcRequest:t})=>{if(!this.ethereumProvider){t.id&&(this.requestWaitingForConnection[t.id]=t);return}this.processRpcRequest(t)});this.iframeWidow=t}disconnect(){this.ethereumProvider=null,I(window,a.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow)}onConnect(t){this.ethereumProvider?this.disconnect():h(window,a.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),this.ethereumProvider=t,this.processPendingRequests(),H.forEach(n=>{t.on(n,i=>this.onProviderEvent(n,i))})}processPendingRequests(){Object.keys(this.requestWaitingForConnection).forEach(t=>{this.processRpcRequest(this.requestWaitingForConnection[t])}),this.requestWaitingForConnection={}}processRpcRequest(t){const{id:n,jsonrpc:i,method:s}=t;if(!this.ethereumProvider||!n)return;(s==="enable"?this.ethereumProvider.enable():this.ethereumProvider.request({...t,id:n})).then(c=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:i,id:n,result:c}})).catch(c=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:i,id:n,error:c}}))}onProviderEvent(t,n){E(this.iframeWidow,f.PROVIDER_ON_EVENT,{event:t,params:n})}forwardRpcResponseToIframe(t){E(this.iframeWidow,f.PROVIDER_RPC_RESPONSE,t)}}class k{constructor(t,n){r(this,"forwardSdkMessage");this.appWindow=t,this.iframeWidow=n,this.forwardSdkMessage=i=>{G(i.data)&&i.origin!==window.location.origin&&(x(i.data)?this.appWindow.parent.postMessage(i.data,"*"):Q(i.data)&&this.iframeWidow.postMessage(i.data,"*"))},this.startListening()}startListening(){this.appWindow.addEventListener("message",this.forwardSdkMessage)}stopListening(){this.appWindow.removeEventListener("message",this.forwardSdkMessage)}}function G(e){return typeof e=="object"&&e!==null&&"id"in e&&typeof e.id=="string"}function x(e){return"method"in e&&typeof e.method=="string"&&"params"in e&&"env"in e&&typeof e.env=="object"&&e.env!==null&&"sdkVersion"in e.env}function Q(e){return"success"in e&&typeof e.success=="boolean"&&"version"in e&&typeof e.version=="string"}function A(e){return!!(e&&typeof e=="object")}const R="_";function j(e){const t=typeof e.baseUrl=="string"?e.baseUrl:"https://swap.cow.fi",n=S(e);return t+"/#"+n+"?"+L(e)}function S(e){const{chainId:t=1,sell:n,buy:i,tradeType:s=w.SWAP}=e,o=[(n==null?void 0:n.asset)||R,(i==null?void 0:i.asset)||R].map(encodeURIComponent).join("/");return`/${t}/widget/${s}/${o}`}function L(e){const t=new URLSearchParams;return B($(t,e),e)}function $(e,t){const{sell:n,buy:i}=t;return n!=null&&n.amount&&e.append("sellAmount",n.amount),i!=null&&i.amount&&e.append("buyAmount",i.amount),e}function B(e,t){const n=t.theme;return n?(A(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)}const O="640px",q="450px",K=20;function Y(e,t){const{params:n,provider:i,listeners:s}=t;let o=i,c=n;const d=z(n);e.innerHTML="",e.appendChild(d);const{contentWindow:u}=d;if(!u)throw console.error("Iframe does not contain a window",d),new Error("Iframe does not contain a window!");const l=[];l.push(J(u,n.appCode)),l.push(...Z(d,n.height)),l.push(X());const _=new F(window,s);let T=g(u,null,o);d.addEventListener("load",()=>C(u,c,o));const D=new k(window,u);return{updateParams:p=>{c=p,C(u,c,o)},updateListeners:p=>_.updateListeners(p),updateProvider:p=>{o=p,T=g(u,T,p)},destroy:()=>{T.disconnect(),_.stopListeningIframe(),l.forEach(p=>window.removeEventListener("message",p)),D.stopListening(),e.removeChild(d)}}}function g(e,t,n){t&&t.disconnect();const i=t||new M(e);return n&&i.onConnect(n),i}function z(e){const{width:t=q,height:n=O}=e,i=document.createElement("iframe");return i.src=j(e),i.width=t,i.height=n,i.style.border="0",i.allow="clipboard-read; clipboard-write",i}function C(e,t,n){const i=!!n,s=S(t),o=L(t).toString(),{theme:c,...d}=t;E(e,f.UPDATE_PARAMS,{urlParams:{pathname:s,search:o},appParams:d,hasProvider:i})}function J(e,t){return h(window,a.ACTIVATE,()=>{E(e,f.UPDATE_APP_DATA,{metaData:t?{appCode:t}:void 0})})}function X(){return h(window,a.INTERCEPT_WINDOW_OPEN,({href:e,rel:t,target:n})=>{const i=e.toString();if(!i.startsWith("http")&&i.match(/^[a-zA-Z0-9]+:\/\//)){window.open(i,n,t);return}})}function Z(e,t=O){return[h(window,a.UPDATE_HEIGHT,n=>{e.style.height=n.height?`${n.height+K}px`:t}),h(window,a.SET_FULL_HEIGHT,({isUpToSmall:n})=>{e.style.height=n?t:`${document.body.offsetHeight}px`})]}const ee=Object.values(w);function te(e,t,n){if(m(e)){const i=e[n];return P(i)?i[t]:i}if(P(e)){const i=e[t];return m(i)?i[n]:i}return e}function m(e){return typeof e!="object"?!1:Object.keys(e).every(t=>ee.includes(t))}const ne=/^\d+$/;function P(e){return typeof e!="object"?!1:Object.keys(e).every(t=>typeof t=="number"||ne.test(t))}exports.TradeType=w;exports.WIDGET_PALETTE_COLORS=V;exports.WidgetMethodsEmit=a;exports.WidgetMethodsListen=f;exports.createCowSwapWidget=Y;exports.isCowSwapWidgetPalette=A;exports.isPerNetworkConfig=P;exports.isPerTradeTypeConfig=m;exports.listenToMessageFromWindow=h;exports.postMessageToWindow=E;exports.resolveFlexibleConfig=te;exports.stopListeningToMessageFromWindow=I;exports.stopListeningWindowListener=v;
|
package/index.mjs
CHANGED
|
@@ -1,49 +1,52 @@
|
|
|
1
|
-
|
|
1
|
+
var O = Object.defineProperty;
|
|
2
|
+
var D = (e, t, n) => t in e ? O(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n;
|
|
3
|
+
var r = (e, t, n) => (D(e, typeof t != "symbol" ? t + "" : t, n), n);
|
|
4
|
+
class L {
|
|
2
5
|
constructor() {
|
|
3
|
-
this
|
|
6
|
+
r(this, "subscriptions", {});
|
|
4
7
|
}
|
|
5
|
-
on(
|
|
6
|
-
const { event:
|
|
7
|
-
this.subscriptions[
|
|
8
|
+
on(t) {
|
|
9
|
+
const { event: n, handler: s } = t;
|
|
10
|
+
this.subscriptions[n] || (this.subscriptions[n] = []), this.subscriptions[n].push(s);
|
|
8
11
|
}
|
|
9
|
-
off(
|
|
10
|
-
const { event:
|
|
11
|
-
this.subscriptions[
|
|
12
|
+
off(t) {
|
|
13
|
+
const { event: n, handler: s } = t;
|
|
14
|
+
this.subscriptions[n] && (this.subscriptions[n] = this.subscriptions[n].filter((i) => i !== s));
|
|
12
15
|
}
|
|
13
|
-
emit(
|
|
14
|
-
this.subscriptions[
|
|
16
|
+
emit(t, n) {
|
|
17
|
+
this.subscriptions[t] && this.subscriptions[t].forEach((s) => s(n));
|
|
15
18
|
}
|
|
16
19
|
}
|
|
17
|
-
const
|
|
18
|
-
function
|
|
19
|
-
const
|
|
20
|
-
key:
|
|
21
|
-
method:
|
|
22
|
-
...typeof
|
|
20
|
+
const A = "cowSwapWidget";
|
|
21
|
+
function l(e, t, n) {
|
|
22
|
+
const i = {
|
|
23
|
+
key: A,
|
|
24
|
+
method: t,
|
|
25
|
+
...typeof n == "object" ? n : {}
|
|
23
26
|
};
|
|
24
27
|
e.postMessage(
|
|
25
|
-
|
|
28
|
+
i,
|
|
26
29
|
"*"
|
|
27
30
|
// TODO: Change to CoW specific origin in production. https://github.com/cowprotocol/cowswap/issues/3828
|
|
28
31
|
);
|
|
29
32
|
}
|
|
30
|
-
function
|
|
31
|
-
const
|
|
32
|
-
!
|
|
33
|
+
function h(e, t, n) {
|
|
34
|
+
const s = (i) => {
|
|
35
|
+
!b(i.data) || i.data.key !== A || i.data.method !== t || n(i.data);
|
|
33
36
|
};
|
|
34
|
-
return e.addEventListener("message",
|
|
37
|
+
return e.addEventListener("message", s), s;
|
|
35
38
|
}
|
|
36
|
-
function
|
|
39
|
+
function b(e) {
|
|
37
40
|
return typeof e == "object" && e !== null && "key" in e && "method" in e && typeof e.key == "string" && typeof e.method == "string";
|
|
38
41
|
}
|
|
39
|
-
function
|
|
40
|
-
e.removeEventListener("message", t);
|
|
41
|
-
}
|
|
42
|
-
function _(e, n) {
|
|
42
|
+
function N(e, t, n) {
|
|
43
43
|
e.removeEventListener("message", n);
|
|
44
44
|
}
|
|
45
|
-
|
|
46
|
-
|
|
45
|
+
function y(e, t) {
|
|
46
|
+
e.removeEventListener("message", t);
|
|
47
|
+
}
|
|
48
|
+
var u = /* @__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))(u || {}), f = /* @__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))(f || {}), m = /* @__PURE__ */ ((e) => (e.SWAP = "swap", e.LIMIT = "limit", e.ADVANCED = "advanced", e))(m || {});
|
|
49
|
+
const te = [
|
|
47
50
|
"primary",
|
|
48
51
|
"background",
|
|
49
52
|
"paper",
|
|
@@ -54,96 +57,108 @@ const q = [
|
|
|
54
57
|
"info",
|
|
55
58
|
"success"
|
|
56
59
|
];
|
|
57
|
-
class
|
|
58
|
-
constructor(
|
|
59
|
-
this
|
|
60
|
+
class U {
|
|
61
|
+
constructor(t, n = []) {
|
|
62
|
+
r(this, "eventEmitter", new L());
|
|
63
|
+
r(this, "listeners", []);
|
|
64
|
+
r(this, "widgetListener");
|
|
65
|
+
this.contentWindow = t, this.updateListeners(n), this.widgetListener = h(
|
|
60
66
|
this.contentWindow,
|
|
61
|
-
|
|
62
|
-
(
|
|
67
|
+
u.EMIT_COW_EVENT,
|
|
68
|
+
(s) => this.eventEmitter.emit(s.event, s.payload)
|
|
63
69
|
);
|
|
64
70
|
}
|
|
65
71
|
stopListeningIframe() {
|
|
66
|
-
|
|
72
|
+
y(this.contentWindow, this.widgetListener);
|
|
67
73
|
}
|
|
68
|
-
updateListeners(
|
|
69
|
-
for (const
|
|
70
|
-
this.eventEmitter.off(
|
|
71
|
-
this.listeners =
|
|
72
|
-
for (const
|
|
73
|
-
this.eventEmitter.on(
|
|
74
|
+
updateListeners(t) {
|
|
75
|
+
for (const n of this.listeners)
|
|
76
|
+
this.eventEmitter.off(n);
|
|
77
|
+
this.listeners = t || [];
|
|
78
|
+
for (const n of this.listeners)
|
|
79
|
+
this.eventEmitter.on(n);
|
|
74
80
|
}
|
|
75
81
|
}
|
|
76
|
-
const
|
|
77
|
-
class
|
|
82
|
+
const V = ["connect", "disconnect", "close", "chainChanged", "accountsChanged"];
|
|
83
|
+
class H {
|
|
78
84
|
/**
|
|
79
85
|
* Creates an instance of IframeRpcProviderBridge.
|
|
80
86
|
* @param iframeWidow - The iFrame window that will post up general RPC messages and to which the IframeRpcProviderBridge will forward the RPC result.
|
|
81
87
|
* Also it will receive some special RPC events coming from the wallet, like connect/chainChanged,accountChanged
|
|
82
88
|
*/
|
|
83
|
-
constructor(
|
|
84
|
-
|
|
89
|
+
constructor(t) {
|
|
90
|
+
/**
|
|
91
|
+
* The Ethereum provider instance.
|
|
92
|
+
* When is null the JSON-RPC bridge is disconnected from the Ethereum provider.
|
|
93
|
+
* */
|
|
94
|
+
r(this, "ethereumProvider", null);
|
|
95
|
+
/** Stored JSON-RPC requests, to queue them when disconnected. */
|
|
96
|
+
r(this, "requestWaitingForConnection", {});
|
|
97
|
+
r(this, "processRpcCallFromWindow", ({ rpcRequest: t }) => {
|
|
85
98
|
if (!this.ethereumProvider) {
|
|
86
99
|
t.id && (this.requestWaitingForConnection[t.id] = t);
|
|
87
100
|
return;
|
|
88
101
|
}
|
|
89
102
|
this.processRpcRequest(t);
|
|
90
|
-
};
|
|
103
|
+
});
|
|
104
|
+
this.iframeWidow = t;
|
|
91
105
|
}
|
|
92
106
|
/**
|
|
93
107
|
* Disconnects the JSON-RPC bridge from the Ethereum provider.
|
|
94
108
|
*/
|
|
95
109
|
disconnect() {
|
|
96
|
-
this.ethereumProvider = null,
|
|
110
|
+
this.ethereumProvider = null, N(window, u.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow);
|
|
97
111
|
}
|
|
98
112
|
/**
|
|
99
113
|
* Handles the 'connect' event and sets up event listeners for Ethereum provider events.
|
|
100
114
|
* @param newProvider - The Ethereum provider to connect.
|
|
101
115
|
*/
|
|
102
|
-
onConnect(
|
|
103
|
-
this.ethereumProvider ? this.disconnect() :
|
|
104
|
-
|
|
116
|
+
onConnect(t) {
|
|
117
|
+
this.ethereumProvider ? this.disconnect() : h(window, u.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow), this.ethereumProvider = t, this.processPendingRequests(), V.forEach((n) => {
|
|
118
|
+
t.on(n, (s) => this.onProviderEvent(n, s));
|
|
105
119
|
});
|
|
106
120
|
}
|
|
107
121
|
processPendingRequests() {
|
|
108
|
-
Object.keys(this.requestWaitingForConnection).forEach((
|
|
109
|
-
this.processRpcRequest(this.requestWaitingForConnection[
|
|
122
|
+
Object.keys(this.requestWaitingForConnection).forEach((t) => {
|
|
123
|
+
this.processRpcRequest(this.requestWaitingForConnection[t]);
|
|
110
124
|
}), this.requestWaitingForConnection = {};
|
|
111
125
|
}
|
|
112
126
|
/**
|
|
113
127
|
* Processes a JSON-RPC request and sends appropriate response or error via the content window.
|
|
114
128
|
* @param request - The JSON-RPC request to be processed.
|
|
115
129
|
*/
|
|
116
|
-
processRpcRequest(
|
|
117
|
-
const { id:
|
|
118
|
-
if (!this.ethereumProvider || !
|
|
130
|
+
processRpcRequest(t) {
|
|
131
|
+
const { id: n, jsonrpc: s, method: i } = t;
|
|
132
|
+
if (!this.ethereumProvider || !n)
|
|
119
133
|
return;
|
|
120
|
-
(
|
|
121
|
-
(
|
|
122
|
-
rpcResponse: { jsonrpc:
|
|
134
|
+
(i === "enable" ? this.ethereumProvider.enable() : this.ethereumProvider.request({ ...t, id: n })).then(
|
|
135
|
+
(a) => this.forwardRpcResponseToIframe({
|
|
136
|
+
rpcResponse: { jsonrpc: s, id: n, result: a }
|
|
123
137
|
})
|
|
124
138
|
).catch(
|
|
125
|
-
(
|
|
126
|
-
rpcResponse: { jsonrpc:
|
|
139
|
+
(a) => this.forwardRpcResponseToIframe({
|
|
140
|
+
rpcResponse: { jsonrpc: s, id: n, error: a }
|
|
127
141
|
})
|
|
128
142
|
);
|
|
129
143
|
}
|
|
130
|
-
onProviderEvent(
|
|
131
|
-
|
|
132
|
-
event:
|
|
133
|
-
params:
|
|
144
|
+
onProviderEvent(t, n) {
|
|
145
|
+
l(this.iframeWidow, f.PROVIDER_ON_EVENT, {
|
|
146
|
+
event: t,
|
|
147
|
+
params: n
|
|
134
148
|
});
|
|
135
149
|
}
|
|
136
150
|
/**
|
|
137
151
|
* Forward a JSON-RPC message to the content window.
|
|
138
152
|
*/
|
|
139
|
-
forwardRpcResponseToIframe(
|
|
140
|
-
|
|
153
|
+
forwardRpcResponseToIframe(t) {
|
|
154
|
+
l(this.iframeWidow, f.PROVIDER_RPC_RESPONSE, t);
|
|
141
155
|
}
|
|
142
156
|
}
|
|
143
|
-
class
|
|
144
|
-
constructor(
|
|
145
|
-
this
|
|
146
|
-
|
|
157
|
+
class F {
|
|
158
|
+
constructor(t, n) {
|
|
159
|
+
r(this, "forwardSdkMessage");
|
|
160
|
+
this.appWindow = t, this.iframeWidow = n, this.forwardSdkMessage = (s) => {
|
|
161
|
+
k(s.data) && s.origin !== window.location.origin && (M(s.data) ? this.appWindow.parent.postMessage(s.data, "*") : G(s.data) && this.iframeWidow.postMessage(s.data, "*"));
|
|
147
162
|
}, this.startListening();
|
|
148
163
|
}
|
|
149
164
|
startListening() {
|
|
@@ -153,114 +168,144 @@ class V {
|
|
|
153
168
|
this.appWindow.removeEventListener("message", this.forwardSdkMessage);
|
|
154
169
|
}
|
|
155
170
|
}
|
|
156
|
-
function
|
|
171
|
+
function k(e) {
|
|
157
172
|
return typeof e == "object" && e !== null && "id" in e && typeof e.id == "string";
|
|
158
173
|
}
|
|
159
|
-
function
|
|
174
|
+
function M(e) {
|
|
160
175
|
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;
|
|
161
176
|
}
|
|
162
|
-
function
|
|
177
|
+
function G(e) {
|
|
163
178
|
return "success" in e && typeof e.success == "boolean" && "version" in e && typeof e.version == "string";
|
|
164
179
|
}
|
|
165
|
-
function
|
|
180
|
+
function x(e) {
|
|
166
181
|
return !!(e && typeof e == "object");
|
|
167
182
|
}
|
|
168
|
-
const
|
|
169
|
-
function
|
|
170
|
-
const
|
|
171
|
-
return
|
|
183
|
+
const T = "_";
|
|
184
|
+
function Q(e) {
|
|
185
|
+
const t = typeof e.baseUrl == "string" ? e.baseUrl : "https://swap.cow.fi", n = v(e);
|
|
186
|
+
return t + "/#" + n + "?" + C(e);
|
|
172
187
|
}
|
|
173
|
-
function
|
|
174
|
-
const { chainId:
|
|
175
|
-
return `/${
|
|
188
|
+
function v(e) {
|
|
189
|
+
const { chainId: t = 1, sell: n, buy: s, tradeType: i = m.SWAP } = e, o = [(n == null ? void 0 : n.asset) || T, (s == null ? void 0 : s.asset) || T].map(encodeURIComponent).join("/");
|
|
190
|
+
return `/${t}/widget/${i}/${o}`;
|
|
176
191
|
}
|
|
177
|
-
function
|
|
178
|
-
const
|
|
179
|
-
return
|
|
192
|
+
function C(e) {
|
|
193
|
+
const t = new URLSearchParams();
|
|
194
|
+
return j($(t, e), e);
|
|
180
195
|
}
|
|
181
|
-
function
|
|
182
|
-
const { sell:
|
|
183
|
-
return
|
|
196
|
+
function $(e, t) {
|
|
197
|
+
const { sell: n, buy: s } = t;
|
|
198
|
+
return n != null && n.amount && e.append("sellAmount", n.amount), s != null && s.amount && e.append("buyAmount", s.amount), e;
|
|
184
199
|
}
|
|
185
|
-
function
|
|
186
|
-
const
|
|
187
|
-
return
|
|
200
|
+
function j(e, t) {
|
|
201
|
+
const n = t.theme;
|
|
202
|
+
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);
|
|
188
203
|
}
|
|
189
|
-
const
|
|
190
|
-
function
|
|
191
|
-
const { params:
|
|
192
|
-
let o =
|
|
193
|
-
const
|
|
194
|
-
e.innerHTML = "", e.appendChild(
|
|
195
|
-
const { contentWindow:
|
|
196
|
-
if (!
|
|
197
|
-
throw console.error("Iframe does not contain a window",
|
|
198
|
-
const
|
|
199
|
-
|
|
200
|
-
const
|
|
201
|
-
let
|
|
202
|
-
|
|
203
|
-
const
|
|
204
|
+
const W = "640px", B = "450px", q = 20;
|
|
205
|
+
function ne(e, t) {
|
|
206
|
+
const { params: n, provider: s, listeners: i } = t;
|
|
207
|
+
let o = s, a = n;
|
|
208
|
+
const c = K(n);
|
|
209
|
+
e.innerHTML = "", e.appendChild(c);
|
|
210
|
+
const { contentWindow: p } = c;
|
|
211
|
+
if (!p)
|
|
212
|
+
throw console.error("Iframe does not contain a window", c), new Error("Iframe does not contain a window!");
|
|
213
|
+
const E = [];
|
|
214
|
+
E.push(Y(p, n.appCode)), E.push(...J(c, n.height)), E.push(z());
|
|
215
|
+
const P = new U(window, i);
|
|
216
|
+
let w = _(p, null, o);
|
|
217
|
+
c.addEventListener("load", () => R(p, a, o));
|
|
218
|
+
const S = new F(window, p);
|
|
204
219
|
return {
|
|
205
|
-
updateParams: (
|
|
206
|
-
|
|
220
|
+
updateParams: (d) => {
|
|
221
|
+
a = d, R(p, a, o);
|
|
207
222
|
},
|
|
208
|
-
updateListeners: (
|
|
209
|
-
updateProvider: (
|
|
210
|
-
o =
|
|
223
|
+
updateListeners: (d) => P.updateListeners(d),
|
|
224
|
+
updateProvider: (d) => {
|
|
225
|
+
o = d, w = _(p, w, d);
|
|
211
226
|
},
|
|
212
227
|
destroy: () => {
|
|
213
|
-
|
|
228
|
+
w.disconnect(), P.stopListeningIframe(), E.forEach((d) => window.removeEventListener("message", d)), S.stopListening(), e.removeChild(c);
|
|
214
229
|
}
|
|
215
230
|
};
|
|
216
231
|
}
|
|
217
|
-
function
|
|
218
|
-
|
|
219
|
-
const
|
|
220
|
-
return
|
|
232
|
+
function _(e, t, n) {
|
|
233
|
+
t && t.disconnect();
|
|
234
|
+
const s = t || new H(e);
|
|
235
|
+
return n && s.onConnect(n), s;
|
|
221
236
|
}
|
|
222
|
-
function
|
|
223
|
-
const { width:
|
|
224
|
-
return
|
|
237
|
+
function K(e) {
|
|
238
|
+
const { width: t = B, height: n = W } = e, s = document.createElement("iframe");
|
|
239
|
+
return s.src = Q(e), s.width = t, s.height = n, s.style.border = "0", s.allow = "clipboard-read; clipboard-write", s;
|
|
225
240
|
}
|
|
226
|
-
function
|
|
227
|
-
const
|
|
228
|
-
|
|
241
|
+
function R(e, t, n) {
|
|
242
|
+
const s = !!n, i = v(t), o = C(t).toString(), { theme: a, ...c } = t;
|
|
243
|
+
l(e, f.UPDATE_PARAMS, {
|
|
229
244
|
urlParams: {
|
|
230
|
-
pathname:
|
|
245
|
+
pathname: i,
|
|
231
246
|
search: o
|
|
232
247
|
},
|
|
233
|
-
appParams:
|
|
234
|
-
hasProvider:
|
|
248
|
+
appParams: c,
|
|
249
|
+
hasProvider: s
|
|
235
250
|
});
|
|
236
251
|
}
|
|
237
|
-
function
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
metaData:
|
|
252
|
+
function Y(e, t) {
|
|
253
|
+
return h(window, u.ACTIVATE, () => {
|
|
254
|
+
l(e, f.UPDATE_APP_DATA, {
|
|
255
|
+
metaData: t ? { appCode: t } : void 0
|
|
241
256
|
});
|
|
242
257
|
});
|
|
243
|
-
return t;
|
|
244
258
|
}
|
|
245
|
-
function
|
|
259
|
+
function z() {
|
|
260
|
+
return h(window, u.INTERCEPT_WINDOW_OPEN, ({ href: e, rel: t, target: n }) => {
|
|
261
|
+
const s = e.toString();
|
|
262
|
+
if (!s.startsWith("http") && s.match(/^[a-zA-Z0-9]+:\/\//)) {
|
|
263
|
+
window.open(s, n, t);
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
function J(e, t = W) {
|
|
246
269
|
return [
|
|
247
|
-
|
|
248
|
-
e.style.height =
|
|
270
|
+
h(window, u.UPDATE_HEIGHT, (n) => {
|
|
271
|
+
e.style.height = n.height ? `${n.height + q}px` : t;
|
|
249
272
|
}),
|
|
250
|
-
|
|
251
|
-
e.style.height =
|
|
273
|
+
h(window, u.SET_FULL_HEIGHT, ({ isUpToSmall: n }) => {
|
|
274
|
+
e.style.height = n ? t : `${document.body.offsetHeight}px`;
|
|
252
275
|
})
|
|
253
276
|
];
|
|
254
277
|
}
|
|
278
|
+
const X = Object.values(m);
|
|
279
|
+
function se(e, t, n) {
|
|
280
|
+
if (g(e)) {
|
|
281
|
+
const s = e[n];
|
|
282
|
+
return I(s) ? s[t] : s;
|
|
283
|
+
}
|
|
284
|
+
if (I(e)) {
|
|
285
|
+
const s = e[t];
|
|
286
|
+
return g(s) ? s[n] : s;
|
|
287
|
+
}
|
|
288
|
+
return e;
|
|
289
|
+
}
|
|
290
|
+
function g(e) {
|
|
291
|
+
return typeof e != "object" ? !1 : Object.keys(e).every((t) => X.includes(t));
|
|
292
|
+
}
|
|
293
|
+
const Z = /^\d+$/;
|
|
294
|
+
function I(e) {
|
|
295
|
+
return typeof e != "object" ? !1 : Object.keys(e).every((t) => typeof t == "number" || Z.test(t));
|
|
296
|
+
}
|
|
255
297
|
export {
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
298
|
+
m as TradeType,
|
|
299
|
+
te as WIDGET_PALETTE_COLORS,
|
|
300
|
+
u as WidgetMethodsEmit,
|
|
301
|
+
f as WidgetMethodsListen,
|
|
302
|
+
ne as createCowSwapWidget,
|
|
303
|
+
x as isCowSwapWidgetPalette,
|
|
304
|
+
I as isPerNetworkConfig,
|
|
305
|
+
g as isPerTradeTypeConfig,
|
|
306
|
+
h as listenToMessageFromWindow,
|
|
307
|
+
l as postMessageToWindow,
|
|
308
|
+
se as resolveFlexibleConfig,
|
|
309
|
+
N as stopListeningToMessageFromWindow,
|
|
310
|
+
y as stopListeningWindowListener
|
|
266
311
|
};
|
package/package.json
CHANGED
package/types.d.ts
CHANGED
|
@@ -2,12 +2,16 @@ import { CowEventListeners, CowEventPayloadMap, CowEvents } from '../../events/s
|
|
|
2
2
|
import { SupportedChainId } from '@cowprotocol/cow-sdk';
|
|
3
3
|
|
|
4
4
|
export type { SupportedChainId } from '@cowprotocol/cow-sdk';
|
|
5
|
+
export type PerTradeTypeConfig<T> = Partial<Record<TradeType, T>>;
|
|
6
|
+
export type PerNetworkConfig<T> = Partial<Record<SupportedChainId, T>>;
|
|
7
|
+
export type FlexibleConfig<T> = T | PerNetworkConfig<T> | PerTradeTypeConfig<T> | PerTradeTypeConfig<PerNetworkConfig<T>> | PerNetworkConfig<PerTradeTypeConfig<T>>;
|
|
5
8
|
export declare enum WidgetMethodsEmit {
|
|
6
9
|
ACTIVATE = "ACTIVATE",
|
|
7
10
|
UPDATE_HEIGHT = "UPDATE_HEIGHT",
|
|
8
11
|
SET_FULL_HEIGHT = "SET_FULL_HEIGHT",
|
|
9
12
|
EMIT_COW_EVENT = "EMIT_COW_EVENT",
|
|
10
|
-
PROVIDER_RPC_REQUEST = "PROVIDER_RPC_REQUEST"
|
|
13
|
+
PROVIDER_RPC_REQUEST = "PROVIDER_RPC_REQUEST",
|
|
14
|
+
INTERCEPT_WINDOW_OPEN = "INTERCEPT_WINDOW_OPEN"
|
|
11
15
|
}
|
|
12
16
|
export declare enum WidgetMethodsListen {
|
|
13
17
|
UPDATE_PARAMS = "UPDATE_PARAMS",
|
|
@@ -71,18 +75,16 @@ export declare enum TradeType {
|
|
|
71
75
|
}
|
|
72
76
|
/**
|
|
73
77
|
* The partner fee
|
|
74
|
-
*
|
|
75
|
-
* Please contact https://cowprotocol.typeform.com/to/rONXaxHV
|
|
76
78
|
*/
|
|
77
79
|
export interface PartnerFee {
|
|
78
80
|
/**
|
|
79
81
|
* The fee in basis points (BPS). One basis point is equivalent to 0.01% (1/100th of a percent)
|
|
80
82
|
*/
|
|
81
|
-
bps: number
|
|
83
|
+
bps: FlexibleConfig<number>;
|
|
82
84
|
/**
|
|
83
85
|
* The Ethereum address of the partner to receive the fee.
|
|
84
86
|
*/
|
|
85
|
-
recipient:
|
|
87
|
+
recipient: FlexibleConfig<string>;
|
|
86
88
|
}
|
|
87
89
|
/**
|
|
88
90
|
* ERC-20 token information
|
|
@@ -252,6 +254,7 @@ export interface WidgetMethodsEmitPayloadMap {
|
|
|
252
254
|
[WidgetMethodsEmit.UPDATE_HEIGHT]: UpdateWidgetHeightPayload;
|
|
253
255
|
[WidgetMethodsEmit.SET_FULL_HEIGHT]: SetWidgetFullHeightPayload;
|
|
254
256
|
[WidgetMethodsEmit.PROVIDER_RPC_REQUEST]: ProviderRpcRequestPayload;
|
|
257
|
+
[WidgetMethodsEmit.INTERCEPT_WINDOW_OPEN]: WindowOpenPayload;
|
|
255
258
|
}
|
|
256
259
|
export interface WidgetMethodsListenPayloadMap {
|
|
257
260
|
[WidgetMethodsListen.UPDATE_APP_DATA]: UpdateAppDataPayload;
|
|
@@ -293,6 +296,11 @@ export type WidgetMethodHandler<T extends WidgetMethodsEmit> = (payload: WidgetM
|
|
|
293
296
|
export interface ProviderRpcRequestPayload {
|
|
294
297
|
rpcRequest: JsonRpcRequestMessage;
|
|
295
298
|
}
|
|
299
|
+
export interface WindowOpenPayload {
|
|
300
|
+
href: string | URL;
|
|
301
|
+
target: string;
|
|
302
|
+
rel: string;
|
|
303
|
+
}
|
|
296
304
|
export interface JsonRpcRequestMessage {
|
|
297
305
|
jsonrpc: '2.0';
|
|
298
306
|
id?: number;
|