Skip to content

Commit 4e6e9ab

Browse files
authored
fix: improve diff (#201)
1 parent f97ef8d commit 4e6e9ab

File tree

2 files changed

+60
-38
lines changed

2 files changed

+60
-38
lines changed

src/reports/code-diff.spec.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,38 @@ import {describe, expect, it} from 'vitest';
22
import pre32 from './mocks/pre3-2.json';
33
import post32 from './mocks/post3-2.json';
44
import {diffCode, downloadContract} from './code-diff';
5-
import {diffSlot} from './raw-storage-diff';
5+
import {diffRawStorage, diffSlot} from './raw-storage-diff';
66

7-
describe.skip('code diffs', () => {
7+
describe('code diffs', () => {
88
it('should diff slots', () => {
99
diffSlot(1, '0x0', {
10-
previousValue: '0x4816b2C2895f97fB918f1aE7Da403750a0eE372e',
11-
newValue: '0xE5e48Ad1F9D1A894188b483DcF91f4FaD6AbA43b',
10+
previousValue: '0x0000000000000000000000003d881c2dc90f00e7a52f06155f77fbec63a779c7',
11+
newValue: '0x00000000000000000000000056401d666f486c495566a29249447c2bb8c56bb2',
1212
});
1313
});
14-
it(
14+
it('should diff contracts', async () => {
15+
await diffRawStorage(1, {
16+
'0x0aa97c284e98396202b6a04024f5e2c65026f3c0': {
17+
label: null,
18+
balanceDiff: null,
19+
stateDiff: {
20+
'0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc': {
21+
previousValue: '0x0000000000000000000000003d881c2dc90f00e7a52f06155f77fbec63a779c7',
22+
newValue: '0x00000000000000000000000056401d666f486c495566a29249447c2bb8c56bb2',
23+
},
24+
},
25+
},
26+
});
27+
});
28+
it.skip(
1529
'should download contract',
1630
() => {
1731
downloadContract(pre32.chainId, pre32.poolConfig.poolConfigurator);
1832
},
1933
{timeout: 30000},
2034
);
2135

22-
it(
36+
it.skip(
2337
'should diff the contract',
2438
() => {
2539
const from = downloadContract(pre32.chainId, pre32.poolConfig.poolConfiguratorImpl);

src/reports/raw-storage-diff.ts

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,21 @@ import {bytes32ToAddress} from '../utils/storageSlots';
33
import {diffCode, downloadContract} from './code-diff';
44
import {RawStorage, SlotDiff} from './snapshot-types';
55
import {isKnownAddress} from '../govv3/utils/checkAddress';
6-
import {Address, getContract, zeroHash} from 'viem';
6+
import {Address, getContract, isAddress, toBytes, zeroHash} from 'viem';
77
import {getClient} from '@bgd-labs/rpc-env';
88
import {IPool_ABI} from '@bgd-labs/aave-address-book/abis';
99

1010
export function diffSlot(chainId: number, address: Address, slot: SlotDiff) {
11+
const fromAddress = isAddress(slot.previousValue)
12+
? slot.previousValue
13+
: bytes32ToAddress(slot.previousValue);
14+
const toAddress = isAddress(slot.newValue) ? slot.newValue : bytes32ToAddress(slot.newValue);
1115
// pure new deployments cannot be diffed, we just download the code in that case
1216
if (slot.previousValue == zeroHash) {
13-
const toAddress = bytes32ToAddress(slot.newValue);
1417
const to = downloadContract(chainId, toAddress);
1518
mkdirSync('./diffs', {recursive: true});
1619
writeFileSync(`./diffs/${chainId}_${address}_${toAddress}.diff`, readFileSync(to), {});
1720
} else {
18-
const fromAddress = bytes32ToAddress(slot.previousValue);
19-
const toAddress = bytes32ToAddress(slot.newValue);
2021
const from = downloadContract(chainId, fromAddress);
2122
const to = downloadContract(chainId, toAddress);
2223
const result = diffCode(from, to);
@@ -50,34 +51,41 @@ export async function diffRawStorage(chainId: number, raw: RawStorage) {
5051
// ... we might want to fetch the owner in that case
5152
}
5253

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]});
54+
if (contractName) {
55+
const path = contractName[0].split('.');
56+
// Diff code logic libraries
57+
if (path[path.length - 1] === 'POOL') {
58+
const oldPool = getContract({
59+
client: getClient(chainId, {}),
60+
abi: IPool_ABI,
61+
address: bytes32ToAddress(raw[contract].stateDiff[erc1967ImplSlot].previousValue),
62+
});
63+
const newPool = getContract({
64+
client: getClient(chainId, {}),
65+
abi: IPool_ABI,
66+
address: bytes32ToAddress(raw[contract].stateDiff[erc1967ImplSlot].previousValue),
67+
});
68+
const addresses = await Promise.all([
69+
oldPool.read.getSupplyLogic(),
70+
newPool.read.getSupplyLogic(),
71+
oldPool.read.getBorrowLogic(),
72+
newPool.read.getBorrowLogic(),
73+
oldPool.read.getLiquidationLogic(),
74+
newPool.read.getLiquidationLogic(),
75+
oldPool.read.getPoolLogic(),
76+
newPool.read.getPoolLogic(),
77+
oldPool.read.getFlashLoanLogic(),
78+
newPool.read.getFlashLoanLogic(),
79+
oldPool.read.getEModeLogic(),
80+
newPool.read.getEModeLogic(),
81+
]);
82+
console.log('addr', addresses);
83+
for (let i = 0; i < addresses.length; i = i + 2) {
84+
diffSlot(chainId, contract, {
85+
previousValue: addresses[i],
86+
newValue: addresses[i + 1],
87+
});
88+
}
8189
}
8290
}
8391
}

0 commit comments

Comments
 (0)