-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
107 lines (79 loc) · 3.02 KB
/
utils.js
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
import { Foundry } from '@adraffy/blocksmith';
import axios from 'axios';
import { SENDER_ADDR } from './addresses';
let foundry = null;
let impersonatedSigner = null;
export const init = async () => {
if (!foundry || !impersonatedSigner) {
const PROVIDER_URL = `https://mainnet.infura.io/v3/${process.env.INFURA_API_KEY}`;
foundry = await Foundry.launch({
infoLog: false,
fork: PROVIDER_URL,
});
await foundry.provider.send('anvil_impersonateAccount', [
SENDER_ADDR
]);
// Get a signer for the impersonated address
impersonatedSigner = await foundry.provider.getSigner(SENDER_ADDR);
}
return { foundry, impersonatedSigner };
}
export const simulateTransactionBundle = async (transactions) => {
console.log("Simulating transactions with Tenderly...");
const API_KEY = process.env.TENDERLY_API_KEY;
const USERNAME = process.env.TENDERLY_USERNAME;
const PROJECT = process.env.TENDERLY_PROJECT;
const url = `https://api.tenderly.co/api/v1/account/${USERNAME}/project/${PROJECT}/simulate-bundle`;
const payload = {
network_id: "1",
block_number: "latest",
simulations: transactions,
};
try {
const response = await axios.post(url, payload, {
headers: {
"X-Access-Key": API_KEY,
"Content-Type": "application/json"
}
});
//console.log("Simulation Results:", response.data);
console.log("Simulation successful");
for (let sim of response.data["simulation_results"]) {
const simulationId = sim["simulation"]["id"];
const shareUrl = `https://api.tenderly.co/api/v1/account/${USERNAME}/project/${PROJECT}/simulations/${simulationId}/share`;
const options = {
method: 'POST',
url: shareUrl,
headers: {
"X-Access-Key": API_KEY,
"Content-Type": "application/json"
}
};
try {
const { status, data } = await axios.request(options);
if (status == 204) {
console.log(`https://www.tdly.co/shared/simulation/${simulationId}`);
} else {
console.error("Failed to share simulation", simulationId);
}
} catch (error) {
console.error(error);
}
}
/*
// response.data["simulation_results"]["0"]["simulation"]
[
"id", "project_id", "owner_id", "network_id", "block_number", "transaction_index", "from", "to",
"input", "gas", "gas_price", "gas_used", "value", "method", "status", "access_list", "queue_origin",
"block_header", "deposit_tx", "system_tx", "nonce", "addresses", "contract_ids", "shared", "created_at"
]
*/
} catch (error) {
console.error("Error during simulation:", error.response?.data || error.message);
}
};
export const assert = (condition, message) => {
if (!condition) {
throw new Error(message || "Assertion failed");
}
}