@cowprotocol/widget-react 0.0.1 → 0.1.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/README.md +58 -0
- package/index.d.ts +1 -0
- package/index.js +1 -1
- package/index.mjs +66 -45
- package/lib/CowSwapWidget.d.ts +4 -4
- package/package.json +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# CoW Swap React widget
|
|
2
|
+
|
|
3
|
+
React component that creates a CoW Swap widget. It is based on [https://npmjs.com/package/@cowprotocol/widget-lib](https://npmjs.com/package/@cowprotocol/widget-lib)
|
|
4
|
+
|
|
5
|
+
## Use it
|
|
6
|
+
|
|
7
|
+
Install dependency
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
# Install the dependency using NPM
|
|
11
|
+
npm install @cowprotocol/widget-react --save
|
|
12
|
+
|
|
13
|
+
# ...or alternativelly use YARN
|
|
14
|
+
yarn add @cowprotocol/widget-react
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Import component and some convenient types
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { CowSwapWidget, CowSwapWidgetParams, CowSwapWidgetSettings } from '@cowprotocol/widget-react'
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Prepare the config for the widget:
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
const cowSwapWidgetParams: CowSwapWidgetParams = {
|
|
27
|
+
container: document.getElementById('cow-swap-widget'),
|
|
28
|
+
metaData: {
|
|
29
|
+
appKey: '<YOUR_APP_KEY>',
|
|
30
|
+
appUrl: '<YOUR_APP_URL>',
|
|
31
|
+
},
|
|
32
|
+
width: 600,
|
|
33
|
+
height: 700,
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const cowSwapWidgetSettings: CowSwapWidgetSettings = {
|
|
37
|
+
tradeType: 'swap',
|
|
38
|
+
}
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Render the component:
|
|
42
|
+
|
|
43
|
+
```jsx
|
|
44
|
+
<CowSwapWidget params={cowSwapWidgetParams} settings={cowSwapWidgetSettings} />
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Developers
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
# Test
|
|
51
|
+
nx test widget-react
|
|
52
|
+
|
|
53
|
+
# Build the library
|
|
54
|
+
nx build widget-react
|
|
55
|
+
|
|
56
|
+
# Publish to NPM
|
|
57
|
+
nx publish widget-react --ver x.y.z --tag latest
|
|
58
|
+
```
|
package/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const p=require("react/jsx-runtime"),i=require("react"),l="2.0",m="*",w=["connect","disconnect","close","chainChanged","accountsChanged"];class g{constructor(e){this.contentWindow=e,window.addEventListener("message",this.processEvent)}ethereumProvider=null;requests={};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={},w.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})})}processEvent=e=>{e.data.jsonrpc==="2.0"&&(this.ethereumProvider?this.processRequest(e.data):this.requests[e.data.id]=e.data)};postMessage(e){this.contentWindow.postMessage({jsonrpc:l,...e},m)}}const E={local:"http://localhost:3000",prod:"https://swap.cow.fi",dev:"https://dev.swap.cow.fi",pr:window.location.origin};var u=(n=>(n.SWAP="swap",n.LIMIT="limit",n.ADVANCED="advanced",n))(u||{});function R(n){const e=E[n.env||"prod"],t=h(n),s=f(n);return e+"/#"+t+"?"+s}function h(n){const{chainId:e=1,tradeAssets:t,tradeType:s=u.SWAP}=n,o=t?[t.sell.asset,t.buy.asset].map(encodeURIComponent).join("/"):"";return`/${e}/widget/${s}/${o}`}function f(n){const{tradeAssets:e,theme:t}=n,s=new URLSearchParams;if(e){const{sell:o,buy:r}=e;o.amount&&s.append("sellAmount",o.amount),r.amount&&s.append("buyAmount",r.amount)}return t&&s.append("theme",t),s}const a="cowSwapWidget";function y(n,e){const{container:t,provider:s}=n,o=W(n,e);t.innerHTML="",t.appendChild(o);const{contentWindow:r}=o;if(!r)throw new Error("Iframe does not contain a window!");return P(r,n.metaData),S(o,n.height),s&&new g(r).onConnect(s),o.addEventListener("load",()=>{d(e,r,o)}),c=>d(c,r,o)}function W(n,e){const{width:t,height:s}=n,o=document.createElement("iframe");return o.src=R(e),o.width=`${t}px`,o.height=`${s}px`,o.style.border="0",o}function d(n,e,t){const s=h(n),o=f(n).toString();n.dynamicHeightEnabled||(t.style.height=""),e.postMessage({key:a,method:"update",urlParams:{pathname:s,search:o},appParams:n},"*")}function P(n,e){window.addEventListener("message",t=>{t.data.key!==a||t.data.method!=="activate"||n.postMessage({key:a,method:"metaData",metaData:e},"*")})}function S(n,e){window.addEventListener("message",t=>{if(t.data.key!==a||t.data.method!=="iframeHeight")return;const s=t.data.height||e;n.style.height=`${s}px`})}function v({params:n,settings:e,provider:t}){const s=i.useRef(),o=i.useRef(null),r=i.useRef(null);return i.useEffect(()=>{o.current&&(t&&s.current!==t&&(r.current=null),r.current?r.current(e):r.current=y({...n,container:o.current},e))},[t,n,e]),i.useEffect(()=>{s.current=t},[t]),p.jsx("div",{ref:o})}exports.CowSwapWidget=v;
|
package/index.mjs
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import { jsx as
|
|
2
|
-
import { useRef as
|
|
3
|
-
const
|
|
4
|
-
class
|
|
1
|
+
import { jsx as f } from "react/jsx-runtime";
|
|
2
|
+
import { useRef as i, useEffect as d } from "react";
|
|
3
|
+
const l = "2.0", w = "*", g = ["connect", "disconnect", "close", "chainChanged", "accountsChanged"];
|
|
4
|
+
class E {
|
|
5
5
|
constructor(e) {
|
|
6
|
-
this.contentWindow = e,
|
|
7
|
-
t.data.jsonrpc === "2.0" && (this.ethereumProvider ? this.processRequest(t.data) : this.requests[t.data.id] = t.data);
|
|
8
|
-
}, window.addEventListener("message", this.processEvent);
|
|
6
|
+
this.contentWindow = e, window.addEventListener("message", this.processEvent);
|
|
9
7
|
}
|
|
8
|
+
ethereumProvider = null;
|
|
9
|
+
requests = {};
|
|
10
10
|
disconnect() {
|
|
11
11
|
this.ethereumProvider = null, window.removeEventListener("message", this.processEvent);
|
|
12
12
|
}
|
|
13
13
|
onConnect(e) {
|
|
14
14
|
this.ethereumProvider = e, Object.keys(this.requests).forEach((t) => {
|
|
15
15
|
this.processRequest(this.requests[t]);
|
|
16
|
-
}), this.requests = {},
|
|
16
|
+
}), this.requests = {}, g.forEach((t) => {
|
|
17
17
|
e.on(t, (s) => {
|
|
18
18
|
this.postMessage({ method: t, params: [s] });
|
|
19
19
|
});
|
|
@@ -28,29 +28,35 @@ class l {
|
|
|
28
28
|
this.postMessage({ id: e.id, error: s });
|
|
29
29
|
});
|
|
30
30
|
}
|
|
31
|
+
processEvent = (e) => {
|
|
32
|
+
e.data.jsonrpc === "2.0" && (this.ethereumProvider ? this.processRequest(e.data) : this.requests[e.data.id] = e.data);
|
|
33
|
+
};
|
|
31
34
|
postMessage(e) {
|
|
32
35
|
this.contentWindow.postMessage(
|
|
33
36
|
{
|
|
34
|
-
jsonrpc:
|
|
37
|
+
jsonrpc: l,
|
|
35
38
|
...e
|
|
36
39
|
},
|
|
37
|
-
|
|
40
|
+
w
|
|
38
41
|
);
|
|
39
42
|
}
|
|
40
43
|
}
|
|
41
|
-
const
|
|
44
|
+
const W = {
|
|
42
45
|
local: "http://localhost:3000",
|
|
43
|
-
prod: "https://swap.cow.fi"
|
|
46
|
+
prod: "https://swap.cow.fi",
|
|
47
|
+
dev: "https://dev.swap.cow.fi",
|
|
48
|
+
pr: window.location.origin
|
|
44
49
|
};
|
|
45
|
-
|
|
46
|
-
|
|
50
|
+
var u = /* @__PURE__ */ ((n) => (n.SWAP = "swap", n.LIMIT = "limit", n.ADVANCED = "advanced", n))(u || {});
|
|
51
|
+
function y(n) {
|
|
52
|
+
const e = W[n.env || "prod"], t = p(n), s = m(n);
|
|
47
53
|
return e + "/#" + t + "?" + s;
|
|
48
54
|
}
|
|
49
|
-
function
|
|
50
|
-
const { chainId: e, tradeAssets: t, tradeType: s } = n, o = t ? [t.sell.asset, t.buy.asset].map(encodeURIComponent).join("/") : "";
|
|
55
|
+
function p(n) {
|
|
56
|
+
const { chainId: e = 1, tradeAssets: t, tradeType: s = u.SWAP } = n, o = t ? [t.sell.asset, t.buy.asset].map(encodeURIComponent).join("/") : "";
|
|
51
57
|
return `/${e}/widget/${s}/${o}`;
|
|
52
58
|
}
|
|
53
|
-
function
|
|
59
|
+
function m(n) {
|
|
54
60
|
const { tradeAssets: e, theme: t } = n, s = new URLSearchParams();
|
|
55
61
|
if (e) {
|
|
56
62
|
const { sell: o, buy: r } = e;
|
|
@@ -58,49 +64,64 @@ function c(n) {
|
|
|
58
64
|
}
|
|
59
65
|
return t && s.append("theme", t), s;
|
|
60
66
|
}
|
|
61
|
-
const
|
|
62
|
-
function
|
|
67
|
+
const a = "cowSwapWidget";
|
|
68
|
+
function P(n, e) {
|
|
63
69
|
const { container: t, provider: s } = n, o = R(n, e);
|
|
64
70
|
t.innerHTML = "", t.appendChild(o);
|
|
65
71
|
const { contentWindow: r } = o;
|
|
66
72
|
if (!r)
|
|
67
73
|
throw new Error("Iframe does not contain a window!");
|
|
68
|
-
return s && new
|
|
74
|
+
return v(r, n.metaData), M(o, n.height), s && new E(r).onConnect(s), o.addEventListener("load", () => {
|
|
75
|
+
h(e, r, o);
|
|
76
|
+
}), (c) => h(c, r, o);
|
|
69
77
|
}
|
|
70
|
-
function
|
|
71
|
-
const t =
|
|
72
|
-
e.
|
|
78
|
+
function R(n, e) {
|
|
79
|
+
const { width: t, height: s } = n, o = document.createElement("iframe");
|
|
80
|
+
return o.src = y(e), o.width = `${t}px`, o.height = `${s}px`, o.style.border = "0", o;
|
|
81
|
+
}
|
|
82
|
+
function h(n, e, t) {
|
|
83
|
+
const s = p(n), o = m(n).toString();
|
|
84
|
+
n.dynamicHeightEnabled || (t.style.height = ""), e.postMessage(
|
|
73
85
|
{
|
|
74
|
-
key:
|
|
86
|
+
key: a,
|
|
75
87
|
method: "update",
|
|
76
88
|
urlParams: {
|
|
77
|
-
pathname:
|
|
78
|
-
search:
|
|
89
|
+
pathname: s,
|
|
90
|
+
search: o
|
|
79
91
|
},
|
|
80
|
-
appParams: n
|
|
92
|
+
appParams: n
|
|
81
93
|
},
|
|
82
94
|
"*"
|
|
83
95
|
);
|
|
84
96
|
}
|
|
85
|
-
function
|
|
86
|
-
|
|
87
|
-
|
|
97
|
+
function v(n, e) {
|
|
98
|
+
window.addEventListener("message", (t) => {
|
|
99
|
+
t.data.key !== a || t.data.method !== "activate" || n.postMessage(
|
|
100
|
+
{
|
|
101
|
+
key: a,
|
|
102
|
+
method: "metaData",
|
|
103
|
+
metaData: e
|
|
104
|
+
},
|
|
105
|
+
"*"
|
|
106
|
+
);
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
function M(n, e) {
|
|
110
|
+
window.addEventListener("message", (t) => {
|
|
111
|
+
if (t.data.key !== a || t.data.method !== "iframeHeight")
|
|
112
|
+
return;
|
|
113
|
+
const s = t.data.height || e;
|
|
114
|
+
n.style.height = `${s}px`;
|
|
115
|
+
});
|
|
88
116
|
}
|
|
89
|
-
function
|
|
90
|
-
const
|
|
91
|
-
return
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
...t,
|
|
97
|
-
container: e.current
|
|
98
|
-
},
|
|
99
|
-
s
|
|
100
|
-
);
|
|
101
|
-
}
|
|
102
|
-
}, [e, n]), /* @__PURE__ */ d("div", { ref: e });
|
|
117
|
+
function S({ params: n, settings: e, provider: t }) {
|
|
118
|
+
const s = i(), o = i(null), r = i(null);
|
|
119
|
+
return d(() => {
|
|
120
|
+
o.current && (t && s.current !== t && (r.current = null), r.current ? r.current(e) : r.current = P({ ...n, container: o.current }, e));
|
|
121
|
+
}, [t, n, e]), d(() => {
|
|
122
|
+
s.current = t;
|
|
123
|
+
}, [t]), /* @__PURE__ */ f("div", { ref: o });
|
|
103
124
|
}
|
|
104
125
|
export {
|
|
105
|
-
|
|
126
|
+
S as CowSwapWidget
|
|
106
127
|
};
|
package/lib/CowSwapWidget.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { CowSwapWidgetParams
|
|
2
|
-
export type CowSwapWidgetParams = Omit<CowSwapWidgetParamsAux, 'container'>;
|
|
1
|
+
import { CowSwapWidgetParams, CowSwapWidgetSettings, EthereumProvider } from '@cowprotocol/widget-lib';
|
|
3
2
|
export interface CowSwapWidgetProps {
|
|
4
|
-
params: CowSwapWidgetParams
|
|
3
|
+
params: Omit<CowSwapWidgetParams, 'container'>;
|
|
5
4
|
settings: CowSwapWidgetSettings;
|
|
5
|
+
provider?: EthereumProvider;
|
|
6
6
|
}
|
|
7
|
-
export declare function CowSwapWidget(
|
|
7
|
+
export declare function CowSwapWidget({ params, settings, provider }: CowSwapWidgetProps): import("react/jsx-runtime").JSX.Element;
|