@cowprotocol/widget-lib 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 +37 -29
- package/index.js +1 -1
- package/index.mjs +22 -22
- package/package.json +1 -1
- package/types.d.ts +6 -9
package/README.md
CHANGED
|
@@ -1,18 +1,28 @@
|
|
|
1
1
|
# CoW Swap Widget Library
|
|
2
2
|
|
|
3
|
-
|
|
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
|
-
|
|
7
|
+
## Live example
|
|
6
8
|
|
|
7
|
-
|
|
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
|
+
```
|
|
9
18
|
npm install @cowprotocol/widget-lib --save
|
|
19
|
+
```
|
|
10
20
|
|
|
11
|
-
|
|
21
|
+
```
|
|
12
22
|
yarn add @cowprotocol/widget-lib
|
|
13
23
|
```
|
|
14
24
|
|
|
15
|
-
Create
|
|
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
|
-
|
|
40
|
-
|
|
41
|
-
|
|
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
|
-
|
|
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
|
-
|
|
50
|
-
|
|
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
|
-
|
|
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.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
|
|
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
|
|
2
|
-
class
|
|
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 = {},
|
|
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:
|
|
32
|
+
jsonrpc: p,
|
|
33
33
|
...s
|
|
34
34
|
},
|
|
35
|
-
|
|
35
|
+
m
|
|
36
36
|
);
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
|
-
const
|
|
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
|
-
|
|
46
|
-
|
|
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
|
|
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
|
|
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 =
|
|
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
|
|
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
|
|
73
|
+
function E(t, s) {
|
|
73
74
|
const { width: e, height: n } = t, o = document.createElement("iframe");
|
|
74
|
-
return o.src =
|
|
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 =
|
|
78
|
-
t.
|
|
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
|
|
87
|
+
appParams: t
|
|
87
88
|
},
|
|
88
89
|
"*"
|
|
89
90
|
);
|
|
90
91
|
}
|
|
91
|
-
function
|
|
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
|
-
|
|
114
|
-
|
|
113
|
+
f as COWSWAP_URLS,
|
|
114
|
+
d as TradeType,
|
|
115
115
|
v as cowSwapWidget
|
|
116
116
|
};
|
package/package.json
CHANGED
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
|
|
28
|
-
tradeType
|
|
29
|
-
env
|
|
27
|
+
chainId?: number;
|
|
28
|
+
tradeType?: TradeType;
|
|
29
|
+
env?: CowSwapWidgetEnv;
|
|
30
30
|
tradeAssets?: TradeAssets;
|
|
31
31
|
theme?: CowSwapTheme;
|
|
32
32
|
}
|
|
33
|
-
export interface
|
|
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?:
|
|
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;
|