Skip to content

Commit

Permalink
Revert commissions fix, updates logaccountbalances (#1370)
Browse files Browse the repository at this point in the history
* Revert "Fix commission for first deposit after fork (#1348)"

This reverts commit b45613c.

* Update logaccountbalances
  • Loading branch information
prasannavl authored Jul 5, 2022
1 parent 9c1cd0a commit 510859b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 363 deletions.
82 changes: 32 additions & 50 deletions src/masternodes/poolpairs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@
// Distributed under the MIT software license, see the accompanying
// file LICENSE or http://www.opensource.org/licenses/mit-license.php.

#include <masternodes/poolpairs.h>

#include <arith_uint256.h>
#include <masternodes/poolpairs.h>
#include <core_io.h>
#include <primitives/transaction.h>
#include <masternodes/govvariables/attributes.h>

#include <tuple>

struct PoolSwapValue {
bool swapEvent;
CAmount blockCommissionA;
Expand Down Expand Up @@ -204,50 +201,28 @@ inline CAmount liquidityReward(CAmount reward, CAmount liquidity, CAmount totalL
return static_cast<CAmount>((arith_uint256(reward) * arith_uint256(liquidity) / arith_uint256(totalLiquidity)).GetLow64());
}

template<typename TIterator>
bool MatchPoolId(TIterator & it, DCT_ID poolId) {
return it.Valid() && it.Key().poolID == poolId;
}

template<typename TIterator, typename ValueType>
void ReadValueMoveToNext(TIterator & it, DCT_ID poolId, ValueType & value, uint32_t & height) {

if (MatchPoolId(it, poolId)) {
if (it.Valid() && it.Key().poolID == poolId) {
value = it.Value();
/// @Note we store keys in desc order so Prev is actually go in forward
it.Prev();
height = MatchPoolId(it, poolId) ? it.Key().height : UINT_MAX;
if (it.Valid() && it.Key().poolID == poolId) {
height = it.Key().height;
} else {
height = UINT_MAX;
}
} else {
value = {};
height = UINT_MAX;
}
}

template<typename By, typename Value>
auto InitPoolVars(CPoolPairView & view, PoolHeightKey poolKey, uint32_t end) {

auto poolId = poolKey.poolID;
auto it = view.LowerBound<By>(poolKey);

auto height = poolKey.height;
static const uint32_t startHeight = Params().GetConsensus().FortCanningSpringHeight;
poolKey.height = std::max(height, startHeight);

while (!MatchPoolId(it, poolId) && poolKey.height < end) {
height = poolKey.height;
it.Seek(poolKey);
poolKey.height++;
}

Value value = MatchPoolId(it, poolId) ? it.Value() : Value{};

return std::make_tuple(std::move(value), std::move(it), height);
}

void CPoolPairView::CalculatePoolRewards(DCT_ID const & poolId, std::function<CAmount()> onLiquidity, uint32_t begin, uint32_t end, std::function<void(RewardType, CTokenAmount, uint32_t)> onReward) {
if (begin >= end) {
return;
}

constexpr const uint32_t PRECISION = 10000;
const auto newCalcHeight = uint32_t(Params().GetConsensus().BayfrontGardensHeight);

Expand All @@ -256,19 +231,28 @@ void CPoolPairView::CalculatePoolRewards(DCT_ID const & poolId, std::function<CA

PoolHeightKey poolKey = {poolId, begin};

auto [poolReward, itPoolReward, startPoolReward] = InitPoolVars<ByPoolReward, CAmount>(*this, poolKey, end);
auto nextPoolReward = startPoolReward;

auto [poolLoanReward, itPoolLoanReward, startPoolLoanReward] = InitPoolVars<ByPoolLoanReward, CAmount>(*this, poolKey, end);
auto nextPoolLoanReward = startPoolLoanReward;
CAmount poolReward = 0;
CAmount poolLoanReward = 0;
auto nextPoolReward = begin;
auto nextPoolLoanReward = begin;
auto itPoolReward = LowerBound<ByPoolReward>(poolKey);
auto itPoolLoanReward = LowerBound<ByPoolLoanReward>(poolKey);

auto [totalLiquidity, itTotalLiquidity, nextTotalLiquidity] = InitPoolVars<ByTotalLiquidity, CAmount>(*this, poolKey, end);
CAmount totalLiquidity = 0;
auto nextTotalLiquidity = begin;
auto itTotalLiquidity = LowerBound<ByTotalLiquidity>(poolKey);

auto [customRewards, itCustomRewards, startCustomRewards] = InitPoolVars<ByCustomReward, CBalances>(*this, poolKey, end);
auto nextCustomRewards = startCustomRewards;
CBalances customRewards;
auto nextCustomRewards = begin;
auto itCustomRewards = LowerBound<ByCustomReward>(poolKey);

auto [poolSwap, itPoolSwap, poolSwapHeight] = InitPoolVars<ByPoolSwap, PoolSwapValue>(*this, poolKey, end);
auto nextPoolSwap = poolSwapHeight;
PoolSwapValue poolSwap{};
auto nextPoolSwap = UINT_MAX;
auto poolSwapHeight = UINT_MAX;
auto itPoolSwap = LowerBound<ByPoolSwap>(poolKey);
if (itPoolSwap.Valid() && itPoolSwap.Key().poolID == poolId) {
nextPoolSwap = itPoolSwap.Key().height;
}

for (auto height = begin; height < end;) {
// find suitable pool liquidity
Expand All @@ -293,7 +277,7 @@ void CPoolPairView::CalculatePoolRewards(DCT_ID const & poolId, std::function<CA
}
const auto liquidity = onLiquidity();
// daily rewards
if (height >= startPoolReward && poolReward != 0) {
if (poolReward != 0) {
CAmount providerReward = 0;
if (height < newCalcHeight) { // old calculation
uint32_t liqWeight = liquidity * PRECISION / totalLiquidity;
Expand All @@ -303,7 +287,7 @@ void CPoolPairView::CalculatePoolRewards(DCT_ID const & poolId, std::function<CA
}
onReward(RewardType::Coinbase, {DCT_ID{0}, providerReward}, height);
}
if (height >= startPoolLoanReward && poolLoanReward != 0) {
if (poolLoanReward != 0) {
CAmount providerReward = liquidityReward(poolLoanReward, liquidity, totalLiquidity);
onReward(RewardType::LoanTokenDEXReward, {DCT_ID{0}, providerReward}, height);
}
Expand All @@ -326,11 +310,9 @@ void CPoolPairView::CalculatePoolRewards(DCT_ID const & poolId, std::function<CA
}
}
// custom rewards
if (height >= startCustomRewards) {
for (const auto& reward : customRewards.balances) {
if (auto providerReward = liquidityReward(reward.second, liquidity, totalLiquidity)) {
onReward(RewardType::Pool, {reward.first, providerReward}, height);
}
for (const auto& reward : customRewards.balances) {
if (auto providerReward = liquidityReward(reward.second, liquidity, totalLiquidity)) {
onReward(RewardType::Pool, {reward.first, providerReward}, height);
}
}
++height;
Expand Down
19 changes: 11 additions & 8 deletions src/masternodes/rpc_accounts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2399,19 +2399,22 @@ UniValue logaccountbalances(const JSONRPCRequest& request) {
if (p.size() > 0) { outToLog = p[0].get_bool(); }
if (p.size() > 1) { outToRpc = p[1].get_bool(); }

LOCK(cs_main);

std::map<std::string, std::vector<CTokenAmount>> accounts;
auto iter = pcustomcsDB->NewIterator();
auto n = IterateKV<CAccountsView::ByBalanceKey, BalanceKey, CAmount>([&](BalanceKey key, CAmount val) {
auto owner = ScriptToString(key.owner);
size_t count{};
pcustomcsview->ForEachBalance([&](CScript const & owner, CTokenAmount balance) {
++count;
auto ownerStr = ScriptToString(owner);
if (outToLog)
LogPrintf("AccountBalance: (%s: %d@%d)\n", owner, val, key.tokenID.v);
LogPrintf("AccountBalance: (%s: %d@%d)\n", ownerStr, balance.nValue, balance.nTokenId.v);
if (outToRpc)
accounts[owner].push_back(CTokenAmount{{key.tokenID.v}, val});
accounts[ownerStr].push_back(CTokenAmount{{balance.nTokenId.v}, balance.nValue});
return true;
}, BalanceKey{}, std::move(iter));
});

if (outToLog)
LogPrintf("IndexStats: (balances: %d)\n", n);
LogPrintf("IndexStats: (balances: %d)\n", count);

if (!outToRpc)
return {};
Expand All @@ -2427,7 +2430,7 @@ UniValue logaccountbalances(const JSONRPCRequest& request) {
}

result.pushKV("accounts", accountsJson);
result.pushKV("count", static_cast<uint64_t>(n));
result.pushKV("count", static_cast<uint64_t>(count));
return result;
}

Expand Down
Loading

0 comments on commit 510859b

Please sign in to comment.