Skip to content

Commit

Permalink
Merge branch 'master' into f/auction_functionnal_tests
Browse files Browse the repository at this point in the history
  • Loading branch information
prasannavl authored Nov 2, 2021
2 parents a07a5e6 + cec60e4 commit 4283aeb
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
36 changes: 23 additions & 13 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2200,15 +2200,26 @@ std::vector<CAuctionBatch> CollectAuctionBatches(const CCollateralLoans& collLoa
maxLoansValue -= loan.nValue;
maxCollateralsValue -= collateralChunkValue;
}
// return res collateral to last batch
for (const auto& collateral : maxCollBalances) {
if (collateral.second) {
auto it = std::find_if(batches.rbegin(), batches.rend(), [&](const CAuctionBatch& batch) {
return batch.collaterals.balances.count(collateral.first) > 0;
});
if (it != batches.rend()) {
it->collaterals.Add({collateral.first, collateral.second});
// return precision loss balanced
for (auto& collateral : maxCollBalances) {
auto it = batches.begin();
auto lastValue = collateral.second;
while (collateral.second > 0) {
if (it == batches.end()) {
it = batches.begin();
if (lastValue == collateral.second) {
// we fail to update any batch
// extreme small collateral going to first batch
it->collaterals.Add({collateral.first, collateral.second});
break;
}
lastValue = collateral.second;
}
if (it->collaterals.balances.count(collateral.first) > 0) {
it->collaterals.Add({collateral.first, 1});
--collateral.second;
}
++it;
}
}
return batches;
Expand Down Expand Up @@ -3089,7 +3100,7 @@ void CChainState::ProcessLoanEvents(const CBlockIndex* pindex, CCustomCSView& ca
// the interest value and move it to the totals, removing it from the
// vault, while also stopping the vault from accumulating interest
// further. Note, however, it's added back so that it's accurate
// for auction calculations.
// for auction calculations.
CBalances totalInterest;
for (auto& loan : loanTokens->balances) {
auto tokenId = loan.first;
Expand All @@ -3107,7 +3118,7 @@ void CChainState::ProcessLoanEvents(const CBlockIndex* pindex, CCustomCSView& ca
// Putting this back in now for auction calculations.
loan.second += subInterest;
}

// Remove the collaterals out of the vault.
// (Prep to get the auction batches instead)
for (const auto& col : collaterals.balances) {
Expand All @@ -3131,8 +3142,7 @@ void CChainState::ProcessLoanEvents(const CBlockIndex* pindex, CCustomCSView& ca
cache.StoreAuctionBatch(vaultId, i, batch);
}

// All done. Ready to save the overall auction.

// All done. Ready to save the overall auction.
cache.StoreAuction(vaultId, CAuctionData{
uint32_t(batches.size()),
pindex->nHeight + chainparams.GetConsensus().blocksCollateralAuction(),
Expand All @@ -3150,7 +3160,7 @@ void CChainState::ProcessLoanEvents(const CBlockIndex* pindex, CCustomCSView& ca
}
auto vault = view.GetVault(vaultId);
assert(vault);

for (uint32_t i = 0; i < data.batchCount; i++) {
auto batch = view.GetAuctionBatch(vaultId, i);
assert(batch);
Expand Down
14 changes: 13 additions & 1 deletion test/functional/feature_loan_auctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,20 @@ def run_test(self):

batches = vault6['batches']
assert_equal(len(batches), 9)

collateralDFI = Decimal('0')
collateralBTC = Decimal('0')
for batch in batches:
assert_equal(len(batch['collaterals']), 2)
collaterals = batch['collaterals']
assert_equal(len(collaterals), 2)
DFI = Decimal(collaterals[0].replace('@DFI', ''))
BTC = Decimal(collaterals[1].replace('@BTC', ''))
assert(DFI * Decimal('220') + BTC * Decimal('220') < 10000)
collateralDFI += DFI
collateralBTC += BTC

assert_equal(collateralDFI, Decimal('200.00000000'))
assert_equal(collateralBTC, Decimal('200.00000000'))

# Case 7 With max possible oracle deviation. Loantoken value 100 -> 129 && collateral value 100 -> 71
# Loan value should end up greater than collateral value
Expand Down

0 comments on commit 4283aeb

Please sign in to comment.