@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 +62 -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 +34 -11
- /package/{utils.d.ts → urlUtils.d.ts} +0 -0
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
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 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
|
|
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 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(
|
|
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 = {}, m.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: u,
|
|
33
|
+
...s
|
|
38
34
|
},
|
|
39
|
-
|
|
35
|
+
p
|
|
40
36
|
);
|
|
41
37
|
}
|
|
42
38
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
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
|
|
48
|
-
const { chainId:
|
|
49
|
-
return `/${
|
|
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
|
|
52
|
-
const { tradeAssets:
|
|
53
|
-
if (
|
|
54
|
-
const { sell: o, buy:
|
|
55
|
-
o.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
|
|
59
|
+
return e && n.append("theme", e), n;
|
|
58
60
|
}
|
|
59
|
-
const
|
|
60
|
-
function
|
|
61
|
-
const { container: e, provider:
|
|
62
|
-
e.innerHTML = "", e.appendChild(
|
|
63
|
-
const { contentWindow:
|
|
64
|
-
if (!
|
|
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
|
|
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
|
|
69
|
-
const
|
|
70
|
-
e.
|
|
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:
|
|
73
|
-
|
|
74
|
-
|
|
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
|
|
80
|
-
|
|
81
|
-
|
|
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
|
-
|
|
85
|
-
|
|
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
|
|
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' | '
|
|
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
|
|
28
|
-
|
|
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
|
-
|
|
35
|
-
height?: number;
|
|
36
|
-
urlParams?: CowSwapWidgetUrlParams;
|
|
59
|
+
metaData: CowSwapWidgetMetaData;
|
|
37
60
|
provider?: EthereumProvider;
|
|
38
61
|
}
|
|
39
62
|
export {};
|
|
File without changes
|