Skip to content

Commit 4283aeb

Browse files
authored
Merge branch 'master' into f/auction_functionnal_tests
2 parents a07a5e6 + cec60e4 commit 4283aeb

File tree

2 files changed

+36
-14
lines changed

2 files changed

+36
-14
lines changed

src/validation.cpp

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2200,15 +2200,26 @@ std::vector<CAuctionBatch> CollectAuctionBatches(const CCollateralLoans& collLoa
22002200
maxLoansValue -= loan.nValue;
22012201
maxCollateralsValue -= collateralChunkValue;
22022202
}
2203-
// return res collateral to last batch
2204-
for (const auto& collateral : maxCollBalances) {
2205-
if (collateral.second) {
2206-
auto it = std::find_if(batches.rbegin(), batches.rend(), [&](const CAuctionBatch& batch) {
2207-
return batch.collaterals.balances.count(collateral.first) > 0;
2208-
});
2209-
if (it != batches.rend()) {
2210-
it->collaterals.Add({collateral.first, collateral.second});
2203+
// return precision loss balanced
2204+
for (auto& collateral : maxCollBalances) {
2205+
auto it = batches.begin();
2206+
auto lastValue = collateral.second;
2207+
while (collateral.second > 0) {
2208+
if (it == batches.end()) {
2209+
it = batches.begin();
2210+
if (lastValue == collateral.second) {
2211+
// we fail to update any batch
2212+
// extreme small collateral going to first batch
2213+
it->collaterals.Add({collateral.first, collateral.second});
2214+
break;
2215+
}
2216+
lastValue = collateral.second;
2217+
}
2218+
if (it->collaterals.balances.count(collateral.first) > 0) {
2219+
it->collaterals.Add({collateral.first, 1});
2220+
--collateral.second;
22112221
}
2222+
++it;
22122223
}
22132224
}
22142225
return batches;
@@ -3089,7 +3100,7 @@ void CChainState::ProcessLoanEvents(const CBlockIndex* pindex, CCustomCSView& ca
30893100
// the interest value and move it to the totals, removing it from the
30903101
// vault, while also stopping the vault from accumulating interest
30913102
// further. Note, however, it's added back so that it's accurate
3092-
// for auction calculations.
3103+
// for auction calculations.
30933104
CBalances totalInterest;
30943105
for (auto& loan : loanTokens->balances) {
30953106
auto tokenId = loan.first;
@@ -3107,7 +3118,7 @@ void CChainState::ProcessLoanEvents(const CBlockIndex* pindex, CCustomCSView& ca
31073118
// Putting this back in now for auction calculations.
31083119
loan.second += subInterest;
31093120
}
3110-
3121+
31113122
// Remove the collaterals out of the vault.
31123123
// (Prep to get the auction batches instead)
31133124
for (const auto& col : collaterals.balances) {
@@ -3131,8 +3142,7 @@ void CChainState::ProcessLoanEvents(const CBlockIndex* pindex, CCustomCSView& ca
31313142
cache.StoreAuctionBatch(vaultId, i, batch);
31323143
}
31333144

3134-
// All done. Ready to save the overall auction.
3135-
3145+
// All done. Ready to save the overall auction.
31363146
cache.StoreAuction(vaultId, CAuctionData{
31373147
uint32_t(batches.size()),
31383148
pindex->nHeight + chainparams.GetConsensus().blocksCollateralAuction(),
@@ -3150,7 +3160,7 @@ void CChainState::ProcessLoanEvents(const CBlockIndex* pindex, CCustomCSView& ca
31503160
}
31513161
auto vault = view.GetVault(vaultId);
31523162
assert(vault);
3153-
3163+
31543164
for (uint32_t i = 0; i < data.batchCount; i++) {
31553165
auto batch = view.GetAuctionBatch(vaultId, i);
31563166
assert(batch);

test/functional/feature_loan_auctions.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,8 +430,20 @@ def run_test(self):
430430

431431
batches = vault6['batches']
432432
assert_equal(len(batches), 9)
433+
434+
collateralDFI = Decimal('0')
435+
collateralBTC = Decimal('0')
433436
for batch in batches:
434-
assert_equal(len(batch['collaterals']), 2)
437+
collaterals = batch['collaterals']
438+
assert_equal(len(collaterals), 2)
439+
DFI = Decimal(collaterals[0].replace('@DFI', ''))
440+
BTC = Decimal(collaterals[1].replace('@BTC', ''))
441+
assert(DFI * Decimal('220') + BTC * Decimal('220') < 10000)
442+
collateralDFI += DFI
443+
collateralBTC += BTC
444+
445+
assert_equal(collateralDFI, Decimal('200.00000000'))
446+
assert_equal(collateralBTC, Decimal('200.00000000'))
435447

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

0 commit comments

Comments
 (0)