Skip to content

Commit

Permalink
Calculate static rewards by getting start balance from the block befo…
Browse files Browse the repository at this point in the history
…re (#3024)

* Calculate static rewards by getting start balance from the block before

* lint: Format Python

* lint: Format CPP
  • Loading branch information
Bushstar authored Sep 2, 2024
1 parent dad975d commit a6a28c3
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/dfi/masternodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -988,8 +988,9 @@ bool CCustomCSView::CalculateOwnerRewards(const CScript &owner, uint32_t targetH

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

Expand Down
77 changes: 76 additions & 1 deletion test/functional/feature_static_pool_rewards.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ def run_test(self):
# Setup test
self.setup_tests()

# Test account update on same block as poolswap
self.static_update_same_block()

# Test new pool reward with single share
self.static_reward_single()

Expand Down Expand Up @@ -472,6 +475,78 @@ def execute_poolswap(self):

return dfi_balance, ltc_balance

def static_update_same_block(self):

# Rollback block
self.rollback_to(self.start_block)

# Fund pool
self.nodes[0].addpoolliquidity(
{self.owner_address: [f"1000@{self.symbolLTC}", f"1000@{self.symbolDFI}"]},
self.address,
)
self.nodes[0].sendtoaddress(self.address, 1)
self.nodes[0].accounttoaccount(self.owner_address, {self.address: "1@DFI"})
self.nodes[0].generate(1)

# Move to fork height
self.nodes[0].generate(self.df24height - self.nodes[0].getblockcount())

# Execute swaps and update account via accounttoaccount on same block
self.nodes[0].accounttoaccount(self.address, {self.owner_address: "1@DFI"})
self.nodes[0].poolswap(
{
"from": self.owner_address,
"tokenFrom": self.symbolLTC,
"amountFrom": 0.1,
"to": self.owner_address,
"tokenTo": self.symbolDFI,
}
)
self.nodes[0].poolswap(
{
"from": self.owner_address,
"tokenFrom": self.symbolLTC,
"amountFrom": 0.1,
"to": self.owner_address,
"tokenTo": self.symbolDFI,
}
)
self.nodes[0].poolswap(
{
"from": self.owner_address,
"tokenFrom": self.symbolDFI,
"amountFrom": 0.1,
"to": self.owner_address,
"tokenTo": self.symbolLTC,
}
)
self.nodes[0].poolswap(
{
"from": self.owner_address,
"tokenFrom": self.symbolDFI,
"amountFrom": 0.1,
"to": self.owner_address,
"tokenTo": self.symbolLTC,
}
)
self.nodes[0].generate(1)

# Check balance
assert_equal(
self.nodes[0].getaccount(self.address),
["0.00199999@DFI", "0.00199999@LTC", "999.99999000@LTC-DFI"],
)

# Check account history
results = []
for result in self.nodes[0].listaccounthistory(self.address, {"depth": 1}):
if result["type"] == "Commission":
results.append(result)
assert_equal(len(results), 2)
assert_equal(results[0]["amounts"], ["0.00199999@LTC"])
assert_equal(results[1]["amounts"], ["0.00199999@DFI"])

def static_commission_single(self):

# Rollback block
Expand Down Expand Up @@ -718,7 +793,7 @@ def liquidity_after_setting_splits(self):
{self.owner_address: [f"1000@{self.symbolLTC}", f"1000@{self.symbolDFI}"]},
self.address,
)
self.nodes[0].generate(2)
self.nodes[0].generate(1)

# Balance started at zero, new balance is the reward
new_pool_reward = Decimal(
Expand Down

0 comments on commit a6a28c3

Please sign in to comment.