@cowprotocol/widget-react 0.1.1 → 0.2.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 +1 -2
- package/index.js +1 -1
- package/index.mjs +52 -54
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -10,7 +10,6 @@ Install dependency
|
|
|
10
10
|
yarn add @cowprotocol/widget-react
|
|
11
11
|
```
|
|
12
12
|
|
|
13
|
-
|
|
14
13
|
```bash
|
|
15
14
|
npm install @cowprotocol/widget-react
|
|
16
15
|
```
|
|
@@ -25,7 +24,7 @@ Prepare the config for the widget:
|
|
|
25
24
|
|
|
26
25
|
```ts
|
|
27
26
|
const cowSwapWidgetParams: CowSwapWidgetParams = {
|
|
28
|
-
|
|
27
|
+
appCode: '<YOUR_APP_KEY>',
|
|
29
28
|
width: '600px',
|
|
30
29
|
height: '700px',
|
|
31
30
|
tradeType: 'swap',
|
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 l=require("react/jsx-runtime"),r=require("react"),w="2.0",g="*",m=["connect","disconnect","close","chainChanged","accountsChanged"];class E{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(s=>{this.processRequest(this.requests[s])}),this.requests={},m.forEach(s=>{e.on(s,n=>{this.postMessage({method:s,params:[n]})})})}processRequest(e){if(!this.ethereumProvider)return;(e.method==="enable"?this.ethereumProvider.enable():this.ethereumProvider.request(e)).then(n=>{this.postMessage({id:e.id,result:n})}).catch(n=>{this.postMessage({id:e.id,error:n})})}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:w,...e},g)}}function y(){return typeof window>"u"||!window?"":`https://swap-dev-git-${window.location.hostname.replace("widget-configurator-git-","").replace("-cowswap.vercel.app","")}-cowswap.vercel.app`}const R={local:"http://localhost:3000",prod:"https://swap.cow.fi",dev:"https://dev.swap.cow.fi",pr:y()};var d=(t=>(t.SWAP="swap",t.LIMIT="limit",t.ADVANCED="advanced",t))(d||{});function v(t){const e=R[t.env||"prod"],s=u(t),n=h(t);return e+"/#"+s+"?"+n}function u(t){const{chainId:e=1,sell:s,buy:n,tradeType:o=d.SWAP}=t,i=[];s?.asset&&i.push(s.asset),n?.asset&&i.push(n.asset);const f=i.map(encodeURIComponent).join("/");return`/${e}/widget/${o}/${f}`}function h(t){const{sell:e,buy:s,theme:n}=t,o=new URLSearchParams;return e?.amount&&o.append("sellAmount",e.amount),s?.amount&&o.append("buyAmount",s.amount),n&&o.append("theme",typeof n=="string"?n:n.baseTheme),o}const a="cowSwapWidget",p="640px",W="450px",P=20;function S(t,e={}){const{provider:s}=e,n=C(e);t.innerHTML="",t.appendChild(n);const{contentWindow:o}=n;if(!o)throw new Error("Iframe does not contain a window!");return b(o,e.appCode),A(n,e.height),s&&new E(o).onConnect(s),n.addEventListener("load",()=>{c(e,o)}),i=>c(i,o)}function C(t){const{width:e=W,height:s=p}=t,n=document.createElement("iframe");return n.src=v(t),n.width=e,n.height=s,n.style.border="0",n}function c(t,e){const s=u(t),n=h(t).toString();e.postMessage({key:a,method:"update",urlParams:{pathname:s,search:n},appParams:{...t,provider:void 0}},"*")}function b(t,e){window.addEventListener("message",s=>{s.data.key!==a||s.data.method!=="activate"||t.postMessage({key:a,method:"metaData",metaData:e?{appCode:e}:void 0},"*")})}function A(t,e=p){window.addEventListener("message",s=>{s.data.key!==a||s.data.method!=="iframeHeight"||(t.style.height=s.data.height?`${s.data.height+P}px`:e)})}function M({params:t,provider:e}){const s=r.useRef(),n=r.useRef(null),o=r.useRef(null);return r.useEffect(()=>{n.current&&(e&&s.current!==e&&(o.current=null),o.current?o.current(t):o.current=S(n.current,t))},[e,t]),r.useEffect(()=>{s.current=e},[e]),l.jsx("div",{ref:n})}exports.CowSwapWidget=M;
|
package/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { jsx as
|
|
1
|
+
import { jsx as w } from "react/jsx-runtime";
|
|
2
2
|
import { useRef as a, useEffect as c } from "react";
|
|
3
|
-
const m = "2.0", g = "*",
|
|
4
|
-
class
|
|
3
|
+
const m = "2.0", g = "*", E = ["connect", "disconnect", "close", "chainChanged", "accountsChanged"];
|
|
4
|
+
class v {
|
|
5
5
|
/**
|
|
6
6
|
* Creates an instance of JsonRpcManager.
|
|
7
7
|
* @param contentWindow - The window for handling events.
|
|
@@ -27,11 +27,11 @@ class E {
|
|
|
27
27
|
* @param ethereumProvider - The Ethereum provider to connect.
|
|
28
28
|
*/
|
|
29
29
|
onConnect(e) {
|
|
30
|
-
this.ethereumProvider = e, Object.keys(this.requests).forEach((
|
|
31
|
-
this.processRequest(this.requests[
|
|
32
|
-
}), this.requests = {},
|
|
33
|
-
e.on(
|
|
34
|
-
this.postMessage({ method:
|
|
30
|
+
this.ethereumProvider = e, Object.keys(this.requests).forEach((s) => {
|
|
31
|
+
this.processRequest(this.requests[s]);
|
|
32
|
+
}), this.requests = {}, E.forEach((s) => {
|
|
33
|
+
e.on(s, (n) => {
|
|
34
|
+
this.postMessage({ method: s, params: [n] });
|
|
35
35
|
});
|
|
36
36
|
});
|
|
37
37
|
}
|
|
@@ -42,10 +42,10 @@ class E {
|
|
|
42
42
|
processRequest(e) {
|
|
43
43
|
if (!this.ethereumProvider)
|
|
44
44
|
return;
|
|
45
|
-
(e.method === "enable" ? this.ethereumProvider.enable() : this.ethereumProvider.request(e)).then((
|
|
46
|
-
this.postMessage({ id: e.id, result:
|
|
47
|
-
}).catch((
|
|
48
|
-
this.postMessage({ id: e.id, error:
|
|
45
|
+
(e.method === "enable" ? this.ethereumProvider.enable() : this.ethereumProvider.request(e)).then((n) => {
|
|
46
|
+
this.postMessage({ id: e.id, result: n });
|
|
47
|
+
}).catch((n) => {
|
|
48
|
+
this.postMessage({ id: e.id, error: n });
|
|
49
49
|
});
|
|
50
50
|
}
|
|
51
51
|
processEvent = (e) => {
|
|
@@ -61,56 +61,54 @@ class E {
|
|
|
61
61
|
);
|
|
62
62
|
}
|
|
63
63
|
}
|
|
64
|
-
function
|
|
65
|
-
return `https://swap-dev-git-${location.hostname.replace("widget-configurator-git-", "").replace("-cowswap.vercel.app", "")}-cowswap.vercel.app`;
|
|
64
|
+
function y() {
|
|
65
|
+
return typeof window > "u" || !window ? "" : `https://swap-dev-git-${window.location.hostname.replace("widget-configurator-git-", "").replace("-cowswap.vercel.app", "")}-cowswap.vercel.app`;
|
|
66
66
|
}
|
|
67
67
|
const W = {
|
|
68
68
|
local: "http://localhost:3000",
|
|
69
69
|
prod: "https://swap.cow.fi",
|
|
70
70
|
dev: "https://dev.swap.cow.fi",
|
|
71
|
-
pr:
|
|
71
|
+
pr: y()
|
|
72
72
|
};
|
|
73
73
|
var u = /* @__PURE__ */ ((t) => (t.SWAP = "swap", t.LIMIT = "limit", t.ADVANCED = "advanced", t))(u || {});
|
|
74
|
-
function
|
|
75
|
-
const e = W[t.env || "prod"],
|
|
76
|
-
return e + "/#" +
|
|
74
|
+
function P(t) {
|
|
75
|
+
const e = W[t.env || "prod"], s = h(t), n = p(t);
|
|
76
|
+
return e + "/#" + s + "?" + n;
|
|
77
77
|
}
|
|
78
78
|
function h(t) {
|
|
79
|
-
const { chainId: e = 1,
|
|
80
|
-
|
|
79
|
+
const { chainId: e = 1, sell: s, buy: n, tradeType: o = u.SWAP } = t, i = [];
|
|
80
|
+
s?.asset && i.push(s.asset), n?.asset && i.push(n.asset);
|
|
81
|
+
const l = i.map(encodeURIComponent).join("/");
|
|
82
|
+
return `/${e}/widget/${o}/${l}`;
|
|
81
83
|
}
|
|
82
84
|
function p(t) {
|
|
83
|
-
const {
|
|
84
|
-
|
|
85
|
-
const { sell: o, buy: r } = e;
|
|
86
|
-
o.amount && s.append("sellAmount", o.amount), r.amount && s.append("buyAmount", r.amount);
|
|
87
|
-
}
|
|
88
|
-
return n && s.append("theme", n), s;
|
|
85
|
+
const { sell: e, buy: s, theme: n } = t, o = new URLSearchParams();
|
|
86
|
+
return e?.amount && o.append("sellAmount", e.amount), s?.amount && o.append("buyAmount", s.amount), n && o.append("theme", typeof n == "string" ? n : n.baseTheme), o;
|
|
89
87
|
}
|
|
90
|
-
const
|
|
91
|
-
function
|
|
92
|
-
const { provider:
|
|
93
|
-
t.innerHTML = "", t.appendChild(
|
|
94
|
-
const { contentWindow: o } =
|
|
88
|
+
const r = "cowSwapWidget", f = "640px", R = "450px", A = 20;
|
|
89
|
+
function C(t, e = {}) {
|
|
90
|
+
const { provider: s } = e, n = S(e);
|
|
91
|
+
t.innerHTML = "", t.appendChild(n);
|
|
92
|
+
const { contentWindow: o } = n;
|
|
95
93
|
if (!o)
|
|
96
94
|
throw new Error("Iframe does not contain a window!");
|
|
97
|
-
return
|
|
95
|
+
return I(o, e.appCode), L(n, e.height), s && new v(o).onConnect(s), n.addEventListener("load", () => {
|
|
98
96
|
d(e, o);
|
|
99
|
-
}), (
|
|
97
|
+
}), (i) => d(i, o);
|
|
100
98
|
}
|
|
101
|
-
function
|
|
102
|
-
const { width: e =
|
|
103
|
-
return
|
|
99
|
+
function S(t) {
|
|
100
|
+
const { width: e = R, height: s = f } = t, n = document.createElement("iframe");
|
|
101
|
+
return n.src = P(t), n.width = e, n.height = s, n.style.border = "0", n;
|
|
104
102
|
}
|
|
105
103
|
function d(t, e) {
|
|
106
|
-
const
|
|
104
|
+
const s = h(t), n = p(t).toString();
|
|
107
105
|
e.postMessage(
|
|
108
106
|
{
|
|
109
|
-
key:
|
|
107
|
+
key: r,
|
|
110
108
|
method: "update",
|
|
111
109
|
urlParams: {
|
|
112
|
-
pathname:
|
|
113
|
-
search:
|
|
110
|
+
pathname: s,
|
|
111
|
+
search: n
|
|
114
112
|
},
|
|
115
113
|
appParams: {
|
|
116
114
|
...t,
|
|
@@ -120,31 +118,31 @@ function d(t, e) {
|
|
|
120
118
|
"*"
|
|
121
119
|
);
|
|
122
120
|
}
|
|
123
|
-
function
|
|
124
|
-
window.addEventListener("message", (
|
|
125
|
-
|
|
121
|
+
function I(t, e) {
|
|
122
|
+
window.addEventListener("message", (s) => {
|
|
123
|
+
s.data.key !== r || s.data.method !== "activate" || t.postMessage(
|
|
126
124
|
{
|
|
127
|
-
key:
|
|
125
|
+
key: r,
|
|
128
126
|
method: "metaData",
|
|
129
|
-
metaData: e ? {
|
|
127
|
+
metaData: e ? { appCode: e } : void 0
|
|
130
128
|
},
|
|
131
129
|
"*"
|
|
132
130
|
);
|
|
133
131
|
});
|
|
134
132
|
}
|
|
135
|
-
function
|
|
136
|
-
window.addEventListener("message", (
|
|
137
|
-
|
|
133
|
+
function L(t, e = f) {
|
|
134
|
+
window.addEventListener("message", (s) => {
|
|
135
|
+
s.data.key !== r || s.data.method !== "iframeHeight" || (t.style.height = s.data.height ? `${s.data.height + A}px` : e);
|
|
138
136
|
});
|
|
139
137
|
}
|
|
140
|
-
function
|
|
141
|
-
const
|
|
138
|
+
function b({ params: t, provider: e }) {
|
|
139
|
+
const s = a(), n = a(null), o = a(null);
|
|
142
140
|
return c(() => {
|
|
143
|
-
|
|
141
|
+
n.current && (e && s.current !== e && (o.current = null), o.current ? o.current(t) : o.current = C(n.current, t));
|
|
144
142
|
}, [e, t]), c(() => {
|
|
145
|
-
|
|
146
|
-
}, [e]), /* @__PURE__ */
|
|
143
|
+
s.current = e;
|
|
144
|
+
}, [e]), /* @__PURE__ */ w("div", { ref: n });
|
|
147
145
|
}
|
|
148
146
|
export {
|
|
149
|
-
|
|
147
|
+
b as CowSwapWidget
|
|
150
148
|
};
|