@cowprotocol/widget-lib 0.1.2 → 0.1.4

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.
@@ -1,13 +1,34 @@
1
1
  import { EthereumProvider, JsonRpcRequest } from './types';
2
+ /**
3
+ * Manages JSON-RPC requests and interactions with an Ethereum provider.
4
+ */
2
5
  export declare class JsonRpcManager {
3
6
  private contentWindow;
4
- ethereumProvider: EthereumProvider | null;
5
- requests: {
6
- [key: string]: JsonRpcRequest;
7
- };
7
+ /**
8
+ * The Ethereum provider instance.
9
+ * When is null the JSON-RPC manager is disconnected from the Ethereum provider.
10
+ * */
11
+ private ethereumProvider;
12
+ /** Stored JSON-RPC requests. */
13
+ private requests;
14
+ /**
15
+ * Creates an instance of JsonRpcManager.
16
+ * @param contentWindow - The window for handling events.
17
+ */
8
18
  constructor(contentWindow: Window);
19
+ /**
20
+ * Disconnects the JSON-RPC manager from the Ethereum provider.
21
+ */
9
22
  disconnect(): void;
23
+ /**
24
+ * Handles the 'connect' event and sets up event listeners for Ethereum provider events.
25
+ * @param ethereumProvider - The Ethereum provider to connect.
26
+ */
10
27
  onConnect(ethereumProvider: EthereumProvider): void;
28
+ /**
29
+ * Processes a JSON-RPC request and sends appropriate response or error via the content window.
30
+ * @param request - The JSON-RPC request to be processed.
31
+ */
11
32
  processRequest(request: JsonRpcRequest): void;
12
33
  private processEvent;
13
34
  private postMessage;
package/README.md CHANGED
@@ -31,29 +31,27 @@ Create a container somewhere in your website, the widget will be rendered inside
31
31
  Import the widget and initialise it:
32
32
 
33
33
  ```js
34
- import { cowSwapWidget } from '@cowprotocol/widget-lib'
34
+ import { cowSwapWidget, CowSwapWidgetParams } from '@cowprotocol/widget-lib'
35
35
 
36
36
  // Initialise the widget
37
37
  const widgetContainer = document.getElementById('cowswap-widget')
38
38
 
39
- const updateWidget = cowSwapWidget({
40
- container: widgetContainer,
41
- width: 600,
42
- height: 640,
43
- metaData: {
44
- appKey: '<YOUR_APP_KEY>', // Just an unique identifier for your app,
45
- appUrl: '<YOUR_APP_URL>'
46
- },
47
- // Optionally, you can provide some additional params to customise your widget
48
- }, {
39
+ const params: CowSwapWidgetParams = {
40
+ appKey: '<YOUR_APP_KEY>', // Just an unique identifier for your app
49
41
  sell: { asset: 'DAI' },
50
42
  buy: { asset: 'USDC', amount: '0.1' },
51
43
  // instantiate your own web3 provider
52
44
  provider: window.ethereum
53
- })
45
+ }
46
+
47
+ const updateWidget = cowSwapWidget(
48
+ widgetContainer,
49
+ // Optionally, you can provide some additional params to customise your widget
50
+ params
51
+ )
54
52
 
55
53
  // You also can change widget configuration on the fly
56
- updateWidget({ tradeType: 'limit' })
54
+ updateWidget({ ...params, tradeType: 'limit' })
57
55
  ```
58
56
 
59
57
  ## Developers
@@ -1,3 +1,12 @@
1
- import { CowSwapWidgetParams, CowSwapWidgetSettings } from './types';
2
- export type UpdateWidgetCallback = (params: CowSwapWidgetSettings) => void;
3
- export declare function cowSwapWidget(params: CowSwapWidgetParams, settings: CowSwapWidgetSettings): UpdateWidgetCallback;
1
+ import { CowSwapWidgetParams } from './types';
2
+ /**
3
+ * Callback function signature for updating the CoW Swap Widget.
4
+ */
5
+ export type UpdateWidgetCallback = (params: CowSwapWidgetParams) => void;
6
+ /**
7
+ * Generates and injects a CoW Swap Widget into the provided container.
8
+ * @param container - The HTML element to inject the widget into.
9
+ * @param params - Parameters for configuring the widget.
10
+ * @returns A callback function to update the widget with new settings.
11
+ */
12
+ export declare function cowSwapWidget(container: HTMLElement, params?: CowSwapWidgetParams): UpdateWidgetCallback;
package/index.iife.js CHANGED
@@ -1 +1 @@
1
- var cowSwapWidget=function(i){"use strict";const l="2.0",w="*",g=["connect","disconnect","close","chainChanged","accountsChanged"];class f{constructor(n){this.contentWindow=n,this.ethereumProvider=null,this.requests={},this.processEvent=e=>{e.data.jsonrpc==="2.0"&&(this.ethereumProvider?this.processRequest(e.data):this.requests[e.data.id]=e.data)},window.addEventListener("message",this.processEvent)}disconnect(){this.ethereumProvider=null,window.removeEventListener("message",this.processEvent)}onConnect(n){this.ethereumProvider=n,Object.keys(this.requests).forEach(e=>{this.processRequest(this.requests[e])}),this.requests={},g.forEach(e=>{n.on(e,s=>{this.postMessage({method:e,params:[s]})})})}processRequest(n){if(!this.ethereumProvider)return;(n.method==="enable"?this.ethereumProvider.enable():this.ethereumProvider.request(n)).then(s=>{this.postMessage({id:n.id,result:s})}).catch(s=>{this.postMessage({id:n.id,error:s})})}postMessage(n){this.contentWindow.postMessage({jsonrpc:l,...n},w)}}const d={local:"http://localhost:3000",prod:"https://swap.cow.fi",dev:"https://dev.swap.cow.fi",pr:window.location.origin};var c=(t=>(t.SWAP="swap",t.LIMIT="limit",t.ADVANCED="advanced",t))(c||{});function E(t){const n=d[t.env||"prod"],e=h(t),s=u(t);return n+"/#"+e+"?"+s}function h(t){const{chainId:n=1,tradeAssets:e,tradeType:s=c.SWAP}=t,o=e?[e.sell.asset,e.buy.asset].map(encodeURIComponent).join("/"):"";return`/${n}/widget/${s}/${o}`}function u(t){const{tradeAssets:n,theme:e}=t,s=new URLSearchParams;if(n){const{sell:o,buy:a}=n;o.amount&&s.append("sellAmount",o.amount),a.amount&&s.append("buyAmount",a.amount)}return e&&s.append("theme",e),s}const r="cowSwapWidget";function y(t,n){const{container:e,provider:s}=t,o=W(t,n);e.innerHTML="",e.appendChild(o);const{contentWindow:a}=o;if(!a)throw new Error("Iframe does not contain a window!");return P(a,t.metaData),v(o,t.height),s&&new f(a).onConnect(s),o.addEventListener("load",()=>{p(n,a,o)}),m=>p(m,a,o)}function W(t,n){const{width:e,height:s}=t,o=document.createElement("iframe");return o.src=E(n),o.width=`${e}px`,o.height=`${s}px`,o.style.border="0",o}function p(t,n,e){const s=h(t),o=u(t).toString();t.dynamicHeightEnabled||(e.style.height=""),n.postMessage({key:r,method:"update",urlParams:{pathname:s,search:o},appParams:t},"*")}function P(t,n){window.addEventListener("message",e=>{e.data.key!==r||e.data.method!=="activate"||t.postMessage({key:r,method:"metaData",metaData:n},"*")})}function v(t,n){window.addEventListener("message",e=>{if(e.data.key!==r||e.data.method!=="iframeHeight")return;const s=e.data.height||n;t.style.height=`${s}px`})}return i.COWSWAP_URLS=d,i.TradeType=c,i.cowSwapWidget=y,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"}),i}({});
1
+ var cowSwapWidget=function(i){"use strict";const g="2.0",l="*",m=["connect","disconnect","close","chainChanged","accountsChanged"];class f{constructor(t){this.contentWindow=t,this.ethereumProvider=null,this.requests={},this.processEvent=e=>{e.data.jsonrpc==="2.0"&&(this.ethereumProvider?this.processRequest(e.data):this.requests[e.data.id]=e.data)},window.addEventListener("message",this.processEvent)}disconnect(){this.ethereumProvider=null,window.removeEventListener("message",this.processEvent)}onConnect(t){this.ethereumProvider=t,Object.keys(this.requests).forEach(e=>{this.processRequest(this.requests[e])}),this.requests={},m.forEach(e=>{t.on(e,n=>{this.postMessage({method:e,params:[n]})})})}processRequest(t){if(!this.ethereumProvider)return;(t.method==="enable"?this.ethereumProvider.enable():this.ethereumProvider.request(t)).then(n=>{this.postMessage({id:t.id,result:n})}).catch(n=>{this.postMessage({id:t.id,error:n})})}postMessage(t){this.contentWindow.postMessage({jsonrpc:g,...t},l)}}function E(){return typeof window>"u"||!window?"":`https://swap-dev-git-${window.location.hostname.replace("widget-configurator-git-","").replace("-cowswap.vercel.app","")}-cowswap.vercel.app`}const d={local:"http://localhost:3000",prod:"https://swap.cow.fi",dev:"https://dev.swap.cow.fi",pr:E()};var c=(s=>(s.SWAP="swap",s.LIMIT="limit",s.ADVANCED="advanced",s))(c||{});function v(s){const t=d[s.env||"prod"],e=h(s),n=u(s);return t+"/#"+e+"?"+n}function h(s){const{chainId:t=1,tradeAssets:e,tradeType:n=c.SWAP}=s,o=e?[e.sell.asset,e.buy.asset].map(encodeURIComponent).join("/"):"";return`/${t}/widget/${n}/${o}`}function u(s){const{tradeAssets:t,theme:e}=s,n=new URLSearchParams;if(t){const{sell:o,buy:a}=t;o.amount&&n.append("sellAmount",o.amount),a.amount&&n.append("buyAmount",a.amount)}return e&&n.append("theme",e),n}const r="cowSwapWidget",p="640px",y="450px";function W(s,t={}){const{provider:e}=t,n=P(t);s.innerHTML="",s.appendChild(n);const{contentWindow:o}=n;if(!o)throw new Error("Iframe does not contain a window!");return S(o,t.appKey),A(n,t.height),e&&new f(o).onConnect(e),n.addEventListener("load",()=>{w(t,o)}),a=>w(a,o)}function P(s){const{width:t=y,height:e=p}=s,n=document.createElement("iframe");return n.src=v(s),n.width=t,n.height=e,n.style.border="0",n}function w(s,t){const e=h(s),n=u(s).toString();t.postMessage({key:r,method:"update",urlParams:{pathname:e,search:n},appParams:{...s,provider:void 0}},"*")}function S(s,t){window.addEventListener("message",e=>{e.data.key!==r||e.data.method!=="activate"||s.postMessage({key:r,method:"metaData",metaData:t?{appKey:t}:void 0},"*")})}function A(s,t=p){window.addEventListener("message",e=>{e.data.key!==r||e.data.method!=="iframeHeight"||(s.style.height=e.data.height?`${e.data.height}px`:t)})}return i.COWSWAP_URLS=d,i.TradeType=c,i.cowSwapWidget=W,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"}),i}({});
package/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const m="2.0",l="*",g=["connect","disconnect","close","chainChanged","accountsChanged"];class w{constructor(s){this.contentWindow=s,this.ethereumProvider=null,this.requests={},this.processEvent=e=>{e.data.jsonrpc==="2.0"&&(this.ethereumProvider?this.processRequest(e.data):this.requests[e.data.id]=e.data)},window.addEventListener("message",this.processEvent)}disconnect(){this.ethereumProvider=null,window.removeEventListener("message",this.processEvent)}onConnect(s){this.ethereumProvider=s,Object.keys(this.requests).forEach(e=>{this.processRequest(this.requests[e])}),this.requests={},g.forEach(e=>{s.on(e,n=>{this.postMessage({method:e,params:[n]})})})}processRequest(s){if(!this.ethereumProvider)return;(s.method==="enable"?this.ethereumProvider.enable():this.ethereumProvider.request(s)).then(n=>{this.postMessage({id:s.id,result:n})}).catch(n=>{this.postMessage({id:s.id,error:n})})}postMessage(s){this.contentWindow.postMessage({jsonrpc:m,...s},l)}}const h={local:"http://localhost:3000",prod:"https://swap.cow.fi",dev:"https://dev.swap.cow.fi",pr:window.location.origin};var r=(t=>(t.SWAP="swap",t.LIMIT="limit",t.ADVANCED="advanced",t))(r||{});function f(t){const s=h[t.env||"prod"],e=u(t),n=p(t);return s+"/#"+e+"?"+n}function u(t){const{chainId:s=1,tradeAssets:e,tradeType:n=r.SWAP}=t,o=e?[e.sell.asset,e.buy.asset].map(encodeURIComponent).join("/"):"";return`/${s}/widget/${n}/${o}`}function p(t){const{tradeAssets:s,theme:e}=t,n=new URLSearchParams;if(s){const{sell:o,buy:a}=s;o.amount&&n.append("sellAmount",o.amount),a.amount&&n.append("buyAmount",a.amount)}return e&&n.append("theme",e),n}const i="cowSwapWidget";function E(t,s){const{container:e,provider:n}=t,o=y(t,s);e.innerHTML="",e.appendChild(o);const{contentWindow:a}=o;if(!a)throw new Error("Iframe does not contain a window!");return P(a,t.metaData),W(o,t.height),n&&new w(a).onConnect(n),o.addEventListener("load",()=>{d(s,a,o)}),c=>d(c,a,o)}function y(t,s){const{width:e,height:n}=t,o=document.createElement("iframe");return o.src=f(s),o.width=`${e}px`,o.height=`${n}px`,o.style.border="0",o}function d(t,s,e){const n=u(t),o=p(t).toString();t.dynamicHeightEnabled||(e.style.height=""),s.postMessage({key:i,method:"update",urlParams:{pathname:n,search:o},appParams:t},"*")}function P(t,s){window.addEventListener("message",e=>{e.data.key!==i||e.data.method!=="activate"||t.postMessage({key:i,method:"metaData",metaData:s},"*")})}function W(t,s){window.addEventListener("message",e=>{if(e.data.key!==i||e.data.method!=="iframeHeight")return;const n=e.data.height||s;t.style.height=`${n}px`})}exports.COWSWAP_URLS=h;exports.TradeType=r;exports.cowSwapWidget=E;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const w="2.0",g="*",l=["connect","disconnect","close","chainChanged","accountsChanged"];class m{constructor(t){this.contentWindow=t,this.ethereumProvider=null,this.requests={},this.processEvent=e=>{e.data.jsonrpc==="2.0"&&(this.ethereumProvider?this.processRequest(e.data):this.requests[e.data.id]=e.data)},window.addEventListener("message",this.processEvent)}disconnect(){this.ethereumProvider=null,window.removeEventListener("message",this.processEvent)}onConnect(t){this.ethereumProvider=t,Object.keys(this.requests).forEach(e=>{this.processRequest(this.requests[e])}),this.requests={},l.forEach(e=>{t.on(e,n=>{this.postMessage({method:e,params:[n]})})})}processRequest(t){if(!this.ethereumProvider)return;(t.method==="enable"?this.ethereumProvider.enable():this.ethereumProvider.request(t)).then(n=>{this.postMessage({id:t.id,result:n})}).catch(n=>{this.postMessage({id:t.id,error:n})})}postMessage(t){this.contentWindow.postMessage({jsonrpc:w,...t},g)}}function f(){return typeof window>"u"||!window?"":`https://swap-dev-git-${window.location.hostname.replace("widget-configurator-git-","").replace("-cowswap.vercel.app","")}-cowswap.vercel.app`}const d={local:"http://localhost:3000",prod:"https://swap.cow.fi",dev:"https://dev.swap.cow.fi",pr:f()};var r=(s=>(s.SWAP="swap",s.LIMIT="limit",s.ADVANCED="advanced",s))(r||{});function E(s){const t=d[s.env||"prod"],e=h(s),n=u(s);return t+"/#"+e+"?"+n}function h(s){const{chainId:t=1,tradeAssets:e,tradeType:n=r.SWAP}=s,o=e?[e.sell.asset,e.buy.asset].map(encodeURIComponent).join("/"):"";return`/${t}/widget/${n}/${o}`}function u(s){const{tradeAssets:t,theme:e}=s,n=new URLSearchParams;if(t){const{sell:o,buy:i}=t;o.amount&&n.append("sellAmount",o.amount),i.amount&&n.append("buyAmount",i.amount)}return e&&n.append("theme",e),n}const a="cowSwapWidget",p="640px",v="450px";function y(s,t={}){const{provider:e}=t,n=P(t);s.innerHTML="",s.appendChild(n);const{contentWindow:o}=n;if(!o)throw new Error("Iframe does not contain a window!");return W(o,t.appKey),A(n,t.height),e&&new m(o).onConnect(e),n.addEventListener("load",()=>{c(t,o)}),i=>c(i,o)}function P(s){const{width:t=v,height:e=p}=s,n=document.createElement("iframe");return n.src=E(s),n.width=t,n.height=e,n.style.border="0",n}function c(s,t){const e=h(s),n=u(s).toString();t.postMessage({key:a,method:"update",urlParams:{pathname:e,search:n},appParams:{...s,provider:void 0}},"*")}function W(s,t){window.addEventListener("message",e=>{e.data.key!==a||e.data.method!=="activate"||s.postMessage({key:a,method:"metaData",metaData:t?{appKey:t}:void 0},"*")})}function A(s,t=p){window.addEventListener("message",e=>{e.data.key!==a||e.data.method!=="iframeHeight"||(s.style.height=e.data.height?`${e.data.height}px`:t)})}exports.COWSWAP_URLS=d;exports.TradeType=r;exports.cowSwapWidget=y;
package/index.mjs CHANGED
@@ -1,116 +1,134 @@
1
- const p = "2.0", m = "*", l = ["connect", "disconnect", "close", "chainChanged", "accountsChanged"];
2
- class w {
3
- constructor(s) {
4
- this.contentWindow = s, this.ethereumProvider = null, this.requests = {}, this.processEvent = (e) => {
1
+ const p = "2.0", w = "*", m = ["connect", "disconnect", "close", "chainChanged", "accountsChanged"];
2
+ class g {
3
+ /**
4
+ * Creates an instance of JsonRpcManager.
5
+ * @param contentWindow - The window for handling events.
6
+ */
7
+ constructor(t) {
8
+ this.contentWindow = t, this.ethereumProvider = null, this.requests = {}, this.processEvent = (e) => {
5
9
  e.data.jsonrpc === "2.0" && (this.ethereumProvider ? this.processRequest(e.data) : this.requests[e.data.id] = e.data);
6
10
  }, window.addEventListener("message", this.processEvent);
7
11
  }
12
+ /**
13
+ * Disconnects the JSON-RPC manager from the Ethereum provider.
14
+ */
8
15
  disconnect() {
9
16
  this.ethereumProvider = null, window.removeEventListener("message", this.processEvent);
10
17
  }
11
- onConnect(s) {
12
- this.ethereumProvider = s, Object.keys(this.requests).forEach((e) => {
18
+ /**
19
+ * Handles the 'connect' event and sets up event listeners for Ethereum provider events.
20
+ * @param ethereumProvider - The Ethereum provider to connect.
21
+ */
22
+ onConnect(t) {
23
+ this.ethereumProvider = t, Object.keys(this.requests).forEach((e) => {
13
24
  this.processRequest(this.requests[e]);
14
- }), this.requests = {}, l.forEach((e) => {
15
- s.on(e, (n) => {
25
+ }), this.requests = {}, m.forEach((e) => {
26
+ t.on(e, (n) => {
16
27
  this.postMessage({ method: e, params: [n] });
17
28
  });
18
29
  });
19
30
  }
20
- processRequest(s) {
31
+ /**
32
+ * Processes a JSON-RPC request and sends appropriate response or error via the content window.
33
+ * @param request - The JSON-RPC request to be processed.
34
+ */
35
+ processRequest(t) {
21
36
  if (!this.ethereumProvider)
22
37
  return;
23
- (s.method === "enable" ? this.ethereumProvider.enable() : this.ethereumProvider.request(s)).then((n) => {
24
- this.postMessage({ id: s.id, result: n });
38
+ (t.method === "enable" ? this.ethereumProvider.enable() : this.ethereumProvider.request(t)).then((n) => {
39
+ this.postMessage({ id: t.id, result: n });
25
40
  }).catch((n) => {
26
- this.postMessage({ id: s.id, error: n });
41
+ this.postMessage({ id: t.id, error: n });
27
42
  });
28
43
  }
29
- postMessage(s) {
44
+ postMessage(t) {
30
45
  this.contentWindow.postMessage(
31
46
  {
32
47
  jsonrpc: p,
33
- ...s
48
+ ...t
34
49
  },
35
- m
50
+ w
36
51
  );
37
52
  }
38
53
  }
54
+ function l() {
55
+ return typeof window > "u" || !window ? "" : `https://swap-dev-git-${window.location.hostname.replace("widget-configurator-git-", "").replace("-cowswap.vercel.app", "")}-cowswap.vercel.app`;
56
+ }
39
57
  const f = {
40
58
  local: "http://localhost:3000",
41
59
  prod: "https://swap.cow.fi",
42
60
  dev: "https://dev.swap.cow.fi",
43
- pr: window.location.origin
61
+ pr: l()
44
62
  };
45
- var d = /* @__PURE__ */ ((t) => (t.SWAP = "swap", t.LIMIT = "limit", t.ADVANCED = "advanced", t))(d || {});
46
- function g(t) {
47
- const s = f[t.env || "prod"], e = h(t), n = u(t);
48
- return s + "/#" + e + "?" + n;
63
+ var c = /* @__PURE__ */ ((s) => (s.SWAP = "swap", s.LIMIT = "limit", s.ADVANCED = "advanced", s))(c || {});
64
+ function E(s) {
65
+ const t = f[s.env || "prod"], e = d(s), n = h(s);
66
+ return t + "/#" + e + "?" + n;
49
67
  }
50
- function h(t) {
51
- const { chainId: s = 1, tradeAssets: e, tradeType: n = d.SWAP } = t, o = e ? [e.sell.asset, e.buy.asset].map(encodeURIComponent).join("/") : "";
52
- return `/${s}/widget/${n}/${o}`;
68
+ function d(s) {
69
+ const { chainId: t = 1, tradeAssets: e, tradeType: n = c.SWAP } = s, o = e ? [e.sell.asset, e.buy.asset].map(encodeURIComponent).join("/") : "";
70
+ return `/${t}/widget/${n}/${o}`;
53
71
  }
54
- function u(t) {
55
- const { tradeAssets: s, theme: e } = t, n = new URLSearchParams();
56
- if (s) {
57
- const { sell: o, buy: a } = s;
58
- o.amount && n.append("sellAmount", o.amount), a.amount && n.append("buyAmount", a.amount);
72
+ function h(s) {
73
+ const { tradeAssets: t, theme: e } = s, n = new URLSearchParams();
74
+ if (t) {
75
+ const { sell: o, buy: i } = t;
76
+ o.amount && n.append("sellAmount", o.amount), i.amount && n.append("buyAmount", i.amount);
59
77
  }
60
78
  return e && n.append("theme", e), n;
61
79
  }
62
- const i = "cowSwapWidget";
63
- function v(t, s) {
64
- const { container: e, provider: n } = t, o = E(t, s);
65
- e.innerHTML = "", e.appendChild(o);
66
- const { contentWindow: a } = o;
67
- if (!a)
80
+ const a = "cowSwapWidget", u = "640px", v = "450px";
81
+ function A(s, t = {}) {
82
+ const { provider: e } = t, n = y(t);
83
+ s.innerHTML = "", s.appendChild(n);
84
+ const { contentWindow: o } = n;
85
+ if (!o)
68
86
  throw new Error("Iframe does not contain a window!");
69
- return y(a, t.metaData), P(o, t.height), n && new w(a).onConnect(n), o.addEventListener("load", () => {
70
- c(s, a, o);
71
- }), (r) => c(r, a, o);
87
+ return P(o, t.appKey), W(n, t.height), e && new g(o).onConnect(e), n.addEventListener("load", () => {
88
+ r(t, o);
89
+ }), (i) => r(i, o);
72
90
  }
73
- function E(t, s) {
74
- const { width: e, height: n } = t, o = document.createElement("iframe");
75
- return o.src = g(s), o.width = `${e}px`, o.height = `${n}px`, o.style.border = "0", o;
91
+ function y(s) {
92
+ const { width: t = v, height: e = u } = s, n = document.createElement("iframe");
93
+ return n.src = E(s), n.width = t, n.height = e, n.style.border = "0", n;
76
94
  }
77
- function c(t, s, e) {
78
- const n = h(t), o = u(t).toString();
79
- t.dynamicHeightEnabled || (e.style.height = ""), s.postMessage(
95
+ function r(s, t) {
96
+ const e = d(s), n = h(s).toString();
97
+ t.postMessage(
80
98
  {
81
- key: i,
99
+ key: a,
82
100
  method: "update",
83
101
  urlParams: {
84
- pathname: n,
85
- search: o
102
+ pathname: e,
103
+ search: n
86
104
  },
87
- appParams: t
105
+ appParams: {
106
+ ...s,
107
+ provider: void 0
108
+ }
88
109
  },
89
110
  "*"
90
111
  );
91
112
  }
92
- function y(t, s) {
113
+ function P(s, t) {
93
114
  window.addEventListener("message", (e) => {
94
- e.data.key !== i || e.data.method !== "activate" || t.postMessage(
115
+ e.data.key !== a || e.data.method !== "activate" || s.postMessage(
95
116
  {
96
- key: i,
117
+ key: a,
97
118
  method: "metaData",
98
- metaData: s
119
+ metaData: t ? { appKey: t } : void 0
99
120
  },
100
121
  "*"
101
122
  );
102
123
  });
103
124
  }
104
- function P(t, s) {
125
+ function W(s, t = u) {
105
126
  window.addEventListener("message", (e) => {
106
- if (e.data.key !== i || e.data.method !== "iframeHeight")
107
- return;
108
- const n = e.data.height || s;
109
- t.style.height = `${n}px`;
127
+ e.data.key !== a || e.data.method !== "iframeHeight" || (s.style.height = e.data.height ? `${e.data.height}px` : t);
110
128
  });
111
129
  }
112
130
  export {
113
131
  f as COWSWAP_URLS,
114
- d as TradeType,
115
- v as cowSwapWidget
132
+ c as TradeType,
133
+ A as cowSwapWidget
116
134
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cowprotocol/widget-lib",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "type": "commonjs",
5
5
  "description": "CoW Swap Widget Library. Allows you to easily embed a CoW Swap widget on your website.",
6
6
  "main": "index.js",
package/types.d.ts CHANGED
@@ -1,19 +1,48 @@
1
+ import type { SupportedChainId } from '@cowprotocol/cow-sdk';
1
2
  export interface JsonRpcRequest {
2
3
  id: number;
3
4
  method: string;
4
5
  params: unknown[];
5
6
  }
6
7
  export interface EthereumProvider {
8
+ /**
9
+ * Subscribes to Ethereum-related events.
10
+ * @param event - The event to subscribe to.
11
+ * @param args - Arguments for the event.
12
+ */
7
13
  on(event: string, args: unknown): void;
14
+ /**
15
+ * Sends a JSON-RPC request to the Ethereum provider and returns the response.
16
+ * @param params - JSON-RPC request parameters.
17
+ * @returns A promise that resolves with the response.
18
+ */
8
19
  request<T>(params: JsonRpcRequest): Promise<T>;
20
+ /**
21
+ * Requests permission to connect to the Ethereum provider.
22
+ * @returns A promise that resolves once permission is granted.
23
+ */
9
24
  enable(): Promise<void>;
10
25
  }
11
26
  export type CowSwapWidgetEnv = 'local' | 'prod' | 'dev' | 'pr';
12
27
  export type CowSwapTheme = 'dark' | 'light';
28
+ /**
29
+ *Trade asset parameters, for example:
30
+ * { asset: 'WBTC', amount: 12 }
31
+ * or
32
+ * { asset: '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48' } // USDC
33
+ */
13
34
  interface TradeAsset {
35
+ /** The asset symbol or identifier. */
14
36
  asset: string;
37
+ /**
38
+ * The amount of the asset (optional).
39
+ * If specified, represents the quantity or value of the asset.
40
+ */
15
41
  amount?: string;
16
42
  }
43
+ /**
44
+ * A pair of assets to trade.
45
+ */
17
46
  export interface TradeAssets {
18
47
  sell: TradeAsset;
19
48
  buy: TradeAsset;
@@ -21,39 +50,82 @@ export interface TradeAssets {
21
50
  export declare enum TradeType {
22
51
  SWAP = "swap",
23
52
  LIMIT = "limit",
53
+ /**
54
+ * Currently it means only TWAP orders.
55
+ * But in the future it can be extended to support other order types.
56
+ */
24
57
  ADVANCED = "advanced"
25
58
  }
26
- export interface CowSwapWidgetUrlParams {
27
- chainId?: number;
28
- tradeType?: TradeType;
29
- env?: CowSwapWidgetEnv;
30
- tradeAssets?: TradeAssets;
31
- theme?: CowSwapTheme;
32
- }
33
59
  export interface CowSwapWidgetPalette {
34
60
  primaryColor: string;
35
61
  screenBackground: string;
36
62
  widgetBackground: string;
37
63
  textColor: string;
38
64
  }
39
- export interface CowSwapWidgetAppParams {
40
- logoUrl?: string;
41
- hideLogo?: boolean;
42
- hideNetworkSelector?: boolean;
43
- dynamicHeightEnabled?: boolean;
44
- enabledTradeTypes?: TradeType[];
45
- palette?: CowSwapWidgetPalette;
46
- }
47
- export type CowSwapWidgetSettings = CowSwapWidgetUrlParams & CowSwapWidgetAppParams;
48
- export interface CowSwapWidgetMetaData {
65
+ interface CowSwapWidgetConfig {
66
+ /**
67
+ * The width of the widget in pixels. Default: 400px
68
+ */
69
+ width: string;
70
+ /**
71
+ * The height of the widget in pixels. Default: 600px
72
+ */
73
+ height: string;
74
+ /**
75
+ * The unique identifier of the widget consumer.
76
+ * Please fill the for to let us know a little about you: <TODO-TYPEFORM>
77
+ */
49
78
  appKey: string;
50
- url: string;
51
- }
52
- export interface CowSwapWidgetParams {
53
- width: number;
54
- height: number;
55
- container: HTMLElement;
56
- metaData: CowSwapWidgetMetaData;
57
- provider?: EthereumProvider;
79
+ /**
80
+ * The widget might be connected to a custom Ethereum provider.
81
+ */
82
+ provider: EthereumProvider;
83
+ /**
84
+ * Network ID.
85
+ */
86
+ chainId: SupportedChainId;
87
+ /**
88
+ * Swap, Limit or Advanced (Twap).
89
+ */
90
+ tradeType: TradeType;
91
+ /**
92
+ * The environment of the widget. Default: prod
93
+ */
94
+ env: CowSwapWidgetEnv;
95
+ /**
96
+ * The assets to trade.
97
+ */
98
+ tradeAssets: TradeAssets;
99
+ /**
100
+ * The theme of the widget UI.
101
+ */
102
+ theme: CowSwapTheme;
103
+ /**
104
+ * Allows to set a custom logo for the widget.
105
+ */
106
+ logoUrl: string;
107
+ /**
108
+ * Option to hide the logo in the widget.
109
+ */
110
+ hideLogo: boolean;
111
+ /**
112
+ * Option to hide the network selector in the widget.
113
+ */
114
+ hideNetworkSelector: boolean;
115
+ /**
116
+ * Enables the ability to switch between trade types in the widget.
117
+ */
118
+ enabledTradeTypes: TradeType[];
119
+ /**
120
+ * Colors palette to customize the widget UI.
121
+ */
122
+ palette: CowSwapWidgetPalette;
123
+ /**
124
+ * The partner fee in basis points.
125
+ * For example: 1.5% = 150 bips
126
+ * Please contact <TODO-TYPEFORM> to enable your partner fee.
127
+ */
128
+ partnerFeeBips: string;
58
129
  }
130
+ export type CowSwapWidgetParams = Partial<CowSwapWidgetConfig>;
59
131
  export {};
package/urlUtils.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { CowSwapWidgetUrlParams } from './types';
2
- export declare function buildWidgetUrl(params: CowSwapWidgetUrlParams): string;
3
- export declare function buildWidgetPath(params: CowSwapWidgetUrlParams): string;
4
- export declare function buildTradeAmountsQuery(params: CowSwapWidgetUrlParams): URLSearchParams;
1
+ import { CowSwapWidgetParams } from './types';
2
+ export declare function buildWidgetUrl(params: CowSwapWidgetParams): string;
3
+ export declare function buildWidgetPath(params: CowSwapWidgetParams): string;
4
+ export declare function buildTradeAmountsQuery(params: CowSwapWidgetParams): URLSearchParams;