Skip to content

Commit

Permalink
add fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
aburkut committed Jan 17, 2025
1 parent f4fa734 commit 63bb318
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 39 deletions.
4 changes: 1 addition & 3 deletions tests/smart-tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ export type StateOverride = {
};

export type StateSimulateApiOverride = {
storage: {
value: Record<string, string>;
};
storage: Record<string, string>;
};

export type StateOverrides = {
Expand Down
71 changes: 35 additions & 36 deletions tests/tenderly-simulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import axios from 'axios';
import { TxObject } from '../src/types';
import { StateOverrides, StateSimulateApiOverride } from './smart-tokens';
import { Provider } from '@ethersproject/providers';
import { Provider, StaticJsonRpcProvider } from '@ethersproject/providers';
import { ethers } from 'ethers';
import { Network } from '../build/constants';
import { Address } from '@paraswap/core';
Expand Down Expand Up @@ -63,6 +63,7 @@ export class EstimateGasSimulation implements TransactionSimulator {

export class TenderlySimulation implements TransactionSimulator {
vnetId: string = '';
rpcURL: string = '';
maxGasLimit = 80000000;

private readonly chainIdToChainNameMap: { [key: number]: string } = {
Expand Down Expand Up @@ -129,7 +130,14 @@ export class TenderlySimulation implements TransactionSimulator {
},
},
);

const rpc: { name: string; url: string } = res.data.rpcs.find(
(rpc: { name: string; url: string }) =>
rpc.name.toLowerCase() === 'Admin RPC'.toLowerCase(),
);

this.vnetId = res.data.id;
this.rpcURL = rpc.url;
} catch (e) {
console.error(`TenderlySimulation_setup:`, e);
throw e;
Expand All @@ -140,8 +148,6 @@ export class TenderlySimulation implements TransactionSimulator {
try {
let stateOverridesParams = {};

console.log('stateOverrides: ', stateOverrides);

if (stateOverrides) {
await process.nextTick(() => {}); // https://stackoverflow.com/questions/69169492/async-external-function-leaves-open-handles-jest-supertest-express
const result = await axios.post(
Expand All @@ -155,8 +161,6 @@ export class TenderlySimulation implements TransactionSimulator {
},
);

console.log('result.data.stateOverrides: ', result.data.stateOverrides);

stateOverridesParams = Object.keys(result.data.stateOverrides).reduce(
(acc, contract) => {
const _storage = result.data.stateOverrides[contract].value;
Expand All @@ -168,9 +172,9 @@ export class TenderlySimulation implements TransactionSimulator {
},
{} as Record<Address, StateSimulateApiOverride>,
);
}

console.log('stateOverridesParams: ', stateOverridesParams);
await this.executeStateOverrides(stateOverridesParams);
}

await process.nextTick(() => {}); // https://stackoverflow.com/questions/69169492/async-external-function-leaves-open-handles-jest-supertest-express
const { data } = await axios.post(
Expand All @@ -191,7 +195,6 @@ export class TenderlySimulation implements TransactionSimulator {
data: params.data,
},
blockNumber: 'pending',
stateOverrides: stateOverridesParams,
},
{
timeout: 30 * 1000,
Expand All @@ -201,34 +204,6 @@ export class TenderlySimulation implements TransactionSimulator {
},
);

console.log(
'URL: ',
`https://api.tenderly.co/api/v1/account/${TENDERLY_ACCOUNT_ID}/project/${TENDERLY_PROJECT}/vnets/${this.vnetId}/transactions`,
);
console.log(
'PARAMS: ',
JSON.stringify({
callArgs: {
from: params.from,
to: params.to,
value:
params.value === '0'
? '0x0'
: ethers.utils.hexStripZeros(
ethers.utils.hexlify(BigInt(params.value)),
),
gas: ethers.utils.hexStripZeros(
ethers.utils.hexlify(BigInt(this.maxGasLimit)),
),
data: params.data,
},
blockNumber: 'pending',
stateOverrides: stateOverridesParams,
}),
);

console.log('TX DATA: ', data);

if (data.status === 'success') {
return {
success: true,
Expand All @@ -253,4 +228,28 @@ export class TenderlySimulation implements TransactionSimulator {
};
}
}

async executeStateOverrides(
stateOverridesParams: Record<string, StateSimulateApiOverride>,
) {
const testNetRPC = new StaticJsonRpcProvider(this.rpcURL);

await Promise.all(
Object.keys(stateOverridesParams).map(address => {
const storage = stateOverridesParams[address].storage;
Object.keys(storage).map(async slot => {
const txHash = await testNetRPC!.send('tenderly_setStorageAt', [
address,
slot,
storage[slot],
]);

const transaction = await testNetRPC!.waitForTransaction(txHash);
if (!transaction.status) {
console.log(`Transaction failed: ${txHash}`);
}
});
}),
);
}
}

0 comments on commit 63bb318

Please sign in to comment.