@cowprotocol/widget-lib 0.0.7 → 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 ADDED
@@ -0,0 +1,70 @@
1
+ # CoW Swap Widget Library
2
+
3
+ Integrate the power of `CowSwap` into your product!
4
+ With the widget, you can create an incredible trading interface. Specify the required pair of currencies, customize the
5
+ look and much more!
6
+
7
+ ## Live example
8
+
9
+ See the widget in action in the [widget configurator](../../apps/widget-configurator/src/main.tsx)
10
+
11
+ ## Docs
12
+
13
+ You can find a detailed description of all widget parameters in the [documentation](../../libs/widget-lib/docs/README.md)
14
+
15
+ ## Quick start
16
+
17
+ ```
18
+ npm install @cowprotocol/widget-lib --save
19
+ ```
20
+
21
+ ```
22
+ yarn add @cowprotocol/widget-lib
23
+ ```
24
+
25
+ Create a container somewhere in your website, the widget will be rendered inside it:
26
+
27
+ ```html
28
+ <div id="cowswap-widget"></div>
29
+ ```
30
+
31
+ Import the widget and initialise it:
32
+
33
+ ```js
34
+ import { cowSwapWidget } from '@cowprotocol/widget-lib'
35
+
36
+ // Initialise the widget
37
+ const widgetContainer = document.getElementById('cowswap-widget')
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
+ }, {
49
+ sell: { asset: 'DAI' },
50
+ buy: { asset: 'USDC', amount: '0.1' },
51
+ // instantiate your own web3 provider
52
+ provider: window.ethereum
53
+ })
54
+
55
+ // You also can change widget configuration on the fly
56
+ updateWidget({ tradeType: 'limit' })
57
+ ```
58
+
59
+ ## Developers
60
+
61
+ #### Test
62
+ ```
63
+ nx test widget-lib
64
+ ```
65
+
66
+ #### Build the library
67
+ ```
68
+ nx build widget-lib
69
+ ```
70
+
package/consts.d.ts CHANGED
@@ -1,5 +1,2 @@
1
1
  import { CowSwapWidgetEnv } from './types';
2
- export declare const DEFAULT_CHAIN = 1;
3
- export declare const DEFAULT_WIDTH = 400;
4
- export declare const DEFAULT_HEIGHT = 640;
5
2
  export declare const COWSWAP_URLS: Record<CowSwapWidgetEnv, string>;
@@ -1,4 +1,3 @@
1
- import { CowSwapWidgetParams, CowSwapWidgetUrlParams } from './types';
2
- type UpdateWidgetCallback = (params: CowSwapWidgetUrlParams) => void;
3
- export declare function cowSwapWidget(params: CowSwapWidgetParams): UpdateWidgetCallback;
4
- export {};
1
+ import { CowSwapWidgetParams, CowSwapWidgetSettings } from './types';
2
+ export type UpdateWidgetCallback = (params: CowSwapWidgetSettings) => void;
3
+ export declare function cowSwapWidget(params: CowSwapWidgetParams, settings: CowSwapWidgetSettings): UpdateWidgetCallback;
package/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export { cowSwapWidget } from './cowSwapWidget';
2
+ export type { UpdateWidgetCallback } from './cowSwapWidget';
2
3
  export { COWSWAP_URLS } from './consts';
3
4
  export * from './types';
package/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const d=1,h=400,u=640,r={local:"http://localhost:3000",barn:"https://barn.cow.fi",prod:"http://swap.cow.fi"},p="2.0",l="*",m=["connect","disconnect","chainChanged","accountsChanged"];class w{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:p,...e},l)}}function f(n){const e=r[n.env??"prod"],t=c(n),s=a(n);return e+"/#"+t+"?"+s}function c(n){const{chainId:e=d,tradeAssets:t}=n,s=t?[t.sell.asset,t.buy.asset].map(encodeURIComponent).join("/"):"";return`/${e}/swap/widget/${s}`}function a(n){const{tradeAssets:e,theme:t}=n,s=new URLSearchParams;if(e){const{sell:o,buy:i}=e;o.amount&&s.append("sellAmount",o.amount),i.amount&&s.append("buyAmount",i.amount)}return t&&s.append("theme",t),s}const g="@cowprotocol/widget-lib";function E(n){const{container:e,provider:t}=n,s=b(n);e.innerHTML="",e.appendChild(s);const{contentWindow:o}=s;if(!o)throw new Error("Iframe does not contain a window!");return t&&new w(o).onConnect(t),i=>W(i,o)}function W(n,e){const t=c(n),s=a(n).toString();e.postMessage({key:g,pathname:t,search:s},"*")}function b(n){const{width:e=h,height:t=u}=n,s=document.createElement("iframe");return s.src=f(n.urlParams??{}),s.width=`${e}px`,s.height=`${t}px`,s.style.border="0",s}exports.COWSWAP_URLS=r;exports.cowSwapWidget=E;
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;
package/index.mjs CHANGED
@@ -1,86 +1,116 @@
1
- const a = {
2
- local: "http://localhost:3000",
3
- barn: "https://barn.cow.fi",
4
- prod: "http://swap.cow.fi"
5
- }, h = "2.0", d = "*", u = ["connect", "disconnect", "chainChanged", "accountsChanged"];
6
- class p {
7
- constructor(e) {
8
- this.contentWindow = e, this.ethereumProvider = null, this.requests = {}, this.processEvent = (t) => {
9
- t.data.jsonrpc === "2.0" && (this.ethereumProvider ? this.processRequest(t.data) : this.requests[t.data.id] = t.data);
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) => {
5
+ e.data.jsonrpc === "2.0" && (this.ethereumProvider ? this.processRequest(e.data) : this.requests[e.data.id] = e.data);
10
6
  }, window.addEventListener("message", this.processEvent);
11
7
  }
12
8
  disconnect() {
13
9
  this.ethereumProvider = null, window.removeEventListener("message", this.processEvent);
14
10
  }
15
- onConnect(e) {
16
- this.ethereumProvider = e, Object.keys(this.requests).forEach((t) => {
17
- this.processRequest(this.requests[t]);
18
- }), this.requests = {}, u.forEach((t) => {
19
- e.on(t, (s) => {
20
- this.postMessage({ method: t, params: [s] });
11
+ onConnect(s) {
12
+ this.ethereumProvider = s, Object.keys(this.requests).forEach((e) => {
13
+ this.processRequest(this.requests[e]);
14
+ }), this.requests = {}, l.forEach((e) => {
15
+ s.on(e, (n) => {
16
+ this.postMessage({ method: e, params: [n] });
21
17
  });
22
18
  });
23
19
  }
24
- processRequest(e) {
20
+ processRequest(s) {
25
21
  if (!this.ethereumProvider)
26
22
  return;
27
- (e.method === "enable" ? this.ethereumProvider.enable() : this.ethereumProvider.request(e)).then((s) => {
28
- this.postMessage({ id: e.id, result: s });
29
- }).catch((s) => {
30
- this.postMessage({ id: e.id, error: s });
23
+ (s.method === "enable" ? this.ethereumProvider.enable() : this.ethereumProvider.request(s)).then((n) => {
24
+ this.postMessage({ id: s.id, result: n });
25
+ }).catch((n) => {
26
+ this.postMessage({ id: s.id, error: n });
31
27
  });
32
28
  }
33
- postMessage(e) {
29
+ postMessage(s) {
34
30
  this.contentWindow.postMessage(
35
31
  {
36
- jsonrpc: h,
37
- ...e
32
+ jsonrpc: p,
33
+ ...s
38
34
  },
39
- d
35
+ m
40
36
  );
41
37
  }
42
38
  }
43
- function l(n) {
44
- const e = a[n.env ?? "prod"], t = r(n), s = c(n);
45
- return e + "/#" + t + "?" + s;
39
+ const f = {
40
+ local: "http://localhost:3000",
41
+ prod: "https://swap.cow.fi",
42
+ dev: "https://dev.swap.cow.fi",
43
+ pr: window.location.origin
44
+ };
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;
46
49
  }
47
- function r(n) {
48
- const { chainId: e = 1, tradeAssets: t } = n, s = t ? [t.sell.asset, t.buy.asset].map(encodeURIComponent).join("/") : "";
49
- return `/${e}/swap/widget/${s}`;
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}`;
50
53
  }
51
- function c(n) {
52
- const { tradeAssets: e, theme: t } = n, s = new URLSearchParams();
53
- if (e) {
54
- const { sell: o, buy: i } = e;
55
- o.amount && s.append("sellAmount", o.amount), i.amount && s.append("buyAmount", i.amount);
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);
56
59
  }
57
- return t && s.append("theme", t), s;
60
+ return e && n.append("theme", e), n;
58
61
  }
59
- const m = "@cowprotocol/widget-lib";
60
- function g(n) {
61
- const { container: e, provider: t } = n, s = w(n);
62
- e.innerHTML = "", e.appendChild(s);
63
- const { contentWindow: o } = s;
64
- if (!o)
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)
65
68
  throw new Error("Iframe does not contain a window!");
66
- return t && new p(o).onConnect(t), (i) => f(i, o);
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);
67
72
  }
68
- function f(n, e) {
69
- const t = r(n), s = c(n).toString();
70
- e.postMessage(
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;
76
+ }
77
+ function c(t, s, e) {
78
+ const n = h(t), o = u(t).toString();
79
+ t.dynamicHeightEnabled || (e.style.height = ""), s.postMessage(
71
80
  {
72
- key: m,
73
- pathname: t,
74
- search: s
81
+ key: i,
82
+ method: "update",
83
+ urlParams: {
84
+ pathname: n,
85
+ search: o
86
+ },
87
+ appParams: t
75
88
  },
76
89
  "*"
77
90
  );
78
91
  }
79
- function w(n) {
80
- const { width: e = 400, height: t = 640 } = n, s = document.createElement("iframe");
81
- return s.src = l(n.urlParams ?? {}), s.width = `${e}px`, s.height = `${t}px`, s.style.border = "0", s;
92
+ function y(t, s) {
93
+ window.addEventListener("message", (e) => {
94
+ e.data.key !== i || e.data.method !== "activate" || t.postMessage(
95
+ {
96
+ key: i,
97
+ method: "metaData",
98
+ metaData: s
99
+ },
100
+ "*"
101
+ );
102
+ });
103
+ }
104
+ function P(t, s) {
105
+ 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`;
110
+ });
82
111
  }
83
112
  export {
84
- a as COWSWAP_URLS,
85
- g as cowSwapWidget
113
+ f as COWSWAP_URLS,
114
+ d as TradeType,
115
+ v as cowSwapWidget
86
116
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cowprotocol/widget-lib",
3
- "version": "0.0.7",
3
+ "version": "0.1.1",
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",
@@ -21,4 +21,4 @@
21
21
  "widget-lib"
22
22
  ],
23
23
  "dependencies": {}
24
- }
24
+ }
package/types.d.ts CHANGED
@@ -1,8 +1,3 @@
1
- declare global {
2
- interface Window {
3
- ethereum?: EthereumProvider;
4
- }
5
- }
6
1
  export interface JsonRpcRequest {
7
2
  id: number;
8
3
  method: string;
@@ -13,7 +8,7 @@ export interface EthereumProvider {
13
8
  request<T>(params: JsonRpcRequest): Promise<T>;
14
9
  enable(): Promise<void>;
15
10
  }
16
- export type CowSwapWidgetEnv = 'local' | 'prod' | 'barn';
11
+ export type CowSwapWidgetEnv = 'local' | 'prod' | 'dev' | 'pr';
17
12
  export type CowSwapTheme = 'dark' | 'light';
18
13
  interface TradeAsset {
19
14
  asset: string;
@@ -23,17 +18,42 @@ export interface TradeAssets {
23
18
  sell: TradeAsset;
24
19
  buy: TradeAsset;
25
20
  }
21
+ export declare enum TradeType {
22
+ SWAP = "swap",
23
+ LIMIT = "limit",
24
+ ADVANCED = "advanced"
25
+ }
26
26
  export interface CowSwapWidgetUrlParams {
27
27
  chainId?: number;
28
+ tradeType?: TradeType;
28
29
  env?: CowSwapWidgetEnv;
29
30
  tradeAssets?: TradeAssets;
30
31
  theme?: CowSwapTheme;
31
32
  }
33
+ export interface CowSwapWidgetPalette {
34
+ primaryColor: string;
35
+ screenBackground: string;
36
+ widgetBackground: string;
37
+ textColor: string;
38
+ }
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 {
49
+ appKey: string;
50
+ url: string;
51
+ }
32
52
  export interface CowSwapWidgetParams {
53
+ width: number;
54
+ height: number;
33
55
  container: HTMLElement;
34
- width?: number;
35
- height?: number;
36
- urlParams?: CowSwapWidgetUrlParams;
56
+ metaData: CowSwapWidgetMetaData;
37
57
  provider?: EthereumProvider;
38
58
  }
39
59
  export {};
File without changes