@cowprotocol/widget-lib 0.0.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/JsonRpcManager.d.ts +14 -0
- package/consts.d.ts +2 -0
- package/cowSwapWidget.d.ts +4 -0
- package/index.d.ts +3 -0
- package/index.js +1 -0
- package/index.mjs +86 -0
- package/package.json +5 -0
- package/types.d.ts +39 -0
- package/urlUtils.d.ts +4 -0
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { EthereumProvider, JsonRpcRequest } from './types';
|
|
2
|
+
export declare class JsonRpcManager {
|
|
3
|
+
private contentWindow;
|
|
4
|
+
ethereumProvider: EthereumProvider | null;
|
|
5
|
+
requests: {
|
|
6
|
+
[key: string]: JsonRpcRequest;
|
|
7
|
+
};
|
|
8
|
+
constructor(contentWindow: Window);
|
|
9
|
+
disconnect(): void;
|
|
10
|
+
onConnect(ethereumProvider: EthereumProvider): void;
|
|
11
|
+
processRequest(request: JsonRpcRequest): void;
|
|
12
|
+
private processEvent;
|
|
13
|
+
private postMessage;
|
|
14
|
+
}
|
package/consts.d.ts
ADDED
package/index.d.ts
ADDED
package/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const d="2.0",h="*",u=["connect","disconnect","chainChanged","accountsChanged"];class p{constructor(t){this.contentWindow=t,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(t){this.ethereumProvider=t,Object.keys(this.requests).forEach(e=>{this.processRequest(this.requests[e])}),this.requests={},u.forEach(e=>{t.on(e,s=>{this.postMessage({method:e,params:[s]})})})}processRequest(t){if(!this.ethereumProvider)return;(t.method==="enable"?this.ethereumProvider.enable():this.ethereumProvider.request(t)).then(s=>{this.postMessage({id:t.id,result:s})}).catch(s=>{this.postMessage({id:t.id,error:s})})}postMessage(t){this.contentWindow.postMessage({jsonrpc:d,...t},h)}}const r={local:"http://localhost:3000",prod:"swap.cow.fi"};function l(n){const t=r[n.env],e=c(n),s=a(n);return t+"/#"+e+"?"+s}function c(n){const{chainId:t,tradeAssets:e}=n,s=e?[e.sell.asset,e.buy.asset].map(encodeURIComponent).join("/"):"";return`/${t}/swap/widget/${s}`}function a(n){const{tradeAssets:t,theme:e}=n,s=new URLSearchParams;if(t){const{sell:o,buy:i}=t;o.amount&&s.append("sellAmount",o.amount),i.amount&&s.append("buyAmount",i.amount)}return e&&s.append("theme",e),s}const m="cowSwapWidget";function g(n){const{container:t,provider:e}=n,s=f(n);t.innerHTML="",t.appendChild(s);const{contentWindow:o}=s;if(!o)throw new Error("Iframe does not contain a window!");return e&&new p(o).onConnect(e),i=>w(i,o)}function w(n,t){const e=c(n),s=a(n).toString();t.postMessage({key:m,pathname:e,search:s},"*")}function f(n){const{width:t,height:e}=n,s=document.createElement("iframe");return s.src=l(n.urlParams),s.width=`${t}px`,s.height=`${e}px`,s.style.border="0",s}exports.COWSWAP_URLS=r;exports.cowSwapWidget=g;
|
package/index.mjs
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
const a = "2.0", h = "*", d = ["connect", "disconnect", "chainChanged", "accountsChanged"];
|
|
2
|
+
class u {
|
|
3
|
+
constructor(t) {
|
|
4
|
+
this.contentWindow = t, 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);
|
|
6
|
+
}, window.addEventListener("message", this.processEvent);
|
|
7
|
+
}
|
|
8
|
+
disconnect() {
|
|
9
|
+
this.ethereumProvider = null, window.removeEventListener("message", this.processEvent);
|
|
10
|
+
}
|
|
11
|
+
onConnect(t) {
|
|
12
|
+
this.ethereumProvider = t, Object.keys(this.requests).forEach((e) => {
|
|
13
|
+
this.processRequest(this.requests[e]);
|
|
14
|
+
}), this.requests = {}, d.forEach((e) => {
|
|
15
|
+
t.on(e, (s) => {
|
|
16
|
+
this.postMessage({ method: e, params: [s] });
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
processRequest(t) {
|
|
21
|
+
if (!this.ethereumProvider)
|
|
22
|
+
return;
|
|
23
|
+
(t.method === "enable" ? this.ethereumProvider.enable() : this.ethereumProvider.request(t)).then((s) => {
|
|
24
|
+
this.postMessage({ id: t.id, result: s });
|
|
25
|
+
}).catch((s) => {
|
|
26
|
+
this.postMessage({ id: t.id, error: s });
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
postMessage(t) {
|
|
30
|
+
this.contentWindow.postMessage(
|
|
31
|
+
{
|
|
32
|
+
jsonrpc: a,
|
|
33
|
+
...t
|
|
34
|
+
},
|
|
35
|
+
h
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
const p = {
|
|
40
|
+
local: "http://localhost:3000",
|
|
41
|
+
prod: "swap.cow.fi"
|
|
42
|
+
};
|
|
43
|
+
function m(n) {
|
|
44
|
+
const t = p[n.env], e = r(n), s = c(n);
|
|
45
|
+
return t + "/#" + e + "?" + s;
|
|
46
|
+
}
|
|
47
|
+
function r(n) {
|
|
48
|
+
const { chainId: t, tradeAssets: e } = n, s = e ? [e.sell.asset, e.buy.asset].map(encodeURIComponent).join("/") : "";
|
|
49
|
+
return `/${t}/swap/widget/${s}`;
|
|
50
|
+
}
|
|
51
|
+
function c(n) {
|
|
52
|
+
const { tradeAssets: t, theme: e } = n, s = new URLSearchParams();
|
|
53
|
+
if (t) {
|
|
54
|
+
const { sell: o, buy: i } = t;
|
|
55
|
+
o.amount && s.append("sellAmount", o.amount), i.amount && s.append("buyAmount", i.amount);
|
|
56
|
+
}
|
|
57
|
+
return e && s.append("theme", e), s;
|
|
58
|
+
}
|
|
59
|
+
const l = "cowSwapWidget";
|
|
60
|
+
function g(n) {
|
|
61
|
+
const { container: t, provider: e } = n, s = w(n);
|
|
62
|
+
t.innerHTML = "", t.appendChild(s);
|
|
63
|
+
const { contentWindow: o } = s;
|
|
64
|
+
if (!o)
|
|
65
|
+
throw new Error("Iframe does not contain a window!");
|
|
66
|
+
return e && new u(o).onConnect(e), (i) => f(i, o);
|
|
67
|
+
}
|
|
68
|
+
function f(n, t) {
|
|
69
|
+
const e = r(n), s = c(n).toString();
|
|
70
|
+
t.postMessage(
|
|
71
|
+
{
|
|
72
|
+
key: l,
|
|
73
|
+
pathname: e,
|
|
74
|
+
search: s
|
|
75
|
+
},
|
|
76
|
+
"*"
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
function w(n) {
|
|
80
|
+
const { width: t, height: e } = n, s = document.createElement("iframe");
|
|
81
|
+
return s.src = m(n.urlParams), s.width = `${t}px`, s.height = `${e}px`, s.style.border = "0", s;
|
|
82
|
+
}
|
|
83
|
+
export {
|
|
84
|
+
p as COWSWAP_URLS,
|
|
85
|
+
g as cowSwapWidget
|
|
86
|
+
};
|
package/package.json
ADDED
package/types.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
declare global {
|
|
2
|
+
interface Window {
|
|
3
|
+
ethereum?: EthereumProvider;
|
|
4
|
+
}
|
|
5
|
+
}
|
|
6
|
+
export interface JsonRpcRequest {
|
|
7
|
+
id: number;
|
|
8
|
+
method: string;
|
|
9
|
+
params: unknown[];
|
|
10
|
+
}
|
|
11
|
+
export interface EthereumProvider {
|
|
12
|
+
on(event: string, args: unknown): void;
|
|
13
|
+
request<T>(params: JsonRpcRequest): Promise<T>;
|
|
14
|
+
enable(): Promise<void>;
|
|
15
|
+
}
|
|
16
|
+
export type CowSwapWidgetEnv = 'local' | 'prod';
|
|
17
|
+
export type CowSwapTheme = 'dark' | 'light';
|
|
18
|
+
interface TradeAsset {
|
|
19
|
+
asset: string;
|
|
20
|
+
amount?: string;
|
|
21
|
+
}
|
|
22
|
+
export interface TradeAssets {
|
|
23
|
+
sell: TradeAsset;
|
|
24
|
+
buy: TradeAsset;
|
|
25
|
+
}
|
|
26
|
+
export interface CowSwapWidgetUrlParams {
|
|
27
|
+
env: CowSwapWidgetEnv;
|
|
28
|
+
chainId: number;
|
|
29
|
+
tradeAssets?: TradeAssets;
|
|
30
|
+
theme?: CowSwapTheme;
|
|
31
|
+
}
|
|
32
|
+
export interface CowSwapWidgetParams {
|
|
33
|
+
urlParams: CowSwapWidgetUrlParams;
|
|
34
|
+
width: number;
|
|
35
|
+
height: number;
|
|
36
|
+
container: HTMLElement;
|
|
37
|
+
provider?: EthereumProvider;
|
|
38
|
+
}
|
|
39
|
+
export {};
|
package/urlUtils.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { CowSwapWidgetUrlParams } from './types';
|
|
2
|
+
export declare function buildWidgetUrl(params: CowSwapWidgetUrlParams): string;
|
|
3
|
+
export declare function buildWidgetPath(params: CowSwapWidgetUrlParams): string;
|
|
4
|
+
export declare function buildTradeAmountsQuery(params: CowSwapWidgetUrlParams): URLSearchParams;
|