Skip to content

Commit

Permalink
feat: simulate against fork (#78)
Browse files Browse the repository at this point in the history
* feat: simulate against fork

---------

Co-authored-by: Ernesto Boado <ebdmrr@gmail.com>
  • Loading branch information
sakulstra and eboadom authored Dec 5, 2023
1 parent 30280fc commit f3917e1
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
18 changes: 18 additions & 0 deletions src/commands/governance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { getCachedIpfs } from '../ipfs/getCachedProposalMetaData';
import { toAddressLink, toTxLink } from '../govv3/utils/markdownUtils';
import { getAccountRPL, getBLockRLP } from '../govv3/proofs';
import { DEFAULT_GOVERNANCE, DEFAULT_GOVERNANCE_CLIENT, FORMAT } from '../utils/constants';
import { getPayloadsController } from '../govv3/payloadsController';

enum DialogOptions {
DETAILS,
Expand All @@ -32,6 +33,23 @@ export function addCommand(program: Command) {
await simulateProposal(DEFAULT_GOVERNANCE, DEFAULT_GOVERNANCE_CLIENT, proposalId);
});

govV3
.command('simulate-payload')
.description('simulates a payloadId on tenderly')
.requiredOption('--chainId <number>', 'the chainId to fork of')
.requiredOption('--payloadId <number>', 'payloadId to simulate via tenderly')
.option('--payloadsController <string>', 'PayloadsController address')
.action(async ({ payloadId: _payloadId, payloadsController: payloadsControllerAddress, chainId }, options) => {
const payloadId = Number(_payloadId);
const payloadsController = getPayloadsController(
payloadsControllerAddress as Hex,
CHAIN_ID_CLIENT_MAP[Number(chainId) as keyof typeof CHAIN_ID_CLIENT_MAP] as PublicClient
);
const logs = await payloadsController.cacheLogs();
const config = await payloadsController.getPayload(payloadId, logs);
await payloadsController.simulatePayloadExecutionOnTenderly(Number(payloadId), config);
});

govV3
.command('view')
.description('shows all the proposals & state')
Expand Down
2 changes: 1 addition & 1 deletion src/govv3/governance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ export const getGovernance = ({
return tenderly.simulateTx(publicClient.chain!.id, tx);
}
const payload = await getSimulationPayloadForExecution(proposalId);
return tenderly.simulate(payload);
return tenderly.simulate(payload, publicClient);
},
async getVotingProofs(proposalId: bigint, voter: Hex, votingChainId: bigint) {
const proposal = await getProposal(proposalId);
Expand Down
2 changes: 1 addition & 1 deletion src/govv3/payloadsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export const getPayloadsController = (address: Hex, publicClient: PublicClient):
}
const payload = await getSimulationPayloadForExecution(id);

return tenderly.simulate(payload);
return tenderly.simulate(payload, publicClient);
},
};
};
24 changes: 22 additions & 2 deletions src/utils/tenderlyClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
fromHex,
pad,
zeroAddress,
PublicClient,
} from 'viem';
import { EOA } from './constants';
import { logError, logInfo, logSuccess, logWarning } from './logger';
Expand Down Expand Up @@ -242,6 +243,7 @@ export type Fork = {
block_number: number;
forkNetworkId: string;
forkUrl: string;
global_head: string;
};

class Tenderly {
Expand Down Expand Up @@ -272,7 +274,7 @@ class Tenderly {
return result;
};

simulate = async (request: TenderlyRequest): Promise<TenderlySimulationResponse> => {
simulate = async (request: TenderlyRequest, publicClient?: PublicClient): Promise<TenderlySimulationResponse> => {
if (!request.state_objects) {
request.state_objects = {};
}
Expand All @@ -282,6 +284,22 @@ class Tenderly {
request.state_objects[request.from].balance = String(parseEther('3'));
}

let apiUrl = `${this.TENDERLY_BASE}/account/${this.ACCOUNT}/project/${this.PROJECT}/simulate`;

if (publicClient) {
const url = publicClient.transport.url! as string;
const tenderlyForkRegex = new RegExp(/https:\/\/rpc.tenderly.co\/fork\/(.*)/);
if (tenderlyForkRegex.test(url)) {
const matches = url.match(tenderlyForkRegex);
if (matches) {
const fork = await this.getForkInfo(matches[1]);
request.root = fork.global_head;
delete (request as any).network_id;
apiUrl = `${this.TENDERLY_BASE}/account/${this.ACCOUNT}/project/${this.PROJECT}/fork/${matches[1]}/simulate`;
}
}
}

const fullRequest = JSON.stringify({
generate_access_list: true,
save: true,
Expand All @@ -292,7 +310,7 @@ class Tenderly {

logInfo('tenderly', `request: ${JSON.stringify(fullRequest)}`);

const response = await fetch(`${this.TENDERLY_BASE}/account/${this.ACCOUNT}/project/${this.PROJECT}/simulate`, {
const response = await fetch(apiUrl, {
method: 'POST',
body: fullRequest,
headers: new Headers({
Expand Down Expand Up @@ -336,6 +354,7 @@ class Tenderly {
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}`,
global_head: result.simulation_fork.global_head,
};
return fork;
};
Expand Down Expand Up @@ -393,6 +412,7 @@ class Tenderly {
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}`,
global_head: result.simulation_fork.global_head,
};
logSuccess(
'tenderly',
Expand Down

0 comments on commit f3917e1

Please sign in to comment.