Skip to content

Commit

Permalink
feat: allow simulation of local payload (#73)
Browse files Browse the repository at this point in the history
* fix: support local payloads

* fix: patch some related issues
  • Loading branch information
sakulstra authored Jan 29, 2024
1 parent 1540b8a commit 84f7bed
Show file tree
Hide file tree
Showing 5 changed files with 2,336 additions and 31 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"typescript": "^5.3.3",
"vitest": "^1.2.1"
},
"type": "module",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"exports": {
Expand Down
66 changes: 51 additions & 15 deletions src/commands/fork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ import { Command } from '@commander-js/extra-typings';
import { tenderly } from '../utils/tenderlyClient';
import { getGovernance } from '../govv3/governance';
import { getPayloadsController } from '../govv3/payloadsController';
import { Hex, PublicClient } from 'viem';
import { DEFAULT_GOVERNANCE, DEFAULT_GOVERNANCE_CLIENT } from '../utils/constants';
import { Hex, createWalletClient, getContract, http } from 'viem';
import { DEFAULT_GOVERNANCE, DEFAULT_GOVERNANCE_CLIENT, EOA } from '../utils/constants';
import { CHAIN_ID_CLIENT_MAP } from '@bgd-labs/js-utils';
import { findPayloadsController } from '../govv3/utils/checkAddress';
import path from 'path';
import { IPayloadsControllerCore_ABI } from '@bgd-labs/aave-address-book';
import { getTransactionReceipt } from 'viem/actions';

export function addCommand(program: Command) {
program
Expand All @@ -15,16 +19,9 @@ export function addCommand(program: Command) {
.option('--alias <string>', 'Set a custom alias')
.option('--proposalId <number>', 'ProposalId to execute')
.option('--payloadId <number>', 'PayloadId to execute')
.option('--payloadsController <string>', 'PayloadsController address')
.option('--artifactPath <string>', 'path to the local payload')
.action(async (options) => {
const {
chainId,
blockNumber,
alias,
payloadId,
proposalId,
payloadsController: payloadsControllerAddress,
} = options;
const { chainId, blockNumber, alias, payloadId, proposalId, artifactPath } = options;
function getAlias() {
const unix = Math.floor(new Date().getTime() / 1000);
if (alias) {
Expand All @@ -42,26 +39,65 @@ export function addCommand(program: Command) {
alias: getAlias(),
blockNumber: Number(blockNumber),
};
const governance = getGovernance({ address: DEFAULT_GOVERNANCE, client: DEFAULT_GOVERNANCE_CLIENT });
if (proposalId) {
const governance = getGovernance({ address: DEFAULT_GOVERNANCE, client: DEFAULT_GOVERNANCE_CLIENT });
const payload = await governance.getSimulationPayloadForExecution(BigInt(proposalId));
const fork = await tenderly.fork({
...forkConfig,
blockNumber: forkConfig.blockNumber || payload.block_number,
});
await tenderly.unwrapAndExecuteSimulationPayloadOnFork(fork, payload);
} else if (payloadId != undefined) {
if (!payloadsControllerAddress) throw new Error('you need to provide a payloadsController');
return;
}
if (payloadId == undefined && artifactPath == undefined) throw new Error('you need to specify an id or artifact');
const payloadsControllerAddress = findPayloadsController(forkConfig.chainId);
if (!payloadsControllerAddress) throw new Error('payloadscontroller not found on specified chain');

if (!payloadId) {
const fork = await tenderly.fork({
...forkConfig,
blockNumber: forkConfig.blockNumber,
});
const payload = await tenderly.deployCode(fork, path.join(process.cwd(), artifactPath!));
const walletProvider = createWalletClient({
account: EOA,
chain: { id: fork.forkNetworkId, name: 'tenderly' } as any,
transport: http(fork.forkUrl),
});
const payloadsController = getPayloadsController(payloadsControllerAddress as Hex, walletProvider);
const hash = await payloadsController.controllerContract.write.createPayload(
[
[
{
target: payload,
withDelegateCall: true,
accessLevel: 1,
value: 0n,
signature: 'execute()',
callData: '0x0',
},
],
],
{} as any
);
const tenderlyPayload = await payloadsController.getSimulationPayloadForExecution(
Number((await payloadsController.controllerContract.read.getPayloadsCount()) - 1)
);
await tenderly.unwrapAndExecuteSimulationPayloadOnFork(fork, tenderlyPayload);
return;
}
if (payloadId != undefined) {
const payloadsController = getPayloadsController(
payloadsControllerAddress as Hex,
CHAIN_ID_CLIENT_MAP[forkConfig.chainId as keyof typeof CHAIN_ID_CLIENT_MAP]
CHAIN_ID_CLIENT_MAP[chainId]
);
const payload = await payloadsController.getSimulationPayloadForExecution(Number(payloadId));
const fork = await tenderly.fork({
...forkConfig,
blockNumber: forkConfig.blockNumber || payload.block_number,
});
await tenderly.unwrapAndExecuteSimulationPayloadOnFork(fork, payload);
return;
}
});
}
Loading

0 comments on commit 84f7bed

Please sign in to comment.