Skip to content

Commit

Permalink
fix: eMode decoding (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
sakulstra committed Oct 4, 2024
1 parent 383fbcc commit c0eea28
Show file tree
Hide file tree
Showing 6 changed files with 486 additions and 23 deletions.
398 changes: 398 additions & 0 deletions src/govv3/__snapshots__/generatePayloadReport.spec.ts.snap

Large diffs are not rendered by default.

24 changes: 22 additions & 2 deletions src/govv3/checks/state.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// Based on https://github.com/Uniswap/governance-seatbelt/blob/main/checks/check-state-changes.ts
// adjusted for viem & aave governance v3
import {type Client, type Hex, getAddress, Address, getContract} from 'viem';
import {type Client, type Hex, getAddress, Address} from 'viem';
import type {StateDiff, TenderlySimulationResponse} from '../../utils/tenderlyClient';
import {findAsset} from '../utils/checkAddress';
import {assetIndexesToAsset, findAsset} from '../utils/checkAddress';
import {addAssetPrice, addAssetSymbol, prettifyNumber, wrapInQuotes} from '../utils/markdownUtils';
import {getDecodedReserveData} from '../utils/reserveConfigurationInterpreter';
import {getContractName} from '../utils/solidityUtils';
import type {ProposalCheck} from './types';
import {bitMapToIndexes} from '../../utils/storageSlots';

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

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

if (type === '_eModeCategories') {
if (before.collateralBitmap !== undefined) {
before.collateralBitmap_decoded = (
await assetIndexesToAsset(client, address, bitMapToIndexes(BigInt(before.collateralBitmap)))
).toString();
after.collateralBitmap_decoded = (
await assetIndexesToAsset(client, address, bitMapToIndexes(BigInt(after.collateralBitmap)))
).toString();
}
if (before.borrowableBitmap !== undefined) {
before.borrowableBitmap_decoded = (
await assetIndexesToAsset(client, address, bitMapToIndexes(BigInt(before.borrowableBitmap)))
).toString();
after.borrowableBitmap_decoded = (
await assetIndexesToAsset(client, address, bitMapToIndexes(BigInt(after.borrowableBitmap)))
).toString();
}
}

if (type === '_streams') {
const asset = await findAsset(client, after.tokenAddress);
after.ratePerSecond = prettifyNumber({
Expand Down
56 changes: 39 additions & 17 deletions src/govv3/generatePayloadReport.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import {CHAIN_ID_CLIENT_MAP} from '@bgd-labs/js-utils';
import {writeFileSync} from 'fs';
import {describe, expect, it} from 'vitest';
import {generateReport} from './generatePayloadReport';
import {MOCK_PAYLOAD} from './mocks/payload';
import {STREAM_PAYLOAD} from './mocks/streamPayload';
import {EMODES_SIMULATION} from './mocks/eModes';

import {findPayloadsController} from './utils/checkAddress';
import {getPayloadsController} from './payloadsController';
import {Address} from 'viem';
Expand Down Expand Up @@ -34,23 +37,30 @@ describe('generatePayloadReport', () => {
/**
* Can be used to generate a new snapshot
*/
it.skip('should generate snapshot', async () => {
const payloadId = 1;
const chainId = 1;
const client = CHAIN_ID_CLIENT_MAP[chainId];
const payloadsControllerAddress = findPayloadsController(Number(chainId));
const payloadsController = getPayloadsController(payloadsControllerAddress as Address, client);
const cache = await localCacheAdapter.getPayload({
chainId,
payloadId,
payloadsController: payloadsControllerAddress!,
});
const result = await payloadsController.simulatePayloadExecutionOnTenderly(
Number(payloadId),
cache.logs,
);
cleanupMock({simulation: result, payloadInfo: cache});
});
it.skip(
'should generate snapshot',
async () => {
const payloadId = 33;
const chainId = 100;
const client = CHAIN_ID_CLIENT_MAP[chainId];
const payloadsControllerAddress = findPayloadsController(Number(chainId));
const payloadsController = getPayloadsController(
payloadsControllerAddress as Address,
client,
);
const cache = await localCacheAdapter.getPayload({
chainId,
payloadId,
payloadsController: payloadsControllerAddress!,
});
const result = await payloadsController.simulatePayloadExecutionOnTenderly(
Number(payloadId),
cache.logs,
);
writeFileSync('eModes.json', cleanupMock({simulation: result, payloadInfo: cache}));
},
{timeout: 30000},
);

it(
'should match snapshot listing',
Expand All @@ -75,4 +85,16 @@ describe('generatePayloadReport', () => {
},
{timeout: 30000},
);

it(
'should match eModes change',
async () => {
const report = await generateReport({
...(EMODES_SIMULATION as any),
client: CHAIN_ID_CLIENT_MAP[Number(EMODES_SIMULATION.simulation.transaction.network_id)],
});
expect(report).toMatchSnapshot();
},
{timeout: 30000},
);
});
1 change: 1 addition & 0 deletions src/govv3/mocks/eModes.ts

Large diffs are not rendered by default.

27 changes: 26 additions & 1 deletion src/govv3/utils/checkAddress.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as addresses from '@bgd-labs/aave-address-book';
import {IPool_ABI} from '@bgd-labs/aave-address-book';
import {findObjectPaths} from 'find-object-paths';
import {type Address, type Client, type Hex, getAddress, getContract} from 'viem';
import {type Address, type Client, HDKey, type Hex, getAddress, getContract} from 'viem';

/**
* Checks if address is listed on address-book
Expand Down Expand Up @@ -68,3 +69,27 @@ export async function findAsset(client: Client, address: Hex) {
};
return assetsCache[chainId][address];
}

let cachedReservesList: readonly Hex[] = [];

export async function assetIndexesToAsset(
client: Client,
poolAddress: Hex,
indexes: number[],
): Promise<string[]> {
if (!cachedReservesList.length)
cachedReservesList = await getContract({
client,
abi: IPool_ABI,
address: poolAddress,
}).read.getReservesList();
return await Promise.all(
indexes.map(async (index) => {
if (index < cachedReservesList.length) {
const reserve = cachedReservesList[index];
return `${(await findAsset(client, reserve)).symbol}(id: ${index})`;
}
return `unknown(id: ${index})`;
}),
);
}
3 changes: 0 additions & 3 deletions src/govv3/utils/reserveConfigurationInterpreter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
// 7239967485535440384849065513280590734864674587685562855040293771882277901892
import {CHAIN_ID_CLIENT_MAP} from '@bgd-labs/js-utils';
import {describe, expect, it} from 'vitest';
import {decodeReserveDataV3} from './reserveConfigurationInterpreter';
import {toHex} from 'viem';

describe('reserveConfigurationInterpreter', () => {
it(
Expand Down

0 comments on commit c0eea28

Please sign in to comment.