@cowprotocol/widget-react 0.0.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/index.d.ts +1 -0
- package/index.js +1 -0
- package/index.mjs +106 -0
- package/lib/CowSwapWidget.d.ts +7 -0
- package/package.json +12 -0
package/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './lib/CowSwapWidget';
|
package/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const d=require("react/jsx-runtime"),a=require("react"),h="2.0",p="*",m=["connect","disconnect","chainChanged","accountsChanged"];class l{constructor(e){this.contentWindow=e,this.ethereumProvider=null,this.requests={},this.processEvent=t=>{t.data.jsonrpc==="2.0"&&(this.ethereumProvider?this.processRequest(t.data):this.requests[t.data.id]=t.data)},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={},m.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:h,...e},p)}}const f={local:"http://localhost:3000",prod:"https://swap.cow.fi"};function g(n){const e=f[n.env],t=c(n),s=u(n);return e+"/#"+t+"?"+s}function c(n){const{chainId:e,tradeAssets:t,tradeType:s}=n,o=t?[t.sell.asset,t.buy.asset].map(encodeURIComponent).join("/"):"";return`/${e}/widget/${s}/${o}`}function u(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 w="cowSwapWidget";function P(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 s&&new l(r).onConnect(s),i=>E(i,r)}function E(n,e){const t=c(n.urlParams),s=u(n.urlParams).toString();e.postMessage({key:w,method:"update",urlParams:{pathname:t,search:s},appParams:n.appParams},"*")}function W(n,e){const{width:t,height:s}=n,o=document.createElement("iframe");return o.src=g(e.urlParams),o.width=`${t}px`,o.height=`${s}px`,o.style.border="0",o}function R(n){const e=a.useRef(null);return a.useEffect(()=>{if(e.current){const{params:t,settings:s}=n;P({...t,container:e.current},s)}},[e,n]),d.jsx("div",{ref:e})}exports.CowSwapWidget=R;
|
package/index.mjs
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { jsx as d } from "react/jsx-runtime";
|
|
2
|
+
import { useRef as u, useEffect as h } from "react";
|
|
3
|
+
const p = "2.0", m = "*", f = ["connect", "disconnect", "chainChanged", "accountsChanged"];
|
|
4
|
+
class l {
|
|
5
|
+
constructor(e) {
|
|
6
|
+
this.contentWindow = e, this.ethereumProvider = null, this.requests = {}, this.processEvent = (t) => {
|
|
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);
|
|
9
|
+
}
|
|
10
|
+
disconnect() {
|
|
11
|
+
this.ethereumProvider = null, window.removeEventListener("message", this.processEvent);
|
|
12
|
+
}
|
|
13
|
+
onConnect(e) {
|
|
14
|
+
this.ethereumProvider = e, Object.keys(this.requests).forEach((t) => {
|
|
15
|
+
this.processRequest(this.requests[t]);
|
|
16
|
+
}), this.requests = {}, f.forEach((t) => {
|
|
17
|
+
e.on(t, (s) => {
|
|
18
|
+
this.postMessage({ method: t, params: [s] });
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
processRequest(e) {
|
|
23
|
+
if (!this.ethereumProvider)
|
|
24
|
+
return;
|
|
25
|
+
(e.method === "enable" ? this.ethereumProvider.enable() : this.ethereumProvider.request(e)).then((s) => {
|
|
26
|
+
this.postMessage({ id: e.id, result: s });
|
|
27
|
+
}).catch((s) => {
|
|
28
|
+
this.postMessage({ id: e.id, error: s });
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
postMessage(e) {
|
|
32
|
+
this.contentWindow.postMessage(
|
|
33
|
+
{
|
|
34
|
+
jsonrpc: p,
|
|
35
|
+
...e
|
|
36
|
+
},
|
|
37
|
+
m
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
const g = {
|
|
42
|
+
local: "http://localhost:3000",
|
|
43
|
+
prod: "https://swap.cow.fi"
|
|
44
|
+
};
|
|
45
|
+
function w(n) {
|
|
46
|
+
const e = g[n.env], t = a(n), s = c(n);
|
|
47
|
+
return e + "/#" + t + "?" + s;
|
|
48
|
+
}
|
|
49
|
+
function a(n) {
|
|
50
|
+
const { chainId: e, tradeAssets: t, tradeType: s } = n, o = t ? [t.sell.asset, t.buy.asset].map(encodeURIComponent).join("/") : "";
|
|
51
|
+
return `/${e}/widget/${s}/${o}`;
|
|
52
|
+
}
|
|
53
|
+
function c(n) {
|
|
54
|
+
const { tradeAssets: e, theme: t } = n, s = new URLSearchParams();
|
|
55
|
+
if (e) {
|
|
56
|
+
const { sell: o, buy: r } = e;
|
|
57
|
+
o.amount && s.append("sellAmount", o.amount), r.amount && s.append("buyAmount", r.amount);
|
|
58
|
+
}
|
|
59
|
+
return t && s.append("theme", t), s;
|
|
60
|
+
}
|
|
61
|
+
const P = "cowSwapWidget";
|
|
62
|
+
function E(n, e) {
|
|
63
|
+
const { container: t, provider: s } = n, o = R(n, e);
|
|
64
|
+
t.innerHTML = "", t.appendChild(o);
|
|
65
|
+
const { contentWindow: r } = o;
|
|
66
|
+
if (!r)
|
|
67
|
+
throw new Error("Iframe does not contain a window!");
|
|
68
|
+
return s && new l(r).onConnect(s), (i) => W(i, r);
|
|
69
|
+
}
|
|
70
|
+
function W(n, e) {
|
|
71
|
+
const t = a(n.urlParams), s = c(n.urlParams).toString();
|
|
72
|
+
e.postMessage(
|
|
73
|
+
{
|
|
74
|
+
key: P,
|
|
75
|
+
method: "update",
|
|
76
|
+
urlParams: {
|
|
77
|
+
pathname: t,
|
|
78
|
+
search: s
|
|
79
|
+
},
|
|
80
|
+
appParams: n.appParams
|
|
81
|
+
},
|
|
82
|
+
"*"
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
function R(n, e) {
|
|
86
|
+
const { width: t, height: s } = n, o = document.createElement("iframe");
|
|
87
|
+
return o.src = w(e.urlParams), o.width = `${t}px`, o.height = `${s}px`, o.style.border = "0", o;
|
|
88
|
+
}
|
|
89
|
+
function q(n) {
|
|
90
|
+
const e = u(null);
|
|
91
|
+
return h(() => {
|
|
92
|
+
if (e.current) {
|
|
93
|
+
const { params: t, settings: s } = n;
|
|
94
|
+
E(
|
|
95
|
+
{
|
|
96
|
+
...t,
|
|
97
|
+
container: e.current
|
|
98
|
+
},
|
|
99
|
+
s
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
}, [e, n]), /* @__PURE__ */ d("div", { ref: e });
|
|
103
|
+
}
|
|
104
|
+
export {
|
|
105
|
+
q as CowSwapWidget
|
|
106
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { CowSwapWidgetParams as CowSwapWidgetParamsAux, CowSwapWidgetSettings } from '@cowprotocol/widget-lib';
|
|
2
|
+
export type CowSwapWidgetParams = Omit<CowSwapWidgetParamsAux, 'container'>;
|
|
3
|
+
export interface CowSwapWidgetProps {
|
|
4
|
+
params: CowSwapWidgetParams;
|
|
5
|
+
settings: CowSwapWidgetSettings;
|
|
6
|
+
}
|
|
7
|
+
export declare function CowSwapWidget(props: CowSwapWidgetProps): JSX.Element;
|