Skip to content

Commit

Permalink
Sum reward balance in voting power
Browse files Browse the repository at this point in the history
  • Loading branch information
reeshavacharya committed Jan 9, 2025
1 parent bbf9b26 commit 9a16827
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
13 changes: 6 additions & 7 deletions dbsync-api/src/controllers/drep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
fetchDrepVoteDetails,
} from '../repository/drep'
import { DrepSortType, DrepStatusType } from '../types/drep'
import { fetchDelegationDetail } from '../repository/delegation'
import { fetchDelegationDetail, fetchDelegationRewardBalance } from '../repository/delegation'
import { fetchStakeAddressId, fetchStakeAddressUtxoSum } from '../repository/stakeAddress'

const router = Router()
Expand Down Expand Up @@ -67,7 +67,6 @@ const getDrepLiveVotingPower = async (req: Request, res: Response) => {
}
const result = await fetchDrepDelegationDetails(dRepId)
const delegatorStakeAddresses = result.map((res: any) => res.stakeAddress)
const activeDelegators: Record<string, bigint> = {}
let totalValue = BigInt(0)

for (let i = 0; i < delegatorStakeAddresses.length; i++) {
Expand All @@ -86,17 +85,17 @@ const getDrepLiveVotingPower = async (req: Request, res: Response) => {
} else if (convertToHexIfBech32(delegatorInfo.drep.drep_id) === dRepId) {
const stakeAddressId = await fetchStakeAddressId(address)
if (stakeAddressId) {
const rewards = await fetchDelegationRewardBalance(stakeAddressId)
const rewardBalance = rewards.rewardbalance == null ? BigInt(0) : BigInt(rewards.rewardbalance)
const restRewardBalance =
rewards.rewardrestbalance == null ? BigInt(0) : BigInt(rewards.rewardrestbalance)
const value = BigInt(await fetchStakeAddressUtxoSum(stakeAddressId))
activeDelegators[delegatorStakeAddresses[i]] = value
totalValue += value
totalValue += value + rewardBalance + restRewardBalance
}
}
}
const votingPower = {
votingPower: totalValue.toString(),
activeDelegators: Object.fromEntries(
Object.entries(activeDelegators).map(([key, value]) => [key, value.toString()])
),
}
return res.status(200).json(votingPower)
}
Expand Down
35 changes: 32 additions & 3 deletions dbsync-api/src/repository/delegation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { prisma } from "../config/db";
import { prisma } from '../config/db'

export const fetchDelegationDetail=async (address:string)=>{
const result = await prisma.$queryRaw`
Expand Down Expand Up @@ -32,5 +32,34 @@ export const fetchDelegationDetail=async (address:string)=>{
'pool',(SELECT row_to_json(latest_pool_delegation) FROM latest_pool_delegation)
) AS result;
` as Record<string,any>[];
return result[0].result
}
return result[0].result
}

export async function fetchDelegationRewardBalance(stakeaddressid: bigint) {
const result = (await prisma.$queryRaw`
SELECT
(SELECT SUM(amount)
FROM reward r
WHERE r.addr_id = ${stakeaddressid}
AND r.earned_epoch >
(SELECT blka.epoch_no
FROM withdrawal w
JOIN tx txa ON txa.id = w.tx_id
JOIN block blka ON blka.id = txa.block_id
WHERE w.addr_id = ${stakeaddressid}
ORDER BY w.tx_id DESC
LIMIT 1)) AS rewardBalance,
(SELECT SUM(amount)
FROM reward_rest r
WHERE r.addr_id = ${stakeaddressid}
AND r.earned_epoch >
(SELECT blka.epoch_no
FROM withdrawal w
JOIN tx txa ON txa.id = w.tx_id
JOIN block blka ON blka.id = txa.block_id
WHERE w.addr_id = ${stakeaddressid}
ORDER BY w.tx_id DESC
LIMIT 1)) AS rewardRestBalance`) as Record<string, any>[]
return result[0]
}

0 comments on commit 9a16827

Please sign in to comment.