Skip to content

Commit

Permalink
Static pool reward (#2924)
Browse files Browse the repository at this point in the history
* Initial work on new pool reward logic

* Fix failing test

* Add loan reward to new owner reward calculations

* Add remaining rewards

* Test multiple liquidity in pools

* Pass 0 for DFI. Avoid shadowing.
  • Loading branch information
Bushstar committed Jul 15, 2024
1 parent f7d4ad6 commit 5370c8c
Show file tree
Hide file tree
Showing 7 changed files with 1,006 additions and 62 deletions.
1 change: 1 addition & 0 deletions src/amount.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ static constexpr CAmount COIN = 100000000;
static constexpr CAmount CENT = 1000000;
static constexpr int64_t WEI_IN_GWEI = 1000000000;
static constexpr int64_t CAMOUNT_TO_GWEI = 10;
static constexpr CAmount HIGH_PRECISION_SCALER = COIN * COIN; // 1,0000,0000,0000,0000

//Converts the given value to decimal format string with COIN precision.
inline std::string GetDecimalString(CAmount nValue)
Expand Down
2 changes: 0 additions & 2 deletions src/dfi/loan.h
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,6 @@ inline auto InterestAddition = [](const CInterestAmount &a, const CInterestAmoun
return interest;
};

static const CAmount HIGH_PRECISION_SCALER = COIN * COIN; // 1,0000,0000,0000,0000

CAmount TotalInterest(const CInterestRateV3 &rate, const uint32_t height);
CInterestAmount TotalInterestCalculation(const CInterestRateV3 &rate, const uint32_t height);
CAmount CeilInterest(const base_uint<128> &value, uint32_t height);
Expand Down
34 changes: 23 additions & 11 deletions src/dfi/masternodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -971,17 +971,29 @@ bool CCustomCSView::CalculateOwnerRewards(const CScript &owner, uint32_t targetH
return true; // no share or target height is before a pool share' one
}
auto onLiquidity = [&]() -> CAmount { return GetBalance(owner, poolId).nValue; };
auto beginHeight = std::max(*height, balanceHeight);
CalculatePoolRewards(
poolId, onLiquidity, beginHeight, targetHeight, [&](RewardType, CTokenAmount amount, uint32_t height) {
auto res = AddBalance(owner, amount);
if (!res) {
LogPrintf("Pool rewards: can't update balance of %s: %s, height %ld\n",
owner.GetHex(),
res.msg,
targetHeight);
}
});
const auto beginHeight = std::max(*height, balanceHeight);
auto onReward = [&](RewardType, const CTokenAmount &amount, const uint32_t height) {
if (auto res = AddBalance(owner, amount); !res) {
LogPrintf(
"Pool rewards: can't update balance of %s: %s, height %ld\n", owner.GetHex(), res.msg, height);
}
};

if (beginHeight < Params().GetConsensus().DF24Height) {
// Calculate just up to the fork height
const auto targetNewHeight = targetHeight >= Params().GetConsensus().DF24Height
? Params().GetConsensus().DF24Height - 1
: targetHeight;
CalculatePoolRewards(poolId, onLiquidity, beginHeight, targetNewHeight, onReward);
}

if (targetHeight >= Params().GetConsensus().DF24Height) {
// Calculate from the fork height
const auto beginNewHeight =
beginHeight < Params().GetConsensus().DF24Height ? Params().GetConsensus().DF24Height : beginHeight;
CalculateStaticPoolRewards(onLiquidity, onReward, poolId.v, beginNewHeight, targetHeight);
}

return true;
});

Expand Down
260 changes: 211 additions & 49 deletions src/dfi/poolpairs.cpp

Large diffs are not rendered by default.

62 changes: 62 additions & 0 deletions src/dfi/poolpairs.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,36 @@ struct PoolShareKey {
}
};

struct TotalRewardPerShareKey {
uint32_t height;
uint32_t poolID;

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream &s, Operation ser_action) {
READWRITE(WrapBigEndian(height));
READWRITE(WrapBigEndian(poolID));
}
};

struct TotalCommissionPerShareValue {
uint32_t tokenA;
uint32_t tokenB;
arith_uint256 commissionA;
arith_uint256 commissionB;

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream &s, Operation ser_action) {
READWRITE(tokenA);
READWRITE(tokenB);
READWRITE(commissionA);
READWRITE(commissionB);
}
};

struct LoanTokenAverageLiquidityKey {
uint32_t sourceID;
uint32_t destID;
Expand Down Expand Up @@ -320,6 +350,12 @@ class CPoolPairView : public virtual CStorageView {
uint32_t end,
std::function<void(RewardType, CTokenAmount, uint32_t)> onReward);

void CalculateStaticPoolRewards(std::function<CAmount()> onLiquidity,
std::function<void(RewardType, CTokenAmount, uint32_t)> onReward,
const uint32_t poolID,
const uint32_t beginHeight,
const uint32_t endHeight);

Res SetLoanDailyReward(const uint32_t height, const CAmount reward);
Res SetDailyReward(uint32_t height, CAmount reward);
Res SetRewardPct(DCT_ID const &poolId, uint32_t height, CAmount rewardPct);
Expand Down Expand Up @@ -349,6 +385,17 @@ class CPoolPairView : public virtual CStorageView {
std::function<bool(const LoanTokenAverageLiquidityKey &key, const uint64_t liquidity)> callback,
const LoanTokenAverageLiquidityKey start = LoanTokenAverageLiquidityKey{});

bool SetTotalRewardPerShare(const TotalRewardPerShareKey &key, const arith_uint256 &totalReward);
arith_uint256 GetTotalRewardPerShare(const TotalRewardPerShareKey &totalReward) const;
bool SetTotalLoanRewardPerShare(const TotalRewardPerShareKey &key, const arith_uint256 &totalReward);
arith_uint256 GetTotalLoanRewardPerShare(const TotalRewardPerShareKey &totalReward) const;
bool SetTotalCustomRewardPerShare(const TotalRewardPerShareKey &key,
const std::map<uint32_t, arith_uint256> &customRewards);
std::map<uint32_t, arith_uint256> GetTotalCustomRewardPerShare(const TotalRewardPerShareKey &key) const;
bool SetTotalCommissionPerShare(const TotalRewardPerShareKey &key,
const TotalCommissionPerShareValue &totalCommission);
TotalCommissionPerShareValue GetTotalCommissionPerShare(const TotalRewardPerShareKey &key) const;

// tags
struct ByID {
static constexpr uint8_t prefix() { return 'i'; }
Expand Down Expand Up @@ -401,6 +448,21 @@ class CPoolPairView : public virtual CStorageView {
struct ByLoanTokenLiquidityAverage {
static constexpr uint8_t prefix() { return '+'; }
};
struct ByTotalRewardPerShare {
static constexpr uint8_t prefix() { return '-'; }
};

struct ByTotalLoanRewardPerShare {
static constexpr uint8_t prefix() { return '='; }
};

struct ByTotalCustomRewardPerShare {
static constexpr uint8_t prefix() { return '_'; }
};

struct ByTotalCommissionPerShare {
static constexpr uint8_t prefix() { return '/'; }
};
};

struct CLiquidityMessage {
Expand Down
Loading

0 comments on commit 5370c8c

Please sign in to comment.