@cowprotocol/widget-lib 0.2.0 → 0.3.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.iife.js +1 -1
- package/index.js +1 -1
- package/index.mjs +47 -48
- package/package.json +1 -1
- package/types.d.ts +13 -4
package/index.iife.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var cowSwapWidget=function(
|
|
1
|
+
var cowSwapWidget=function(i){"use strict";const m="2.0",f="*",l=["connect","disconnect","close","chainChanged","accountsChanged"];class E{constructor(t){this.contentWindow=t,this.ethereumProvider=null,this.requests={},this.processEvent=e=>{e.data.jsonrpc==="2.0"&&(this.ethereumProvider?this.processRequest(e.data):this.requests[e.data.id]=e.data)},window.addEventListener("message",this.processEvent)}disconnect(){this.ethereumProvider=null,window.removeEventListener("message",this.processEvent)}onConnect(t){this.ethereumProvider=t,Object.keys(this.requests).forEach(e=>{this.processRequest(this.requests[e])}),this.requests={},l.forEach(e=>{t.on(e,n=>{this.postMessage({method:e,params:[n]})})})}processRequest(t){if(!this.ethereumProvider)return;(t.method==="enable"?this.ethereumProvider.enable():this.ethereumProvider.request(t)).then(n=>{this.postMessage({id:t.id,result:n})}).catch(n=>{this.postMessage({id:t.id,error:n})})}postMessage(t){this.contentWindow.postMessage({jsonrpc:m,...t},f)}}function v(){return typeof window>"u"||!window?"":`https://swap-dev-git-${window.location.hostname.replace("widget-configurator-git-","").replace("-cowswap.vercel.app","")}-cowswap.vercel.app`}const d={local:"http://localhost:3000",prod:"https://swap.cow.fi",dev:"https://dev.swap.cow.fi",pr:v()};var c=(s=>(s.SWAP="swap",s.LIMIT="limit",s.ADVANCED="advanced",s))(c||{});const h="_";function P(s){const t=d[s.env||"prod"],e=u(s),n=p(s);return t+"/#"+e+"?"+n}function u(s){const{chainId:t=1,sell:e,buy:n,tradeType:o=c.SWAP}=s,r=[(e==null?void 0:e.asset)||h,(n==null?void 0:n.asset)||h].map(encodeURIComponent).join("/");return`/${t}/widget/${o}/${r}`}function p(s){const{sell:t,buy:e,theme:n}=s,o=new URLSearchParams;return t!=null&&t.amount&&o.append("sellAmount",t.amount),e!=null&&e.amount&&o.append("buyAmount",e.amount),n&&o.append("theme",typeof n=="string"?n:n.baseTheme),o}const a="cowSwapWidget",w="640px",W="450px",S=20;function y(s,t={}){const{provider:e}=t,n=T(t);s.innerHTML="",s.appendChild(n);const{contentWindow:o}=n;if(!o)throw new Error("Iframe does not contain a window!");return A(o,t.appCode),M(n,t.height),e&&new E(o).onConnect(e),n.addEventListener("load",()=>{g(t,o)}),r=>g(r,o)}function T(s){const{width:t=W,height:e=w}=s,n=document.createElement("iframe");return n.src=P(s),n.width=t,n.height=e,n.style.border="0",n}function g(s,t){const e=u(s),n=p(s).toString();t.postMessage({key:a,method:"update",urlParams:{pathname:e,search:n},appParams:{...s,provider:void 0}},"*")}function A(s,t){window.addEventListener("message",e=>{e.data.key!==a||e.data.method!=="activate"||s.postMessage({key:a,method:"metaData",metaData:t?{appCode:t}:void 0},"*")})}function M(s,t=w){window.addEventListener("message",e=>{e.data.key!==a||e.data.method!=="iframeHeight"||(s.style.height=e.data.height?`${e.data.height+S}px`:t)})}return i.COWSWAP_URLS=d,i.TradeType=c,i.cowSwapWidget=y,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"}),i}({});
|
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const g="2.0",m="*",
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const g="2.0",m="*",f=["connect","disconnect","close","chainChanged","accountsChanged"];class l{constructor(t){this.contentWindow=t,this.ethereumProvider=null,this.requests={},this.processEvent=e=>{e.data.jsonrpc==="2.0"&&(this.ethereumProvider?this.processRequest(e.data):this.requests[e.data.id]=e.data)},window.addEventListener("message",this.processEvent)}disconnect(){this.ethereumProvider=null,window.removeEventListener("message",this.processEvent)}onConnect(t){this.ethereumProvider=t,Object.keys(this.requests).forEach(e=>{this.processRequest(this.requests[e])}),this.requests={},f.forEach(e=>{t.on(e,n=>{this.postMessage({method:e,params:[n]})})})}processRequest(t){if(!this.ethereumProvider)return;(t.method==="enable"?this.ethereumProvider.enable():this.ethereumProvider.request(t)).then(n=>{this.postMessage({id:t.id,result:n})}).catch(n=>{this.postMessage({id:t.id,error:n})})}postMessage(t){this.contentWindow.postMessage({jsonrpc:g,...t},m)}}function E(){return typeof window>"u"||!window?"":`https://swap-dev-git-${window.location.hostname.replace("widget-configurator-git-","").replace("-cowswap.vercel.app","")}-cowswap.vercel.app`}const h={local:"http://localhost:3000",prod:"https://swap.cow.fi",dev:"https://dev.swap.cow.fi",pr:E()};var r=(s=>(s.SWAP="swap",s.LIMIT="limit",s.ADVANCED="advanced",s))(r||{});const c="_";function v(s){const t=h[s.env||"prod"],e=p(s),n=u(s);return t+"/#"+e+"?"+n}function p(s){const{chainId:t=1,sell:e,buy:n,tradeType:o=r.SWAP}=s,i=[(e==null?void 0:e.asset)||c,(n==null?void 0:n.asset)||c].map(encodeURIComponent).join("/");return`/${t}/widget/${o}/${i}`}function u(s){const{sell:t,buy:e,theme:n}=s,o=new URLSearchParams;return t!=null&&t.amount&&o.append("sellAmount",t.amount),e!=null&&e.amount&&o.append("buyAmount",e.amount),n&&o.append("theme",typeof n=="string"?n:n.baseTheme),o}const a="cowSwapWidget",w="640px",P="450px",W=20;function y(s,t={}){const{provider:e}=t,n=S(t);s.innerHTML="",s.appendChild(n);const{contentWindow:o}=n;if(!o)throw new Error("Iframe does not contain a window!");return T(o,t.appCode),A(n,t.height),e&&new l(o).onConnect(e),n.addEventListener("load",()=>{d(t,o)}),i=>d(i,o)}function S(s){const{width:t=P,height:e=w}=s,n=document.createElement("iframe");return n.src=v(s),n.width=t,n.height=e,n.style.border="0",n}function d(s,t){const e=p(s),n=u(s).toString();t.postMessage({key:a,method:"update",urlParams:{pathname:e,search:n},appParams:{...s,provider:void 0}},"*")}function T(s,t){window.addEventListener("message",e=>{e.data.key!==a||e.data.method!=="activate"||s.postMessage({key:a,method:"metaData",metaData:t?{appCode:t}:void 0},"*")})}function A(s,t=w){window.addEventListener("message",e=>{e.data.key!==a||e.data.method!=="iframeHeight"||(s.style.height=e.data.height?`${e.data.height+W}px`:t)})}exports.COWSWAP_URLS=h;exports.TradeType=r;exports.cowSwapWidget=y;
|
package/index.mjs
CHANGED
|
@@ -4,9 +4,9 @@ class f {
|
|
|
4
4
|
* Creates an instance of JsonRpcManager.
|
|
5
5
|
* @param contentWindow - The window for handling events.
|
|
6
6
|
*/
|
|
7
|
-
constructor(
|
|
8
|
-
this.contentWindow =
|
|
9
|
-
|
|
7
|
+
constructor(t) {
|
|
8
|
+
this.contentWindow = t, this.ethereumProvider = null, this.requests = {}, this.processEvent = (e) => {
|
|
9
|
+
e.data.jsonrpc === "2.0" && (this.ethereumProvider ? this.processRequest(e.data) : this.requests[e.data.id] = e.data);
|
|
10
10
|
}, window.addEventListener("message", this.processEvent);
|
|
11
11
|
}
|
|
12
12
|
/**
|
|
@@ -19,12 +19,12 @@ class f {
|
|
|
19
19
|
* Handles the 'connect' event and sets up event listeners for Ethereum provider events.
|
|
20
20
|
* @param ethereumProvider - The Ethereum provider to connect.
|
|
21
21
|
*/
|
|
22
|
-
onConnect(
|
|
23
|
-
this.ethereumProvider =
|
|
24
|
-
this.processRequest(this.requests[
|
|
25
|
-
}), this.requests = {}, g.forEach((
|
|
26
|
-
|
|
27
|
-
this.postMessage({ method:
|
|
22
|
+
onConnect(t) {
|
|
23
|
+
this.ethereumProvider = t, Object.keys(this.requests).forEach((e) => {
|
|
24
|
+
this.processRequest(this.requests[e]);
|
|
25
|
+
}), this.requests = {}, g.forEach((e) => {
|
|
26
|
+
t.on(e, (n) => {
|
|
27
|
+
this.postMessage({ method: e, params: [n] });
|
|
28
28
|
});
|
|
29
29
|
});
|
|
30
30
|
}
|
|
@@ -32,20 +32,20 @@ class f {
|
|
|
32
32
|
* Processes a JSON-RPC request and sends appropriate response or error via the content window.
|
|
33
33
|
* @param request - The JSON-RPC request to be processed.
|
|
34
34
|
*/
|
|
35
|
-
processRequest(
|
|
35
|
+
processRequest(t) {
|
|
36
36
|
if (!this.ethereumProvider)
|
|
37
37
|
return;
|
|
38
|
-
(
|
|
39
|
-
this.postMessage({ id:
|
|
38
|
+
(t.method === "enable" ? this.ethereumProvider.enable() : this.ethereumProvider.request(t)).then((n) => {
|
|
39
|
+
this.postMessage({ id: t.id, result: n });
|
|
40
40
|
}).catch((n) => {
|
|
41
|
-
this.postMessage({ id:
|
|
41
|
+
this.postMessage({ id: t.id, error: n });
|
|
42
42
|
});
|
|
43
43
|
}
|
|
44
|
-
postMessage(
|
|
44
|
+
postMessage(t) {
|
|
45
45
|
this.contentWindow.postMessage(
|
|
46
46
|
{
|
|
47
47
|
jsonrpc: w,
|
|
48
|
-
...
|
|
48
|
+
...t
|
|
49
49
|
},
|
|
50
50
|
m
|
|
51
51
|
);
|
|
@@ -60,44 +60,43 @@ const E = {
|
|
|
60
60
|
dev: "https://dev.swap.cow.fi",
|
|
61
61
|
pr: l()
|
|
62
62
|
};
|
|
63
|
-
var
|
|
63
|
+
var d = /* @__PURE__ */ ((s) => (s.SWAP = "swap", s.LIMIT = "limit", s.ADVANCED = "advanced", s))(d || {});
|
|
64
|
+
const r = "_";
|
|
64
65
|
function v(s) {
|
|
65
|
-
const
|
|
66
|
-
return
|
|
67
|
-
}
|
|
68
|
-
function d(s) {
|
|
69
|
-
const { chainId: e = 1, sell: t, buy: n, tradeType: o = c.SWAP } = s, i = [];
|
|
70
|
-
t?.asset && i.push(t.asset), n?.asset && i.push(n.asset);
|
|
71
|
-
const u = i.map(encodeURIComponent).join("/");
|
|
72
|
-
return `/${e}/widget/${o}/${u}`;
|
|
66
|
+
const t = E[s.env || "prod"], e = h(s), n = p(s);
|
|
67
|
+
return t + "/#" + e + "?" + n;
|
|
73
68
|
}
|
|
74
69
|
function h(s) {
|
|
75
|
-
const { sell: e, buy:
|
|
76
|
-
return
|
|
70
|
+
const { chainId: t = 1, sell: e, buy: n, tradeType: o = d.SWAP } = s, i = [(e == null ? void 0 : e.asset) || r, (n == null ? void 0 : n.asset) || r].map(encodeURIComponent).join("/");
|
|
71
|
+
return `/${t}/widget/${o}/${i}`;
|
|
72
|
+
}
|
|
73
|
+
function p(s) {
|
|
74
|
+
const { sell: t, buy: e, theme: n } = s, o = new URLSearchParams();
|
|
75
|
+
return t != null && t.amount && o.append("sellAmount", t.amount), e != null && e.amount && o.append("buyAmount", e.amount), n && o.append("theme", typeof n == "string" ? n : n.baseTheme), o;
|
|
77
76
|
}
|
|
78
|
-
const a = "cowSwapWidget",
|
|
79
|
-
function
|
|
80
|
-
const { provider:
|
|
77
|
+
const a = "cowSwapWidget", u = "640px", P = "450px", W = 20;
|
|
78
|
+
function M(s, t = {}) {
|
|
79
|
+
const { provider: e } = t, n = y(t);
|
|
81
80
|
s.innerHTML = "", s.appendChild(n);
|
|
82
81
|
const { contentWindow: o } = n;
|
|
83
82
|
if (!o)
|
|
84
83
|
throw new Error("Iframe does not contain a window!");
|
|
85
|
-
return
|
|
86
|
-
|
|
87
|
-
}), (i) =>
|
|
84
|
+
return T(o, t.appCode), A(n, t.height), e && new f(o).onConnect(e), n.addEventListener("load", () => {
|
|
85
|
+
c(t, o);
|
|
86
|
+
}), (i) => c(i, o);
|
|
88
87
|
}
|
|
89
|
-
function
|
|
90
|
-
const { width:
|
|
91
|
-
return n.src = v(s), n.width =
|
|
88
|
+
function y(s) {
|
|
89
|
+
const { width: t = P, height: e = u } = s, n = document.createElement("iframe");
|
|
90
|
+
return n.src = v(s), n.width = t, n.height = e, n.style.border = "0", n;
|
|
92
91
|
}
|
|
93
|
-
function
|
|
94
|
-
const
|
|
95
|
-
|
|
92
|
+
function c(s, t) {
|
|
93
|
+
const e = h(s), n = p(s).toString();
|
|
94
|
+
t.postMessage(
|
|
96
95
|
{
|
|
97
96
|
key: a,
|
|
98
97
|
method: "update",
|
|
99
98
|
urlParams: {
|
|
100
|
-
pathname:
|
|
99
|
+
pathname: e,
|
|
101
100
|
search: n
|
|
102
101
|
},
|
|
103
102
|
appParams: {
|
|
@@ -108,25 +107,25 @@ function r(s, e) {
|
|
|
108
107
|
"*"
|
|
109
108
|
);
|
|
110
109
|
}
|
|
111
|
-
function
|
|
112
|
-
window.addEventListener("message", (
|
|
113
|
-
|
|
110
|
+
function T(s, t) {
|
|
111
|
+
window.addEventListener("message", (e) => {
|
|
112
|
+
e.data.key !== a || e.data.method !== "activate" || s.postMessage(
|
|
114
113
|
{
|
|
115
114
|
key: a,
|
|
116
115
|
method: "metaData",
|
|
117
|
-
metaData:
|
|
116
|
+
metaData: t ? { appCode: t } : void 0
|
|
118
117
|
},
|
|
119
118
|
"*"
|
|
120
119
|
);
|
|
121
120
|
});
|
|
122
121
|
}
|
|
123
|
-
function
|
|
124
|
-
window.addEventListener("message", (
|
|
125
|
-
|
|
122
|
+
function A(s, t = u) {
|
|
123
|
+
window.addEventListener("message", (e) => {
|
|
124
|
+
e.data.key !== a || e.data.method !== "iframeHeight" || (s.style.height = e.data.height ? `${e.data.height + W}px` : t);
|
|
126
125
|
});
|
|
127
126
|
}
|
|
128
127
|
export {
|
|
129
128
|
E as COWSWAP_URLS,
|
|
130
|
-
|
|
131
|
-
|
|
129
|
+
d as TradeType,
|
|
130
|
+
M as cowSwapWidget
|
|
132
131
|
};
|
package/package.json
CHANGED
package/types.d.ts
CHANGED
|
@@ -51,10 +51,15 @@ export declare enum TradeType {
|
|
|
51
51
|
}
|
|
52
52
|
export interface CowSwapWidgetPalette {
|
|
53
53
|
baseTheme: CowSwapTheme;
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
54
|
+
primary: string;
|
|
55
|
+
background: string;
|
|
56
|
+
paper: string;
|
|
57
|
+
text: string;
|
|
58
|
+
danger: string;
|
|
59
|
+
warning: string;
|
|
60
|
+
alert: string;
|
|
61
|
+
info: string;
|
|
62
|
+
success: string;
|
|
58
63
|
}
|
|
59
64
|
interface CowSwapWidgetConfig {
|
|
60
65
|
/**
|
|
@@ -78,6 +83,10 @@ interface CowSwapWidgetConfig {
|
|
|
78
83
|
* Network ID.
|
|
79
84
|
*/
|
|
80
85
|
chainId: SupportedChainId;
|
|
86
|
+
/**
|
|
87
|
+
* The token lists urls to use in the widget
|
|
88
|
+
*/
|
|
89
|
+
tokenLists: string[];
|
|
81
90
|
/**
|
|
82
91
|
* Swap, Limit or Advanced (Twap).
|
|
83
92
|
*/
|