-
Notifications
You must be signed in to change notification settings - Fork 1
/
ethers-adapter.ts
102 lines (90 loc) · 2.9 KB
/
ethers-adapter.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
import { BigNumberish, providers, Signer } from "ethers";
import { TypedDataSigner } from "@ethersproject/abstract-signer";
import {
TransactionResponse,
Web3LibAdapter,
TransactionRequest,
TransactionReceipt
} from "@bosonprotocol/common";
export type Provider =
| providers.UrlJsonRpcProvider
| providers.AlchemyProvider
| providers.AlchemyWebSocketProvider
| providers.AnkrProvider
| providers.CloudflareProvider
| providers.InfuraProvider
| providers.InfuraWebSocketProvider
| providers.JsonRpcProvider
| providers.JsonRpcBatchProvider
| providers.NodesmithProvider
| providers.PocketProvider
| providers.StaticJsonRpcProvider
| providers.Web3Provider
| providers.WebSocketProvider
| providers.IpcProvider;
/**
* `Web3LibAdapter` implementation targeting `ethers`.
*/
export class EthersAdapter implements Web3LibAdapter {
private _signer: Signer;
private _provider: Provider;
uuid: string;
constructor(provider: Provider, signer?: Signer) {
this._provider = provider;
this.uuid = crypto.randomUUID();
this._signer = signer
? signer.connect(this._provider)
: this._provider.getSigner();
}
public async getSignerAddress() {
return this._signer.getAddress();
}
public async isSignerContract(): Promise<boolean> {
const address = await this.getSignerAddress();
const code = await this._provider.getCode(address);
return code != "0x";
}
public async getChainId(): Promise<number> {
return this._signer.getChainId();
}
public async getBalance(
addressOrName: string,
blockNumber?: string | number
): Promise<BigNumberish> {
return this._provider.getBalance(addressOrName, blockNumber);
}
public async estimateGas(
transactionRequest: TransactionRequest
): Promise<BigNumberish> {
return this._signer.estimateGas(transactionRequest);
}
public async sendTransaction(
transactionRequest: TransactionRequest
): Promise<TransactionResponse> {
return this._signer.sendTransaction(transactionRequest);
}
public async call(transactionRequest: TransactionRequest): Promise<string> {
return this._provider.call(transactionRequest);
}
public async send(rpcMethod: string, payload: unknown[]): Promise<string> {
if (
this._signer &&
"_signTypedData" in this._signer &&
rpcMethod === "eth_signTypedData_v4"
) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [signerAddress, dataToSign] = payload as [string, string];
const { types, domain, message: value } = JSON.parse(dataToSign);
delete types["EIP712Domain"];
return (this._signer as TypedDataSigner)["_signTypedData"](
domain,
types,
value
);
}
return this._provider.send(rpcMethod, payload);
}
public async getTransactionReceipt(txHash): Promise<TransactionReceipt> {
return this._provider.getTransactionReceipt(txHash);
}
}