Skip to content

Commit c0eea28

Browse files
committed
fix: eMode decoding (#161)
1 parent 383fbcc commit c0eea28

File tree

6 files changed

+486
-23
lines changed

6 files changed

+486
-23
lines changed

src/govv3/__snapshots__/generatePayloadReport.spec.ts.snap

Lines changed: 398 additions & 0 deletions
Large diffs are not rendered by default.

src/govv3/checks/state.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
// Based on https://github.com/Uniswap/governance-seatbelt/blob/main/checks/check-state-changes.ts
22
// adjusted for viem & aave governance v3
3-
import {type Client, type Hex, getAddress, Address, getContract} from 'viem';
3+
import {type Client, type Hex, getAddress, Address} from 'viem';
44
import type {StateDiff, TenderlySimulationResponse} from '../../utils/tenderlyClient';
5-
import {findAsset} from '../utils/checkAddress';
5+
import {assetIndexesToAsset, findAsset} from '../utils/checkAddress';
66
import {addAssetPrice, addAssetSymbol, prettifyNumber, wrapInQuotes} from '../utils/markdownUtils';
77
import {getDecodedReserveData} from '../utils/reserveConfigurationInterpreter';
88
import {getContractName} from '../utils/solidityUtils';
99
import type {ProposalCheck} from './types';
10+
import {bitMapToIndexes} from '../../utils/storageSlots';
1011

1112
type ValueType = string | Record<string, string>;
1213

@@ -135,6 +136,25 @@ export async function deepDiff({
135136
after.configuration.data_decoded = getDecodedReserveData(address, after.configuration.data);
136137
}
137138

139+
if (type === '_eModeCategories') {
140+
if (before.collateralBitmap !== undefined) {
141+
before.collateralBitmap_decoded = (
142+
await assetIndexesToAsset(client, address, bitMapToIndexes(BigInt(before.collateralBitmap)))
143+
).toString();
144+
after.collateralBitmap_decoded = (
145+
await assetIndexesToAsset(client, address, bitMapToIndexes(BigInt(after.collateralBitmap)))
146+
).toString();
147+
}
148+
if (before.borrowableBitmap !== undefined) {
149+
before.borrowableBitmap_decoded = (
150+
await assetIndexesToAsset(client, address, bitMapToIndexes(BigInt(before.borrowableBitmap)))
151+
).toString();
152+
after.borrowableBitmap_decoded = (
153+
await assetIndexesToAsset(client, address, bitMapToIndexes(BigInt(after.borrowableBitmap)))
154+
).toString();
155+
}
156+
}
157+
138158
if (type === '_streams') {
139159
const asset = await findAsset(client, after.tokenAddress);
140160
after.ratePerSecond = prettifyNumber({

src/govv3/generatePayloadReport.spec.ts

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import {CHAIN_ID_CLIENT_MAP} from '@bgd-labs/js-utils';
2+
import {writeFileSync} from 'fs';
23
import {describe, expect, it} from 'vitest';
34
import {generateReport} from './generatePayloadReport';
45
import {MOCK_PAYLOAD} from './mocks/payload';
56
import {STREAM_PAYLOAD} from './mocks/streamPayload';
7+
import {EMODES_SIMULATION} from './mocks/eModes';
8+
69
import {findPayloadsController} from './utils/checkAddress';
710
import {getPayloadsController} from './payloadsController';
811
import {Address} from 'viem';
@@ -34,23 +37,30 @@ describe('generatePayloadReport', () => {
3437
/**
3538
* Can be used to generate a new snapshot
3639
*/
37-
it.skip('should generate snapshot', async () => {
38-
const payloadId = 1;
39-
const chainId = 1;
40-
const client = CHAIN_ID_CLIENT_MAP[chainId];
41-
const payloadsControllerAddress = findPayloadsController(Number(chainId));
42-
const payloadsController = getPayloadsController(payloadsControllerAddress as Address, client);
43-
const cache = await localCacheAdapter.getPayload({
44-
chainId,
45-
payloadId,
46-
payloadsController: payloadsControllerAddress!,
47-
});
48-
const result = await payloadsController.simulatePayloadExecutionOnTenderly(
49-
Number(payloadId),
50-
cache.logs,
51-
);
52-
cleanupMock({simulation: result, payloadInfo: cache});
53-
});
40+
it.skip(
41+
'should generate snapshot',
42+
async () => {
43+
const payloadId = 33;
44+
const chainId = 100;
45+
const client = CHAIN_ID_CLIENT_MAP[chainId];
46+
const payloadsControllerAddress = findPayloadsController(Number(chainId));
47+
const payloadsController = getPayloadsController(
48+
payloadsControllerAddress as Address,
49+
client,
50+
);
51+
const cache = await localCacheAdapter.getPayload({
52+
chainId,
53+
payloadId,
54+
payloadsController: payloadsControllerAddress!,
55+
});
56+
const result = await payloadsController.simulatePayloadExecutionOnTenderly(
57+
Number(payloadId),
58+
cache.logs,
59+
);
60+
writeFileSync('eModes.json', cleanupMock({simulation: result, payloadInfo: cache}));
61+
},
62+
{timeout: 30000},
63+
);
5464

5565
it(
5666
'should match snapshot listing',
@@ -75,4 +85,16 @@ describe('generatePayloadReport', () => {
7585
},
7686
{timeout: 30000},
7787
);
88+
89+
it(
90+
'should match eModes change',
91+
async () => {
92+
const report = await generateReport({
93+
...(EMODES_SIMULATION as any),
94+
client: CHAIN_ID_CLIENT_MAP[Number(EMODES_SIMULATION.simulation.transaction.network_id)],
95+
});
96+
expect(report).toMatchSnapshot();
97+
},
98+
{timeout: 30000},
99+
);
78100
});

src/govv3/mocks/eModes.ts

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

src/govv3/utils/checkAddress.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as addresses from '@bgd-labs/aave-address-book';
2+
import {IPool_ABI} from '@bgd-labs/aave-address-book';
23
import {findObjectPaths} from 'find-object-paths';
3-
import {type Address, type Client, type Hex, getAddress, getContract} from 'viem';
4+
import {type Address, type Client, HDKey, type Hex, getAddress, getContract} from 'viem';
45

56
/**
67
* Checks if address is listed on address-book
@@ -68,3 +69,27 @@ export async function findAsset(client: Client, address: Hex) {
6869
};
6970
return assetsCache[chainId][address];
7071
}
72+
73+
let cachedReservesList: readonly Hex[] = [];
74+
75+
export async function assetIndexesToAsset(
76+
client: Client,
77+
poolAddress: Hex,
78+
indexes: number[],
79+
): Promise<string[]> {
80+
if (!cachedReservesList.length)
81+
cachedReservesList = await getContract({
82+
client,
83+
abi: IPool_ABI,
84+
address: poolAddress,
85+
}).read.getReservesList();
86+
return await Promise.all(
87+
indexes.map(async (index) => {
88+
if (index < cachedReservesList.length) {
89+
const reserve = cachedReservesList[index];
90+
return `${(await findAsset(client, reserve)).symbol}(id: ${index})`;
91+
}
92+
return `unknown(id: ${index})`;
93+
}),
94+
);
95+
}

src/govv3/utils/reserveConfigurationInterpreter.spec.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
// 7239967485535440384849065513280590734864674587685562855040293771882277901892
2-
import {CHAIN_ID_CLIENT_MAP} from '@bgd-labs/js-utils';
31
import {describe, expect, it} from 'vitest';
42
import {decodeReserveDataV3} from './reserveConfigurationInterpreter';
5-
import {toHex} from 'viem';
63

74
describe('reserveConfigurationInterpreter', () => {
85
it(

0 commit comments

Comments
 (0)