@@ -460,77 +460,71 @@ export const fetchDrepRegistrationDetails = async (dRepId: string) => {
460460
461461export 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
0 commit comments