forked from sakulstra/tenderly-fork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tenderly.js
58 lines (49 loc) · 1.69 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
56
57
58
require("dotenv").config();
const axios = require("axios");
const FORK_NETWORK_ID = process.env.FORK_NETWORK_ID || "1";
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,
},
});
const CHAIN_ID = process.env.CHAIN_ID || 3030;
class TenderlyFork {
constructor(fork_id) {
if (fork_id) this.fork_id = fork_id;
}
async init() {
console.log(`Creating fork for ${FORK_NETWORK_ID} on ${CHAIN_ID}`);
const response = await tenderly.post(
`account/${TENDERLY_ACCOUNT}/project/${TENDERLY_PROJECT}/fork`,
{
network_id: FORK_NETWORK_ID,
chain_config: { chain_id: Number(CHAIN_ID) },
}
);
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 };