@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 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, CowSwapWidgetSettings } from '@cowprotocol/widget-react'
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
- container: document.getElementById('cow-swap-widget'),
28
- metaData: {
29
- appKey: '<YOUR_APP_KEY>',
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} settings={cowSwapWidgetSettings} />
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 p=require("react/jsx-runtime"),i=require("react"),l="2.0",m="*",w=["connect","disconnect","close","chainChanged","accountsChanged"];class g{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(t=>{this.processRequest(this.requests[t])}),this.requests={},w.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})})}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},m)}}const E={local:"http://localhost:3000",prod:"https://swap.cow.fi",dev:"https://dev.swap.cow.fi",pr:window.location.origin};var u=(n=>(n.SWAP="swap",n.LIMIT="limit",n.ADVANCED="advanced",n))(u||{});function R(n){const e=E[n.env||"prod"],t=h(n),s=f(n);return e+"/#"+t+"?"+s}function h(n){const{chainId:e=1,tradeAssets:t,tradeType:s=u.SWAP}=n,o=t?[t.sell.asset,t.buy.asset].map(encodeURIComponent).join("/"):"";return`/${e}/widget/${s}/${o}`}function f(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 a="cowSwapWidget";function y(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 P(r,n.metaData),S(o,n.height),s&&new g(r).onConnect(s),o.addEventListener("load",()=>{d(e,r,o)}),c=>d(c,r,o)}function W(n,e){const{width:t,height:s}=n,o=document.createElement("iframe");return o.src=R(e),o.width=`${t}px`,o.height=`${s}px`,o.style.border="0",o}function d(n,e,t){const s=h(n),o=f(n).toString();n.dynamicHeightEnabled||(t.style.height=""),e.postMessage({key:a,method:"update",urlParams:{pathname:s,search:o},appParams:n},"*")}function P(n,e){window.addEventListener("message",t=>{t.data.key!==a||t.data.method!=="activate"||n.postMessage({key:a,method:"metaData",metaData:e},"*")})}function S(n,e){window.addEventListener("message",t=>{if(t.data.key!==a||t.data.method!=="iframeHeight")return;const s=t.data.height||e;n.style.height=`${s}px`})}function v({params:n,settings:e,provider:t}){const s=i.useRef(),o=i.useRef(null),r=i.useRef(null);return i.useEffect(()=>{o.current&&(t&&s.current!==t&&(r.current=null),r.current?r.current(e):r.current=y({...n,container:o.current},e))},[t,n,e]),i.useEffect(()=>{s.current=t},[t]),p.jsx("div",{ref:o})}exports.CowSwapWidget=v;
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 f } from "react/jsx-runtime";
2
- import { useRef as i, useEffect as d } from "react";
3
- const l = "2.0", w = "*", g = ["connect", "disconnect", "close", "chainChanged", "accountsChanged"];
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((t) => {
15
- this.processRequest(this.requests[t]);
16
- }), this.requests = {}, g.forEach((t) => {
17
- e.on(t, (s) => {
18
- this.postMessage({ method: t, params: [s] });
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: l,
57
+ jsonrpc: m,
38
58
  ...e
39
59
  },
40
- w
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: window.location.origin
71
+ pr: v()
49
72
  };
50
- var u = /* @__PURE__ */ ((n) => (n.SWAP = "swap", n.LIMIT = "limit", n.ADVANCED = "advanced", n))(u || {});
51
- function y(n) {
52
- const e = W[n.env || "prod"], t = p(n), s = m(n);
53
- return e + "/#" + t + "?" + s;
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 p(n) {
56
- const { chainId: e = 1, tradeAssets: t, tradeType: s = u.SWAP } = n, o = t ? [t.sell.asset, t.buy.asset].map(encodeURIComponent).join("/") : "";
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 m(n) {
60
- const { tradeAssets: e, theme: t } = n, s = new URLSearchParams();
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 t && s.append("theme", t), s;
88
+ return n && s.append("theme", n), s;
66
89
  }
67
- const a = "cowSwapWidget";
68
- function P(n, e) {
69
- const { container: t, provider: s } = n, o = R(n, e);
70
- t.innerHTML = "", t.appendChild(o);
71
- const { contentWindow: r } = o;
72
- if (!r)
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 v(r, n.metaData), M(o, n.height), s && new E(r).onConnect(s), o.addEventListener("load", () => {
75
- h(e, r, o);
76
- }), (c) => h(c, r, o);
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 R(n, e) {
79
- const { width: t, height: s } = n, o = document.createElement("iframe");
80
- return o.src = y(e), o.width = `${t}px`, o.height = `${s}px`, o.style.border = "0", o;
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 h(n, e, t) {
83
- const s = p(n), o = m(n).toString();
84
- n.dynamicHeightEnabled || (t.style.height = ""), e.postMessage(
105
+ function d(t, e) {
106
+ const n = h(t), s = p(t).toString();
107
+ e.postMessage(
85
108
  {
86
- key: a,
109
+ key: i,
87
110
  method: "update",
88
111
  urlParams: {
89
- pathname: s,
90
- search: o
112
+ pathname: n,
113
+ search: s
91
114
  },
92
- appParams: n
115
+ appParams: {
116
+ ...t,
117
+ provider: void 0
118
+ }
93
119
  },
94
120
  "*"
95
121
  );
96
122
  }
97
- function v(n, e) {
98
- window.addEventListener("message", (t) => {
99
- t.data.key !== a || t.data.method !== "activate" || n.postMessage(
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: a,
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(n, e) {
110
- window.addEventListener("message", (t) => {
111
- if (t.data.key !== a || t.data.method !== "iframeHeight")
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 S({ params: n, settings: e, provider: t }) {
118
- const s = i(), o = i(null), r = i(null);
119
- return d(() => {
120
- o.current && (t && s.current !== t && (r.current = null), r.current ? r.current(e) : r.current = P({ ...n, container: o.current }, e));
121
- }, [t, n, e]), d(() => {
122
- s.current = t;
123
- }, [t]), /* @__PURE__ */ f("div", { ref: o });
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
- S as CowSwapWidget
149
+ L as CowSwapWidget
127
150
  };
@@ -1,7 +1,6 @@
1
- import { CowSwapWidgetParams, CowSwapWidgetSettings, EthereumProvider } from '@cowprotocol/widget-lib';
1
+ import { CowSwapWidgetParams, EthereumProvider } from '@cowprotocol/widget-lib';
2
2
  export interface CowSwapWidgetProps {
3
- params: Omit<CowSwapWidgetParams, 'container'>;
4
- settings: CowSwapWidgetSettings;
3
+ params: CowSwapWidgetParams;
5
4
  provider?: EthereumProvider;
6
5
  }
7
- export declare function CowSwapWidget({ params, settings, provider }: CowSwapWidgetProps): import("react/jsx-runtime").JSX.Element;
6
+ export declare function CowSwapWidget({ params, provider }: CowSwapWidgetProps): import("react/jsx-runtime").JSX.Element;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cowprotocol/widget-react",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "main": "./index.js",
5
5
  "types": "./index.d.ts",
6
6
  "exports": {