-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.ts
More file actions
159 lines (145 loc) · 4.93 KB
/
client.ts
File metadata and controls
159 lines (145 loc) · 4.93 KB
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
import axios from 'axios';
import { ZeroAddress } from 'ethers';
import { Inject, Service } from 'typedi';
import { diConstants } from '@bonadocs/di';
import { BonadocsLogger } from '@bonadocs/logger';
import { ConfigService } from '../../configuration';
import { Tenderly } from '../../configuration/config.interface';
import { BaseHttpClient } from '../base';
import { BundleSimulationResult, EVMCall, SimulationResponseData, SimulationResult } from './types';
@Service()
export class TenderlyApiClient extends BaseHttpClient {
constructor(
@Inject() configService: ConfigService,
@Inject(diConstants.logger) logger: BonadocsLogger,
) {
const tenderlyConfigs = configService.getTransformed<Tenderly>('tenderly');
super(
configService,
logger,
axios.create({
baseURL: `https://api.tenderly.co/api/v1/account/${tenderlyConfigs.username}/project/${tenderlyConfigs.project}`,
validateStatus: () => true,
timeout: 60000,
headers: {
'X-Access-Key': tenderlyConfigs.accessKey,
},
}),
);
}
async simulateSingle(
chainId: number,
call: EVMCall,
): Promise<SimulationResponseData | undefined> {
try {
const response = await this.request<SimulationResult>({
url: '/simulate',
method: 'post',
data: this.callToSimulationData(chainId, call),
});
if (response.status !== 200) {
this.logger.error(`Unexpected response status: ${response.status}`);
return undefined;
}
return this.normalizeSimulationResult(response.data);
} catch (error) {
if (axios.isAxiosError(error)) {
this.logger.error('Axios error during simulation request', error.message);
} else {
this.logger.error('Unexpected error during simulation request', error);
}
return undefined;
}
}
async simulateBundle(
chainId: number,
calls: EVMCall[],
): Promise<SimulationResponseData[] | undefined> {
try {
const response = await this.request<BundleSimulationResult>({
url: '/simulate-bundle',
method: 'post',
data: { simulations: calls.map((call) => this.callToSimulationData(chainId, call)) },
});
if (response.status !== 200) {
this.logger.error(`Unexpected response status: ${response.status}`);
return undefined;
}
return response.data.simulation_results.map((result) =>
this.normalizeSimulationResult(result),
);
} catch (error) {
if (axios.isAxiosError(error)) {
this.logger.error('Axios error during bundle simulation request', error.message);
} else {
this.logger.error('Unexpected error during bundle simulation request', error);
}
return undefined;
}
}
private callToSimulationData(chainId: number, call: EVMCall) {
const stateObjects = Object.fromEntries(
call.simulationOverrides.accounts.map((acct) => [
acct.address,
{
storage: acct.storage,
},
]),
);
return {
/* Simulation Configuration */
save: false,
save_if_fails: false,
simulation_type: 'quick',
network_id: chainId.toString(10),
state_objects: stateObjects,
/* Standard EVM Transaction object */
from: call.overrides.from || ZeroAddress,
to: call.to,
input: call.data,
gas: call.overrides.gasLimit,
gas_price: call.overrides.gasPrice || call.overrides.maxFeePerGas,
value: call.overrides.value,
};
}
private normalizeSimulationResult(data: SimulationResult): SimulationResponseData {
const resultData = data.transaction;
return {
error: !resultData.error_info
? undefined
: {
address: resultData.error_info.address,
message: resultData.error_info.error_message || 'execution reverted',
},
receipt: {
from: resultData.from!,
to: resultData.to || null,
status: resultData.status ? 1 : 0,
blockHash: resultData.block_hash!,
type: 2,
blockNumber: resultData.block_number!,
hash: resultData.hash!,
index: resultData.index!,
root: null,
gasUsed: BigInt(resultData.gas_used!),
contractAddress: resultData.transaction_info!.contract_address || null,
cumulativeGasUsed: BigInt(resultData.cumulative_gas_used!),
gasPrice: BigInt(resultData.gas_price!),
effectiveGasPrice: BigInt(resultData.effective_gas_price!),
logsBloom: '0x',
logs:
resultData.transaction_info!.logs?.map((l, i) => ({
data: l.raw!.data!,
address: l.raw!.address!,
topics: l.raw!.topics!,
transactionIndex: resultData.index!,
removed: false,
index: i,
blockHash: resultData.block_hash!,
blockNumber: resultData.block_number!,
transactionHash: resultData.hash!,
})) || [],
},
};
}
}