|
| 1 | +import {writeFileSync, mkdirSync, readFileSync} from 'fs'; |
| 2 | +import {bytes32ToAddress} from '../utils/storageSlots'; |
| 3 | +import {diffCode, downloadContract} from './code-diff'; |
| 4 | +import {RawStorage, SlotDiff} from './snapshot-types'; |
| 5 | +import {isKnownAddress} from '../govv3/utils/checkAddress'; |
| 6 | +import {Address, getContract, zeroHash} from 'viem'; |
| 7 | +import {getClient} from '@bgd-labs/rpc-env'; |
| 8 | +import {IPool_ABI} from '@bgd-labs/aave-address-book/abis'; |
| 9 | + |
| 10 | +export function diffSlot(chainId: number, address: Address, slot: SlotDiff) { |
| 11 | + // pure new deployments cannot be diffed, we just download the code in that case |
| 12 | + if (slot.previousValue == zeroHash) { |
| 13 | + const toAddress = bytes32ToAddress(slot.newValue); |
| 14 | + const to = downloadContract(chainId, toAddress); |
| 15 | + mkdirSync('./diffs', {recursive: true}); |
| 16 | + writeFileSync(`./diffs/${chainId}_${address}_${toAddress}.diff`, readFileSync(to), {}); |
| 17 | + } else { |
| 18 | + const fromAddress = bytes32ToAddress(slot.previousValue); |
| 19 | + const toAddress = bytes32ToAddress(slot.newValue); |
| 20 | + const from = downloadContract(chainId, fromAddress); |
| 21 | + const to = downloadContract(chainId, toAddress); |
| 22 | + const result = diffCode(from, to); |
| 23 | + mkdirSync('./diffs', {recursive: true}); |
| 24 | + writeFileSync(`./diffs/${chainId}_${address}_${fromAddress}_${toAddress}.diff`, result, {}); |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +export async function diffRawStorage(chainId: number, raw: RawStorage) { |
| 29 | + // ERC1967 slot https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/proxy/ERC1967/ERC1967Utils.sol#L21C53-L21C119 |
| 30 | + const erc1967ImplSlot = '0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc'; |
| 31 | + const erc1967AdminSlot = '0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103'; |
| 32 | + await Promise.all( |
| 33 | + (Object.keys(raw) as (keyof typeof raw)[]).map(async (contract) => { |
| 34 | + if (raw[contract]) { |
| 35 | + const contractName = isKnownAddress(contract, chainId); |
| 36 | + // label known contracts if no label was set on foundry |
| 37 | + if (!raw[contract].label) { |
| 38 | + if (contractName) raw[contract].label = contractName.join(', '); |
| 39 | + } |
| 40 | + |
| 41 | + // create contract diff if storage changed |
| 42 | + if (raw[contract].stateDiff[erc1967ImplSlot]) { |
| 43 | + raw[contract].stateDiff[erc1967ImplSlot].label = 'Implementation slot'; |
| 44 | + diffSlot(chainId, contract, raw[contract].stateDiff[erc1967ImplSlot]); |
| 45 | + } |
| 46 | + |
| 47 | + // admin slot |
| 48 | + if (raw[contract].stateDiff[erc1967AdminSlot]) { |
| 49 | + raw[contract].stateDiff[erc1967ImplSlot].label = 'Admin slot'; |
| 50 | + // ... we might want to fetch the owner in that case |
| 51 | + } |
| 52 | + |
| 53 | + // Diff code logic libraries |
| 54 | + if (contractName && contractName[contractName.length - 1] === 'POOL') { |
| 55 | + const oldPool = getContract({ |
| 56 | + client: getClient(chainId, {}), |
| 57 | + abi: IPool_ABI, |
| 58 | + address: bytes32ToAddress(raw[contract].stateDiff[erc1967ImplSlot].previousValue), |
| 59 | + }); |
| 60 | + const newPool = getContract({ |
| 61 | + client: getClient(chainId, {}), |
| 62 | + abi: IPool_ABI, |
| 63 | + address: bytes32ToAddress(raw[contract].stateDiff[erc1967ImplSlot].previousValue), |
| 64 | + }); |
| 65 | + const addresses = await Promise.all([ |
| 66 | + oldPool.read.getSupplyLogic(), |
| 67 | + newPool.read.getSupplyLogic(), |
| 68 | + oldPool.read.getBorrowLogic(), |
| 69 | + newPool.read.getBorrowLogic(), |
| 70 | + oldPool.read.getLiquidationLogic(), |
| 71 | + newPool.read.getLiquidationLogic(), |
| 72 | + oldPool.read.getPoolLogic(), |
| 73 | + newPool.read.getPoolLogic(), |
| 74 | + oldPool.read.getFlashLoanLogic(), |
| 75 | + newPool.read.getFlashLoanLogic(), |
| 76 | + oldPool.read.getEModeLogic(), |
| 77 | + newPool.read.getEModeLogic(), |
| 78 | + ]); |
| 79 | + for (let i = 0; i < addresses.length; i = i + 2) { |
| 80 | + diffSlot(chainId, contract, {previousValue: addresses[i], newValue: addresses[i + 1]}); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + }), |
| 85 | + ); |
| 86 | +} |
0 commit comments