@cowprotocol/widget-lib 0.0.6 → 0.1.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/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # CoW Swap Widget Library
2
+
3
+ ## Use it
4
+
5
+ Install dependency
6
+
7
+ ```bash
8
+ # Install the dependency using NPM
9
+ npm install @cowprotocol/widget-lib --save
10
+
11
+ # ...or alternativelly use YARN
12
+ yarn add @cowprotocol/widget-lib
13
+ ```
14
+
15
+ Create an empty div somewhere in your website:
16
+
17
+ ```html
18
+ <div id="cowswap-widget"></div>
19
+ ```
20
+
21
+ Import the widget and initialise it:
22
+
23
+ ```js
24
+ import { cowSwapWidget } from '@cowprotocol/widget-lib'
25
+
26
+ // Initialise the widget
27
+ const widgetContainer = document.getElementById('cowswap-widget')
28
+ cowSwapWidget({
29
+ container: widgetContainer
30
+ })
31
+
32
+
33
+ // Additionally, you can pass some additional params to customise your widget
34
+ provider = /* instantiate your own web3 provider */
35
+ cowSwapWidget({
36
+ container: widgetContainer,
37
+ width: 600
38
+ height: 640
39
+ urlParams: {
40
+ sell: { asset: 'DAI' },
41
+ buy: { asset: 'USDC', amount: '0.1' }
42
+ },
43
+ provider
44
+ })
45
+ ```
46
+
47
+ ## Developers
48
+
49
+ ```bash
50
+ # Run demo project locally
51
+ # Will open http://localhost:3012/src/demo/index.html
52
+ nx serve widget-lib
53
+
54
+ # Test
55
+ nx test widget-lib
56
+
57
+ # Build the library
58
+ nx build widget-lib
59
+
60
+ # Publish to NPM
61
+ nx publish widget-lib --ver 0.0.7 --tag latest
62
+ ```
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 d={local:"http://localhost:3000",prod:"https://swap.cow.fi",dev:"https://dev.swap.cow.fi",pr:window.location.origin};function f(t){const s=d[t.env],e=h(t),n=u(t);return s+"/#"+e+"?"+n}function h(t){const{chainId:s,tradeAssets:e,tradeType:n}=t,o=e?[e.sell.asset,e.buy.asset].map(encodeURIComponent).join("/"):"";return`/${s}/widget/${n}/${o}`}function u(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 P(t,s){const{container:e,provider:n}=t,o=E(t,s);e.innerHTML="",e.appendChild(o);const{contentWindow:a}=o;if(!a)throw new Error("Iframe does not contain a window!");return y(a,t.metaData),W(o,t.height),n&&new w(a).onConnect(n),o.addEventListener("load",()=>{c(s,a,o)}),r=>c(r,a,o)}function E(t,s){const{width:e,height:n}=t,o=document.createElement("iframe");return o.src=f(s.urlParams),o.width=`${e}px`,o.height=`${n}px`,o.style.border="0",o}function c(t,s,e){const n=h(t.urlParams),o=u(t.urlParams).toString();t.appParams.dynamicHeightEnabled||(e.style.height=""),s.postMessage({key:i,method:"update",urlParams:{pathname:n,search:o},appParams:t.appParams},"*")}function y(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`})}var p=(t=>(t.SWAP="swap",t.LIMIT="limit",t.ADVANCED="advanced",t))(p||{});exports.COWSWAP_URLS=d;exports.TradeType=p;exports.cowSwapWidget=P;
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 u = "2.0", p = "*", m = ["connect", "disconnect", "close", "chainChanged", "accountsChanged"];
2
+ class l {
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 = {}, m.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: u,
33
+ ...s
38
34
  },
39
- d
35
+ p
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 w = {
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
+ function f(t) {
46
+ const s = w[t.env], e = d(t), n = h(t);
47
+ return s + "/#" + e + "?" + n;
46
48
  }
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}`;
49
+ function d(t) {
50
+ const { chainId: s, tradeAssets: e, tradeType: n } = t, o = e ? [e.sell.asset, e.buy.asset].map(encodeURIComponent).join("/") : "";
51
+ return `/${s}/widget/${n}/${o}`;
50
52
  }
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);
53
+ function h(t) {
54
+ const { tradeAssets: s, theme: e } = t, n = new URLSearchParams();
55
+ if (s) {
56
+ const { sell: o, buy: a } = s;
57
+ o.amount && n.append("sellAmount", o.amount), a.amount && n.append("buyAmount", a.amount);
56
58
  }
57
- return t && s.append("theme", t), s;
59
+ return e && n.append("theme", e), n;
58
60
  }
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)
61
+ const i = "cowSwapWidget";
62
+ function v(t, s) {
63
+ const { container: e, provider: n } = t, o = g(t, s);
64
+ e.innerHTML = "", e.appendChild(o);
65
+ const { contentWindow: a } = o;
66
+ if (!a)
65
67
  throw new Error("Iframe does not contain a window!");
66
- return t && new p(o).onConnect(t), (i) => f(i, o);
68
+ return E(a, t.metaData), P(o, t.height), n && new l(a).onConnect(n), o.addEventListener("load", () => {
69
+ c(s, a, o);
70
+ }), (r) => c(r, a, o);
67
71
  }
68
- function f(n, e) {
69
- const t = r(n), s = c(n).toString();
70
- e.postMessage(
72
+ function g(t, s) {
73
+ const { width: e, height: n } = t, o = document.createElement("iframe");
74
+ return o.src = f(s.urlParams), o.width = `${e}px`, o.height = `${n}px`, o.style.border = "0", o;
75
+ }
76
+ function c(t, s, e) {
77
+ const n = d(t.urlParams), o = h(t.urlParams).toString();
78
+ t.appParams.dynamicHeightEnabled || (e.style.height = ""), s.postMessage(
71
79
  {
72
- key: m,
73
- pathname: t,
74
- search: s
80
+ key: i,
81
+ method: "update",
82
+ urlParams: {
83
+ pathname: n,
84
+ search: o
85
+ },
86
+ appParams: t.appParams
75
87
  },
76
88
  "*"
77
89
  );
78
90
  }
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;
91
+ function E(t, s) {
92
+ window.addEventListener("message", (e) => {
93
+ e.data.key !== i || e.data.method !== "activate" || t.postMessage(
94
+ {
95
+ key: i,
96
+ method: "metaData",
97
+ metaData: s
98
+ },
99
+ "*"
100
+ );
101
+ });
102
+ }
103
+ function P(t, s) {
104
+ window.addEventListener("message", (e) => {
105
+ if (e.data.key !== i || e.data.method !== "iframeHeight")
106
+ return;
107
+ const n = e.data.height || s;
108
+ t.style.height = `${n}px`;
109
+ });
82
110
  }
111
+ var y = /* @__PURE__ */ ((t) => (t.SWAP = "swap", t.LIMIT = "limit", t.ADVANCED = "advanced", t))(y || {});
83
112
  export {
84
- a as COWSWAP_URLS,
85
- g as cowSwapWidget
113
+ w as COWSWAP_URLS,
114
+ y 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.6",
3
+ "version": "0.1.0",
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,45 @@ 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
- chainId?: number;
28
- env?: CowSwapWidgetEnv;
27
+ chainId: number;
28
+ tradeType: TradeType;
29
+ env: CowSwapWidgetEnv;
29
30
  tradeAssets?: TradeAssets;
30
31
  theme?: CowSwapTheme;
31
32
  }
33
+ export interface InjectedWidgetPalette {
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?: InjectedWidgetPalette;
46
+ }
47
+ export interface CowSwapWidgetSettings {
48
+ urlParams: CowSwapWidgetUrlParams;
49
+ appParams: CowSwapWidgetAppParams;
50
+ }
51
+ export interface CowSwapWidgetMetaData {
52
+ appKey: string;
53
+ url: string;
54
+ }
32
55
  export interface CowSwapWidgetParams {
56
+ width: number;
57
+ height: number;
33
58
  container: HTMLElement;
34
- width?: number;
35
- height?: number;
36
- urlParams?: CowSwapWidgetUrlParams;
59
+ metaData: CowSwapWidgetMetaData;
37
60
  provider?: EthereumProvider;
38
61
  }
39
62
  export {};
File without changes