diff --git a/src/logging.cpp b/src/logging.cpp index 367f898151..529af630a3 100644 --- a/src/logging.cpp +++ b/src/logging.cpp @@ -155,6 +155,7 @@ const CLogCategoryDesc LogCategories[] = {BCLog::ORACLE, "oracle"}, {BCLog::LOAN, "loan"}, {BCLog::ACCOUNTCHANGE, "accountchange"}, + {BCLog::FUTURESWAP, "futureswap"}, {BCLog::ALL, "1"}, {BCLog::ALL, "all"}, }; diff --git a/src/logging.h b/src/logging.h index c1034a1f20..7125f829a1 100644 --- a/src/logging.h +++ b/src/logging.h @@ -61,6 +61,7 @@ namespace BCLog { ORACLE = (1 << 24), LOAN = (1 << 25), ACCOUNTCHANGE = (1 << 26), + FUTURESWAP = (1 << 27), ALL = ~(uint32_t)0, }; diff --git a/src/masternodes/accounts.cpp b/src/masternodes/accounts.cpp index d7aae0c96b..6d9f62c879 100644 --- a/src/masternodes/accounts.cpp +++ b/src/masternodes/accounts.cpp @@ -101,7 +101,7 @@ uint32_t CAccountsView::GetBalancesHeight(CScript const & owner) Res CAccountsView::StoreFuturesUserValues(const CFuturesUserKey& key, const CFuturesUserValue& futures) { - if (!WriteBy(key, futures)) { + if (!WriteBy(key, futures)) { return Res::Err("Failed to store futures"); } @@ -110,12 +110,12 @@ Res CAccountsView::StoreFuturesUserValues(const CFuturesUserKey& key, const CFut void CAccountsView::ForEachFuturesUserValues(std::function callback, const CFuturesUserKey& start) { - ForEach(callback, start); + ForEach(callback, start); } Res CAccountsView::EraseFuturesUserValues(const CFuturesUserKey& key) { - if (!EraseBy(key)) { + if (!EraseBy(key)) { return Res::Err("Failed to erase futures"); } @@ -125,10 +125,24 @@ Res CAccountsView::EraseFuturesUserValues(const CFuturesUserKey& key) boost::optional CAccountsView::GetMostRecentFuturesHeight() { const CFuturesUserKey key{std::numeric_limits::max(), {}, std::numeric_limits::max()}; - auto it = LowerBound(key); + auto it = LowerBound(key); if (it.Valid()) { return it.Key().height; } return {}; } + +Res CAccountsView::StoreFuturesDestValues(const CFuturesUserKey& key, const CTokenAmount& destination) +{ + if (!WriteBy(key, destination)) { + return Res::Err("Failed to store futures destination"); + } + + return Res::Ok(); +} + +void CAccountsView::ForEachFuturesDestValues(std::function callback, const CFuturesUserKey& start) +{ + ForEach(callback, start); +} diff --git a/src/masternodes/accounts.h b/src/masternodes/accounts.h index 6de6e84625..655019f8fb 100644 --- a/src/masternodes/accounts.h +++ b/src/masternodes/accounts.h @@ -70,14 +70,17 @@ class CAccountsView : public virtual CStorageView Res UpdateBalancesHeight(CScript const & owner, uint32_t height); Res StoreFuturesUserValues(const CFuturesUserKey& key, const CFuturesUserValue& futures); + Res StoreFuturesDestValues(const CFuturesUserKey& key, const CTokenAmount& destination); Res EraseFuturesUserValues(const CFuturesUserKey& key); boost::optional GetMostRecentFuturesHeight(); void ForEachFuturesUserValues(std::function callback, const CFuturesUserKey& start = {}); + void ForEachFuturesDestValues(std::function callback, const CFuturesUserKey& start = {}); // tags struct ByBalanceKey { static constexpr uint8_t prefix() { return 'a'; } }; struct ByHeightKey { static constexpr uint8_t prefix() { return 'b'; } }; - struct ByFuturesKey { static constexpr uint8_t prefix() { return 'J'; } }; + struct ByFuturesSourceKey { static constexpr uint8_t prefix() { return 'J'; } }; + struct ByFuturesDestKey { static constexpr uint8_t prefix() { return 's'; } }; private: Res SetBalance(CScript const & owner, CTokenAmount amount); diff --git a/src/masternodes/masternodes.h b/src/masternodes/masternodes.h index f282a70fd4..b352d52913 100644 --- a/src/masternodes/masternodes.h +++ b/src/masternodes/masternodes.h @@ -363,7 +363,7 @@ class CCustomCSView CFoundationsDebtView :: Debt, CAnchorRewardsView :: BtcTx, CTokensView :: ID, Symbol, CreationTx, LastDctId, - CAccountsView :: ByBalanceKey, ByHeightKey, ByFuturesKey, + CAccountsView :: ByBalanceKey, ByHeightKey, ByFuturesSourceKey, ByFuturesDestKey, CCommunityBalancesView :: ById, CUndosView :: ByUndoKey, CPoolPairView :: ByID, ByPair, ByShare, ByIDPair, ByPoolSwap, ByReserves, ByRewardPct, ByRewardLoanPct, diff --git a/src/validation.cpp b/src/validation.cpp index d6816246d8..9ccffe629d 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -3361,7 +3361,11 @@ void CChainState::ProcessFutures(const CBlockIndex* pindex, CCustomCSView& cache const auto& premiumPrice = futuresPrices.at(destId).premium; if (premiumPrice > 0) { const auto total = DivideAmounts(futuresValues.source.nValue, premiumPrice); - cache.AddBalance(key.owner, {destId, total}); + CTokenAmount destination{destId, total}; + cache.AddBalance(key.owner, destination); + cache.StoreFuturesDestValues(key, destination); + LogPrint(BCLog::FUTURESWAP, "ProcessFutures(): Owner %s source %s destination %s\n", + key.owner.GetHex(), futuresValues.source.ToString(), destination.ToString()); } } catch (const std::out_of_range&) { unpaidContracts.emplace(key, futuresValues); @@ -3374,7 +3378,11 @@ void CChainState::ProcessFutures(const CBlockIndex* pindex, CCustomCSView& cache try { const auto& discountPrice = futuresPrices.at(futuresValues.source.nTokenId).discount; const auto total = MultiplyAmounts(futuresValues.source.nValue, discountPrice); - cache.AddBalance(key.owner, {tokenDUSD->first, total}); + CTokenAmount destination{tokenDUSD->first, total}; + cache.AddBalance(key.owner, destination); + cache.StoreFuturesDestValues(key, destination); + LogPrint(BCLog::FUTURESWAP, "ProcessFutures(): Owner %s source %s destination %s\n", + key.owner.GetHex(), futuresValues.source.ToString(), destination.ToString()); } catch (const std::out_of_range&) { unpaidContracts.emplace(key, futuresValues); }