@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 +70 -0
- package/consts.d.ts +0 -3
- package/cowSwapWidget.d.ts +3 -4
- package/index.d.ts +1 -0
- package/index.js +1 -1
- package/index.mjs +84 -54
- package/package.json +2 -2
- package/types.d.ts +29 -9
- /package/{utils.d.ts → urlUtils.d.ts} +0 -0
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
package/cowSwapWidget.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { CowSwapWidgetParams,
|
|
2
|
-
type UpdateWidgetCallback = (params:
|
|
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
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});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,86 +1,116 @@
|
|
|
1
|
-
const
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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(
|
|
16
|
-
this.ethereumProvider =
|
|
17
|
-
this.processRequest(this.requests[
|
|
18
|
-
}), this.requests = {},
|
|
19
|
-
|
|
20
|
-
this.postMessage({ method:
|
|
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(
|
|
20
|
+
processRequest(s) {
|
|
25
21
|
if (!this.ethereumProvider)
|
|
26
22
|
return;
|
|
27
|
-
(
|
|
28
|
-
this.postMessage({ id:
|
|
29
|
-
}).catch((
|
|
30
|
-
this.postMessage({ id:
|
|
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(
|
|
29
|
+
postMessage(s) {
|
|
34
30
|
this.contentWindow.postMessage(
|
|
35
31
|
{
|
|
36
|
-
jsonrpc:
|
|
37
|
-
...
|
|
32
|
+
jsonrpc: p,
|
|
33
|
+
...s
|
|
38
34
|
},
|
|
39
|
-
|
|
35
|
+
m
|
|
40
36
|
);
|
|
41
37
|
}
|
|
42
38
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
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
|
|
48
|
-
const { chainId:
|
|
49
|
-
return `/${
|
|
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
|
|
52
|
-
const { tradeAssets:
|
|
53
|
-
if (
|
|
54
|
-
const { sell: o, buy:
|
|
55
|
-
o.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
|
|
60
|
+
return e && n.append("theme", e), n;
|
|
58
61
|
}
|
|
59
|
-
const
|
|
60
|
-
function
|
|
61
|
-
const { container: e, provider:
|
|
62
|
-
e.innerHTML = "", e.appendChild(
|
|
63
|
-
const { contentWindow:
|
|
64
|
-
if (!
|
|
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
|
|
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
|
|
69
|
-
const
|
|
70
|
-
e.
|
|
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:
|
|
73
|
-
|
|
74
|
-
|
|
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
|
|
80
|
-
|
|
81
|
-
|
|
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
|
-
|
|
85
|
-
|
|
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.
|
|
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' | '
|
|
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
|
-
|
|
35
|
-
height?: number;
|
|
36
|
-
urlParams?: CowSwapWidgetUrlParams;
|
|
56
|
+
metaData: CowSwapWidgetMetaData;
|
|
37
57
|
provider?: EthereumProvider;
|
|
38
58
|
}
|
|
39
59
|
export {};
|
|
File without changes
|