-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
102 additions
and
9 deletions.
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
packages/solidity-contracts/integration-tests/utils/fork.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<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}`); | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.