Skip to content

Commit d9f1e77

Browse files
Change api/drep/live-delegation response
1 parent 22ad35f commit d9f1e77

File tree

3 files changed

+73
-108
lines changed

3 files changed

+73
-108
lines changed

dbsync-api/src/controllers/drep.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ router.get('/:id', handlerWrapper(getDrepDetails))
8484
router.get('/:id/vote', handlerWrapper(getDrepVoteDetails))
8585
router.get('/:id/delegation', handlerWrapper(getDrepDelegationDetails))
8686
router.get('/:id/registration', handlerWrapper(getDrepRegistrationDetails))
87-
router.get('/:id/active-delegation', handlerWrapper(getDrepActiveDelegation))
88-
router.get('/:id/active-delegators', handlerWrapper(getDrepActiveDelegators))
87+
router.get('/:id/live-delegation', handlerWrapper(getDrepActiveDelegation))
88+
router.get('/:id/live-delegators', handlerWrapper(getDrepActiveDelegators))
8989

9090
export default router

dbsync-api/src/repository/drep.ts

Lines changed: 58 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -460,77 +460,71 @@ export const fetchDrepRegistrationDetails = async (dRepId: string) => {
460460

461461
export const fetchDrepActiveDelegation = async (drepId: string) => {
462462
const result = (await prisma.$queryRaw`
463-
WITH latest AS (
463+
WITH liveRecord AS (WITH latest AS (
464+
WITH stakes AS (
465+
SELECT DISTINCT sa.id AS id, sa.view AS stakeAddress
466+
FROM delegation_vote dv
467+
JOIN drep_hash dh ON dh.id = dv.drep_hash_id
468+
JOIN stake_address sa ON sa.id = dv.addr_id
469+
WHERE dh.raw = DECODE(${drepId}, 'hex')
470+
)
464471
SELECT
465-
sa.view AS stake_view,
466-
dh.view AS dh_view,
467-
sa.id AS stake_addr_id,
468-
dh.raw,
469-
dh.id,
470-
ROW_NUMBER() OVER (PARTITION BY sa.id ORDER BY dv.tx_id DESC) AS rn
471-
FROM stake_address sa
472-
JOIN delegation_vote dv ON sa.id = dv.addr_id
473-
JOIN drep_hash dh ON dh.id = dv.drep_hash_id
474-
ORDER BY dv.tx_id DESC
472+
stakes.stakeAddress,
473+
stakes.id
474+
FROM stakes
475+
JOIN LATERAL (
476+
SELECT
477+
ENCODE(tx.hash, 'hex') AS tx_id,
478+
b.epoch_no,
479+
b.time,
480+
dh.raw AS raw_check
481+
FROM delegation_vote dv
482+
JOIN drep_hash dh ON dh.id = dv.drep_hash_id
483+
JOIN tx ON tx.id = dv.tx_id
484+
JOIN block b ON b.id = tx.block_id
485+
WHERE dv.addr_id = stakes.id
486+
ORDER BY dv.tx_id DESC
487+
LIMIT 1
488+
) AS subquery ON subquery.raw_check = DECODE(${drepId}, 'hex')
489+
GROUP BY stakes.stakeAddress, stakes.id
490+
ORDER BY stakes.id
475491
)
476492
SELECT
477-
dh.view,
478-
latest.stake_view,
479-
SUM(uv.value) AS total_value,
480-
(SELECT SUM(amount)
493+
COUNT(DISTINCT(latest.stakeAddress)) AS activeDelegators,
494+
COALESCE(SUM(uv.value), 0) +
495+
COALESCE((
496+
SELECT SUM(amount)
481497
FROM reward r
482-
WHERE r.addr_id = latest.stake_addr_id
498+
WHERE r.addr_id = latest.id
483499
AND r.earned_epoch >
484500
(SELECT blka.epoch_no
485501
FROM withdrawal w
486502
JOIN tx txa ON txa.id = w.tx_id
487503
JOIN block blka ON blka.id = txa.block_id
488-
WHERE w.addr_id = latest.stake_addr_id
504+
WHERE w.addr_id = latest.id
489505
ORDER BY w.tx_id DESC
490-
LIMIT 1)) AS rewardBalance,
491-
(SELECT SUM(amount)
506+
LIMIT 1)
507+
), 0) +
508+
COALESCE((
509+
SELECT SUM(amount)
492510
FROM reward_rest r
493-
WHERE r.addr_id = latest.stake_addr_id
494-
AND r.earned_epoch >
495-
(SELECT blka.epoch_no
496-
FROM withdrawal w
497-
JOIN tx txa ON txa.id = w.tx_id
498-
JOIN block blka ON blka.id = txa.block_id
499-
WHERE w.addr_id = latest.stake_addr_id
500-
ORDER BY w.tx_id DESC
501-
LIMIT 1)) AS rewardRestBalance
502-
FROM drep_hash dh
503-
JOIN latest ON dh.id = latest.id
504-
JOIN utxo_view uv ON uv.stake_address_id = latest.stake_addr_id
505-
WHERE latest.rn = 1
506-
AND (dh.view != 'drep_always_no_confidence'
507-
OR dh.view != 'drep_always_abstain')
508-
AND dh.raw = decode(${drepId}, 'hex')
509-
GROUP BY latest.stake_addr_id, dh.view, latest.stake_view;
511+
WHERE r.addr_id = latest.id
512+
AND r.earned_epoch >
513+
(SELECT blka.epoch_no
514+
FROM withdrawal w
515+
JOIN tx txa ON txa.id = w.tx_id
516+
JOIN block blka ON blka.id = txa.block_id
517+
WHERE w.addr_id = latest.id
518+
ORDER BY w.tx_id DESC
519+
LIMIT 1)
520+
), 0) AS liveVotingPower
521+
FROM latest
522+
LEFT JOIN utxo_view uv ON uv.stake_address_id = latest.id
523+
GROUP BY latest.stakeAddress, latest.id)
524+
SELECT SUM(activedelegators) as activeDelegators, SUM(livevotingpower) as liveVotingPower
525+
FROM liveRecord
510526
`) as Record<string, any>[]
511527

512-
const response: Record<string, any> = {}
513-
514-
for (const row of result) {
515-
const { view: drepId, stake_view: stakeView, total_value, rewardbalance, rewardrestbalance } = row
516-
517-
const totalRewardBalance: BigInt =
518-
(rewardbalance != null ? BigInt(rewardbalance) : BigInt(0)) +
519-
(rewardrestbalance != null ? BigInt(rewardrestbalance) : BigInt(0))
520-
521-
if (!response[drepId]) {
522-
response[drepId] = {
523-
delegators: [],
524-
}
525-
}
526-
527-
response[drepId].delegators.push({
528-
[stakeView]: {
529-
utxoBalance: BigInt(total_value).toString(),
530-
rewardBalance: totalRewardBalance.toString(),
531-
},
532-
})
533-
}
534528
const latestEpoch = await prisma.epoch.findFirst({
535529
orderBy: {
536530
start_time: 'desc',
@@ -547,26 +541,14 @@ export const fetchDrepActiveDelegation = async (drepId: string) => {
547541
epoch_no: latestEpoch ? (latestEpoch.no as number) : 0,
548542
},
549543
})
550-
551-
const calculateSum = (data: Record<string, any>): string => {
552-
let totalSum = BigInt(0)
553-
for (const drepId in data) {
554-
const delegators = data[drepId].delegators
555-
for (const delegator of delegators) {
556-
for (const stakeView in delegator) {
557-
const { utxoBalance, rewardBalance } = delegator[stakeView]
558-
totalSum += BigInt(utxoBalance) + BigInt(rewardBalance)
559-
}
560-
}
561-
}
562-
return totalSum.toString()
563-
}
564-
565-
const votingPower = calculateSum(response)
566544
const totalVotingPower = drepDistr._sum.amount as bigint
567-
const decimalInfluence = Number(votingPower) / Number(totalVotingPower)
545+
const decimalInfluence = Number(result[0].livevotingpower) / Number(totalVotingPower)
568546
const influence = (decimalInfluence * 100).toFixed(4) + '%'
569-
response.influence = influence
547+
const response = {
548+
liveDelegators: result[0].activedelegators ? parseInt(result[0].activedelegators) : 0,
549+
liveVotingPower: result[0].livevotingpower ? result[0].livevotingpower.toString() : '0',
550+
influence: influence,
551+
}
570552
return response
571553
}
572554

dbsync-api/swagger.yaml

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ paths:
143143
description: Bad request. The `address` query parameter is missing or invalid.
144144
'500':
145145
description: Internal server error.
146-
/api/drep/{id}/active-delegators:
146+
/api/drep/{id}/live-delegators:
147147
get:
148148
summary: Get active delegators for a DRep
149149
description: Fetch the list of active delegators associated with the specified DRep ID.
@@ -205,10 +205,10 @@ paths:
205205
example: Provide a valid Drep ID
206206
'500':
207207
description: Internal server error.
208-
/api/drep/{id}/active-delegation:
208+
/api/drep/{id}/live-delegation:
209209
get:
210210
summary: Fetch active delegation for a Drep
211-
description: Retrieves active delegation details for a given Drep ID.
211+
description: Retrieves live delegation details for a given Drep ID.
212212
parameters:
213213
- name: id
214214
in: path
@@ -224,33 +224,16 @@ paths:
224224
application/json:
225225
schema:
226226
type: object
227-
additionalProperties:
228-
type: object
229-
properties:
230-
delegators:
231-
type: array
232-
items:
233-
type: object
234-
additionalProperties:
235-
type: object
236-
properties:
237-
utxoBalance:
238-
type: string
239-
description: The UTxO balance for the delegator.
240-
example: '18992131258148'
241-
rewardBalance:
242-
type: string
243-
description: The reward balance for the delegator.
244-
example: '243137806039'
245-
example:
246-
'drep15fy2nszna039pdregpryhzahnwcr2rred4nx2qaec7k07nvcq26':
247-
delegators:
248-
- stake_test1urq9sjgt4n92cnvr02450z6fqksux7xc5k4z29j77uew5gqlvr3uc:
249-
utxoBalance: '18992131258148'
250-
rewardBalance: '243137806039'
251-
- stake_test1uzgzs48ckhg2j7l8wxuz4rd8zt8sfpmy7nuxt7dn96t6utgyer5a5:
252-
utxoBalance: '2998147525'
253-
rewardBalance: '0'
227+
properties:
228+
liveDelegators:
229+
type: integer
230+
example: 17
231+
liveVotingPower:
232+
type: string
233+
example: "20027234084683"
234+
influence:
235+
type: string
236+
example: "19.9043%"
254237
'400':
255238
description: Bad Request - Invalid Drep ID
256239
content:

0 commit comments

Comments
 (0)