@cowprotocol/widget-react 0.2.3 → 0.3.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/index.js +1 -1
- package/index.mjs +76 -73
- package/package.json +2 -2
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";var l=Object.defineProperty;var E=(n,e,t)=>e in n?l(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t;var c=(n,e,t)=>(E(n,typeof e!="symbol"?e+"":e,t),t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const W=require("react/jsx-runtime"),i=require("react"),R="2.0",P="*",v=["connect","disconnect","close","chainChanged","accountsChanged"];class S{constructor(e){c(this,"ethereumProvider",null);c(this,"requests",{});c(this,"processEvent",e=>{e.data.jsonrpc==="2.0"&&(this.ethereumProvider?this.processRequest(e.data):this.requests[e.data.id]=e.data)});this.contentWindow=e,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={},v.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:R,...e},P)}}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 p={local:"http://localhost:3000",prod:"https://swap.cow.fi",dev:"https://dev.swap.cow.fi",pr:y()};var d=(n=>(n.SWAP="swap",n.LIMIT="limit",n.ADVANCED="advanced",n))(d||{});const u="_";function T(n){const e=p[n.env||"prod"],t=f(n),s=w(n);return e+"/#"+t+"?"+s}function f(n){const{chainId:e=1,sell:t,buy:s,tradeType:o=d.SWAP}=n,r=[(t==null?void 0:t.asset)||u,(s==null?void 0:s.asset)||u].map(encodeURIComponent).join("/");return`/${e}/widget/${o}/${r}`}function w(n){const{sell:e,buy:t,theme:s}=n,o=new URLSearchParams;return e!=null&&e.amount&&o.append("sellAmount",e.amount),t!=null&&t.amount&&o.append("buyAmount",t.amount),s&&o.append("theme",typeof s=="string"?s:s.baseTheme),o}const a="cowSwapWidget",g="640px",C="450px",A=20;function m(n,e={}){const{provider:t}=e,s=M(e);n.innerHTML="",n.appendChild(s);const{contentWindow:o}=s;if(!o)throw new Error("Iframe does not contain a window!");return _(o,e.appCode),L(s,e.height),t&&new S(o).onConnect(t),s.addEventListener("load",()=>{h(e,o)}),r=>h(r,o)}function M(n){const{width:e=C,height:t=g}=n,s=document.createElement("iframe");return s.src=T(n),s.width=e,s.height=t,s.style.border="0",s}function h(n,e){const t=f(n),s=w(n).toString();e.postMessage({key:a,method:"update",urlParams:{pathname:t,search:s},appParams:{...n,provider:void 0}},"*")}function _(n,e){window.addEventListener("message",t=>{t.data.key!==a||t.data.method!=="activate"||n.postMessage({key:a,method:"metaData",metaData:e?{appCode:e}:void 0},"*")})}function L(n,e=g){window.addEventListener("message",t=>{t.data.key!==a||t.data.method!=="iframeHeight"||(n.style.height=t.data.height?`${t.data.height+A}px`:e)})}function q({params:n,provider:e}){const t=i.useRef(),s=i.useRef(null),o=i.useRef(null);return i.useEffect(()=>{s.current&&(e&&t.current!==e&&(o.current=null),o.current?o.current(n):o.current=m(s.current,n))},[e,n]),i.useEffect(()=>{t.current=e},[e]),W.jsx("div",{ref:s})}exports.COWSWAP_URLS=p;exports.CowSwapWidget=q;exports.TradeType=d;exports.cowSwapWidget=m;
|
package/index.mjs
CHANGED
|
@@ -1,21 +1,27 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
var g = Object.defineProperty;
|
|
2
|
+
var l = (n, e, t) => e in n ? g(n, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[e] = t;
|
|
3
|
+
var r = (n, e, t) => (l(n, typeof e != "symbol" ? e + "" : e, t), t);
|
|
4
|
+
import { jsx as E } from "react/jsx-runtime";
|
|
5
|
+
import { useRef as a, useEffect as d } from "react";
|
|
6
|
+
const v = "2.0", P = "*", W = ["connect", "disconnect", "close", "chainChanged", "accountsChanged"];
|
|
7
|
+
class R {
|
|
5
8
|
/**
|
|
6
9
|
* Creates an instance of JsonRpcManager.
|
|
7
10
|
* @param contentWindow - The window for handling events.
|
|
8
11
|
*/
|
|
9
12
|
constructor(e) {
|
|
13
|
+
/**
|
|
14
|
+
* The Ethereum provider instance.
|
|
15
|
+
* When is null the JSON-RPC manager is disconnected from the Ethereum provider.
|
|
16
|
+
* */
|
|
17
|
+
r(this, "ethereumProvider", null);
|
|
18
|
+
/** Stored JSON-RPC requests. */
|
|
19
|
+
r(this, "requests", {});
|
|
20
|
+
r(this, "processEvent", (e) => {
|
|
21
|
+
e.data.jsonrpc === "2.0" && (this.ethereumProvider ? this.processRequest(e.data) : this.requests[e.data.id] = e.data);
|
|
22
|
+
});
|
|
10
23
|
this.contentWindow = e, window.addEventListener("message", this.processEvent);
|
|
11
24
|
}
|
|
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
25
|
/**
|
|
20
26
|
* Disconnects the JSON-RPC manager from the Ethereum provider.
|
|
21
27
|
*/
|
|
@@ -27,11 +33,11 @@ class v {
|
|
|
27
33
|
* @param ethereumProvider - The Ethereum provider to connect.
|
|
28
34
|
*/
|
|
29
35
|
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:
|
|
36
|
+
this.ethereumProvider = e, Object.keys(this.requests).forEach((t) => {
|
|
37
|
+
this.processRequest(this.requests[t]);
|
|
38
|
+
}), this.requests = {}, W.forEach((t) => {
|
|
39
|
+
e.on(t, (o) => {
|
|
40
|
+
this.postMessage({ method: t, params: [o] });
|
|
35
41
|
});
|
|
36
42
|
});
|
|
37
43
|
}
|
|
@@ -42,86 +48,83 @@ class v {
|
|
|
42
48
|
processRequest(e) {
|
|
43
49
|
if (!this.ethereumProvider)
|
|
44
50
|
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:
|
|
51
|
+
(e.method === "enable" ? this.ethereumProvider.enable() : this.ethereumProvider.request(e)).then((o) => {
|
|
52
|
+
this.postMessage({ id: e.id, result: o });
|
|
53
|
+
}).catch((o) => {
|
|
54
|
+
this.postMessage({ id: e.id, error: o });
|
|
49
55
|
});
|
|
50
56
|
}
|
|
51
|
-
processEvent = (e) => {
|
|
52
|
-
e.data.jsonrpc === "2.0" && (this.ethereumProvider ? this.processRequest(e.data) : this.requests[e.data.id] = e.data);
|
|
53
|
-
};
|
|
54
57
|
postMessage(e) {
|
|
55
58
|
this.contentWindow.postMessage(
|
|
56
59
|
{
|
|
57
|
-
jsonrpc:
|
|
60
|
+
jsonrpc: v,
|
|
58
61
|
...e
|
|
59
62
|
},
|
|
60
|
-
|
|
63
|
+
P
|
|
61
64
|
);
|
|
62
65
|
}
|
|
63
66
|
}
|
|
64
67
|
function y() {
|
|
65
68
|
return typeof window > "u" || !window ? "" : `https://swap-dev-git-${window.location.hostname.replace("widget-configurator-git-", "").replace("-cowswap.vercel.app", "")}-cowswap.vercel.app`;
|
|
66
69
|
}
|
|
67
|
-
const
|
|
70
|
+
const T = {
|
|
68
71
|
local: "http://localhost:3000",
|
|
69
72
|
prod: "https://swap.cow.fi",
|
|
70
73
|
dev: "https://dev.swap.cow.fi",
|
|
71
74
|
pr: y()
|
|
72
75
|
};
|
|
73
|
-
var
|
|
74
|
-
const
|
|
75
|
-
function
|
|
76
|
-
const e =
|
|
77
|
-
return e + "/#" +
|
|
76
|
+
var p = /* @__PURE__ */ ((n) => (n.SWAP = "swap", n.LIMIT = "limit", n.ADVANCED = "advanced", n))(p || {});
|
|
77
|
+
const h = "_";
|
|
78
|
+
function A(n) {
|
|
79
|
+
const e = T[n.env || "prod"], t = f(n), o = w(n);
|
|
80
|
+
return e + "/#" + t + "?" + o;
|
|
78
81
|
}
|
|
79
|
-
function
|
|
80
|
-
const { chainId: e = 1, sell:
|
|
81
|
-
return `/${e}/widget/${
|
|
82
|
+
function f(n) {
|
|
83
|
+
const { chainId: e = 1, sell: t, buy: o, tradeType: s = p.SWAP } = n, i = [(t == null ? void 0 : t.asset) || h, (o == null ? void 0 : o.asset) || h].map(encodeURIComponent).join("/");
|
|
84
|
+
return `/${e}/widget/${s}/${i}`;
|
|
82
85
|
}
|
|
83
|
-
function
|
|
84
|
-
const { sell: e, buy:
|
|
85
|
-
return e
|
|
86
|
+
function w(n) {
|
|
87
|
+
const { sell: e, buy: t, theme: o } = n, s = new URLSearchParams();
|
|
88
|
+
return e != null && e.amount && s.append("sellAmount", e.amount), t != null && t.amount && s.append("buyAmount", t.amount), o && s.append("theme", typeof o == "string" ? o : o.baseTheme), s;
|
|
86
89
|
}
|
|
87
|
-
const
|
|
88
|
-
function
|
|
89
|
-
const { provider:
|
|
90
|
-
|
|
91
|
-
const { contentWindow:
|
|
92
|
-
if (!
|
|
90
|
+
const c = "cowSwapWidget", m = "640px", C = "450px", M = 20;
|
|
91
|
+
function S(n, e = {}) {
|
|
92
|
+
const { provider: t } = e, o = _(e);
|
|
93
|
+
n.innerHTML = "", n.appendChild(o);
|
|
94
|
+
const { contentWindow: s } = o;
|
|
95
|
+
if (!s)
|
|
93
96
|
throw new Error("Iframe does not contain a window!");
|
|
94
|
-
return
|
|
95
|
-
u(e,
|
|
96
|
-
}), (i) => u(i,
|
|
97
|
+
return I(s, e.appCode), L(o, e.height), t && new R(s).onConnect(t), o.addEventListener("load", () => {
|
|
98
|
+
u(e, s);
|
|
99
|
+
}), (i) => u(i, s);
|
|
97
100
|
}
|
|
98
|
-
function
|
|
99
|
-
const { width: e =
|
|
100
|
-
return
|
|
101
|
+
function _(n) {
|
|
102
|
+
const { width: e = C, height: t = m } = n, o = document.createElement("iframe");
|
|
103
|
+
return o.src = A(n), o.width = e, o.height = t, o.style.border = "0", o;
|
|
101
104
|
}
|
|
102
|
-
function u(
|
|
103
|
-
const
|
|
105
|
+
function u(n, e) {
|
|
106
|
+
const t = f(n), o = w(n).toString();
|
|
104
107
|
e.postMessage(
|
|
105
108
|
{
|
|
106
|
-
key:
|
|
109
|
+
key: c,
|
|
107
110
|
method: "update",
|
|
108
111
|
urlParams: {
|
|
109
|
-
pathname:
|
|
110
|
-
search:
|
|
112
|
+
pathname: t,
|
|
113
|
+
search: o
|
|
111
114
|
},
|
|
112
115
|
appParams: {
|
|
113
|
-
...
|
|
116
|
+
...n,
|
|
114
117
|
provider: void 0
|
|
115
118
|
}
|
|
116
119
|
},
|
|
117
120
|
"*"
|
|
118
121
|
);
|
|
119
122
|
}
|
|
120
|
-
function
|
|
121
|
-
window.addEventListener("message", (
|
|
122
|
-
|
|
123
|
+
function I(n, e) {
|
|
124
|
+
window.addEventListener("message", (t) => {
|
|
125
|
+
t.data.key !== c || t.data.method !== "activate" || n.postMessage(
|
|
123
126
|
{
|
|
124
|
-
key:
|
|
127
|
+
key: c,
|
|
125
128
|
method: "metaData",
|
|
126
129
|
metaData: e ? { appCode: e } : void 0
|
|
127
130
|
},
|
|
@@ -129,22 +132,22 @@ function M(t, e) {
|
|
|
129
132
|
);
|
|
130
133
|
});
|
|
131
134
|
}
|
|
132
|
-
function
|
|
133
|
-
window.addEventListener("message", (
|
|
134
|
-
|
|
135
|
+
function L(n, e = m) {
|
|
136
|
+
window.addEventListener("message", (t) => {
|
|
137
|
+
t.data.key !== c || t.data.method !== "iframeHeight" || (n.style.height = t.data.height ? `${t.data.height + M}px` : e);
|
|
135
138
|
});
|
|
136
139
|
}
|
|
137
|
-
function
|
|
138
|
-
const
|
|
139
|
-
return
|
|
140
|
-
|
|
141
|
-
}, [e,
|
|
142
|
-
|
|
143
|
-
}, [e]), /* @__PURE__ */
|
|
140
|
+
function b({ params: n, provider: e }) {
|
|
141
|
+
const t = a(), o = a(null), s = a(null);
|
|
142
|
+
return d(() => {
|
|
143
|
+
o.current && (e && t.current !== e && (s.current = null), s.current ? s.current(n) : s.current = S(o.current, n));
|
|
144
|
+
}, [e, n]), d(() => {
|
|
145
|
+
t.current = e;
|
|
146
|
+
}, [e]), /* @__PURE__ */ E("div", { ref: o });
|
|
144
147
|
}
|
|
145
148
|
export {
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
149
|
+
T as COWSWAP_URLS,
|
|
150
|
+
b as CowSwapWidget,
|
|
151
|
+
p as TradeType,
|
|
152
|
+
S as cowSwapWidget
|
|
150
153
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cowprotocol/widget-react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"type": "commonjs",
|
|
5
5
|
"description": "CoW Swap Widget Library. Allows you to easily embed a CoW Swap widget on your React application.",
|
|
6
6
|
"main": "index.js",
|
|
@@ -22,6 +22,6 @@
|
|
|
22
22
|
"react"
|
|
23
23
|
],
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@cowprotocol/widget-lib": "^0.
|
|
25
|
+
"@cowprotocol/widget-lib": "^0.3.0"
|
|
26
26
|
}
|
|
27
27
|
}
|