@cowprotocol/widget-react 0.1.0 → 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 +10 -16
- package/index.js +1 -1
- package/index.mjs +81 -58
- package/lib/CowSwapWidget.d.ts +3 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -7,33 +7,27 @@ React component that creates a CoW Swap widget. It is based on [https://npmjs.co
|
|
|
7
7
|
Install dependency
|
|
8
8
|
|
|
9
9
|
```bash
|
|
10
|
-
# Install the dependency using NPM
|
|
11
|
-
npm install @cowprotocol/widget-react --save
|
|
12
|
-
|
|
13
|
-
# ...or alternativelly use YARN
|
|
14
10
|
yarn add @cowprotocol/widget-react
|
|
15
11
|
```
|
|
16
12
|
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install @cowprotocol/widget-react
|
|
16
|
+
```
|
|
17
|
+
|
|
17
18
|
Import component and some convenient types
|
|
18
19
|
|
|
19
20
|
```ts
|
|
20
|
-
import { CowSwapWidget, CowSwapWidgetParams
|
|
21
|
+
import { CowSwapWidget, CowSwapWidgetParams } from '@cowprotocol/widget-react'
|
|
21
22
|
```
|
|
22
23
|
|
|
23
24
|
Prepare the config for the widget:
|
|
24
25
|
|
|
25
26
|
```ts
|
|
26
27
|
const cowSwapWidgetParams: CowSwapWidgetParams = {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
appUrl: '<YOUR_APP_URL>',
|
|
31
|
-
},
|
|
32
|
-
width: 600,
|
|
33
|
-
height: 700,
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const cowSwapWidgetSettings: CowSwapWidgetSettings = {
|
|
28
|
+
appKey: '<YOUR_APP_KEY>',
|
|
29
|
+
width: '600px',
|
|
30
|
+
height: '700px',
|
|
37
31
|
tradeType: 'swap',
|
|
38
32
|
}
|
|
39
33
|
```
|
|
@@ -41,7 +35,7 @@ const cowSwapWidgetSettings: CowSwapWidgetSettings = {
|
|
|
41
35
|
Render the component:
|
|
42
36
|
|
|
43
37
|
```jsx
|
|
44
|
-
<CowSwapWidget params={cowSwapWidgetParams}
|
|
38
|
+
<CowSwapWidget params={cowSwapWidgetParams} />
|
|
45
39
|
```
|
|
46
40
|
|
|
47
41
|
## Developers
|
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
|
|
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
4
|
class E {
|
|
5
|
+
/**
|
|
6
|
+
* Creates an instance of JsonRpcManager.
|
|
7
|
+
* @param contentWindow - The window for handling events.
|
|
8
|
+
*/
|
|
5
9
|
constructor(e) {
|
|
6
10
|
this.contentWindow = e, window.addEventListener("message", this.processEvent);
|
|
7
11
|
}
|
|
12
|
+
/**
|
|
13
|
+
* The Ethereum provider instance.
|
|
14
|
+
* When is null the JSON-RPC manager is disconnected from the Ethereum provider.
|
|
15
|
+
* */
|
|
8
16
|
ethereumProvider = null;
|
|
17
|
+
/** Stored JSON-RPC requests. */
|
|
9
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;
|
|
@@ -34,94 +54,97 @@ class E {
|
|
|
34
54
|
postMessage(e) {
|
|
35
55
|
this.contentWindow.postMessage(
|
|
36
56
|
{
|
|
37
|
-
jsonrpc:
|
|
57
|
+
jsonrpc: m,
|
|
38
58
|
...e
|
|
39
59
|
},
|
|
40
|
-
|
|
60
|
+
g
|
|
41
61
|
);
|
|
42
62
|
}
|
|
43
63
|
}
|
|
64
|
+
function v() {
|
|
65
|
+
return `https://swap-dev-git-${location.hostname.replace("widget-configurator-git-", "").replace("-cowswap.vercel.app", "")}-cowswap.vercel.app`;
|
|
66
|
+
}
|
|
44
67
|
const W = {
|
|
45
68
|
local: "http://localhost:3000",
|
|
46
69
|
prod: "https://swap.cow.fi",
|
|
47
70
|
dev: "https://dev.swap.cow.fi",
|
|
48
|
-
pr:
|
|
71
|
+
pr: v()
|
|
49
72
|
};
|
|
50
|
-
var u = /* @__PURE__ */ ((
|
|
51
|
-
function y(
|
|
52
|
-
const e = W[
|
|
53
|
-
return e + "/#" +
|
|
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;
|
|
54
77
|
}
|
|
55
|
-
function
|
|
56
|
-
const { chainId: e = 1, 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("/") : "";
|
|
57
80
|
return `/${e}/widget/${s}/${o}`;
|
|
58
81
|
}
|
|
59
|
-
function
|
|
60
|
-
const { tradeAssets: e, theme:
|
|
82
|
+
function p(t) {
|
|
83
|
+
const { tradeAssets: e, theme: n } = t, s = new URLSearchParams();
|
|
61
84
|
if (e) {
|
|
62
85
|
const { sell: o, buy: r } = e;
|
|
63
86
|
o.amount && s.append("sellAmount", o.amount), r.amount && s.append("buyAmount", r.amount);
|
|
64
87
|
}
|
|
65
|
-
return
|
|
88
|
+
return n && s.append("theme", n), s;
|
|
66
89
|
}
|
|
67
|
-
const
|
|
68
|
-
function
|
|
69
|
-
const {
|
|
70
|
-
t.innerHTML = "", t.appendChild(
|
|
71
|
-
const { contentWindow:
|
|
72
|
-
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)
|
|
73
96
|
throw new Error("Iframe does not contain a window!");
|
|
74
|
-
return
|
|
75
|
-
|
|
76
|
-
}), (
|
|
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);
|
|
77
100
|
}
|
|
78
|
-
function
|
|
79
|
-
const { width:
|
|
80
|
-
return
|
|
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;
|
|
81
104
|
}
|
|
82
|
-
function
|
|
83
|
-
const
|
|
84
|
-
|
|
105
|
+
function d(t, e) {
|
|
106
|
+
const n = h(t), s = p(t).toString();
|
|
107
|
+
e.postMessage(
|
|
85
108
|
{
|
|
86
|
-
key:
|
|
109
|
+
key: i,
|
|
87
110
|
method: "update",
|
|
88
111
|
urlParams: {
|
|
89
|
-
pathname:
|
|
90
|
-
search:
|
|
112
|
+
pathname: n,
|
|
113
|
+
search: s
|
|
91
114
|
},
|
|
92
|
-
appParams:
|
|
115
|
+
appParams: {
|
|
116
|
+
...t,
|
|
117
|
+
provider: void 0
|
|
118
|
+
}
|
|
93
119
|
},
|
|
94
120
|
"*"
|
|
95
121
|
);
|
|
96
122
|
}
|
|
97
|
-
function
|
|
98
|
-
window.addEventListener("message", (
|
|
99
|
-
|
|
123
|
+
function C(t, e) {
|
|
124
|
+
window.addEventListener("message", (n) => {
|
|
125
|
+
n.data.key !== i || n.data.method !== "activate" || t.postMessage(
|
|
100
126
|
{
|
|
101
|
-
key:
|
|
127
|
+
key: i,
|
|
102
128
|
method: "metaData",
|
|
103
|
-
metaData: e
|
|
129
|
+
metaData: e ? { appKey: e } : void 0
|
|
104
130
|
},
|
|
105
131
|
"*"
|
|
106
132
|
);
|
|
107
133
|
});
|
|
108
134
|
}
|
|
109
|
-
function M(
|
|
110
|
-
window.addEventListener("message", (
|
|
111
|
-
|
|
112
|
-
return;
|
|
113
|
-
const s = t.data.height || e;
|
|
114
|
-
n.style.height = `${s}px`;
|
|
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);
|
|
115
138
|
});
|
|
116
139
|
}
|
|
117
|
-
function
|
|
118
|
-
const
|
|
119
|
-
return
|
|
120
|
-
|
|
121
|
-
}, [
|
|
122
|
-
|
|
123
|
-
}, [
|
|
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 });
|
|
124
147
|
}
|
|
125
148
|
export {
|
|
126
|
-
|
|
149
|
+
L as CowSwapWidget
|
|
127
150
|
};
|
package/lib/CowSwapWidget.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { CowSwapWidgetParams,
|
|
1
|
+
import { CowSwapWidgetParams, EthereumProvider } from '@cowprotocol/widget-lib';
|
|
2
2
|
export interface CowSwapWidgetProps {
|
|
3
|
-
params:
|
|
4
|
-
settings: CowSwapWidgetSettings;
|
|
3
|
+
params: CowSwapWidgetParams;
|
|
5
4
|
provider?: EthereumProvider;
|
|
6
5
|
}
|
|
7
|
-
export declare function CowSwapWidget({ params,
|
|
6
|
+
export declare function CowSwapWidget({ params, provider }: CowSwapWidgetProps): import("react/jsx-runtime").JSX.Element;
|