Skip to content

Commit 2b433a1

Browse files
Merge branch 'main' into feat/block-details-api
2 parents 254d03e + 89bd831 commit 2b433a1

File tree

7 files changed

+75
-63
lines changed

7 files changed

+75
-63
lines changed

src/controllers/address.ts

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
1-
import { Request, Response, Router } from "express";
2-
import { handlerWrapper } from "../errors/AppError";
3-
import { convertToHexIfBech32, validateAddress } from "../helpers/validator";
4-
import { fetchFaucetBalance } from "../repository/address";
1+
import { Request, Response, Router } from 'express'
2+
import { handlerWrapper } from '../errors/AppError'
3+
import { convertToHexIfBech32, validateAddress } from '../helpers/validator'
4+
import { fetchFaucetBalance } from '../repository/address'
55

6-
const router = Router();
6+
const router = Router()
77

88
const getFaucetBalance = async (req: Request, res: Response): Promise<any> => {
9-
let address = convertToHexIfBech32(req.query.address as string)
10-
if (validateAddress(address)){
11-
address = address.length === 56 ? `e0${address}`:address
12-
}else{
13-
return res.status(400).json({message:'Provide a valid address'})
14-
}
9+
let address = convertToHexIfBech32(req.query.address as string)
10+
if (validateAddress(address)) {
11+
const balance = await fetchFaucetBalance(address)
12+
return res.status(200).json(balance)
13+
} else {
14+
return res.status(400).json({ message: 'Provide a valid address' })
15+
}
16+
}
1517

16-
const balance = await fetchFaucetBalance(address)
18+
router.get('/balance', handlerWrapper(getFaucetBalance))
1719

18-
return res.status(200).json(balance);
19-
};
20-
21-
router.get('/balance', handlerWrapper(getFaucetBalance));
22-
23-
export default router;
20+
export default router

src/controllers/delegation.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
import { Request, Response, Router } from "express";
2-
import { handlerWrapper } from "../errors/AppError";
3-
import { convertToHexIfBech32, validateAddress } from "../helpers/validator";
4-
import { fetchDelegationDetail } from "../repository/delegation";
1+
import { Request, Response, Router } from 'express'
2+
import { handlerWrapper } from '../errors/AppError'
3+
import { convertToHexIfBech32, validateAddress } from '../helpers/validator'
4+
import { fetchDelegationDetail } from '../repository/delegation'
55

6-
const router = Router();
6+
const router = Router()
77

88
const getDelegation = async (req: Request, res: Response): Promise<any> => {
9-
let address = convertToHexIfBech32(req.query.address as string);
10-
if (validateAddress(address)){
11-
address = address.length === 56 ? `e0${address}`:address
12-
}else{
13-
return res.status(400).json({message:'Provide a valid address'})
14-
}
15-
const result = await fetchDelegationDetail(address)
16-
return res.status(200).json(result);
17-
};
9+
let address = convertToHexIfBech32(req.query.address as string)
10+
if (validateAddress(address)) {
11+
const result = await fetchDelegationDetail(address)
12+
return res.status(200).json(result)
13+
} else {
14+
return res.status(400).json({ message: 'Provide a valid address' })
15+
}
16+
}
1817

19-
router.get('/', handlerWrapper(getDelegation));
18+
router.get('/', handlerWrapper(getDelegation))
2019

21-
export default router;
20+
export default router

src/controllers/governance.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ const cache = new NodeCache({ stdTTL: 3600, checkperiod: 600 })
99

1010
const updateCache = async () => {
1111
const activeTotalStake = await fetchActiveToalStake()
12-
const liveTotalStake = await fetchLiveTotalStake()
12+
//const liveTotalStake = await fetchLiveTotalStake()
1313

1414
cache.set('activeTotalStake', activeTotalStake)
15-
cache.set('liveTotalStake', liveTotalStake)
15+
//cache.set('liveTotalStake', liveTotalStake)
1616
}
1717

1818
updateCache()
@@ -35,6 +35,6 @@ const getToalLiveStake = async (req: Request, res: Response) => {
3535
}
3636

3737
router.get('/active-stake', handlerWrapper(getToalActiveStake))
38-
router.get('/live-stake', handlerWrapper(getToalLiveStake))
38+
//router.get('/live-stake', handlerWrapper(getToalLiveStake))
3939

4040
export default router

src/controllers/stakeAddress.ts

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
1-
import { Request, Response, Router } from "express";
2-
import { handlerWrapper } from "../errors/AppError";
3-
import { convertToHexIfBech32, validateAddress } from "../helpers/validator";
4-
import { fetchStakeAddressDetails } from "../repository/stakeAddress";
1+
import { Request, Response, Router } from 'express'
2+
import { handlerWrapper } from '../errors/AppError'
3+
import { convertToHexIfBech32, validateAddress } from '../helpers/validator'
4+
import { fetchStakeAddressDetails } from '../repository/stakeAddress'
55

6-
const router = Router();
6+
const router = Router()
77

88
const getStakeAddressDetails = async (req: Request, res: Response): Promise<any> => {
9-
let address = convertToHexIfBech32(req.query.address as string)
10-
if (validateAddress(address)){
11-
address = address.length === 56 ? `e0${address}`:address
12-
}else{
13-
return res.status(400).json({message:'Provide a valid address'})
14-
}
9+
let address = convertToHexIfBech32(req.query.address as string)
10+
if (validateAddress(address)) {
11+
const result = await fetchStakeAddressDetails(address)
12+
return res.status(200).json(result)
13+
} else {
14+
return res.status(400).json({ message: 'Provide a valid address' })
15+
}
16+
}
1517

16-
const result = await fetchStakeAddressDetails(address)
17-
return res.status(200).json(result);
18-
};
18+
router.get('/', handlerWrapper(getStakeAddressDetails))
1919

20-
router.get('/', handlerWrapper(getStakeAddressDetails));
21-
22-
export default router;
20+
export default router

src/helpers/validator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export function fromHex(prefix: string, hex: string) {
105105

106106
export function validateAddress(value: string): boolean {
107107
if (isHexValue(value)) {
108-
return value.length === 56 || value.length === 58
108+
return value.length === 58
109109
}
110110
return false
111111
}

src/repository/drep.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ export const fetchDrepLiveStats = async (drepId: string, isScript?: boolean) =>
573573

574574
const drepVotingHistory = (await prisma.$queryRaw`
575575
WITH firstRegistration AS (
576-
SELECT b.time AS firstRegistrationTime
576+
SELECT b.epoch_no AS firstRegistrationEpoch
577577
FROM drep_registration dr
578578
JOIN drep_hash dh ON dh.id = dr.drep_hash_id
579579
JOIN tx ON tx.id = dr.tx_id
@@ -586,10 +586,8 @@ export const fetchDrepLiveStats = async (drepId: string, isScript?: boolean) =>
586586
govActionsAfter AS (
587587
SELECT COALESCE(COUNT(DISTINCT gp.id), 0) AS totalGovActionsAfter
588588
FROM gov_action_proposal AS gp
589-
JOIN tx ON tx.id = gp.tx_id
590-
JOIN block b ON b.id = tx.block_id
591589
LEFT JOIN firstRegistration ON TRUE
592-
WHERE b.time > firstRegistration.firstRegistrationTime
590+
WHERE gp.expiration > firstRegistration.firstRegistrationEpoch
593591
),
594592
votedGovActions AS (
595593
SELECT COUNT(DISTINCT gp.id) AS voted
@@ -959,4 +957,4 @@ export const fetchDrepDelegationHistory = async (dRepId: string, isScript?: bool
959957
})
960958
flattenedDelegations.sort((a: Delegation, b: Delegation) => new Date(b.time).getTime() - new Date(a.time).getTime())
961959
return flattenedDelegations
962-
}
960+
}

src/repository/proposal.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { Prisma } from '@prisma/client'
22
import { prisma } from '../config/db'
33
import { formatResult } from '../helpers/formatter'
44
import { GovActionStateTypes, ProposalTypes, SortTypes } from '../types/proposal'
5+
import { ProposalTypes, SortTypes } from '../types/proposal'
6+
57

68
export const fetchProposals = async (
79
page: number,
@@ -569,7 +571,24 @@ export const fetchProposalVoteCount = async (proposalId: string, proposalIndex:
569571
`
570572
const spoSelectionQuery = Prisma.sql`SELECT 'spo' AS voterRole, (SELECT jsonb_agg(to_jsonb(poolVoteRecord)) FROM poolVoteRecord) AS voteRecord`
571573
const ccVoteQuery = Prisma.sql`
572-
, committeeVoteRecord AS (
574+
, RatifiedCommitteeGovAction AS (
575+
SELECT gap.id
576+
FROM gov_action_proposal gap
577+
WHERE gap.type = 'NewCommittee'
578+
AND ratified_epoch IS NOT NULL
579+
ORDER BY gap.id DESC
580+
LIMIT 1
581+
),
582+
Committee AS (
583+
SELECT Count(DISTINCT(ch.raw)) as committeeMembersCount
584+
FROM committee c
585+
JOIN committee_member cm ON cm.committee_id = c.id
586+
JOIN committee_hash ch ON cm.committee_hash_id = ch.id
587+
LEFT JOIN RatifiedCommitteeGovAction rc
588+
ON rc.id = c.gov_action_proposal_id
589+
WHERE c.gov_action_proposal_id IS NULL
590+
),
591+
committeeVoteRecord AS (
573592
WITH CommitteeVotes AS (
574593
-- Count committee votes for each vote type
575594
SELECT
@@ -597,8 +616,8 @@ export const fetchProposalVoteCount = async (proposalId: string, proposalIndex:
597616
WHERE cm.expiration_epoch > (SELECT epoch FROM latestOrGovActionExpiration)
598617
ORDER BY ch.raw, cm.expiration_epoch DESC
599618
)
600-
SELECT 'total_distribution' as vote_type, COUNT(DISTINCT hash) AS count
601-
FROM CommitteeData
619+
SELECT 'total_distribution' as vote_type, committeeMembersCount AS count
620+
FROM Committee
602621
UNION ALL
603622
SELECT 'yes_votes' as vote_type, COALESCE(cc_Yes_Votes, 0) AS count FROM CommitteeVotes
604623
UNION ALL
@@ -1390,6 +1409,7 @@ export const fetchProposalById = async (proposalId: string, proposaIndex: number
13901409
any,
13911410
any
13921411
>[]
1412+
if (result.length == 0) return result
13931413
const proposalVoteCount = includeVoteCount
13941414
? { vote: await fetchProposalVoteCount(proposalId, proposaIndex) }
13951415
: undefined

0 commit comments

Comments
 (0)