-
Notifications
You must be signed in to change notification settings - Fork 80
/
CustomProvider.ts
58 lines (54 loc) · 2.32 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
import dotenv from 'dotenv';
import { ethers } from 'ethers';
import * as fs from 'fs';
import { ProviderWrapper } from 'hardhat/plugins';
class CustomProvider extends ProviderWrapper {
public lastBlockSnapshot: number;
public lastCounterRand: number;
public lastBlockSnapshotForDecrypt: number;
constructor(protected readonly _wrappedProvider: any) {
super(_wrappedProvider);
this.lastBlockSnapshot = 0; // Initialize the variable
this.lastCounterRand = 0;
this.lastBlockSnapshotForDecrypt = 0;
}
async request(args: { method: string; params?: any[] }) {
if (args.method === 'eth_estimateGas') {
const estimatedGasLimit = BigInt(await this._wrappedProvider.request(args));
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' });
this.lastBlockSnapshot = parseInt(blockNumberHex);
this.lastBlockSnapshotForDecrypt = parseInt(blockNumberHex);
const parsedEnvCoprocessor = dotenv.parse(
fs.readFileSync('node_modules/fhevm-core-contracts/addresses/.env.exec'),
);
const coprocAdd = parsedEnvCoprocessor.TFHE_EXECUTOR_CONTRACT_ADDRESS;
this.lastCounterRand = await this._wrappedProvider.request({
method: 'eth_getStorageAt',
params: [coprocAdd, '0xa436a06f0efce5ea38c956a21e24202a59b3b746d48a23fb52b4a5bc33fe3e00', 'latest'],
});
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 = args.params![0];
return this.lastBlockSnapshot;
}
if (args.method === 'set_lastBlockSnapshotForDecrypt') {
this.lastBlockSnapshotForDecrypt = args.params![0];
return this.lastBlockSnapshotForDecrypt;
}
const result = this._wrappedProvider.request(args);
return result;
}
}
export default CustomProvider;