Skip to content

Commit

Permalink
Fix Reward Aggregator (#406)
Browse files Browse the repository at this point in the history
* Fix Reward Aggregator

* Apply panleone review

* Apply new suggestions

* Optimise rewardsText

* Zero out nRewardUpdateHeight on Activity reset()
  • Loading branch information
JSKitty authored Oct 7, 2024
1 parent dd8b02d commit bb8412e
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions scripts/dashboard/Activity.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ let txCount = 0;
const updating = ref(false);
const isHistorySynced = ref(false);
const rewardAmount = ref(0);
let nRewardUpdateHeight = 0;
const ticker = computed(() => cChainParams.current.TICKER);
const explorerUrl = ref(getNetwork()?.strUrl);
const txMap = computed(() => {
Expand Down Expand Up @@ -86,6 +87,24 @@ async function update(txToAdd = 0) {
const orderedTxs = Array.from(wallet.getTransactions()).sort(
(a, b) => a.blockHeight - b.blockHeight
);
// For Rewards: aggregate the total amount
if (props.rewards) {
for (const tx of orderedTxs) {
// If this Tx Height is under our last scanned height, we stop
if (tx.blockHeight <= nRewardUpdateHeight) break;
// Only compute rewards
if (!tx.isCoinStake()) continue;
// Aggregate the total rewards
rewardAmount.value += wallet.toHistoricalTXs([tx])[0].amount;
}
// Keep track of the scan block height
if (orderedTxs.length) {
nRewardUpdateHeight = orderedTxs[0].blockHeight;
}
}
// Prepare the Tx History list
while (found < txCount + txToAdd) {
if (orderedTxs.length == 0) {
isHistorySynced.value = true;
Expand All @@ -96,6 +115,8 @@ async function update(txToAdd = 0) {
newTxs.push(tx);
found++;
}
// Convert to MPW's Activity format and render it
const arrTXs = wallet.toHistoricalTXs(newTxs);
await parseTXs(arrTXs);
txCount = found;
Expand Down Expand Up @@ -235,27 +256,16 @@ async function parseTXs(arrTXs) {
txs.value = newTxs;
}
if (props.rewards) {
watch(
txs,
(txs) =>
(rewardAmount.value = txs.reduce((acc, tx) => acc + tx.amount, 0))
);
}
const rewardsText = computed(() => {
const nCoins = rewardAmount.value / COIN;
const strBal = nCoins.toFixed(displayDecimals.value);
const nLen = strBal.length;
return `${beautifyNumber(
strBal,
nLen >= 10 ? '15px' : '15px'
)} <span style="font-size:15px; opacity: 0.55;">${ticker.value}</span>`;
const strBal = rewardAmount.value.toLocaleString('en-GB');
return `${strBal} <span style="font-size:15px; opacity: 0.55;">${ticker.value}</span>`;
});
function reset() {
txs.value = [];
txCount = 0;
nRewardUpdateHeight = 0;
update(0);
}
Expand Down

0 comments on commit bb8412e

Please sign in to comment.