-
Notifications
You must be signed in to change notification settings - Fork 13
/
tenderly.js
55 lines (47 loc) · 1.6 KB
/
tenderly.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
require("dotenv").config();
const axios = require("axios");
const TENDERLY_KEY = process.env.TENDERLY_KEY;
const TENDERLY_ACCOUNT = process.env.TENDERLY_ACCOUNT;
const TENDERLY_PROJECT = process.env.TENDERLY_PROJECT;
if (!TENDERLY_KEY) throw new Error("Tenderly key not set!");
if (!TENDERLY_ACCOUNT) throw new Error("Tenderly account not set!");
if (!TENDERLY_PROJECT) throw new Error("Tenderly project not set!");
const tenderly = axios.create({
baseURL: "https://api.tenderly.co/api/v1/",
headers: {
"X-Access-Key": TENDERLY_KEY,
},
});
class TenderlyFork {
constructor(fork_id) {
if (fork_id) this.fork_id = fork_id;
}
async init(forkNetworkId, chainId) {
console.log(`Creating fork for ${forkNetworkId} on ${chainId}`);
const response = await tenderly.post(
`account/${TENDERLY_ACCOUNT}/project/${TENDERLY_PROJECT}/fork`,
{
network_id: forkNetworkId,
chain_config: { chain_id: Number(chainId) },
}
);
this.fork_id = response.data.simulation_fork.id;
}
async fund_account(address, amount) {
if (!this.fork_id) throw new Error("Fork not initialized!");
await tenderly.post(
`account/${TENDERLY_ACCOUNT}/project/${TENDERLY_PROJECT}/fork/${this.fork_id}/balance`,
{ accounts: [address], amount }
);
}
get_rpc_url() {
if (!this.fork_id) throw new Error("Fork not initialized!");
return `https://rpc.tenderly.co/fork/${this.fork_id}`;
}
async deleteFork() {
await tenderly.delete(
`account/${TENDERLY_ACCOUNT}/project/${TENDERLY_PROJECT}/fork/${this.fork_id}`
);
}
}
module.exports = { TenderlyFork };