diff --git a/packages/solidity-contracts/integration-tests/utils/fork.ts b/packages/solidity-contracts/integration-tests/utils/fork.ts new file mode 100644 index 00000000..4b43ae21 --- /dev/null +++ b/packages/solidity-contracts/integration-tests/utils/fork.ts @@ -0,0 +1,87 @@ +import axios, { AxiosResponse } from 'axios'; +import * as dotenv from 'dotenv'; +import { JsonRpcProvider, Signer } from 'ethers'; + +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>; +}; + +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 { + 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}`); + }, + }; +} \ No newline at end of file diff --git a/packages/solidity-contracts/package.json b/packages/solidity-contracts/package.json index a997adca..8ce5dd6f 100644 --- a/packages/solidity-contracts/package.json +++ b/packages/solidity-contracts/package.json @@ -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", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4c081144..7d5ed25d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -193,6 +193,9 @@ importers: '@typescript-eslint/parser': specifier: ^5.43.0 version: 5.62.0(eslint@8.57.0)(typescript@4.9.5) + axios: + specifier: ^1.7.7 + version: 1.7.7(debug@4.3.4) chai: specifier: ^4.3.7 version: 4.4.1 @@ -2652,8 +2655,8 @@ packages: resolution: {integrity: sha512-M0JtH+hlOL5pLQwHOLNYZaXuhqmvS8oExsqB1SBYgA4Dk7u/xx+YdGHXaK5pyUfed5mYXdlYiphWq3G8cRi5JQ==} engines: {node: '>=4'} - axios@1.7.5: - resolution: {integrity: sha512-fZu86yCo+svH3uqJ/yTdQ0QHpQu5oL+/QE+QPSv6BZSkDAoky9vytxp7u5qk83OJFS3kEBcesWni9WTZAv3tSw==} + axios@1.7.7: + resolution: {integrity: sha512-S4kL7XrjgBmvdGut0sN3yJxqYzrDOnivkBiN0OFs6hLiUam3UPvswUo0kqGyhqUZGEOytHyumEdXsAkgCOUf3Q==} axobject-query@3.2.1: resolution: {integrity: sha512-jsyHu61e6N4Vbz/v18DHwWYKK0bSWLqn47eeDSKPB7m8tqMHF9YJ+mhIk2lVteyZrY8tnSj/jHOv4YiTCuCJgg==} @@ -3393,11 +3396,13 @@ packages: eslint@5.16.0: resolution: {integrity: sha512-S3Rz11i7c8AA5JPv7xAH+dOyq/Cu/VXHiHXBPOU1k/JAM5dXqQPt3qcrhpHSorXmrpu2g0gkIBVXAqCpzfoZIg==} engines: {node: ^6.14.0 || ^8.10.0 || >=9.10.0} + deprecated: This version is no longer supported. Please see https://eslint.org/version-support for other options. hasBin: true eslint@8.57.0: resolution: {integrity: sha512-dZ6+mexnaTIbSBZWgou51U6OmzIhYM2VcNdtiTtI7qPNZm35Akpr0f6vtw3w1Kmn5PYo+tZVfh13WrhpS6oLqQ==} engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} + deprecated: This version is no longer supported. Please see https://eslint.org/version-support for other options. hasBin: true espree@5.0.1: @@ -7915,7 +7920,7 @@ snapshots: '@openzeppelin/defender-admin-client@1.54.1(bufferutil@4.0.5)(debug@4.3.4)(utf-8-validate@5.0.7)': dependencies: '@openzeppelin/defender-base-client': 1.54.1(debug@4.3.4) - axios: 1.7.5(debug@4.3.4) + axios: 1.7.7(debug@4.3.4) ethers: 5.7.2(bufferutil@4.0.5)(utf-8-validate@5.0.7) lodash: 4.17.21 node-fetch: 2.7.0 @@ -7929,7 +7934,7 @@ snapshots: dependencies: amazon-cognito-identity-js: 6.3.12 async-retry: 1.3.3 - axios: 1.7.5(debug@4.3.4) + axios: 1.7.7(debug@4.3.4) lodash: 4.17.21 node-fetch: 2.7.0 transitivePeerDependencies: @@ -7946,7 +7951,7 @@ snapshots: '@openzeppelin/defender-sdk-deploy-client@1.12.0(debug@4.3.4)': dependencies: '@openzeppelin/defender-sdk-base-client': 1.12.0 - axios: 1.7.5(debug@4.3.4) + axios: 1.7.7(debug@4.3.4) lodash: 4.17.21 transitivePeerDependencies: - debug @@ -7955,7 +7960,7 @@ snapshots: '@openzeppelin/defender-sdk-network-client@1.12.0(debug@4.3.4)': dependencies: '@openzeppelin/defender-sdk-base-client': 1.12.0 - axios: 1.7.5(debug@4.3.4) + axios: 1.7.7(debug@4.3.4) lodash: 4.17.21 transitivePeerDependencies: - debug @@ -9083,7 +9088,7 @@ snapshots: axe-core@4.7.0: {} - axios@1.7.5(debug@4.3.4): + axios@1.7.7(debug@4.3.4): dependencies: follow-redirects: 1.15.6(debug@4.3.4) form-data: 4.0.0 @@ -10776,7 +10781,7 @@ snapshots: '@ethersproject/transactions': 5.7.0 '@ethersproject/wallet': 5.7.0 '@types/qs': 6.9.15 - axios: 1.7.5(debug@4.3.4) + axios: 1.7.7(debug@4.3.4) chalk: 4.1.2 chokidar: 3.6.0 debug: 4.3.4(supports-color@8.1.1) @@ -13108,4 +13113,4 @@ snapshots: zksync-ethers@5.9.2(ethers@5.7.2(bufferutil@4.0.5)(utf-8-validate@5.0.7)): dependencies: - ethers: 5.7.2(bufferutil@4.0.5)(utf-8-validate@5.0.7) \ No newline at end of file + ethers: 5.7.2(bufferutil@4.0.5)(utf-8-validate@5.0.7)