Skip to content

Commit

Permalink
Merge pull request #277 from cardanoapi/enhancement/dbsync-api
Browse files Browse the repository at this point in the history
Enhancement/dbsync api
  • Loading branch information
Sital999 authored Dec 12, 2024
2 parents 14b1bc6 + 2a173b9 commit e566ae1
Show file tree
Hide file tree
Showing 5 changed files with 747 additions and 395 deletions.
69 changes: 53 additions & 16 deletions dbsync-api/src/controllers/drep.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,68 @@
import { Request, Response, Router } from "express";
import { handlerWrapper } from "../errors/AppError";
import { convertToHexIfBech32, isHexValue } from "../helpers/validator";
import { fetchDrepDetails, fetchDrepList } from "../repository/drep";
import {
fetchDrepDelegationDetails,
fetchDrepDetails,
fetchDrepList,
fetchDrepRegistrationDetails,
fetchDrepVoteDetails
} from "../repository/drep";
import { DrepSortType, DrepStatusType } from "../types/drep";

const router = Router();

const getDrepDetails = async (req: Request, res: Response): Promise<any> => {
const dRepId = convertToHexIfBech32(req.params.id as string)
if (dRepId && !isHexValue(dRepId)){
return res.status(400).json({message:'Provide a valid Drep ID'})
}
const result = await fetchDrepDetails(dRepId)
return res.status(200).json(result);
const dRepId = convertToHexIfBech32(req.params.id as string)
if (dRepId && !isHexValue(dRepId)) {
return res.status(400).json({message: 'Provide a valid Drep ID'})
}
const result = await fetchDrepDetails(dRepId)
return res.status(200).json(result);
};

const getDrepList= async(req:Request,res:Response )=>{
const size = req.query.size ? +req.query.size : 10
const page = req.query.page ? +req.query.page : 1
const status = req.query.status ? req.query.status as DrepStatusType : undefined
const sort = req.query.sort ? req.query.sort as DrepSortType : undefined
const search = convertToHexIfBech32(req.query.search as string)
const { items,totalCount } = await fetchDrepList(page,size,search,status,sort)
return res.status(200).json({total:totalCount,page,size,items})
const getDrepList = async (req: Request, res: Response) => {
const size = req.query.size ? +req.query.size : 10
const page = req.query.page ? +req.query.page : 1
const status = req.query.status ? req.query.status as DrepStatusType : undefined
const sort = req.query.sort ? req.query.sort as DrepSortType : undefined
const search = convertToHexIfBech32(req.query.search as string)
const {items, totalCount} = await fetchDrepList(page, size, search, status, sort)
return res.status(200).json({total: totalCount, page, size, items})
}

const getDrepVoteDetails = async (req: Request, res: Response) => {
const dRepId = convertToHexIfBech32(req.params.id as string)
if (dRepId && !isHexValue(dRepId)) {
return res.status(400).json({message: 'Provide a valid Drep ID'})
}
const result = await fetchDrepVoteDetails(dRepId)
return res.status(200).json(result);
}

const getDrepDelegationDetails = async (req: Request, res: Response) => {
const dRepId = convertToHexIfBech32(req.params.id as string)
if (dRepId && !isHexValue(dRepId)) {
return res.status(400).json({message: 'Provide a valid Drep ID'})
}
const result = await fetchDrepDelegationDetails(dRepId)
return res.status(200).json(result);
}

const getDrepRegistrationDetails = async (req: Request, res: Response) => {
const dRepId = convertToHexIfBech32(req.params.id as string)
if (dRepId && !isHexValue(dRepId)) {
return res.status(400).json({message: 'Provide a valid Drep ID'})
}
const result = await fetchDrepRegistrationDetails(dRepId)
return res.status(200).json(result);
}

router.get('/', handlerWrapper(getDrepList));
router.get('/:id', handlerWrapper(getDrepDetails));
router.get('/',handlerWrapper(getDrepList));
router.get('/:id/vote', handlerWrapper(getDrepVoteDetails))
router.get('/:id/delegation', handlerWrapper(getDrepDelegationDetails))
router.get('/:id/registration', handlerWrapper(getDrepRegistrationDetails))


export default router;
31 changes: 30 additions & 1 deletion dbsync-api/src/helpers/formatter.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
export function formatResult(result:Array<Record<string,any>>){
return result.map((item)=>item.result)
}
}

export function combineArraysWithSameObjectKey(array1: any, array2: any) {
// Create a map for array2 based on the stake_address
const map2 = new Map();
array2.forEach((item: any) => {
const { stakeAddress, ...rest } = item.json_build_object;
map2.set(stakeAddress, rest); // Use stake_address as the key
});

// Combine the data from array1 and array2 based on stake_address
return array1.map((item1: any) => {
const { stakeAddress, ...rest1 } = item1.json_build_object;
const matchedData = map2.get(stakeAddress);

if (matchedData) {
// Combine both objects if match is found
return {
json_build_object: {
...rest1,
...matchedData,
stakeAddress
}
};
} else {
// Return the original object if no match is found in array2
return item1;
}
});
}
7 changes: 6 additions & 1 deletion dbsync-api/src/repository/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ export const fetchFaucetBalance= async (address:string)=>{
GROUP BY stake_address.hash_raw
` as Array<Record<string, number>>
return result.length?+result[0].total_value:0
}
}

// UTXO SUM
// + REWARD_REST
// + REWARD
// (aLSO MAKE SURE THAT IT IS NOT ALREADY WITHEDRAWAN BY SEEING THE WITHDRAWALS TABLE)
Loading

0 comments on commit e566ae1

Please sign in to comment.