forked from zama-ai/fhevm-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomProvider.ts
62 lines (56 loc) · 2.37 KB
/
CustomProvider.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
import { ethers } from "ethers";
import { ProviderWrapper } from "hardhat/plugins";
import type { EIP1193Provider, RequestArguments } from "hardhat/types";
interface Test {
request: EIP1193Provider["request"];
}
class CustomProvider extends ProviderWrapper implements Test {
public lastBlockSnapshot: number;
public lastCounterRand: number;
public lastBlockSnapshotForDecrypt: number;
constructor(protected readonly _wrappedProvider: EIP1193Provider) {
super(_wrappedProvider);
this.lastBlockSnapshot = 0; // Initialize the variable
this.lastCounterRand = 0;
this.lastBlockSnapshotForDecrypt = 0;
}
async request(args: RequestArguments): ReturnType<EIP1193Provider["request"]> {
if (args.method === "eth_estimateGas") {
const estimatedGasLimit = BigInt((await this._wrappedProvider.request(args)) as bigint);
const increasedGasLimit = ethers.toBeHex((estimatedGasLimit * 120n) / 100n); // override estimated gasLimit by 120%, to avoid some edge case with ethermint gas estimation
return increasedGasLimit;
}
if (args.method === "evm_revert") {
const result = await this._wrappedProvider.request(args);
const blockNumberHex = (await this._wrappedProvider.request({ method: "eth_blockNumber" })) as string;
this.lastBlockSnapshot = parseInt(blockNumberHex);
this.lastBlockSnapshotForDecrypt = parseInt(blockNumberHex);
const callData = {
to: "0x000000000000000000000000000000000000005d",
data: "0x1f20d85c",
};
this.lastCounterRand = (await this._wrappedProvider.request({
method: "eth_call",
params: [callData, "latest"],
})) as number;
return result;
}
if (args.method === "get_lastBlockSnapshot") {
return [this.lastBlockSnapshot, this.lastCounterRand];
}
if (args.method === "get_lastBlockSnapshotForDecrypt") {
return this.lastBlockSnapshotForDecrypt;
}
if (args.method === "set_lastBlockSnapshot") {
this.lastBlockSnapshot = Array.isArray(args.params!) && args.params[0];
return this.lastBlockSnapshot;
}
if (args.method === "set_lastBlockSnapshotForDecrypt") {
this.lastBlockSnapshotForDecrypt = Array.isArray(args.params!) && args.params[0];
return this.lastBlockSnapshotForDecrypt;
}
const result = this._wrappedProvider.request(args);
return result;
}
}
export default CustomProvider;