From 6ffb241a506e53697b062b673f1ddbf4dcb9fc3f Mon Sep 17 00:00:00 2001 From: sakulstra Date: Thu, 25 Jan 2024 20:58:26 +0100 Subject: [PATCH] fix: support local payloads --- src/commands/fork.ts | 68 +++++++++++++++++++++++++-------- src/govv3/utils/checkAddress.ts | 7 ++++ 2 files changed, 59 insertions(+), 16 deletions(-) diff --git a/src/commands/fork.ts b/src/commands/fork.ts index 59fc965..b339ae4 100644 --- a/src/commands/fork.ts +++ b/src/commands/fork.ts @@ -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 @@ -15,16 +18,9 @@ export function addCommand(program: Command) { .option('--alias ', 'Set a custom alias') .option('--proposalId ', 'ProposalId to execute') .option('--payloadId ', 'PayloadId to execute') - .option('--payloadsController ', 'PayloadsController address') + .option('--artifactPath ', '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) { @@ -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; } }); } diff --git a/src/govv3/utils/checkAddress.ts b/src/govv3/utils/checkAddress.ts index e53dc93..21a2d81 100644 --- a/src/govv3/utils/checkAddress.ts +++ b/src/govv3/utils/checkAddress.ts @@ -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; +}