Skip to content

Commit 6aa8178

Browse files
authored
chore: Improve simulations (#616)
1 parent 037502d commit 6aa8178

File tree

3 files changed

+60
-42
lines changed

3 files changed

+60
-42
lines changed

bot/src/keepers/collateral.ts

+19-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { AuctionInitialInfo } from 'auctions-core/src/types';
22
import { bidWithCallee, enrichAuction } from 'auctions-core/src/auctions';
33
import getSigner from 'auctions-core/src/signer';
4+
import { fetchVATbalanceDAI, fetchBalanceDAI } from 'auctions-core/src/wallet';
45
import { KEEPER_COLLATERAL_MINIMUM_NET_PROFIT_DAI } from '../variables';
56
import { checkAndAuthorizeCollateral, checkAndAuthorizeWallet } from '../authorisation';
67
import { setupWallet } from '../signer';
@@ -49,18 +50,22 @@ const checkAndParticipateIfPossible = async function (network: string, auction:
4950
return;
5051
}
5152

52-
// check auction's profit
53+
// check auction's gross profit
5354
if (!auctionTransaction.transactionGrossProfit || auctionTransaction.transactionGrossProfit.isLessThan(0)) {
5455
if (auctionTransaction.transactionGrossProfit) {
55-
const profit = `${auctionTransaction.transactionGrossProfit.toFixed(0)} DAI`;
56+
const grossProfit = `${auctionTransaction.transactionGrossProfit.toFixed(0)} DAI`;
5657
console.info(
57-
`collateral keeper: auction "${auction.id}" is not yet profitable (current profit: ${profit})`
58+
`collateral keeper: auction "${auction.id}" is not yet executable (current gross profit: ${grossProfit})`
5859
);
5960
} else {
6061
console.info(`collateral keeper: auction "${auction.id}" is not tradable`);
6162
}
6263
return;
6364
}
65+
const grossProfit = `${auctionTransaction.transactionGrossProfit.toFixed(0)} DAI`;
66+
console.info(
67+
`collateral keeper: auction "${auction.id}" gross profit is ${grossProfit}, moving on to check net profit`
68+
);
6469

6570
// check auction's net profit – profit without transaction fees
6671
if (
@@ -70,7 +75,7 @@ const checkAndParticipateIfPossible = async function (network: string, auction:
7075
console.info(
7176
`collateral keeper: auction "${
7277
auction.id
73-
}" net profit is smaller than min profit (${auctionTransaction.transactionNetProfit.toFixed(
78+
}" net profit is smaller than min net profit (${auctionTransaction.transactionNetProfit.toFixed(
7479
0
7580
)} < ${KEEPER_COLLATERAL_MINIMUM_NET_PROFIT_DAI})`
7681
);
@@ -107,6 +112,10 @@ const checkAndParticipateIfPossible = async function (network: string, auction:
107112
return;
108113
}
109114

115+
// save previous balances
116+
const preVatBalanceDai = await fetchVATbalanceDAI(network, walletAddress);
117+
const preErcBalanceDai = await fetchBalanceDAI(network, walletAddress);
118+
110119
// bid on the Auction
111120
console.info(`collateral keeper: auction "${auctionTransaction.id}": attempting swap execution`);
112121
const bidHash = await bidWithCallee(
@@ -118,6 +127,12 @@ const checkAndParticipateIfPossible = async function (network: string, auction:
118127
console.info(
119128
`collateral keeper: auction "${auctionTransaction.id}" was succesfully executed via "${bidHash}" transaction`
120129
);
130+
131+
// display profit
132+
const postVatBalanceDai = await fetchVATbalanceDAI(network, walletAddress);
133+
const postErcBalanceDai = await fetchBalanceDAI(network, walletAddress);
134+
console.info(`DAI VAT profit from the transaction: ${postVatBalanceDai.minus(preVatBalanceDai).toFixed()}`);
135+
console.info(`DAI ERC profit from the transaction: ${postErcBalanceDai.minus(preErcBalanceDai).toFixed()}`);
121136
};
122137

123138
const participateInAuction = async function (network: string, auction: AuctionInitialInfo) {

core/simulations/configs/vaultLiquidation.ts

+40-35
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ import createVaultWithCollateral, {
99
} from '../helpers/createVaultWithCollateral';
1010
import promptToSelectOneOption from '../helpers/promptToSelectOneOption';
1111
import promptToGetBlockNumber from '../helpers/promptToGetBlockNumber';
12+
import getProvider from '../../src/provider';
1213

13-
import { fetchMaximumAuctionDurationInSeconds } from '../../src/fetch';
14+
import fetchAuctionsByCollateralType, { fetchMaximumAuctionDurationInSeconds } from '../../src/fetch';
1415
import { getAllCollateralTypes } from '../../src/constants/COLLATERALS';
16+
import { setCollateralDebtCeilingToGlobal } from '../../helpers/hardhat/contractParametrization';
1517

1618
const TWO_YEARS_IN_MINUTES = 60 * 24 * 30 * 12 * 2;
1719

@@ -23,8 +25,10 @@ const simulation: Simulation = {
2325
entry: async () => {
2426
const number = await promptToGetBlockNumber();
2527
await resetNetworkAndSetupWallet(number);
28+
await addDaiToBalance();
29+
await addMkrToBalance();
2630
const collateralType = await promptToSelectOneOption(
27-
'Select the collateral symbol to add to the VAT.',
31+
'Select the collateral type',
2832
getAllCollateralTypes()
2933
);
3034
return {
@@ -33,34 +37,29 @@ const simulation: Simulation = {
3337
},
3438
},
3539
{
36-
title: 'Create the vault',
40+
title: 'Create underwater vault',
3741
entry: async context => {
3842
await adjustLimitsAndRates(context.collateralType);
3943
const collateralOwned = await calculateMinCollateralAmountToOpenVault(context.collateralType);
4044
console.info(`Minimum collateral amount to open vault: ${collateralOwned.toFixed()}`);
41-
const latestVaultId = await createVaultWithCollateral(context.collateralType, collateralOwned);
45+
await setCollateralDebtCeilingToGlobal(context.collateralType);
46+
const latestVaultId = await createVaultWithCollateral(
47+
context.collateralType,
48+
collateralOwned.multipliedBy(1)
49+
);
4250
console.info(`Created Vault id: ${latestVaultId}`);
43-
return { ...context, latestVaultId };
44-
},
45-
},
46-
{
47-
title: 'Skip time',
48-
entry: async context => {
51+
52+
console.info(`Skipping ${TWO_YEARS_IN_MINUTES} minutes...`);
4953
await warpTime(TWO_YEARS_IN_MINUTES, 60);
50-
return context;
51-
},
52-
},
53-
{
54-
title: 'Collect stability fees',
55-
entry: async context => {
56-
const collateralType = context.collateralType;
57-
const latestVaultId = context.latestVaultId;
54+
55+
console.info(`Collecting stability fees...`);
5856
const vaultBefore = await fetchVault(TEST_NETWORK, latestVaultId);
59-
console.info(`stability fees before ${vaultBefore.stabilityFeeRate}`);
60-
await collectStabilityFees(TEST_NETWORK, collateralType);
57+
console.info(`Stability fee before ${vaultBefore.stabilityFeeRate}`);
58+
await collectStabilityFees(TEST_NETWORK, context.collateralType);
6159
const vaultAfter = await fetchVault(TEST_NETWORK, latestVaultId);
62-
console.info(`stability fees after ${vaultAfter.stabilityFeeRate}`);
63-
return context;
60+
console.info(`Stability fee after ${vaultAfter.stabilityFeeRate}`);
61+
62+
return { ...context, latestVaultId };
6463
},
6564
},
6665
{
@@ -79,20 +78,26 @@ const simulation: Simulation = {
7978
TEST_NETWORK,
8079
context.collateralType
8180
);
82-
const warpSeconds = Math.floor(auctionLifetime / 2);
83-
if (!warpSeconds) {
84-
throw new Error('Auction lifetime is too short to warp time.');
85-
}
86-
console.info(`Skipping time: ${warpSeconds} seconds`);
81+
const INITIAL_WARP_PARTS = 1 / 13;
82+
const warpSeconds = Math.floor(auctionLifetime * INITIAL_WARP_PARTS);
83+
console.info(`Initial warp of ${INITIAL_WARP_PARTS} of an auction time: ${warpSeconds} seconds`);
8784
await warpTime(warpSeconds, 1);
88-
return context;
89-
},
90-
},
91-
{
92-
title: 'Add DAI and MKR to the wallet',
93-
entry: async () => {
94-
await addDaiToBalance();
95-
await addMkrToBalance();
85+
const provider = await getProvider(TEST_NETWORK);
86+
const STEP_SECONDS = 30;
87+
while (true) {
88+
const initialAuctions = await fetchAuctionsByCollateralType(TEST_NETWORK, context.collateralType);
89+
if (!initialAuctions[0] || !initialAuctions[0].isActive) {
90+
console.info('No active auctions are found, exiting the "evm_mine" loop');
91+
break;
92+
}
93+
console.info(`Gradually skipping time, one block every ${STEP_SECONDS} seconds`);
94+
try {
95+
await provider.send('evm_mine', []);
96+
await new Promise(resolve => setTimeout(resolve, STEP_SECONDS * 1000));
97+
} catch (error) {
98+
console.error('evm_mine failed with', error);
99+
}
100+
}
96101
},
97102
},
98103
],

core/src/auctions.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -82,20 +82,18 @@ export const enrichMarketDataRecordsWithValues = async function (
8282
exchangeFees: ExchangeFees,
8383
amount: BigNumber = new BigNumber(1)
8484
): Promise<Record<string, MarketData>> {
85-
const exchangeFeePerUnitDAI = exchangeFees.exchangeFeeDAI.dividedBy(amount);
8685
let enrichedMarketDataRecords = {};
8786
for (const marketId in marketDataRecords) {
8887
let marketData = marketDataRecords[marketId];
8988
// enrich with values dependent on marketUnitPrice
90-
let marketUnitPrice = marketData.marketUnitPrice;
89+
const marketUnitPrice = marketData.marketUnitPrice;
9190
if (marketUnitPrice.isNaN()) {
9291
enrichedMarketDataRecords = {
9392
...enrichedMarketDataRecords,
9493
[marketId]: { ...marketData },
9594
};
9695
continue;
9796
}
98-
marketUnitPrice = marketUnitPrice.plus(exchangeFeePerUnitDAI);
9997
marketData = {
10098
...marketData,
10199
marketUnitPrice,

0 commit comments

Comments
 (0)