Skip to content

Commit

Permalink
chore: setup tenderly fork config
Browse files Browse the repository at this point in the history
  • Loading branch information
viraj124 committed Oct 29, 2024
1 parent 0bb1072 commit 48d8d00
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 9 deletions.
87 changes: 87 additions & 0 deletions packages/solidity-contracts/integration-tests/utils/fork.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import axios, { AxiosResponse } from 'axios';

Check failure on line 1 in packages/solidity-contracts/integration-tests/utils/fork.ts

View workflow job for this annotation

GitHub Actions / validate

Import "AxiosResponse" is only used as types
import * as dotenv from 'dotenv';
import { JsonRpcProvider, Signer } from 'ethers';

Check failure on line 3 in packages/solidity-contracts/integration-tests/utils/fork.ts

View workflow job for this annotation

GitHub Actions / validate

Import "Signer" is only used as types

dotenv.config();

type TenderlyFork = {
block_number?: number;
network_id: string;
transaction_index?: number;
initial_balance?: number;
chain_config?: {
chain_id: number;
homestead_block: number;
dao_fork_support: boolean;
eip_150_block: number;
eip_150_hash: string;
eip_155_block: number;
eip_158_block: number;
byzantium_block: number;
constantinople_block: number;
petersburg_block: number;
istanbul_block: number;
berlin_block: number;
};
};

export type EthersOnTenderlyFork = {
id: number;
provider: JsonRpcProvider;
blockNumber: number;
/**
* map from address to given address' balance
*/
accounts: { [key: string]: string };
signers: Signer[];
removeFork: () => Promise<AxiosResponse<any, any>>;
};

export const anAxiosOnTenderly = () =>
axios.create({
baseURL: 'https://api.tenderly.co/api/v1',
headers: {
'X-Access-Key': process.env.TENDERLY_ACCESS_KEY,
'Content-Type': 'application/json',
},
});

export async function forkForTest(
fork: TenderlyFork
): Promise<EthersOnTenderlyFork> {
const projectUrl = `account/${process.env.TENDERLY_USER}/project/${process.env.TENDERLY_PROJECT}`;
const axiosOnTenderly = anAxiosOnTenderly();

const forkResponse = await axiosOnTenderly.post(`${projectUrl}/fork`, fork);
const forkId = forkResponse.data.root_transaction.fork_id;

const provider = new JsonRpcProvider(
`https://rpc.tenderly.co/fork/${forkId}`
);

const bn = (
forkResponse.data.root_transaction.receipt.blockNumber as string
).replace('0x', '');
const blockNumber: number = Number.parseInt(bn, 16);

console.info(
`\nForked with fork id ${forkId} at block number ${blockNumber}\nhttps://dashboard.tenderly.co/${process.env.TENDERLY_USER}/${process.env.TENDERLY_PROJECT}/fork/${forkId}\n`
);

const accounts = forkResponse.data.simulation_fork.accounts;
const signers = await Promise.all(
Object.keys(accounts).map(async (address) => provider.getSigner(address))
);

return {
provider,
accounts,
signers,
blockNumber,
id: forkId,
removeFork: async () => {
console.log('Removing test fork', forkId);
return await axiosOnTenderly.delete(`${projectUrl}/fork/${forkId}`);
},
};
}
1 change: 1 addition & 0 deletions packages/solidity-contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"@types/node": "^18.11.9",
"@typescript-eslint/eslint-plugin": "^5.43.0",
"@typescript-eslint/parser": "^5.43.0",
"axios": "^1.7.7",
"chai": "^4.3.7",
"cors": "2.8.5",
"dotenv": "^16.0.3",
Expand Down
23 changes: 14 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 48d8d00

Please sign in to comment.