@cowprotocol/widget-lib 0.1.0 → 0.1.2

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
@@ -1,18 +1,28 @@
1
1
  # CoW Swap Widget Library
2
2
 
3
- ## Use it
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!
4
6
 
5
- Install dependency
7
+ ## Live example
6
8
 
7
- ```bash
8
- # Install the dependency using NPM
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
+ ```
9
18
  npm install @cowprotocol/widget-lib --save
19
+ ```
10
20
 
11
- # ...or alternativelly use YARN
21
+ ```
12
22
  yarn add @cowprotocol/widget-lib
13
23
  ```
14
24
 
15
- Create an empty div somewhere in your website:
25
+ Create a container somewhere in your website, the widget will be rendered inside it:
16
26
 
17
27
  ```html
18
28
  <div id="cowswap-widget"></div>
@@ -25,38 +35,36 @@ import { cowSwapWidget } from '@cowprotocol/widget-lib'
25
35
 
26
36
  // Initialise the widget
27
37
  const widgetContainer = document.getElementById('cowswap-widget')
28
- cowSwapWidget({
29
- container: widgetContainer
30
- })
31
38
 
32
-
33
- // Additionally, you can pass some additional params to customise your widget
34
- provider = /* instantiate your own web3 provider */
35
- cowSwapWidget({
39
+ const updateWidget = cowSwapWidget({
36
40
  container: widgetContainer,
37
- width: 600
38
- height: 640
39
- urlParams: {
40
- sell: { asset: 'DAI' },
41
- buy: { asset: 'USDC', amount: '0.1' }
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>'
42
46
  },
43
- provider
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
44
53
  })
54
+
55
+ // You also can change widget configuration on the fly
56
+ updateWidget({ tradeType: 'limit' })
45
57
  ```
46
58
 
47
59
  ## Developers
48
60
 
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
61
+ #### Test
62
+ ```
55
63
  nx test widget-lib
64
+ ```
56
65
 
57
- # Build the library
66
+ #### Build the library
67
+ ```
58
68
  nx build widget-lib
59
-
60
- # Publish to NPM
61
- nx publish widget-lib --ver 0.0.7 --tag latest
62
69
  ```
70
+
package/index.iife.js ADDED
@@ -0,0 +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}({});
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 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;
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,5 +1,5 @@
1
- const u = "2.0", p = "*", m = ["connect", "disconnect", "close", "chainChanged", "accountsChanged"];
2
- class l {
1
+ const p = "2.0", m = "*", l = ["connect", "disconnect", "close", "chainChanged", "accountsChanged"];
2
+ class w {
3
3
  constructor(s) {
4
4
  this.contentWindow = s, this.ethereumProvider = null, this.requests = {}, this.processEvent = (e) => {
5
5
  e.data.jsonrpc === "2.0" && (this.ethereumProvider ? this.processRequest(e.data) : this.requests[e.data.id] = e.data);
@@ -11,7 +11,7 @@ class l {
11
11
  onConnect(s) {
12
12
  this.ethereumProvider = s, Object.keys(this.requests).forEach((e) => {
13
13
  this.processRequest(this.requests[e]);
14
- }), this.requests = {}, m.forEach((e) => {
14
+ }), this.requests = {}, l.forEach((e) => {
15
15
  s.on(e, (n) => {
16
16
  this.postMessage({ method: e, params: [n] });
17
17
  });
@@ -29,28 +29,29 @@ class l {
29
29
  postMessage(s) {
30
30
  this.contentWindow.postMessage(
31
31
  {
32
- jsonrpc: u,
32
+ jsonrpc: p,
33
33
  ...s
34
34
  },
35
- p
35
+ m
36
36
  );
37
37
  }
38
38
  }
39
- const w = {
39
+ const f = {
40
40
  local: "http://localhost:3000",
41
41
  prod: "https://swap.cow.fi",
42
42
  dev: "https://dev.swap.cow.fi",
43
43
  pr: window.location.origin
44
44
  };
45
- function f(t) {
46
- const s = w[t.env], e = d(t), n = h(t);
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);
47
48
  return s + "/#" + e + "?" + n;
48
49
  }
49
- function d(t) {
50
- const { chainId: s, tradeAssets: e, tradeType: n } = t, o = e ? [e.sell.asset, e.buy.asset].map(encodeURIComponent).join("/") : "";
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("/") : "";
51
52
  return `/${s}/widget/${n}/${o}`;
52
53
  }
53
- function h(t) {
54
+ function u(t) {
54
55
  const { tradeAssets: s, theme: e } = t, n = new URLSearchParams();
55
56
  if (s) {
56
57
  const { sell: o, buy: a } = s;
@@ -60,22 +61,22 @@ function h(t) {
60
61
  }
61
62
  const i = "cowSwapWidget";
62
63
  function v(t, s) {
63
- const { container: e, provider: n } = t, o = g(t, s);
64
+ const { container: e, provider: n } = t, o = E(t, s);
64
65
  e.innerHTML = "", e.appendChild(o);
65
66
  const { contentWindow: a } = o;
66
67
  if (!a)
67
68
  throw new Error("Iframe does not contain a window!");
68
- return E(a, t.metaData), P(o, t.height), n && new l(a).onConnect(n), o.addEventListener("load", () => {
69
+ return y(a, t.metaData), P(o, t.height), n && new w(a).onConnect(n), o.addEventListener("load", () => {
69
70
  c(s, a, o);
70
71
  }), (r) => c(r, a, o);
71
72
  }
72
- function g(t, s) {
73
+ function E(t, s) {
73
74
  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
+ return o.src = g(s), o.width = `${e}px`, o.height = `${n}px`, o.style.border = "0", o;
75
76
  }
76
77
  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(
78
+ const n = h(t), o = u(t).toString();
79
+ t.dynamicHeightEnabled || (e.style.height = ""), s.postMessage(
79
80
  {
80
81
  key: i,
81
82
  method: "update",
@@ -83,12 +84,12 @@ function c(t, s, e) {
83
84
  pathname: n,
84
85
  search: o
85
86
  },
86
- appParams: t.appParams
87
+ appParams: t
87
88
  },
88
89
  "*"
89
90
  );
90
91
  }
91
- function E(t, s) {
92
+ function y(t, s) {
92
93
  window.addEventListener("message", (e) => {
93
94
  e.data.key !== i || e.data.method !== "activate" || t.postMessage(
94
95
  {
@@ -108,9 +109,8 @@ function P(t, s) {
108
109
  t.style.height = `${n}px`;
109
110
  });
110
111
  }
111
- var y = /* @__PURE__ */ ((t) => (t.SWAP = "swap", t.LIMIT = "limit", t.ADVANCED = "advanced", t))(y || {});
112
112
  export {
113
- w as COWSWAP_URLS,
114
- y as TradeType,
113
+ f as COWSWAP_URLS,
114
+ d as TradeType,
115
115
  v as cowSwapWidget
116
116
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cowprotocol/widget-lib",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
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
@@ -24,13 +24,13 @@ export declare enum TradeType {
24
24
  ADVANCED = "advanced"
25
25
  }
26
26
  export interface CowSwapWidgetUrlParams {
27
- chainId: number;
28
- tradeType: TradeType;
29
- env: CowSwapWidgetEnv;
27
+ chainId?: number;
28
+ tradeType?: TradeType;
29
+ env?: CowSwapWidgetEnv;
30
30
  tradeAssets?: TradeAssets;
31
31
  theme?: CowSwapTheme;
32
32
  }
33
- export interface InjectedWidgetPalette {
33
+ export interface CowSwapWidgetPalette {
34
34
  primaryColor: string;
35
35
  screenBackground: string;
36
36
  widgetBackground: string;
@@ -42,12 +42,9 @@ export interface CowSwapWidgetAppParams {
42
42
  hideNetworkSelector?: boolean;
43
43
  dynamicHeightEnabled?: boolean;
44
44
  enabledTradeTypes?: TradeType[];
45
- palette?: InjectedWidgetPalette;
46
- }
47
- export interface CowSwapWidgetSettings {
48
- urlParams: CowSwapWidgetUrlParams;
49
- appParams: CowSwapWidgetAppParams;
45
+ palette?: CowSwapWidgetPalette;
50
46
  }
47
+ export type CowSwapWidgetSettings = CowSwapWidgetUrlParams & CowSwapWidgetAppParams;
51
48
  export interface CowSwapWidgetMetaData {
52
49
  appKey: string;
53
50
  url: string;