Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cleanup eMode rendering #157

Merged
merged 3 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 37 additions & 59 deletions src/reports/__snapshots__/diff-report.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ exports[`report > should generate a well formatted report for 3.0 to 3.1 1`] = `
exports[`report > should generate a well formatted report for 3.1 1`] = `
"## Reserve changes

### Reserve altered
### Reserves altered

#### GHO ([0x40D16FC0246aD3160Ccc09B8D0D3A2cD28aE6C2f](https://etherscan.io/address/0x40D16FC0246aD3160Ccc09B8D0D3A2cD28aE6C2f))

Expand All @@ -455,60 +455,17 @@ exports[`report > should generate a well formatted report for 3.1 1`] = `
| baseVariableBorrowRate | 7 % | 6 % |
| interestRate | ![before](/.assets/e03975867d1c729d1a52bbec364211baff2dab5a.svg) | ![after](/.assets/a1a7ab1f1b9fcdf6ffa41ac7a8b4daf2daf98328.svg) |

#### wstETH ([0x7f39C581F595B53c5cb19bD0b3f8dA6c935E2Ca0](https://etherscan.io/address/0x7f39C581F595B53c5cb19bD0b3f8dA6c935E2Ca0))

| description | value before | value after |
| --- | --- | --- |
| eMode.liquidationBonus | 1 % | 2 % |


#### ETHx ([0xA35b1B31Ce002FBF2058D22F30f95D405200A15b](https://etherscan.io/address/0xA35b1B31Ce002FBF2058D22F30f95D405200A15b))

| description | value before | value after |
| --- | --- | --- |
| eMode.liquidationBonus | 1 % | 2 % |


#### cbETH ([0xBe9895146f7AF43049ca1c1AE358B0541Ea49704](https://etherscan.io/address/0xBe9895146f7AF43049ca1c1AE358B0541Ea49704))

| description | value before | value after |
| --- | --- | --- |
| eMode.liquidationBonus | 1 % | 2 % |


#### WETH ([0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2](https://etherscan.io/address/0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2))

| description | value before | value after |
| --- | --- | --- |
| eMode.liquidationBonus | 1 % | 2 % |


#### weETH ([0xCd5fE23C85820F7B72D0926FC9b05b43E359b7ee](https://etherscan.io/address/0xCd5fE23C85820F7B72D0926FC9b05b43E359b7ee))

| description | value before | value after |
| --- | --- | --- |
| eMode.liquidationBonus | 1 % | 2 % |


#### rETH ([0xae78736Cd615f374D3085123A210448E74Fc6393](https://etherscan.io/address/0xae78736Cd615f374D3085123A210448E74Fc6393))

| description | value before | value after |
| --- | --- | --- |
| eMode.liquidationBonus | 1 % | 2 % |

## Emodes changes

#### osETH ([0xf1C9acDc66974dFB6dEcB12aA385b9cD01190E38](https://etherscan.io/address/0xf1C9acDc66974dFB6dEcB12aA385b9cD01190E38))
### EMode: ETH correlated(id: 1)

| description | value before | value after |
| --- | --- | --- |
| eMode.label (unchanged) | ETH correlated | ETH correlated |
| eMode.ltv (unchanged) | 93 % | 93 % |
| eMode.liquidationThreshold (unchanged) | 95 % | 95 % |
| eMode.liquidationBonus | 1 % | 2 % |
| eMode.priceSource (unchanged) | 0x0000000000000000000000000000000000000000 | 0x0000000000000000000000000000000000000000 |


## Emodes changes

### EMode 1:ETH correlated

| eMode.liquidationBonus | 1 % | 2 % |
## Raw diff

\`\`\`json
Expand Down Expand Up @@ -691,18 +648,39 @@ exports[`report > should generate a well formatted report for 3.2 1`] = `

## Emodes changes

### EMode 1:Stablecoins
### EMode: Stablecoins(id: 1)

| eMode.borrowableBitmap | - | DAI, USDC, USDT, EURS, jEUR, EURA, miMATIC, USDC |
| eMode.collateralBitmap | - | DAI, USDC, USDT, EURS, jEUR, EURA, miMATIC, USDC |
| eMode.label (unchanged) | Stablecoins | Stablecoins |
| eMode.ltv (unchanged) | 93 % | 93 % |
| eMode.liquidationThreshold (unchanged) | 95 % | 95 % |
| eMode.liquidationBonus (unchanged) | 1 % | 1 % |
| eMode.priceSource (unchanged) | 0x0000000000000000000000000000000000000000 | 0x0000000000000000000000000000000000000000 |


### EMode: MATIC correlated(id: 2)

| eMode.borrowableBitmap | - | WMATIC, stMATIC, MaticX |
| eMode.collateralBitmap | - | WMATIC, stMATIC, MaticX |
| eMode.label (unchanged) | MATIC correlated | MATIC correlated |
| eMode.ltv (unchanged) | 92.5 % | 92.5 % |
| eMode.liquidationThreshold (unchanged) | 95 % | 95 % |
| eMode.liquidationBonus (unchanged) | 1 % | 1 % |
| eMode.priceSource (unchanged) | 0x0000000000000000000000000000000000000000 | 0x0000000000000000000000000000000000000000 |


### EMode: ETH correlated(id: 3)

| eMode.borrowableBitmap | - | DAI,USDC,USDT,EURS,jEUR,EURA,miMATIC,USDC |
| eMode.collateralBitmap | - | DAI,USDC,USDT,EURS,jEUR,EURA,miMATIC,USDC |
### EMode 2:MATIC correlated
| eMode.borrowableBitmap | - | WETH, wstETH |
| eMode.collateralBitmap | - | WETH, wstETH |
| eMode.label (unchanged) | ETH correlated | ETH correlated |
| eMode.ltv (unchanged) | 90 % | 90 % |
| eMode.liquidationThreshold (unchanged) | 93 % | 93 % |
| eMode.liquidationBonus (unchanged) | 1 % | 1 % |
| eMode.priceSource (unchanged) | 0x0000000000000000000000000000000000000000 | 0x0000000000000000000000000000000000000000 |

| eMode.borrowableBitmap | - | WMATIC,stMATIC,MaticX |
| eMode.collateralBitmap | - | WMATIC,stMATIC,MaticX |
### EMode 3:ETH correlated

| eMode.borrowableBitmap | - | WETH,wstETH |
| eMode.collateralBitmap | - | WETH,wstETH |
## Raw diff

\`\`\`json
Expand Down
46 changes: 4 additions & 42 deletions src/reports/diff-reports.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import hash from 'object-hash';
import {diff} from './diff';
import {renderEmode, renderEmodeDiff} from './emode';
import {renderEmodeDiff} from './emode';
import {fetchRateStrategyImage} from './fetch-IR-strategy';
import {renderReserve, renderReserveDiff} from './reserve';
import {AaveV3Reserve, type AaveV3Snapshot} from './snapshot-types';
Expand Down Expand Up @@ -42,12 +42,6 @@ export async function diffReports<A extends AaveV3Snapshot, B extends AaveV3Snap
report += renderStrategy(post.strategies[reserveKey]);

report += `| interestRate | ![ir](/.assets/${imageHash}.svg) |\n`;
if (
post.reserves[reserveKey].eModeCategory &&
post.reserves[reserveKey].eModeCategory !== 0
) {
report += renderEmode(post.eModes[post.reserves[reserveKey].eModeCategory]);
}

return report;
}
Expand All @@ -69,18 +63,7 @@ export async function diffReports<A extends AaveV3Snapshot, B extends AaveV3Snap
const preIrHash = hash(pre.strategies[reserveKey]);
const postIrHash = hash(post.strategies[reserveKey]);
const hasChangedIr = preIrHash !== postIrHash;
const eModeCategoryChanged =
diffResult.reserves[reserveKey].eModeCategory?.hasOwnProperty('from');
const eModeParamsChanged =
!eModeCategoryChanged &&
hasDiff(diffResult.eModes?.[diffResult.reserves[reserveKey].eModeCategory as any]);
if (
!hasChangedReserveProperties &&
!hasChangedIr &&
!eModeCategoryChanged &&
!eModeParamsChanged
)
return;
if (!hasChangedReserveProperties && !hasChangedIr) return;
// diff reserve
let report = renderReserveDiff(diffResult.reserves[reserveKey] as any, chainId);
// diff irs
Expand All @@ -90,28 +73,6 @@ export async function diffReports<A extends AaveV3Snapshot, B extends AaveV3Snap
);
report += `| interestRate | ![before](/.assets/${preIrHash}.svg) | ![after](/.assets/${postIrHash}.svg) |`;
}
// diff eModes
if (eModeCategoryChanged) {
report += renderEmodeDiff(
diff(
pre.eModes[(diffResult.reserves[reserveKey].eModeCategory as any).from] || {},
post.eModes[(diffResult.reserves[reserveKey].eModeCategory as any).to],
) as any,
pre,
post,
);
}

if (eModeParamsChanged && !eModeCategoryChanged) {
report += renderEmodeDiff(
diff(
pre.eModes[diffResult.reserves[reserveKey].eModeCategory as any] || {},
post.eModes[diffResult.reserves[reserveKey].eModeCategory as any],
) as any,
pre,
post,
);
}

return report;
}
Expand Down Expand Up @@ -143,12 +104,13 @@ export async function diffReports<A extends AaveV3Snapshot, B extends AaveV3Snap
for (const eMode of Object.keys(diffResult.eModes)) {
const hasChanges = hasDiff(diffResult.eModes?.[eMode]);
if (hasChanges) {
content += `### EMode ${pre.eModes[eMode].eModeCategory}:${pre.eModes[eMode].label}\n\n`;
content += `### EMode: ${pre.eModes[eMode].label}(id: ${pre.eModes[eMode].eModeCategory})\n\n`;
content += renderEmodeDiff(
diff(pre.eModes[eMode] || {}, post.eModes[eMode] || {}) as any,
pre,
post,
);
content += '\n\n';
}
}
}
Expand Down
17 changes: 4 additions & 13 deletions src/reports/emode.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {describe, expect, it} from 'vitest';
import {diff} from './diff';
import {renderEmode, renderEmodeDiff} from './emode';
import {fetchRateStrategyImage} from './fetch-IR-strategy';
import {renderEmodeDiff} from './emode';

const EMODE_1 = {
eModeCategory: 1,
Expand All @@ -22,22 +21,14 @@ const EMODE_2 = {
};

describe('eMode', () => {
it('should properly render new strategy', () => {
const out = renderEmode(EMODE_1);
expect(out).eq(`| eMode.label | Stablecoins |
| eMode.ltv | 97 % |
| eMode.liquidationThreshold | 97.5 % |
| eMode.liquidationBonus | 1 % |
| eMode.priceSource | 0x0000000000000000000000000000000000000000 |
`);
});

it('should properly render strategy diff', () => {
const result = diff(EMODE_1, EMODE_2);
expect(renderEmodeDiff(result as any)).eq(`| eMode.label | Stablecoins | Stablecoins (altered) |
expect(renderEmodeDiff(result as any, {} as any, {} as any))
.eq(`| eMode.label | Stablecoins | Stablecoins (altered) |
| eMode.ltv | 97 % | 2.5 % |
| eMode.liquidationThreshold | 97.5 % | 5 % |
| eMode.liquidationBonus | 1 % | -90 % |
| eMode.priceSource (unchanged) | 0x0000000000000000000000000000000000000000 | 0x0000000000000000000000000000000000000000 |
`);
});
});
47 changes: 21 additions & 26 deletions src/reports/emode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ export function renderEModeValue<T extends keyof AaveV3Emode>(
return emode[key] === 0 ? '0 %' : `${((emode[key] as number) - 10000) / 100} %`;
if (key === 'borrowableBitmap' || key === 'collateralBitmap') {
const indexes = bitMapToIndexes(BigInt(emode[key]));
return indexes.map(
(i) =>
snapshot.reserves[
Object.keys(snapshot.reserves).find((key) => snapshot.reserves[key].id === i) as any
].symbol,
);
return indexes
.map(
(i) =>
snapshot.reserves[
Object.keys(snapshot.reserves).find((key) => snapshot.reserves[key].id === i) as any
].symbol,
)
.join(', ');
}
return emode[key];
}
Expand Down Expand Up @@ -50,17 +52,6 @@ function sortEmodeKeys(a: keyof AaveV3Emode, b: keyof AaveV3Emode) {

const OMIT_KEYS: (keyof AaveV3Emode)[] = ['eModeCategory'];

export function renderEmode(eMode: AaveV3Emode) {
let content = '';
(Object.keys(eMode) as (keyof AaveV3Emode)[])
.filter((key) => !OMIT_KEYS.includes(key))
.sort(sortEmodeKeys)
.map((key) => {
content += `| eMode.${key} | ${renderEModeValue(key, eMode)} |\n`;
});
return content;
}

export type EmodeDiff<A extends AaveV3Emode = AaveV3Emode> = {
[key in keyof AaveV3Emode]: A[key] & {
from: A[key] | null;
Expand All @@ -73,17 +64,21 @@ export function renderEmodeDiff(diff: EmodeDiff, pre: AaveV3Snapshot, post: Aave

(Object.keys(diff) as (keyof AaveV3Emode)[])
.filter((key) => !OMIT_KEYS.includes(key))
.filter((key) => diff[key].hasOwnProperty('from'))
.sort(sortEmodeKeys)
.map((key) => {
content += `| eMode.${key} | ${renderEModeValue(
key,
{
...diff,
[key]: diff[key].from,
},
pre,
)} | ${renderEModeValue(key, {...diff, [key]: diff[key].to}, post)} |\n`;
if (typeof diff[key] === 'object' && diff[key].hasOwnProperty('from'))
content += `| eMode.${key} | ${renderEModeValue(
key,
{
...diff,
[key]: diff[key].from,
},
pre,
)} | ${renderEModeValue(key, {...diff, [key]: diff[key].to}, post)} |\n`;
else {
const value = renderEModeValue(key, diff, pre);
content += `| eMode.${key} (unchanged) | ${value} | ${value} |\n`;
}
});

return content;
Expand Down
Loading