-
Notifications
You must be signed in to change notification settings - Fork 0
/
Context.ts
149 lines (138 loc) · 3.96 KB
/
Context.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { JsonRpcProvider, Networkish } from "@ethersproject/providers";
import { CChainId } from "./internal/constants";
import { Web3ContextParams } from "./internal/interfaces/context";
import { isAddress } from "./internal/utils";
import { UnsupportedProtocolError } from "./internal/utils/error";
const supportedProtocols = ["https:"];
if (typeof process !== "undefined" && process.env?.TESTING) {
supportedProtocols.push("http:");
}
/**
* The Context class is an utility component that holds the configuration passed to Clique instance.
*/
export class Context {
protected state: Web3ContextParams;
// INTERNAL CONTEXT STATE
/**
* @param {Object} params
*
* @constructor
*/
constructor(contextParams: Web3ContextParams) {
if (!contextParams.daoAddress || !isAddress(contextParams.daoAddress)) {
throw new Error("Missing DAO address");
} else if (!contextParams.daoChainId) {
throw new Error("Missing DAO chainId");
} else if (!Object.keys(contextParams.web3Providers).length) {
throw new Error("No web3 endpoints defined");
}
const _web3Providers: any = {};
for (const key in contextParams.web3Providers) {
if (
Object.prototype.hasOwnProperty.call(contextParams.web3Providers, key)
) {
const _web3ProviderItem =
contextParams.web3Providers[Number(key) as CChainId];
_web3Providers[Number(key)] = Context.resolveWeb3Providers(
_web3ProviderItem
);
}
}
this.state = {
signer: contextParams.signer,
daoAddress: contextParams.daoAddress,
daoChainId: contextParams.daoChainId,
web3Providers: _web3Providers
};
}
/**
* set web3Providers and signer
* @param contextParams
*/
set(contextParams: Partial<Web3ContextParams>) {
const _state = Object.assign({}, this.state || {}) as Web3ContextParams;
if (contextParams.signer) {
_state.signer = contextParams.signer;
}
if (contextParams.web3Providers) {
const _web3Providers: any = {};
for (const key in contextParams.web3Providers) {
if (
Object.prototype.hasOwnProperty.call(contextParams.web3Providers, key)
) {
const _web3ProviderItem =
contextParams.web3Providers[Number(key) as CChainId];
_web3Providers[Number(key)] = Context.resolveWeb3Providers(
_web3ProviderItem
);
}
}
_state.web3Providers = _web3Providers;
}
this.state = _state;
}
/**
* Getter for the Signer
*
* @var signer
*
* @returns {Signer}
*
* @public
*/
get signer() {
return this.state.signer;
}
/**
* Getter for the web3 providers
*
* @var web3Providers
*
* @returns {JsonRpcProvider[]}
*
* @public
*/
get web3Providers(): {
[key in CChainId]: JsonRpcProvider[];
} {
return this.state.web3Providers as { [key in CChainId]: JsonRpcProvider[] };
}
/**
* dao address
*/
get daoDaoAddress(): string {
return this.state.daoAddress;
}
/**
* dao chainId
*/
get daoDaoChainId(): CChainId {
return this.state.daoChainId;
}
// INTERNAL HELPERS
private static resolveWeb3Providers(
endpoints: string | JsonRpcProvider | (string | JsonRpcProvider)[],
network?: Networkish
): JsonRpcProvider[] {
if (Array.isArray(endpoints)) {
return endpoints.map(item => {
if (typeof item === "string") {
const url = new URL(item);
if (!supportedProtocols.includes(url.protocol)) {
throw new UnsupportedProtocolError(url.protocol);
}
return new JsonRpcProvider(url.href, network);
}
return item;
});
} else if (typeof endpoints === "string") {
const url = new URL(endpoints);
if (!supportedProtocols.includes(url.protocol)) {
throw new UnsupportedProtocolError(url.protocol);
}
return [new JsonRpcProvider(url.href, network)];
} else {
return [endpoints];
}
}
}