@cowprotocol/widget-react 0.0.2 → 0.1.1
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 +52 -0
- package/index.d.ts +1 -0
- package/index.js +1 -1
- package/index.mjs +98 -54
- package/lib/CowSwapWidget.d.ts +3 -4
- package/package.json +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
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
|
+
yarn add @cowprotocol/widget-react
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install @cowprotocol/widget-react
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Import component and some convenient types
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import { CowSwapWidget, CowSwapWidgetParams } from '@cowprotocol/widget-react'
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Prepare the config for the widget:
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
const cowSwapWidgetParams: CowSwapWidgetParams = {
|
|
28
|
+
appKey: '<YOUR_APP_KEY>',
|
|
29
|
+
width: '600px',
|
|
30
|
+
height: '700px',
|
|
31
|
+
tradeType: 'swap',
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Render the component:
|
|
36
|
+
|
|
37
|
+
```jsx
|
|
38
|
+
<CowSwapWidget params={cowSwapWidgetParams} />
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Developers
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
# Test
|
|
45
|
+
nx test widget-react
|
|
46
|
+
|
|
47
|
+
# Build the library
|
|
48
|
+
nx build widget-react
|
|
49
|
+
|
|
50
|
+
# Publish to NPM
|
|
51
|
+
nx publish widget-react --ver x.y.z --tag latest
|
|
52
|
+
```
|
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 f=require("react/jsx-runtime"),i=require("react"),l="2.0",g="*",m=["connect","disconnect","close","chainChanged","accountsChanged"];class w{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(n=>{this.processRequest(this.requests[n])}),this.requests={},m.forEach(n=>{e.on(n,s=>{this.postMessage({method:n,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},g)}}function E(){return`https://swap-dev-git-${location.hostname.replace("widget-configurator-git-","").replace("-cowswap.vercel.app","")}-cowswap.vercel.app`}const v={local:"http://localhost:3000",prod:"https://swap.cow.fi",dev:"https://dev.swap.cow.fi",pr:E()};var d=(t=>(t.SWAP="swap",t.LIMIT="limit",t.ADVANCED="advanced",t))(d||{});function y(t){const e=v[t.env||"prod"],n=u(t),s=h(t);return e+"/#"+n+"?"+s}function u(t){const{chainId:e=1,tradeAssets:n,tradeType:s=d.SWAP}=t,o=n?[n.sell.asset,n.buy.asset].map(encodeURIComponent).join("/"):"";return`/${e}/widget/${s}/${o}`}function h(t){const{tradeAssets:e,theme:n}=t,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 n&&s.append("theme",n),s}const c="cowSwapWidget",p="640px",R="450px";function W(t,e={}){const{provider:n}=e,s=P(e);t.innerHTML="",t.appendChild(s);const{contentWindow:o}=s;if(!o)throw new Error("Iframe does not contain a window!");return A(o,e.appKey),S(s,e.height),n&&new w(o).onConnect(n),s.addEventListener("load",()=>{a(e,o)}),r=>a(r,o)}function P(t){const{width:e=R,height:n=p}=t,s=document.createElement("iframe");return s.src=y(t),s.width=e,s.height=n,s.style.border="0",s}function a(t,e){const n=u(t),s=h(t).toString();e.postMessage({key:c,method:"update",urlParams:{pathname:n,search:s},appParams:{...t,provider:void 0}},"*")}function A(t,e){window.addEventListener("message",n=>{n.data.key!==c||n.data.method!=="activate"||t.postMessage({key:c,method:"metaData",metaData:e?{appKey:e}:void 0},"*")})}function S(t,e=p){window.addEventListener("message",n=>{n.data.key!==c||n.data.method!=="iframeHeight"||(t.style.height=n.data.height?`${n.data.height}px`:e)})}function C({params:t,provider:e}){const n=i.useRef(),s=i.useRef(null),o=i.useRef(null);return i.useEffect(()=>{s.current&&(e&&n.current!==e&&(o.current=null),o.current?o.current(t):o.current=W(s.current,t))},[e,t]),i.useEffect(()=>{n.current=e},[e]),f.jsx("div",{ref:s})}exports.CowSwapWidget=C;
|
package/index.mjs
CHANGED
|
@@ -1,24 +1,44 @@
|
|
|
1
|
-
import { jsx as
|
|
2
|
-
import { useRef as
|
|
3
|
-
const
|
|
4
|
-
class
|
|
1
|
+
import { jsx as l } from "react/jsx-runtime";
|
|
2
|
+
import { useRef as a, useEffect as c } from "react";
|
|
3
|
+
const m = "2.0", g = "*", w = ["connect", "disconnect", "close", "chainChanged", "accountsChanged"];
|
|
4
|
+
class E {
|
|
5
|
+
/**
|
|
6
|
+
* Creates an instance of JsonRpcManager.
|
|
7
|
+
* @param contentWindow - The window for handling events.
|
|
8
|
+
*/
|
|
5
9
|
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);
|
|
10
|
+
this.contentWindow = e, window.addEventListener("message", this.processEvent);
|
|
9
11
|
}
|
|
12
|
+
/**
|
|
13
|
+
* The Ethereum provider instance.
|
|
14
|
+
* When is null the JSON-RPC manager is disconnected from the Ethereum provider.
|
|
15
|
+
* */
|
|
16
|
+
ethereumProvider = null;
|
|
17
|
+
/** Stored JSON-RPC requests. */
|
|
18
|
+
requests = {};
|
|
19
|
+
/**
|
|
20
|
+
* Disconnects the JSON-RPC manager from the Ethereum provider.
|
|
21
|
+
*/
|
|
10
22
|
disconnect() {
|
|
11
23
|
this.ethereumProvider = null, window.removeEventListener("message", this.processEvent);
|
|
12
24
|
}
|
|
25
|
+
/**
|
|
26
|
+
* Handles the 'connect' event and sets up event listeners for Ethereum provider events.
|
|
27
|
+
* @param ethereumProvider - The Ethereum provider to connect.
|
|
28
|
+
*/
|
|
13
29
|
onConnect(e) {
|
|
14
|
-
this.ethereumProvider = e, Object.keys(this.requests).forEach((
|
|
15
|
-
this.processRequest(this.requests[
|
|
16
|
-
}), this.requests = {},
|
|
17
|
-
e.on(
|
|
18
|
-
this.postMessage({ method:
|
|
30
|
+
this.ethereumProvider = e, Object.keys(this.requests).forEach((n) => {
|
|
31
|
+
this.processRequest(this.requests[n]);
|
|
32
|
+
}), this.requests = {}, w.forEach((n) => {
|
|
33
|
+
e.on(n, (s) => {
|
|
34
|
+
this.postMessage({ method: n, params: [s] });
|
|
19
35
|
});
|
|
20
36
|
});
|
|
21
37
|
}
|
|
38
|
+
/**
|
|
39
|
+
* Processes a JSON-RPC request and sends appropriate response or error via the content window.
|
|
40
|
+
* @param request - The JSON-RPC request to be processed.
|
|
41
|
+
*/
|
|
22
42
|
processRequest(e) {
|
|
23
43
|
if (!this.ethereumProvider)
|
|
24
44
|
return;
|
|
@@ -28,79 +48,103 @@ class l {
|
|
|
28
48
|
this.postMessage({ id: e.id, error: s });
|
|
29
49
|
});
|
|
30
50
|
}
|
|
51
|
+
processEvent = (e) => {
|
|
52
|
+
e.data.jsonrpc === "2.0" && (this.ethereumProvider ? this.processRequest(e.data) : this.requests[e.data.id] = e.data);
|
|
53
|
+
};
|
|
31
54
|
postMessage(e) {
|
|
32
55
|
this.contentWindow.postMessage(
|
|
33
56
|
{
|
|
34
|
-
jsonrpc:
|
|
57
|
+
jsonrpc: m,
|
|
35
58
|
...e
|
|
36
59
|
},
|
|
37
|
-
|
|
60
|
+
g
|
|
38
61
|
);
|
|
39
62
|
}
|
|
40
63
|
}
|
|
41
|
-
|
|
64
|
+
function v() {
|
|
65
|
+
return `https://swap-dev-git-${location.hostname.replace("widget-configurator-git-", "").replace("-cowswap.vercel.app", "")}-cowswap.vercel.app`;
|
|
66
|
+
}
|
|
67
|
+
const W = {
|
|
42
68
|
local: "http://localhost:3000",
|
|
43
|
-
prod: "https://swap.cow.fi"
|
|
69
|
+
prod: "https://swap.cow.fi",
|
|
70
|
+
dev: "https://dev.swap.cow.fi",
|
|
71
|
+
pr: v()
|
|
44
72
|
};
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
73
|
+
var u = /* @__PURE__ */ ((t) => (t.SWAP = "swap", t.LIMIT = "limit", t.ADVANCED = "advanced", t))(u || {});
|
|
74
|
+
function y(t) {
|
|
75
|
+
const e = W[t.env || "prod"], n = h(t), s = p(t);
|
|
76
|
+
return e + "/#" + n + "?" + s;
|
|
48
77
|
}
|
|
49
|
-
function
|
|
50
|
-
const { chainId: e, tradeAssets:
|
|
78
|
+
function h(t) {
|
|
79
|
+
const { chainId: e = 1, tradeAssets: n, tradeType: s = u.SWAP } = t, o = n ? [n.sell.asset, n.buy.asset].map(encodeURIComponent).join("/") : "";
|
|
51
80
|
return `/${e}/widget/${s}/${o}`;
|
|
52
81
|
}
|
|
53
|
-
function
|
|
54
|
-
const { tradeAssets: e, theme:
|
|
82
|
+
function p(t) {
|
|
83
|
+
const { tradeAssets: e, theme: n } = t, s = new URLSearchParams();
|
|
55
84
|
if (e) {
|
|
56
85
|
const { sell: o, buy: r } = e;
|
|
57
86
|
o.amount && s.append("sellAmount", o.amount), r.amount && s.append("buyAmount", r.amount);
|
|
58
87
|
}
|
|
59
|
-
return
|
|
88
|
+
return n && s.append("theme", n), s;
|
|
60
89
|
}
|
|
61
|
-
const
|
|
62
|
-
function
|
|
63
|
-
const {
|
|
64
|
-
t.innerHTML = "", t.appendChild(
|
|
65
|
-
const { contentWindow:
|
|
66
|
-
if (!
|
|
90
|
+
const i = "cowSwapWidget", f = "640px", P = "450px";
|
|
91
|
+
function R(t, e = {}) {
|
|
92
|
+
const { provider: n } = e, s = A(e);
|
|
93
|
+
t.innerHTML = "", t.appendChild(s);
|
|
94
|
+
const { contentWindow: o } = s;
|
|
95
|
+
if (!o)
|
|
67
96
|
throw new Error("Iframe does not contain a window!");
|
|
68
|
-
return s && new
|
|
97
|
+
return C(o, e.appKey), M(s, e.height), n && new E(o).onConnect(n), s.addEventListener("load", () => {
|
|
98
|
+
d(e, o);
|
|
99
|
+
}), (r) => d(r, o);
|
|
100
|
+
}
|
|
101
|
+
function A(t) {
|
|
102
|
+
const { width: e = P, height: n = f } = t, s = document.createElement("iframe");
|
|
103
|
+
return s.src = y(t), s.width = e, s.height = n, s.style.border = "0", s;
|
|
69
104
|
}
|
|
70
|
-
function
|
|
71
|
-
const
|
|
105
|
+
function d(t, e) {
|
|
106
|
+
const n = h(t), s = p(t).toString();
|
|
72
107
|
e.postMessage(
|
|
73
108
|
{
|
|
74
|
-
key:
|
|
109
|
+
key: i,
|
|
75
110
|
method: "update",
|
|
76
111
|
urlParams: {
|
|
77
|
-
pathname:
|
|
112
|
+
pathname: n,
|
|
78
113
|
search: s
|
|
79
114
|
},
|
|
80
|
-
appParams:
|
|
115
|
+
appParams: {
|
|
116
|
+
...t,
|
|
117
|
+
provider: void 0
|
|
118
|
+
}
|
|
81
119
|
},
|
|
82
120
|
"*"
|
|
83
121
|
);
|
|
84
122
|
}
|
|
85
|
-
function
|
|
86
|
-
|
|
87
|
-
|
|
123
|
+
function C(t, e) {
|
|
124
|
+
window.addEventListener("message", (n) => {
|
|
125
|
+
n.data.key !== i || n.data.method !== "activate" || t.postMessage(
|
|
126
|
+
{
|
|
127
|
+
key: i,
|
|
128
|
+
method: "metaData",
|
|
129
|
+
metaData: e ? { appKey: e } : void 0
|
|
130
|
+
},
|
|
131
|
+
"*"
|
|
132
|
+
);
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
function M(t, e = f) {
|
|
136
|
+
window.addEventListener("message", (n) => {
|
|
137
|
+
n.data.key !== i || n.data.method !== "iframeHeight" || (t.style.height = n.data.height ? `${n.data.height}px` : e);
|
|
138
|
+
});
|
|
88
139
|
}
|
|
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 });
|
|
140
|
+
function L({ params: t, provider: e }) {
|
|
141
|
+
const n = a(), s = a(null), o = a(null);
|
|
142
|
+
return c(() => {
|
|
143
|
+
s.current && (e && n.current !== e && (o.current = null), o.current ? o.current(t) : o.current = R(s.current, t));
|
|
144
|
+
}, [e, t]), c(() => {
|
|
145
|
+
n.current = e;
|
|
146
|
+
}, [e]), /* @__PURE__ */ l("div", { ref: s });
|
|
103
147
|
}
|
|
104
148
|
export {
|
|
105
|
-
|
|
149
|
+
L as CowSwapWidget
|
|
106
150
|
};
|
package/lib/CowSwapWidget.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { CowSwapWidgetParams
|
|
2
|
-
export type CowSwapWidgetParams = Omit<CowSwapWidgetParamsAux, 'container'>;
|
|
1
|
+
import { CowSwapWidgetParams, EthereumProvider } from '@cowprotocol/widget-lib';
|
|
3
2
|
export interface CowSwapWidgetProps {
|
|
4
3
|
params: CowSwapWidgetParams;
|
|
5
|
-
|
|
4
|
+
provider?: EthereumProvider;
|
|
6
5
|
}
|
|
7
|
-
export declare function CowSwapWidget(
|
|
6
|
+
export declare function CowSwapWidget({ params, provider }: CowSwapWidgetProps): import("react/jsx-runtime").JSX.Element;
|