Skip to content

Commit

Permalink
fix: added tenderly cli new method (#67)
Browse files Browse the repository at this point in the history
* fix: added tenderly cli new method

* fix: linting

* fix: added missing type

* fix: improve types

* fix: allow customizing project

---------

Co-authored-by: sakulstra <lukasstrassel@googlemail.com>
  • Loading branch information
sendra and sakulstra authored Nov 10, 2023
1 parent f6a40b6 commit f3917e0
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ node_modules
yarn-error.log

# proposal cache
cache
cache
.idea
61 changes: 54 additions & 7 deletions src/utils/tenderlyClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,14 @@ export type TenderlySimulationResponse = {

export type TenderlyTraceResponse = TransactionInfo;

export type Fork = {
id: string;
chainId: string;
block_number: number;
forkNetworkId: string;
forkUrl: string;
};

class Tenderly {
TENDERLY_BASE: string = `https://api.tenderly.co/api/v1`;

Expand Down Expand Up @@ -299,6 +307,37 @@ class Tenderly {
return await response.json();
};

/**
* Fork api to get fork information from an existing fork id
* @param project name of the Tenderly project where the fork was created
* @param forkId id of the fork created in tenderly
* @returns fork object
*/
getForkInfo = async (forkId: string, project?: string): Promise<Fork> => {
const response: Response = await fetch(
`${this.TENDERLY_BASE}/account/${this.ACCOUNT}/project/${project || this.PROJECT}/fork/${forkId}`,
{
method: 'GET',
headers: new Headers({
'Content-Type': 'application/json',
'X-Access-Key': this.ACCESS_TOKEN,
}),
}
);
if (response.status !== 200) {
console.log(await response.text());
throw new Error(`TenderlyError: ${response.statusText}`);
}
const result = await response.json();
const fork = {
id: result.simulation_fork.id,
chainId: result.simulation_fork.network_id,
block_number: result.simulation_fork.block_number,
forkNetworkId: result.simulation_fork.chain_config.chain_id,
forkUrl: `https://rpc.tenderly.co/fork/${result.simulation_fork.id}`,
};
return fork;
};
/**
* Trace api lacks most information we need, so simulateTx uses the simulation api to replicate the trace.
* @param chainId
Expand All @@ -316,7 +355,15 @@ class Tenderly {
return this.simulate(simulationPayload);
};

fork = async ({ chainId, blockNumber, alias }: { chainId: number; blockNumber?: number; alias?: string }) => {
fork = async ({
chainId,
blockNumber,
alias,
}: {
chainId: number;
blockNumber?: number;
alias?: string;
}): Promise<Fork> => {
const forkingPoint = {
network_id: chainId,
chain_config: { chain_id: 3030 },
Expand Down Expand Up @@ -351,7 +398,7 @@ class Tenderly {
return fork;
};

deployCode = (fork: any, filePath: string, from?: Hex) => {
deployCode = (fork: Fork, filePath: string, from?: Hex) => {
const walletProvider = createWalletClient({
account: from || EOA,
chain: { id: 3030, name: 'tenderly' } as any,
Expand All @@ -367,7 +414,7 @@ class Tenderly {
} as any);
};

warpTime = async (fork: any, timestamp: bigint) => {
warpTime = async (fork: Fork, timestamp: bigint) => {
const publicProvider = createPublicClient({
chain: { id: 3030 } as any,
transport: http(fork.forkUrl),
Expand All @@ -389,7 +436,7 @@ class Tenderly {
}
};

warpBlocks = async (fork: any, blockNumber: bigint) => {
warpBlocks = async (fork: Fork, blockNumber: bigint) => {
const publicProvider = createPublicClient({
chain: { id: 3030 } as any,
transport: http(fork.forkUrl),
Expand All @@ -406,7 +453,7 @@ class Tenderly {
}
};

unwrapAndExecuteSimulationPayloadOnFork = async (fork: any, request: TenderlyRequest) => {
unwrapAndExecuteSimulationPayloadOnFork = async (fork: Fork, request: TenderlyRequest) => {
// 0. fund account
await this.fundAccount(fork, request.from);

Expand Down Expand Up @@ -464,7 +511,7 @@ class Tenderly {
}
};

fundAccount = (fork: { id: string }, address: Hex) => {
fundAccount = (fork: Fork, address: Hex) => {
logInfo('tenderly', 'fund account');
return fetch(`${this.TENDERLY_BASE}/account/${this.ACCOUNT}/project/${this.PROJECT}/fork/${fork.id}/balance`, {
method: 'POST',
Expand All @@ -476,7 +523,7 @@ class Tenderly {
});
};

replaceCode = (fork: any, address: Hex, code: Hex) => {
replaceCode = (fork: Fork, address: Hex, code: Hex) => {
const publicProvider = createPublicClient({
chain: { id: 3030 } as any,
transport: http(fork.forkUrl),
Expand Down

0 comments on commit f3917e0

Please sign in to comment.