-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
web3.ts
180 lines (158 loc) · 5.34 KB
/
web3.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
This file is part of web3.js.
web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/
// eslint-disable-next-line max-classes-per-file
import {
Web3Context,
Web3ContextInitOptions,
Web3ContextObject,
Web3SubscriptionConstructor,
isSupportedProvider,
} from 'web3-core';
import { Web3Eth, RegisteredSubscription, registeredSubscriptions } from 'web3-eth';
import Contract from 'web3-eth-contract';
import { ENS, registryAddresses } from 'web3-eth-ens';
import { Iban } from 'web3-eth-iban';
import { Personal } from 'web3-eth-personal';
import { Net } from 'web3-net';
import * as utils from 'web3-utils';
import { isNullish } from 'web3-utils';
import {
Address,
ContractAbi,
ContractInitOptions,
EthExecutionAPI,
SupportedProviders,
} from 'web3-types';
import { InvalidMethodParamsError } from 'web3-errors';
import abi from './abi.js';
import { initAccountsForContext } from './accounts.js';
import { Web3EthInterface } from './types.js';
import { Web3PkgInfo } from './version.js';
export class Web3<
CustomRegisteredSubscription extends {
[key: string]: Web3SubscriptionConstructor<EthExecutionAPI>;
} = RegisteredSubscription,
> extends Web3Context<EthExecutionAPI, CustomRegisteredSubscription & RegisteredSubscription> {
public static version = Web3PkgInfo.version;
public static utils = utils;
public static modules = {
Web3Eth,
Iban,
Net,
ENS,
Personal,
};
public utils: typeof utils;
public eth: Web3EthInterface;
public constructor(
providerOrContext?:
| string
| SupportedProviders<EthExecutionAPI>
| Web3ContextInitOptions<EthExecutionAPI, CustomRegisteredSubscription>,
) {
if (
isNullish(providerOrContext) ||
(typeof providerOrContext === 'string' && providerOrContext.trim() === '') ||
(typeof providerOrContext !== 'string' &&
!isSupportedProvider(providerOrContext as SupportedProviders<EthExecutionAPI>) &&
!(providerOrContext as Web3ContextInitOptions).provider)
) {
console.warn(
'NOTE: web3.js is running without provider. You need to pass a provider in order to interact with the network!',
);
}
let contextInitOptions: Web3ContextInitOptions<EthExecutionAPI> = {};
if (
typeof providerOrContext === 'string' ||
isSupportedProvider(providerOrContext as SupportedProviders)
) {
contextInitOptions.provider = providerOrContext as
| undefined
| string
| SupportedProviders;
} else if (providerOrContext) {
contextInitOptions = providerOrContext as Web3ContextInitOptions;
} else {
contextInitOptions = {};
}
contextInitOptions.registeredSubscriptions = {
// all the Eth standard subscriptions
...registeredSubscriptions,
// overridden and combined with any custom subscriptions
...(contextInitOptions.registeredSubscriptions ?? {}),
} as CustomRegisteredSubscription;
super(contextInitOptions);
const accounts = initAccountsForContext(this);
// Init protected properties
this._wallet = accounts.wallet;
this._accountProvider = accounts;
this.utils = utils;
// Have to use local alias to initiate contract context
// eslint-disable-next-line @typescript-eslint/no-this-alias
const self = this;
class ContractBuilder<Abi extends ContractAbi> extends Contract<Abi> {
public constructor(jsonInterface: Abi);
public constructor(jsonInterface: Abi, address: Address);
public constructor(jsonInterface: Abi, options: ContractInitOptions);
public constructor(jsonInterface: Abi, address: Address, options: ContractInitOptions);
public constructor(
jsonInterface: Abi,
addressOrOptions?: Address | ContractInitOptions,
options?: ContractInitOptions,
) {
if (typeof addressOrOptions === 'object' && typeof options === 'object') {
throw new InvalidMethodParamsError(
'Should not provide options at both 2nd and 3rd parameters',
);
}
if (isNullish(addressOrOptions)) {
super(jsonInterface, options, self.getContextObject() as Web3ContextObject);
} else if (typeof addressOrOptions === 'object') {
super(
jsonInterface,
addressOrOptions,
self.getContextObject() as Web3ContextObject,
);
} else if (typeof addressOrOptions === 'string') {
super(
jsonInterface,
addressOrOptions,
options ?? {},
self.getContextObject() as Web3ContextObject,
);
} else {
throw new InvalidMethodParamsError();
}
super.subscribeToContextEvents(self);
}
}
const eth = self.use(Web3Eth);
// Eth Module
this.eth = Object.assign(eth, {
// ENS module
ens: self.use(ENS, registryAddresses.main), // registry address defaults to main network
// Iban helpers
Iban,
net: self.use(Net),
personal: self.use(Personal),
// Contract helper and module
Contract: ContractBuilder,
// ABI Helpers
abi,
// Accounts helper
accounts,
});
}
}
export default Web3;