Skip to content

Commit

Permalink
feat: handle drep/info endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
MSzalowski committed Oct 29, 2024
1 parent dd31dcb commit fdb487f
Show file tree
Hide file tree
Showing 4 changed files with 301 additions and 10 deletions.
12 changes: 6 additions & 6 deletions backend/src/ada-holder/ada-holder.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Controller, Get, Query } from '@nestjs/common';
import { ApiOperation, ApiQuery, ApiTags } from '@nestjs/swagger';
import { Controller, Get, Param } from '@nestjs/common';
import { ApiOperation, ApiParam, ApiTags } from '@nestjs/swagger';
import { AdaHolderService } from './ada-holder.service';

@ApiTags('ada-holder')
Expand All @@ -9,15 +9,15 @@ export class AdaHolderController {

@Get('get-current-delegation/:stakeKey')
@ApiOperation({ summary: 'Get current delegation of a stake key' })
@ApiQuery({ name: 'stakeKey', type: 'string', required: true })
async getCurrentDelegation(@Query('stakeKey') stakeKey: string) {
@ApiParam({ name: 'stakeKey', type: 'string', required: true })
async getCurrentDelegation(@Param('stakeKey') stakeKey: string) {
return this.adaHolderService.getCurrentDelegation(stakeKey);
}

@Get('get-voting-power/:stakeKey')
@ApiOperation({ summary: 'Get voting power of a stake key' })
@ApiQuery({ name: 'stakeKey', type: 'string', required: true })
async getVotingPower(@Query('stakeKey') stakeKey: string) {
@ApiParam({ name: 'stakeKey', type: 'string', required: true })
async getVotingPower(@Param('stakeKey') stakeKey: string) {
return this.adaHolderService.getVotingPower(stakeKey);
}
}
21 changes: 17 additions & 4 deletions backend/src/drep/drep.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { Controller, Get, Query } from '@nestjs/common';
import { ApiOperation, ApiQuery, ApiResponse, ApiTags } from '@nestjs/swagger';
import { Controller, Get, Param, Query } from '@nestjs/common';
import {
ApiOperation,
ApiParam,
ApiQuery,
ApiResponse,
ApiTags,
} from '@nestjs/swagger';
import { DrepService } from './drep.service';
import { DRepListParamsDto } from './drep.dto';

Expand All @@ -10,8 +16,8 @@ export class DrepController {

@Get('get-voting-power/:drepId')
@ApiOperation({ summary: 'Get voting power of a drep id' })
@ApiQuery({ name: 'drepId', type: 'string', required: true })
async getVotingPower(@Query('drepId') drepId: string) {
@ApiParam({ name: 'drepId', type: 'string', required: true })
async getVotingPower(@Param('drepId') drepId: string) {
return this.drepService.getVotingPower(drepId);
}

Expand Down Expand Up @@ -60,4 +66,11 @@ export class DrepController {
async getDRepList(@Query() query: DRepListParamsDto) {
return this.drepService.listDReps(query);
}

@Get('info/:drepId')
@ApiOperation({ summary: 'Get information of a DRep' })
@ApiParam({ name: 'drepId', type: 'string', required: true })
async getDRepInfo(@Param('drepId') drepId: string) {
return this.drepService.getDRepInfo(drepId);
}
}
63 changes: 63 additions & 0 deletions backend/src/drep/drep.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,69 @@ export class DrepService {
};
}

async getDRepInfo(drepId: string) {
const sqlFilePath = path.join(__dirname, '../sql', 'get-drep-info.sql');
const sql = fs.readFileSync(sqlFilePath, 'utf8');
try {
const result = await this.dataSource.query(sql, [drepId]);

if (result.length === 0) {
return null;
}

const [
{
has_script: isScriptBased,
is_registed_as_drep: isRegisteredAsDRep,
was_registered_as_drep: wasRegisteredAsDRep,
is_registered_as_sole_voter: isRegisteredAsSoleVoter,
was_registered_as_sole_voter: wasRegisteredAsSoleVoter,
deposit,
url,
data_hash: dataHash,
voting_power: votingPower,
drep_register_tx: dRepRegisterTx,
drep_retire_tx: dRepRetireTx,
sole_voter_register_tx: soleVoterRegisterTx,
sole_voter_retire_tx: soleVoterRetireTx,
payment_address: paymentAddress,
given_name: givenName,
objectives,
motivations,
qualifications,
image_url: imageUrl,
image_hash: imageHash,
},
] = result;

return {
isScriptBased,
isRegisteredAsDRep: Boolean(isRegisteredAsDRep),
wasRegisteredAsDRep: Boolean(wasRegisteredAsDRep),
isRegisteredAsSoleVoter: Boolean(isRegisteredAsSoleVoter),
wasRegisteredAsSoleVoter: Boolean(wasRegisteredAsSoleVoter),
deposit,
url,
dataHash,
votingPower,
dRepRegisterTx,
dRepRetireTx,
soleVoterRegisterTx,
soleVoterRetireTx,
paymentAddress,
givenName,
objectives,
motivations,
qualifications,
imageUrl,
imageHash,
};
} catch (error) {
console.error('Error executing getDRepInfo query:', error);
return null;
}
}

private mapDRepListItem(dRep: RawQueryDRepListItemType): DRepListItemType {
return {
dRepHash: dRep.drep_id,
Expand Down
215 changes: 215 additions & 0 deletions backend/src/sql/get-drep-info.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
WITH DRepId AS (
SELECT
decode($1, 'hex') AS raw
),
AllRegistrationEntries AS (
SELECT
drep_registration.voting_anchor_id AS voting_anchor_id,
drep_registration.deposit AS deposit,
tx.hash AS tx_hash,
tx.id as tx_id
FROM
drep_registration
CROSS JOIN DRepId
JOIN drep_hash ON drep_hash.id = drep_registration.drep_hash_id
JOIN tx ON tx.id = drep_registration.tx_id

WHERE
drep_hash.raw = DRepId.raw
ORDER BY drep_registration.tx_id asc
),
LatestRegistrationEntry AS (
SELECT
drep_registration.voting_anchor_id AS voting_anchor_id,
drep_registration.deposit AS deposit,
tx.hash AS tx_hash
FROM
drep_registration
CROSS JOIN DrepId
JOIN drep_hash ON drep_hash.id = drep_registration.drep_hash_id
JOIN tx ON tx.id = drep_registration.tx_id
WHERE
drep_hash.raw = DRepId.raw
ORDER BY
drep_registration.tx_id DESC
LIMIT 1
),
IsScriptHash AS (
SELECT EXISTS(
SELECT
drep_hash.has_script
FROM
drep_hash
CROSS JOIN DRepId
WHERE
drep_hash.raw = DRepId.raw
AND
drep_hash.has_script = true
) AS has_script),
IsRegisteredAsDRep AS (
SELECT
(LatestRegistrationEntry.deposit IS NULL
OR LatestRegistrationEntry.deposit > 0)
AND LatestRegistrationEntry.voting_anchor_id IS NOT NULL AS value
FROM
LatestRegistrationEntry
),
IsRegisteredAsSoleVoter AS (
SELECT
(LatestRegistrationEntry.deposit IS NULL
OR LatestRegistrationEntry.deposit > 0)
AND LatestRegistrationEntry.voting_anchor_id IS NULL AS value
FROM
LatestRegistrationEntry
),
CurrentDeposit AS (
SELECT
GREATEST(drep_registration.deposit, 0) AS value
FROM
drep_registration
JOIN drep_hash ON drep_hash.id = drep_registration.drep_hash_id
CROSS JOIN DRepId
WHERE
drep_registration.deposit IS NOT NULL
AND drep_hash.raw = DRepId.raw
ORDER BY
drep_registration.tx_id DESC
LIMIT 1
),
WasRegisteredAsDRep AS (
SELECT
(EXISTS (
SELECT
*
FROM
drep_registration
JOIN drep_hash ON drep_hash.id = drep_registration.drep_hash_id
CROSS JOIN DRepId
WHERE
drep_hash.raw = DRepId.raw
AND drep_registration.voting_anchor_id IS NOT NULL)) AS value
),
WasRegisteredAsSoleVoter AS (
SELECT
(EXISTS (
SELECT
*
FROM
drep_registration
JOIN drep_hash ON drep_hash.id = drep_registration.drep_hash_id
CROSS JOIN DRepId
WHERE
drep_hash.raw = DRepId.raw
AND drep_registration.voting_anchor_id IS NULL)) AS value
),
CurrentMetadata AS (
SELECT
voting_anchor.url AS url,
encode(voting_anchor.data_hash, 'hex') AS data_hash
FROM
LatestRegistrationEntry
LEFT JOIN voting_anchor ON voting_anchor.id = LatestRegistrationEntry.voting_anchor_id
LIMIT 1
),
CurrentVotingPower AS (
SELECT
amount AS amount
FROM
drep_hash
JOIN DRepId ON drep_hash.raw = DRepId.raw
LEFT JOIN drep_distr ON drep_distr.hash_id = drep_hash.id
ORDER BY
drep_distr.epoch_no DESC
LIMIT 1
),
DRepRegister AS (
SELECT
encode(AllRegistrationEntries.tx_hash, 'hex') as tx_hash,
AllRegistrationEntries.tx_id
FROM
(SELECT 1) AS dummy
LEFT JOIN
AllRegistrationEntries ON AllRegistrationEntries.voting_anchor_id IS NOT NULL and not (coalesce(deposit,0) < 0)
ORDER BY
AllRegistrationEntries.tx_id DESC
LIMIT 1
),
DRepRetire AS (
SELECT
encode(AllRegistrationEntries.tx_hash, 'hex') as tx_hash,
AllRegistrationEntries.tx_id as tx_id
FROM
DRepRegister
LEFT JOIN
AllRegistrationEntries ON (AllRegistrationEntries.deposit < 0
OR AllRegistrationEntries.voting_anchor_id IS NULL)
and AllRegistrationEntries.tx_id > DRepRegister.tx_id
ORDER BY
AllRegistrationEntries.tx_id asc

LIMIT 1
),

SoleVoterRegister AS (
SELECT
encode(AllRegistrationEntries.tx_hash, 'hex') as tx_hash,
AllRegistrationEntries.tx_id
FROM
(SELECT 1) AS dummy
LEFT JOIN
AllRegistrationEntries ON AllRegistrationEntries.voting_anchor_id IS NULL and not (coalesce(deposit,0) < 0)
ORDER BY
AllRegistrationEntries.tx_id DESC
LIMIT 1
),
SoleVoterRetire AS (
SELECT
encode(AllRegistrationEntries.tx_hash, 'hex') as tx_hash
FROM
SoleVoterRegister
LEFT JOIN
AllRegistrationEntries ON (AllRegistrationEntries.deposit < 0
OR AllRegistrationEntries.voting_anchor_id IS NOT NULL)
AND AllRegistrationEntries.tx_id > SoleVoterRegister.tx_id
ORDER BY
AllRegistrationEntries.tx_id asc

LIMIT 1
)
SELECT
IsScriptHash.has_script,
IsRegisteredAsDRep.value,
WasRegisteredAsDRep.value,
IsRegisteredAsSoleVoter.value,
WasRegisteredAsSoleVoter.value,
CurrentDeposit.value,
CurrentMetadata.url,
CurrentMetadata.data_hash,
CurrentVotingPower.amount,
DRepRegister.tx_hash,
DRepRetire.tx_hash,
SoleVoterRegister.tx_hash,
SoleVoterRetire.tx_hash,
off_chain_vote_drep_data.payment_address,
off_chain_vote_drep_data.given_name,
off_chain_vote_drep_data.objectives,
off_chain_vote_drep_data.motivations,
off_chain_vote_drep_data.qualifications,
off_chain_vote_drep_data.image_url,
off_chain_vote_drep_data.image_hash
FROM
IsRegisteredAsDRep
CROSS JOIN IsRegisteredAsSoleVoter
CROSS JOIN WasRegisteredAsDRep
CROSS JOIN WasRegisteredAsSoleVoter
CROSS JOIN CurrentDeposit
CROSS JOIN CurrentMetadata
CROSS JOIN CurrentVotingPower
CROSS JOIN DRepRegister
CROSS JOIN DRepRetire
CROSS JOIN SoleVoterRegister
CROSS JOIN SoleVoterRetire
CROSS JOIN LatestRegistrationEntry
CROSS JOIN IsScriptHash
LEFT JOIN off_chain_vote_data ON off_chain_vote_data.voting_anchor_id = LatestRegistrationEntry.voting_anchor_id
LEFT JOIN off_chain_vote_drep_data ON off_chain_vote_drep_data.off_chain_vote_data_id = off_chain_vote_data.id

0 comments on commit fdb487f

Please sign in to comment.