@@ -2200,15 +2200,26 @@ std::vector<CAuctionBatch> CollectAuctionBatches(const CCollateralLoans& collLoa
2200
2200
maxLoansValue -= loan.nValue ;
2201
2201
maxCollateralsValue -= collateralChunkValue;
2202
2202
}
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 ;
2211
2221
}
2222
+ ++it;
2212
2223
}
2213
2224
}
2214
2225
return batches;
@@ -3089,7 +3100,7 @@ void CChainState::ProcessLoanEvents(const CBlockIndex* pindex, CCustomCSView& ca
3089
3100
// the interest value and move it to the totals, removing it from the
3090
3101
// vault, while also stopping the vault from accumulating interest
3091
3102
// further. Note, however, it's added back so that it's accurate
3092
- // for auction calculations.
3103
+ // for auction calculations.
3093
3104
CBalances totalInterest;
3094
3105
for (auto & loan : loanTokens->balances ) {
3095
3106
auto tokenId = loan.first ;
@@ -3107,7 +3118,7 @@ void CChainState::ProcessLoanEvents(const CBlockIndex* pindex, CCustomCSView& ca
3107
3118
// Putting this back in now for auction calculations.
3108
3119
loan.second += subInterest;
3109
3120
}
3110
-
3121
+
3111
3122
// Remove the collaterals out of the vault.
3112
3123
// (Prep to get the auction batches instead)
3113
3124
for (const auto & col : collaterals.balances ) {
@@ -3131,8 +3142,7 @@ void CChainState::ProcessLoanEvents(const CBlockIndex* pindex, CCustomCSView& ca
3131
3142
cache.StoreAuctionBatch (vaultId, i, batch);
3132
3143
}
3133
3144
3134
- // All done. Ready to save the overall auction.
3135
-
3145
+ // All done. Ready to save the overall auction.
3136
3146
cache.StoreAuction (vaultId, CAuctionData{
3137
3147
uint32_t (batches.size ()),
3138
3148
pindex->nHeight + chainparams.GetConsensus ().blocksCollateralAuction (),
@@ -3150,7 +3160,7 @@ void CChainState::ProcessLoanEvents(const CBlockIndex* pindex, CCustomCSView& ca
3150
3160
}
3151
3161
auto vault = view.GetVault (vaultId);
3152
3162
assert (vault);
3153
-
3163
+
3154
3164
for (uint32_t i = 0 ; i < data.batchCount ; i++) {
3155
3165
auto batch = view.GetAuctionBatch (vaultId, i);
3156
3166
assert (batch);
0 commit comments