-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathutils.ts
42 lines (36 loc) · 1.26 KB
/
utils.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
import { Contract } from '@ethersproject/contracts';
import { getAddress } from '@ethersproject/address';
import { AddressZero } from '@ethersproject/constants';
import { defaultAbiCoder } from '@ethersproject/abi';
import { JsonRpcSigner, Web3Provider } from '@ethersproject/providers';
// returns the checksummed address if the address is valid, otherwise returns false
export function isAddress(value: any): string | false {
try {
return getAddress(value);
} catch {
return false;
}
}
// account is not optional
export function getSigner(library: Web3Provider, account: string): JsonRpcSigner {
return library.getSigner(account).connectUnchecked();
}
// account is optional
export function getProviderOrSigner(
library: Web3Provider,
account?: string
): Web3Provider | JsonRpcSigner {
return account ? getSigner(library, account) : library;
}
export function getContract(
address: string,
ABI: any,
library: Web3Provider,
account?: string
): Contract {
if (!isAddress(address) || address === AddressZero) {
throw Error(`Invalid 'address' parameter '${address}'.`);
}
return new Contract(address, ABI, getProviderOrSigner(library, account) as any);
}
export const bytesAreSafe = (address) => !address.match(/.{2}/g).find((byte) => byte === '5b');