Skip to content

Commit

Permalink
improve simulation logging
Browse files Browse the repository at this point in the history
  • Loading branch information
valiafetisov committed Oct 21, 2022
1 parent b7736c9 commit 162de7c
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 45 deletions.
1 change: 0 additions & 1 deletion core/helpers/hardhat/erc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { overwriteUintMappingInAddress, runBalanceSlotDiscoveryLoopForERC20Token
export const determineBalanceSlot = async (
collateralType: CollateralType
): Promise<[string, 'vyper' | 'solidity'] | [null, null]> => {
console.info('Determining balance slot...');
const collateralConfig = getCollateralConfigByType(collateralType);
const tokenContractAddress = await getContractAddressByName(TEST_NETWORK, collateralConfig.symbol);
try {
Expand Down
56 changes: 15 additions & 41 deletions core/simulations/configs/onboardNewCollateral.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,18 @@
import hre from 'hardhat';
import BigNumber from '../../src/bignumber';
import { warpTime, resetNetworkAndSetupWallet } from '../../helpers/hardhat/network';
import { addDaiToBalance } from '../../helpers/hardhat/balance';
import { Simulation } from '../types';
import prompts from 'prompts';
import { getAllCollateralTypes, getCollateralConfigByType } from '../../src/constants/COLLATERALS';
import { getAllCollateralTypes } from '../../src/constants/COLLATERALS';
import { collectStabilityFees, fetchVault, liquidateVault } from '../../src/vaults';
import { TEST_NETWORK } from '../../helpers/constants';
import createVaultWithCollateral, {
calculateMinCollateralAmountToOpenVault,
} from '../helpers/createVaultWithCollateral';
import deploySpell from '../helpers/deploySpell';
import deploySpell, { getAllSpellNames } from '../helpers/deploySpell';
import executeSpell from '../helpers/executeSpell';
import { getContractAddressByName } from '../../src/contracts';
import { getCurrentOraclePrice } from '../../src/oracles';
import getProvider from '../../src/provider';
import { getCurrentOraclePriceByCollateralType } from '../../src/oracles';
import { overwriteCurrentOraclePrice } from '../../helpers/hardhat/overwrites';

const promptCollateralSelection = async () => {
const { collateralType } = await prompts([
{
type: 'select',
name: 'collateralType',
message: 'Select the collateral symbol to add to the VAT.',
choices: getAllCollateralTypes().map(collateral => ({
title: collateral,
value: collateral,
})),
},
]);
return collateralType;
};
import promptToSelectOneOption from '../helpers/promptToSelectOneOption';

const simulation: Simulation = {
title: 'Onboard new collateral',
Expand All @@ -45,7 +27,11 @@ const simulation: Simulation = {
{
title: 'Deploy the spell',
entry: async () => {
const spellAddress = await deploySpell(TEST_NETWORK, 'RETH-A onboarding');
const spellName = await promptToSelectOneOption(
'Select the spell you want to deploy',
getAllSpellNames()
);
const spellAddress = await deploySpell(TEST_NETWORK, spellName);
return {
spellAddress,
};
Expand All @@ -54,32 +40,20 @@ const simulation: Simulation = {
{
title: 'Execute the spell',
entry: async context => {
const printValueChangedByTheSpell = async function () {
// MCD_PSM_GUSD_A tout uint256
const address = await getContractAddressByName(TEST_NETWORK, 'MCD_PSM_GUSD_A');
const contract = await hre.ethers.getContractAt(
['function tout() external view returns (uint256)'],
address
);
const value = await contract.tout();
console.info(`tout`, value);
};
await printValueChangedByTheSpell();
await executeSpell(context.spellAddress);
await printValueChangedByTheSpell();
},
},
{
title: 'Create new auction',
entry: async () => {
const collateralType = await promptCollateralSelection();
const collateralType = await promptToSelectOneOption(
'Select the collateral symbol to add to the VAT',
getAllCollateralTypes()
);
// overwrite oracle price
await overwriteCurrentOraclePrice(TEST_NETWORK, collateralType, new BigNumber(1000));
const collateralConfig = getCollateralConfigByType(collateralType);
const provider = await getProvider(TEST_NETWORK);
const oracleAddress = await getContractAddressByName(TEST_NETWORK, 'PIP_RETH');
const oraclePrice = await getCurrentOraclePrice(collateralConfig.oracle, provider, oracleAddress);
console.info('new oracle price', oraclePrice.toFixed());
const oraclePrice = await getCurrentOraclePriceByCollateralType(TEST_NETWORK, collateralType);
console.info(`New ${collateralType} oracle price is ${oraclePrice.toFixed()} DAI`);
// create and liquidate vault
const collateralOwned = await calculateMinCollateralAmountToOpenVault(collateralType);
const vaultId = await createVaultWithCollateral(collateralType, collateralOwned);
Expand Down
8 changes: 6 additions & 2 deletions core/simulations/helpers/deploySpell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ import { ethers } from 'ethers';
import getSigner from '../../src/signer';
import compiledSpells from '../../bytecode/compiledSpells.json';

export const getAllSpellNames = function (): string[] {
return Object.keys(compiledSpells).sort();
};

const deploySpell = async function (network: string, name: string): Promise<string> {
console.info(`deploying spell ${name}`);
console.info(`Deploying spell "${name}"`);

if (!(name in compiledSpells)) {
throw new Error(`spel "${name}" not found in compiled spells`);
Expand All @@ -14,7 +18,7 @@ const deploySpell = async function (network: string, name: string): Promise<stri
const factory = new ethers.ContractFactory([], bytecode, signer);
const contract = await factory.deploy();
await contract.deployTransaction.wait();
console.info(`spell has been deployed at ${contract.address}`);
console.info(`Spell has been deployed at ${contract.address}`);
return contract.address;
};

Expand Down
21 changes: 21 additions & 0 deletions core/simulations/helpers/promptToSelectOneOption.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import prompts from 'prompts';

const promptToSelectOneOption = async (title: string, values: string[]) => {
if (values.length <= 1) {
return values[0];
}
const { selectedValue } = await prompts([
{
type: 'select',
name: 'selectedValue',
message: title,
choices: values.map(value => ({
title: value,
value: value,
})),
},
]);
return selectedValue;
};

export default promptToSelectOneOption;
8 changes: 8 additions & 0 deletions core/src/oracles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ const getNextOraclePriceChange = async (
return nextPriceChange;
};

export const getCurrentOraclePriceByCollateralType = async function (network: string, collateralType: string) {
const collateralConfig = getCollateralConfigByType(collateralType);
const provider = await getProvider(network);
const oracleAddress = await getOracleAddressByCollateralType(network, collateralType);
const oraclePrice = await getCurrentOraclePrice(collateralConfig.oracle, provider, oracleAddress);
return oraclePrice;
};

const _getOsmPrices = async (
network: string,
oracleAddress: string,
Expand Down
2 changes: 1 addition & 1 deletion core/src/tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const DEFAULT_NOTIFICATION_DURATION = 3;
const NUMBER_OF_BLOCKS_TO_CONFIRM = 5;

const defaultNotifier = function (_: string, messageContent: MessageContent) {
console.info(`transaction ${messageContent.key}: ${messageContent.content}`);
console.info(messageContent.content);
};

const trackTransaction = async function (
Expand Down

0 comments on commit 162de7c

Please sign in to comment.