Skip to content

Commit

Permalink
fix: support local payloads
Browse files Browse the repository at this point in the history
  • Loading branch information
sakulstra committed Jan 25, 2024
1 parent 1540b8a commit 6ffb241
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 16 deletions.
68 changes: 52 additions & 16 deletions src/commands/fork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ 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';

export function addCommand(program: Command) {
program
Expand All @@ -15,16 +18,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 @@ -50,18 +46,58 @@ export function addCommand(program: Command) {
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');
const payloadsController = getPayloadsController(
payloadsControllerAddress as Hex,
CHAIN_ID_CLIENT_MAP[forkConfig.chainId as keyof typeof CHAIN_ID_CLIENT_MAP]
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');
const payloadsController = getPayloadsController(
payloadsControllerAddress as Hex,
CHAIN_ID_CLIENT_MAP[forkConfig.chainId]
);
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 pc = getContract({
abi: IPayloadsControllerCore_ABI,
address: payloadsControllerAddress,
client: { wallet: walletProvider },
});
const payloadId = await pc.write.createPayload(
[
[
{
target: payload,
withDelegateCall: true,
accessLevel: 1,
value: 0n,
signature: 'execute()',
callData: '0x0',
},
],
],
{} as any
);
const tenderlyPayload = await payloadsController.getSimulationPayloadForExecution(Number(payloadId));
await tenderly.unwrapAndExecuteSimulationPayloadOnFork(fork, tenderlyPayload);
return;
}
if (payloadId != undefined) {
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;
}
});
}
7 changes: 7 additions & 0 deletions src/govv3/utils/checkAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,10 @@ export function isKnownAddress(value: Address, chainId: number): string[] | void
if (typeof results === 'string') return [results];
return results;
}

export function findPayloadsController(chainId: number): Address | void {
const key = Object.keys(addresses).find(
(key) => addresses[key].CHAIN_ID === chainId && addresses[key].PAYLOADS_CONTROLLER
);
if (key) return addresses[key].PAYLOADS_CONTROLLER;
}

0 comments on commit 6ffb241

Please sign in to comment.