Skip to content
This repository has been archived by the owner on Nov 1, 2023. It is now read-only.

Commit

Permalink
feat: add eMode updates
Browse files Browse the repository at this point in the history
  • Loading branch information
sakulstra committed Sep 25, 2023
1 parent 18cab4f commit fabbafa
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 2 deletions.
81 changes: 81 additions & 0 deletions generator/features/eModesUpdates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import {CodeArtifact, DEPENDENCIES, FeatureModule, PoolIdentifier} from '../types';
import {NumberInputValues, addressInput, eModesSelect, percentInput, stringInput} from '../prompts';
import {Hex} from 'viem';

async function subCli(pool: PoolIdentifier) {
console.log(`Fetching information for PriceFeeds on ${pool}`);
const eModeCategories = await eModesSelect({
message: 'Select the eModes you want to amend',
pool,
});
const answers: EmodeUpdates = [];
for (const eModeCategory of eModeCategories) {
console.log(`collecting info for ${eModeCategory}`);
answers.push({
eModeCategory,
ltv: await percentInput({
message: 'ltv',
}),
liqThreshold: await percentInput({
message: 'liqThreshold',
}),
liqBonus: await percentInput({
message: 'liqBonus',
}),
priceSource: await addressInput({
message: 'Price Source',
}),
label: await stringInput({message: 'label'}),
});
}
return answers;
}

type EmodeUpdate = {
eModeCategory: string;
ltv: NumberInputValues;
liqThreshold: NumberInputValues;
liqBonus: NumberInputValues;
priceSource: Hex;
label: string;
};

type EmodeUpdates = EmodeUpdate[];

export const eModeUpdates: FeatureModule<EmodeUpdates> = {
value: 'eModeCategoriesUpdates (altering eModes)',
async cli(opt, pool) {
const response: EmodeUpdates = await subCli(pool);
return response;
},
build(opt, pool, cfg) {
const response: CodeArtifact = {
code: {
dependencies: [DEPENDENCIES.Assets, DEPENDENCIES.Engine],
fn: [
`function eModeCategoriesUpdates() public pure override returns (IEngine.EModeCategoryUpdate[] memory) {
IEngine.EModeCategoryUpdate[] memory eModeUpdates = new IEngine.EModeCategoryUpdate[](${
cfg.length
});
${cfg
.map(
(cfg, ix) => `eModeUpdates[${ix}] = IEngine.EModeCategoryUpdate({
eModeCategory: ${cfg.eModeCategory},
ltv: ${cfg.ltv},
liqThreshold: ${cfg.liqThreshold},
liqBonus: ${cfg.liqBonus},
priceSource: ${cfg.priceSource},
label: ${cfg.label}
});`
)
.join('\n')}
return eModeUpdates;
}`,
],
},
};
return response;
},
};
2 changes: 1 addition & 1 deletion generator/features/priceFeedsUpdates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type PriceFeedUpdate = {

type PriceFeedUpdates = PriceFeedUpdate[];

export const capsUpdates: FeatureModule<PriceFeedUpdates> = {
export const priceFeedsUpdates: FeatureModule<PriceFeedUpdates> = {
value: 'PriceFeedsUpdates (replacing priceFeeds)',
async cli(opt, pool) {
const response: PriceFeedUpdates = await subCli(pool);
Expand Down
6 changes: 5 additions & 1 deletion generator/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import {generateScript} from './templates/script.template';
import {generateAIP} from './templates/aip.template';
import {collateralsUpdates} from './features/collateralsUpdates';
import {borrowsUpdates} from './features/borrowsUpdates';
import {eModeUpdates} from './features/eModesUpdates';
import {priceFeedsUpdates} from './features/priceFeedsUpdates';

const prettierSolCfg = await prettier.resolveConfig('foo.sol');
const prettierMDCfg = await prettier.resolveConfig('foo.md');
Expand Down Expand Up @@ -108,6 +110,8 @@ const FEATURE_MODULES_V3 = [
collateralsUpdates,
borrowsUpdates,
flashBorrower,
priceFeedsUpdates,
eModeUpdates,
{
value: 'Something different supported by config engine(but not the generator, yet)',
cli: async (opt, pool) => {
Expand All @@ -133,7 +137,7 @@ for (const pool of options.pools) {
});
let artifacts: CodeArtifact[] = [];
for (const feature of features) {
const module: FeatureModule = v2
const module: FeatureModule<any> = v2
? FEATURE_MODULES_V2.find((m) => m.value === feature)!
: FEATURE_MODULES_V3.find((m) => m.value === feature)!;
if (module.cli) {
Expand Down
21 changes: 21 additions & 0 deletions generator/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export async function addressInput({message, disableKeepCurrent}: GenericPrompt)
const value = await input({
message,
validate: (value) => (isAddress(value) ? true : 'Must be a valid address'),
...(disableKeepCurrent ? {} : {default: ENGINE_FLAGS.KEEP_CURRENT_ADDRESS}),
});
return getAddress(value);
}
Expand Down Expand Up @@ -124,3 +125,23 @@ export async function eModeSelect({message, disableKeepCurrent, pool}: EModeSele
});
return translateJsNumberToSol(eMode);
}

export async function eModesSelect({message, pool}: EModeSelectPrompt) {
const eModes = getEModes(pool as any);
const eMode = await checkbox({
message,
choices: [
...Object.keys(eModes)
.map((eMode) => ({name: eMode, value: eModes[eMode]}))
.filter((e) => e.value != 0),
],
});
return eMode.map((eMode) => translateJsNumberToSol(eMode));
}

export async function stringInput({message, disableKeepCurrent}: GenericPrompt) {
return input({
message,
...(disableKeepCurrent ? {} : {default: ENGINE_FLAGS.KEEP_CURRENT_STRING}),
});
}

0 comments on commit fabbafa

Please sign in to comment.