Skip to content

Commit

Permalink
Store future swap destination amount
Browse files Browse the repository at this point in the history
  • Loading branch information
Bushstar committed Mar 28, 2022
1 parent 0daecaa commit 2e653f9
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
};
Expand Down
1 change: 1 addition & 0 deletions src/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ namespace BCLog {
ORACLE = (1 << 24),
LOAN = (1 << 25),
ACCOUNTCHANGE = (1 << 26),
FUTURESWAP = (1 << 27),
ALL = ~(uint32_t)0,
};

Expand Down
22 changes: 18 additions & 4 deletions src/masternodes/accounts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ uint32_t CAccountsView::GetBalancesHeight(CScript const & owner)

Res CAccountsView::StoreFuturesUserValues(const CFuturesUserKey& key, const CFuturesUserValue& futures)
{
if (!WriteBy<ByFuturesKey>(key, futures)) {
if (!WriteBy<ByFuturesSourceKey>(key, futures)) {
return Res::Err("Failed to store futures");
}

Expand All @@ -110,12 +110,12 @@ Res CAccountsView::StoreFuturesUserValues(const CFuturesUserKey& key, const CFut

void CAccountsView::ForEachFuturesUserValues(std::function<bool(const CFuturesUserKey&, const CFuturesUserValue&)> callback, const CFuturesUserKey& start)
{
ForEach<ByFuturesKey, CFuturesUserKey, CFuturesUserValue>(callback, start);
ForEach<ByFuturesSourceKey, CFuturesUserKey, CFuturesUserValue>(callback, start);
}

Res CAccountsView::EraseFuturesUserValues(const CFuturesUserKey& key)
{
if (!EraseBy<ByFuturesKey>(key)) {
if (!EraseBy<ByFuturesSourceKey>(key)) {
return Res::Err("Failed to erase futures");
}

Expand All @@ -125,10 +125,24 @@ Res CAccountsView::EraseFuturesUserValues(const CFuturesUserKey& key)
boost::optional<uint32_t> CAccountsView::GetMostRecentFuturesHeight()
{
const CFuturesUserKey key{std::numeric_limits<uint32_t>::max(), {}, std::numeric_limits<uint32_t>::max()};
auto it = LowerBound<ByFuturesKey>(key);
auto it = LowerBound<ByFuturesSourceKey>(key);
if (it.Valid()) {
return it.Key().height;
}

return {};
}

Res CAccountsView::StoreFuturesDestValues(const CFuturesUserKey& key, const CTokenAmount& destination)
{
if (!WriteBy<ByFuturesDestKey>(key, destination)) {
return Res::Err("Failed to store futures destination");
}

return Res::Ok();
}

void CAccountsView::ForEachFuturesDestValues(std::function<bool(const CFuturesUserKey&, const CTokenAmount&)> callback, const CFuturesUserKey& start)
{
ForEach<ByFuturesSourceKey, CFuturesUserKey, CTokenAmount>(callback, start);
}
5 changes: 4 additions & 1 deletion src/masternodes/accounts.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint32_t> GetMostRecentFuturesHeight();
void ForEachFuturesUserValues(std::function<bool(const CFuturesUserKey&, const CFuturesUserValue&)> callback, const CFuturesUserKey& start = {});
void ForEachFuturesDestValues(std::function<bool(const CFuturesUserKey&, const CTokenAmount&)> 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);
Expand Down
2 changes: 1 addition & 1 deletion src/masternodes/masternodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 10 additions & 2 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down

0 comments on commit 2e653f9

Please sign in to comment.