@cowprotocol/widget-lib 0.0.1 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/consts.d.ts +3 -0
- package/index.js +1 -1
- package/index.mjs +44 -44
- package/package.json +22 -3
- package/types.d.ts +6 -6
- /package/{urlUtils.d.ts → utils.d.ts} +0 -0
package/consts.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const d="2.0",
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const d=1,h=400,u=640,r={local:"http://localhost:3000",barn:"https://barn.cow.fi",prod:"http://swap.cow.fi"},p="2.0",l="*",m=["connect","disconnect","chainChanged","accountsChanged"];class w{constructor(e){this.contentWindow=e,this.ethereumProvider=null,this.requests={},this.processEvent=t=>{t.data.jsonrpc==="2.0"&&(this.ethereumProvider?this.processRequest(t.data):this.requests[t.data.id]=t.data)},window.addEventListener("message",this.processEvent)}disconnect(){this.ethereumProvider=null,window.removeEventListener("message",this.processEvent)}onConnect(e){this.ethereumProvider=e,Object.keys(this.requests).forEach(t=>{this.processRequest(this.requests[t])}),this.requests={},m.forEach(t=>{e.on(t,s=>{this.postMessage({method:t,params:[s]})})})}processRequest(e){if(!this.ethereumProvider)return;(e.method==="enable"?this.ethereumProvider.enable():this.ethereumProvider.request(e)).then(s=>{this.postMessage({id:e.id,result:s})}).catch(s=>{this.postMessage({id:e.id,error:s})})}postMessage(e){this.contentWindow.postMessage({jsonrpc:p,...e},l)}}function f(n){const e=r[n.env??"prod"],t=c(n),s=a(n);return e+"/#"+t+"?"+s}function c(n){const{chainId:e=d,tradeAssets:t}=n,s=t?[t.sell.asset,t.buy.asset].map(encodeURIComponent).join("/"):"";return`/${e}/swap/widget/${s}`}function a(n){const{tradeAssets:e,theme:t}=n,s=new URLSearchParams;if(e){const{sell:o,buy:i}=e;o.amount&&s.append("sellAmount",o.amount),i.amount&&s.append("buyAmount",i.amount)}return t&&s.append("theme",t),s}const g="@cowprotocol/widget-lib";function E(n){const{container:e,provider:t}=n,s=b(n);e.innerHTML="",e.appendChild(s);const{contentWindow:o}=s;if(!o)throw new Error("Iframe does not contain a window!");return t&&new w(o).onConnect(t),i=>W(i,o)}function W(n,e){const t=c(n),s=a(n).toString();e.postMessage({key:g,pathname:t,search:s},"*")}function b(n){const{width:e=h,height:t=u}=n,s=document.createElement("iframe");return s.src=f(n.urlParams??{}),s.width=`${e}px`,s.height=`${t}px`,s.style.border="0",s}exports.COWSWAP_URLS=r;exports.cowSwapWidget=E;
|
package/index.mjs
CHANGED
|
@@ -1,86 +1,86 @@
|
|
|
1
|
-
const a =
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
const a = {
|
|
2
|
+
local: "http://localhost:3000",
|
|
3
|
+
barn: "https://barn.cow.fi",
|
|
4
|
+
prod: "http://swap.cow.fi"
|
|
5
|
+
}, h = "2.0", d = "*", u = ["connect", "disconnect", "chainChanged", "accountsChanged"];
|
|
6
|
+
class p {
|
|
7
|
+
constructor(e) {
|
|
8
|
+
this.contentWindow = e, this.ethereumProvider = null, this.requests = {}, this.processEvent = (t) => {
|
|
9
|
+
t.data.jsonrpc === "2.0" && (this.ethereumProvider ? this.processRequest(t.data) : this.requests[t.data.id] = t.data);
|
|
6
10
|
}, window.addEventListener("message", this.processEvent);
|
|
7
11
|
}
|
|
8
12
|
disconnect() {
|
|
9
13
|
this.ethereumProvider = null, window.removeEventListener("message", this.processEvent);
|
|
10
14
|
}
|
|
11
|
-
onConnect(
|
|
12
|
-
this.ethereumProvider =
|
|
13
|
-
this.processRequest(this.requests[
|
|
14
|
-
}), this.requests = {},
|
|
15
|
-
|
|
16
|
-
this.postMessage({ method:
|
|
15
|
+
onConnect(e) {
|
|
16
|
+
this.ethereumProvider = e, Object.keys(this.requests).forEach((t) => {
|
|
17
|
+
this.processRequest(this.requests[t]);
|
|
18
|
+
}), this.requests = {}, u.forEach((t) => {
|
|
19
|
+
e.on(t, (s) => {
|
|
20
|
+
this.postMessage({ method: t, params: [s] });
|
|
17
21
|
});
|
|
18
22
|
});
|
|
19
23
|
}
|
|
20
|
-
processRequest(
|
|
24
|
+
processRequest(e) {
|
|
21
25
|
if (!this.ethereumProvider)
|
|
22
26
|
return;
|
|
23
|
-
(
|
|
24
|
-
this.postMessage({ id:
|
|
27
|
+
(e.method === "enable" ? this.ethereumProvider.enable() : this.ethereumProvider.request(e)).then((s) => {
|
|
28
|
+
this.postMessage({ id: e.id, result: s });
|
|
25
29
|
}).catch((s) => {
|
|
26
|
-
this.postMessage({ id:
|
|
30
|
+
this.postMessage({ id: e.id, error: s });
|
|
27
31
|
});
|
|
28
32
|
}
|
|
29
|
-
postMessage(
|
|
33
|
+
postMessage(e) {
|
|
30
34
|
this.contentWindow.postMessage(
|
|
31
35
|
{
|
|
32
|
-
jsonrpc:
|
|
33
|
-
...
|
|
36
|
+
jsonrpc: h,
|
|
37
|
+
...e
|
|
34
38
|
},
|
|
35
|
-
|
|
39
|
+
d
|
|
36
40
|
);
|
|
37
41
|
}
|
|
38
42
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
};
|
|
43
|
-
function m(n) {
|
|
44
|
-
const t = p[n.env], e = r(n), s = c(n);
|
|
45
|
-
return t + "/#" + e + "?" + s;
|
|
43
|
+
function l(n) {
|
|
44
|
+
const e = a[n.env ?? "prod"], t = r(n), s = c(n);
|
|
45
|
+
return e + "/#" + t + "?" + s;
|
|
46
46
|
}
|
|
47
47
|
function r(n) {
|
|
48
|
-
const { chainId:
|
|
49
|
-
return `/${
|
|
48
|
+
const { chainId: e = 1, tradeAssets: t } = n, s = t ? [t.sell.asset, t.buy.asset].map(encodeURIComponent).join("/") : "";
|
|
49
|
+
return `/${e}/swap/widget/${s}`;
|
|
50
50
|
}
|
|
51
51
|
function c(n) {
|
|
52
|
-
const { tradeAssets:
|
|
53
|
-
if (
|
|
54
|
-
const { sell: o, buy: i } =
|
|
52
|
+
const { tradeAssets: e, theme: t } = n, s = new URLSearchParams();
|
|
53
|
+
if (e) {
|
|
54
|
+
const { sell: o, buy: i } = e;
|
|
55
55
|
o.amount && s.append("sellAmount", o.amount), i.amount && s.append("buyAmount", i.amount);
|
|
56
56
|
}
|
|
57
|
-
return
|
|
57
|
+
return t && s.append("theme", t), s;
|
|
58
58
|
}
|
|
59
|
-
const
|
|
59
|
+
const m = "@cowprotocol/widget-lib";
|
|
60
60
|
function g(n) {
|
|
61
|
-
const { container:
|
|
62
|
-
|
|
61
|
+
const { container: e, provider: t } = n, s = w(n);
|
|
62
|
+
e.innerHTML = "", e.appendChild(s);
|
|
63
63
|
const { contentWindow: o } = s;
|
|
64
64
|
if (!o)
|
|
65
65
|
throw new Error("Iframe does not contain a window!");
|
|
66
|
-
return
|
|
66
|
+
return t && new p(o).onConnect(t), (i) => f(i, o);
|
|
67
67
|
}
|
|
68
|
-
function f(n,
|
|
69
|
-
const
|
|
70
|
-
|
|
68
|
+
function f(n, e) {
|
|
69
|
+
const t = r(n), s = c(n).toString();
|
|
70
|
+
e.postMessage(
|
|
71
71
|
{
|
|
72
|
-
key:
|
|
73
|
-
pathname:
|
|
72
|
+
key: m,
|
|
73
|
+
pathname: t,
|
|
74
74
|
search: s
|
|
75
75
|
},
|
|
76
76
|
"*"
|
|
77
77
|
);
|
|
78
78
|
}
|
|
79
79
|
function w(n) {
|
|
80
|
-
const { width:
|
|
81
|
-
return s.src =
|
|
80
|
+
const { width: e = 400, height: t = 640 } = n, s = document.createElement("iframe");
|
|
81
|
+
return s.src = l(n.urlParams ?? {}), s.width = `${e}px`, s.height = `${t}px`, s.style.border = "0", s;
|
|
82
82
|
}
|
|
83
83
|
export {
|
|
84
|
-
|
|
84
|
+
a as COWSWAP_URLS,
|
|
85
85
|
g as cowSwapWidget
|
|
86
86
|
};
|
package/package.json
CHANGED
|
@@ -1,5 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cowprotocol/widget-lib",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"type": "commonjs"
|
|
5
|
-
|
|
3
|
+
"version": "0.0.3",
|
|
4
|
+
"type": "commonjs",
|
|
5
|
+
"description": "CoW Swap Widget Library. Allows you to easily embed a CoW Swap widget on your website.",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
"import": "./index.js",
|
|
9
|
+
"require": "./index.mjs"
|
|
10
|
+
},
|
|
11
|
+
"license": "ISC",
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "https://github.com/cowprotocol/cowswap"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"cowprotocol",
|
|
18
|
+
"widget",
|
|
19
|
+
"cowswap",
|
|
20
|
+
"dex",
|
|
21
|
+
"widget-lib"
|
|
22
|
+
],
|
|
23
|
+
"dependencies": {}
|
|
24
|
+
}
|
package/types.d.ts
CHANGED
|
@@ -13,7 +13,7 @@ export interface EthereumProvider {
|
|
|
13
13
|
request<T>(params: JsonRpcRequest): Promise<T>;
|
|
14
14
|
enable(): Promise<void>;
|
|
15
15
|
}
|
|
16
|
-
export type CowSwapWidgetEnv = 'local' | 'prod';
|
|
16
|
+
export type CowSwapWidgetEnv = 'local' | 'prod' | 'barn';
|
|
17
17
|
export type CowSwapTheme = 'dark' | 'light';
|
|
18
18
|
interface TradeAsset {
|
|
19
19
|
asset: string;
|
|
@@ -24,16 +24,16 @@ export interface TradeAssets {
|
|
|
24
24
|
buy: TradeAsset;
|
|
25
25
|
}
|
|
26
26
|
export interface CowSwapWidgetUrlParams {
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
chainId?: number;
|
|
28
|
+
env?: CowSwapWidgetEnv;
|
|
29
29
|
tradeAssets?: TradeAssets;
|
|
30
30
|
theme?: CowSwapTheme;
|
|
31
31
|
}
|
|
32
32
|
export interface CowSwapWidgetParams {
|
|
33
|
-
urlParams: CowSwapWidgetUrlParams;
|
|
34
|
-
width: number;
|
|
35
|
-
height: number;
|
|
36
33
|
container: HTMLElement;
|
|
34
|
+
width?: number;
|
|
35
|
+
height?: number;
|
|
36
|
+
urlParams?: CowSwapWidgetUrlParams;
|
|
37
37
|
provider?: EthereumProvider;
|
|
38
38
|
}
|
|
39
39
|
export {};
|
|
File without changes
|