Skip to content

Commit fa98833

Browse files
authored
Merge pull request #340 from cardanoapi/feat/dbsync/proposal-apis
API for protocol parameters & cost model info in proposal details
2 parents ee2ad5d + bf4b182 commit fa98833

File tree

5 files changed

+89
-6
lines changed

5 files changed

+89
-6
lines changed

dbsync-api/src/controllers/blockchain.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Request, Response, Router } from 'express'
22
import { handlerWrapper } from '../errors/AppError'
3-
import { fetchEpochDuration } from '../repository/blockchain'
3+
import { fetchEpochDuration, fetchEpochParams } from '../repository/blockchain'
44

55
const router = Router()
66

@@ -10,6 +10,13 @@ const getEpochDuration = async (req: Request, res: Response): Promise<any> => {
1010
return res.status(200).json(result)
1111
}
1212

13+
const getEpochParams = async (req: Request, res: Response): Promise<any> => {
14+
const epoch_no = req.query.epoch_no as string
15+
const result = await fetchEpochParams(parseInt(epoch_no) ? parseInt(epoch_no) : undefined)
16+
return res.status(200).json(result)
17+
}
18+
1319
router.get('/epoch', handlerWrapper(getEpochDuration))
20+
router.get('/epoch/params', handlerWrapper(getEpochParams))
1421

1522
export default router

dbsync-api/src/controllers/proposal.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const getProposals = async (req: Request, res: Response) => {
1818
if (!validateHash(proposal)) {
1919
return res.status(400).json({ message: 'Provide valid proposal Id' })
2020
}
21-
proposal = proposal.includes('#') ? proposal.slice(0, -2) : proposal
21+
proposal = proposal.includes('#') ? proposal.split('#')[0] : proposal
2222
}
2323
const { items, totalCount } = await fetchProposals(page, size, proposal, type, sort, includeVoteCount)
2424
return res.status(200).json({ totalCount: Math.round(totalCount / size), page, size, items })

dbsync-api/src/repository/blockchain.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Prisma } from '@prisma/client'
12
import { prisma } from '../config/db'
23

34
export async function fetchEpochDuration(limit: number) {
@@ -23,3 +24,25 @@ export async function fetchEpochDuration(limit: number) {
2324

2425
return parsedResult
2526
}
27+
28+
export async function fetchEpochParams(epoch_no?: number) {
29+
const result = (await prisma.$queryRaw`
30+
SELECT
31+
jsonb_set(
32+
ROW_TO_JSON(epoch_param)::jsonb,
33+
'{cost_model}',
34+
CASE
35+
WHEN cost_model.id IS NOT NULL THEN
36+
ROW_TO_JSON(cost_model)::jsonb
37+
ELSE
38+
'null'::jsonb
39+
END
40+
) AS epoch_param
41+
FROM
42+
epoch_param
43+
LEFT JOIN
44+
cost_model ON epoch_param.cost_model_id = cost_model.id
45+
WHERE epoch_no = ${epoch_no ? epoch_no : Prisma.sql`(SELECT no from epoch order by no desc limit 1)`}
46+
LIMIT 1;`) as Record<string, any>[]
47+
return result[0].epoch_param
48+
}

dbsync-api/src/repository/proposal.ts

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,18 @@ SELECT
126126
'type', gov_action_proposal.type::text,
127127
'details', CASE
128128
when gov_action_proposal.type = 'TreasuryWithdrawals' then
129-
json_build_object('Reward Address', stake_address.view, 'Amount', treasury_withdrawal.amount)
129+
(
130+
SELECT json_agg(
131+
jsonb_build_object(
132+
'receivingAddress', stake_address.view,
133+
'amount', treasury_withdrawal.amount
134+
)
135+
)
136+
FROM treasury_withdrawal
137+
LEFT JOIN stake_address
138+
ON stake_address.id = treasury_withdrawal.stake_address_id
139+
WHERE treasury_withdrawal.gov_action_proposal_id = gov_action_proposal.id
140+
)
130141
when gov_action_proposal.type::text = 'InfoAction' then
131142
json_build_object('data', gov_action_proposal.description)
132143
when gov_action_proposal.type::text = 'HardForkInitiation' then
@@ -185,7 +196,16 @@ SELECT
185196
'createdSlotNo', creator_block.slot_no,
186197
'url', voting_anchor.url,
187198
'metadataHash', encode(voting_anchor.data_hash, 'hex'),
188-
'protocolParams', ROW_TO_JSON(proposal_params),
199+
'protocolParams', jsonb_set(
200+
ROW_TO_JSON(proposal_params)::jsonb,
201+
'{cost_model}',
202+
CASE
203+
WHEN cost_model.id IS NOT NULL THEN
204+
ROW_TO_JSON(cost_model)::jsonb
205+
ELSE
206+
'null'::jsonb
207+
END
208+
),
189209
'title', off_chain_vote_gov_action_data.title,
190210
'abstract', off_chain_vote_gov_action_data.abstract,
191211
'motivation', off_chain_vote_gov_action_data.motivation,
@@ -226,6 +246,7 @@ FROM
226246
JOIN block AS creator_block ON creator_block.id = creator_tx.block_id
227247
LEFT JOIN voting_anchor ON voting_anchor.id = gov_action_proposal.voting_anchor_id
228248
LEFT JOIN param_proposal as proposal_params ON gov_action_proposal.param_proposal = proposal_params.id
249+
LEFT JOIN cost_model AS cost_model ON proposal_params.cost_model_id = cost_model.id
229250
LEFT JOIN off_chain_vote_data ON off_chain_vote_data.voting_anchor_id = voting_anchor.id
230251
LEFT JOIN off_chain_vote_gov_action_data ON off_chain_vote_gov_action_data.off_chain_vote_data_id = off_chain_vote_data.id
231252
LEFT JOIN voting_procedure ON voting_procedure.gov_action_proposal_id = gov_action_proposal.id
@@ -300,6 +321,7 @@ GROUP BY
300321
epoch_utils.last_epoch_no,
301322
epoch_utils.last_epoch_end_time,
302323
proposal_params,
324+
cost_model.id,
303325
voting_anchor.url,
304326
voting_anchor.data_hash,
305327
always_no_confidence_voting_power.amount,
@@ -1155,7 +1177,16 @@ export const fetchProposalById = async (proposalId: string, proposaIndex: number
11551177
'createdSlotNo', creator_block.slot_no,
11561178
'url', voting_anchor.url,
11571179
'metadataHash', encode(voting_anchor.data_hash, 'hex'),
1158-
'protocolParams', ROW_TO_JSON(proposal_params),
1180+
'protocolParams', jsonb_set(
1181+
ROW_TO_JSON(proposal_params)::jsonb,
1182+
'{cost_model}',
1183+
CASE
1184+
WHEN cost_model.id IS NOT NULL THEN
1185+
ROW_TO_JSON(cost_model)::jsonb
1186+
ELSE
1187+
'null'::jsonb
1188+
END
1189+
),
11591190
'title', off_chain_vote_gov_action_data.title,
11601191
'abstract', off_chain_vote_gov_action_data.abstract,
11611192
'motivation', off_chain_vote_gov_action_data.motivation,
@@ -1196,6 +1227,7 @@ export const fetchProposalById = async (proposalId: string, proposaIndex: number
11961227
JOIN block AS creator_block ON creator_block.id = creator_tx.block_id
11971228
LEFT JOIN voting_anchor ON voting_anchor.id = gov_action_proposal.voting_anchor_id
11981229
LEFT JOIN param_proposal as proposal_params ON gov_action_proposal.param_proposal = proposal_params.id
1230+
LEFT JOIN cost_model AS cost_model ON proposal_params.cost_model_id = cost_model.id
11991231
LEFT JOIN off_chain_vote_data ON off_chain_vote_data.voting_anchor_id = voting_anchor.id
12001232
LEFT JOIN off_chain_vote_gov_action_data ON off_chain_vote_gov_action_data.off_chain_vote_data_id = off_chain_vote_data.id
12011233
LEFT JOIN voting_procedure ON voting_procedure.gov_action_proposal_id = gov_action_proposal.id
@@ -1265,6 +1297,7 @@ export const fetchProposalById = async (proposalId: string, proposaIndex: number
12651297
epoch_utils.last_epoch_no,
12661298
epoch_utils.last_epoch_end_time,
12671299
proposal_params,
1300+
cost_model.id,
12681301
voting_anchor.url,
12691302
voting_anchor.data_hash,
12701303
always_no_confidence_voting_power.amount,

dbsync-api/swagger.yaml

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ paths:
605605
description: The information of the last 'n' epochs.
606606
responses:
607607
'200':
608-
description: The faucet balance for the given address
608+
description: epoch details
609609
content:
610610
application/json:
611611
schema:
@@ -635,6 +635,26 @@ paths:
635635
description: Invalid address provided
636636
'500':
637637
description: Internal server error
638+
/api/blockchain/epoch/params:
639+
get:
640+
summary: Get protocol parameters of specific epoch if specified. Else, get latest protocol parameters
641+
description: Get protocol parameters of specific epoch if specified. Else, get latest protocol parameters
642+
tags:
643+
- Blockchain
644+
parameters:
645+
- in: query
646+
name: epoch_no
647+
schema:
648+
type: integer
649+
required: false
650+
description: get protocol parameters of specified epoch.
651+
responses:
652+
'200':
653+
description: protocol parameters
654+
'400':
655+
description: Invalid address provided
656+
'500':
657+
description: Internal server error
638658
/api/drep:
639659
get:
640660
summary: Fetch DRep list

0 commit comments

Comments
 (0)