-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add endpoint get pool's delegation history by pool
- Loading branch information
1 parent
d7a1078
commit b9ad7ab
Showing
13 changed files
with
306 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
webserver/server/app/controllers/DelegationForPoolController.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { Body, Controller, TsoaResponse, Res, Post, Route, SuccessResponse } from 'tsoa'; | ||
import { StatusCodes } from 'http-status-codes'; | ||
import tx from 'pg-tx'; | ||
import pool from '../services/PgPoolSingleton'; | ||
import type { ErrorShape } from '../../../shared/errors'; | ||
import { genErrorMessage } from '../../../shared/errors'; | ||
import { Errors } from '../../../shared/errors'; | ||
import type { EndpointTypes } from '../../../shared/routes'; | ||
import { Routes } from '../../../shared/routes'; | ||
import { getAddressTypes } from '../models/utils'; | ||
import { delegationsForPool } from '../services/DelegationForPool'; | ||
import { DelegationForPoolResponse } from '../../../shared/models/DelegationForPool'; | ||
|
||
const route = Routes.delegationForPool; | ||
|
||
@Route('delegation/pool') | ||
export class DelegationForPoolController extends Controller { | ||
@SuccessResponse(`${StatusCodes.OK}`) | ||
@Post() | ||
public async delegationForPool( | ||
@Body() | ||
requestBody: EndpointTypes[typeof route]['input'], | ||
@Res() | ||
errorResponse: TsoaResponse< | ||
StatusCodes.BAD_REQUEST | StatusCodes.CONFLICT | StatusCodes.UNPROCESSABLE_ENTITY, | ||
ErrorShape | ||
> | ||
): Promise<EndpointTypes[typeof route]['response']> { | ||
const response = await tx< | ||
DelegationForPoolResponse | ||
>(pool, async dbTx => { | ||
const data = await delegationsForPool({ | ||
pools: requestBody.pools.map(poolId => Buffer.from(poolId, 'hex')), | ||
range: requestBody.range, | ||
dbTx | ||
}); | ||
|
||
return data.map(data => ({ | ||
credential: data.credential as string, | ||
isDelegation: data.is_delegation as boolean, | ||
txId: data.tx_id as string, | ||
})); | ||
}); | ||
|
||
return response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
webserver/server/app/models/delegation/delegationForAddress.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
webserver/server/app/models/delegation/delegationsForPool.queries.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** Types generated for queries found in "app/models/delegation/delegationsForPool.sql" */ | ||
import { PreparedQuery } from '@pgtyped/query'; | ||
|
||
/** 'SqlStakeDelegationByPool' parameters type */ | ||
export interface ISqlStakeDelegationByPoolParams { | ||
max_slot: number; | ||
min_slot: number; | ||
pools: readonly (Buffer)[]; | ||
} | ||
|
||
/** 'SqlStakeDelegationByPool' return type */ | ||
export interface ISqlStakeDelegationByPoolResult { | ||
credential: string | null; | ||
is_delegation: boolean | null; | ||
tx_id: string | null; | ||
} | ||
|
||
/** 'SqlStakeDelegationByPool' query type */ | ||
export interface ISqlStakeDelegationByPoolQuery { | ||
params: ISqlStakeDelegationByPoolParams; | ||
result: ISqlStakeDelegationByPoolResult; | ||
} | ||
|
||
const sqlStakeDelegationByPoolIR: any = {"usedParamSet":{"pools":true,"min_slot":true,"max_slot":true},"params":[{"name":"pools","required":true,"transform":{"type":"array_spread"},"locs":[{"a":160,"b":166},{"a":505,"b":511},{"a":572,"b":578}]},{"name":"min_slot","required":true,"transform":{"type":"scalar"},"locs":[{"a":603,"b":612}]},{"name":"max_slot","required":true,"transform":{"type":"scalar"},"locs":[{"a":635,"b":644}]}],"statement":"SELECT \n\tencode(credential, 'hex') as credential,\n\tencode(\"Transaction\".hash, 'hex') as tx_id,\n\tCOALESCE(\"StakeDelegationCredentialRelation\".pool_credential IN :pools!, false) as is_delegation\nFROM \"StakeDelegationCredentialRelation\"\nJOIN \"StakeCredential\" ON stake_credential = \"StakeCredential\".id\nJOIN \"Transaction\" ON \"Transaction\".id = \"StakeDelegationCredentialRelation\".tx_id\nJOIN \"Block\" ON \"Transaction\".block_id = \"Block\".id\nWHERE \n (\n\t\t\"StakeDelegationCredentialRelation\".pool_credential IN :pools! OR\n\t \t\"StakeDelegationCredentialRelation\".previous_pool IN :pools!\n\t) AND\n\t\"Block\".slot > :min_slot! AND\n\t\"Block\".slot <= :max_slot!\nORDER BY (\"Block\".height, \"Transaction\".tx_index) ASC"}; | ||
|
||
/** | ||
* Query generated from SQL: | ||
* ``` | ||
* SELECT | ||
* encode(credential, 'hex') as credential, | ||
* encode("Transaction".hash, 'hex') as tx_id, | ||
* COALESCE("StakeDelegationCredentialRelation".pool_credential IN :pools!, false) as is_delegation | ||
* FROM "StakeDelegationCredentialRelation" | ||
* JOIN "StakeCredential" ON stake_credential = "StakeCredential".id | ||
* JOIN "Transaction" ON "Transaction".id = "StakeDelegationCredentialRelation".tx_id | ||
* JOIN "Block" ON "Transaction".block_id = "Block".id | ||
* WHERE | ||
* ( | ||
* "StakeDelegationCredentialRelation".pool_credential IN :pools! OR | ||
* "StakeDelegationCredentialRelation".previous_pool IN :pools! | ||
* ) AND | ||
* "Block".slot > :min_slot! AND | ||
* "Block".slot <= :max_slot! | ||
* ORDER BY ("Block".height, "Transaction".tx_index) ASC | ||
* ``` | ||
*/ | ||
export const sqlStakeDelegationByPool = new PreparedQuery<ISqlStakeDelegationByPoolParams,ISqlStakeDelegationByPoolResult>(sqlStakeDelegationByPoolIR); | ||
|
||
|
20 changes: 20 additions & 0 deletions
20
webserver/server/app/models/delegation/delegationsForPool.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
@name sqlStakeDelegationByPool | ||
@param pools -> (...) | ||
*/ | ||
SELECT | ||
encode(credential, 'hex') as credential, | ||
encode("Transaction".hash, 'hex') as tx_id, | ||
COALESCE("StakeDelegationCredentialRelation".pool_credential IN :pools!, false) as is_delegation | ||
FROM "StakeDelegationCredentialRelation" | ||
JOIN "StakeCredential" ON stake_credential = "StakeCredential".id | ||
JOIN "Transaction" ON "Transaction".id = "StakeDelegationCredentialRelation".tx_id | ||
JOIN "Block" ON "Transaction".block_id = "Block".id | ||
WHERE | ||
( | ||
"StakeDelegationCredentialRelation".pool_credential IN :pools! OR | ||
"StakeDelegationCredentialRelation".previous_pool IN :pools! | ||
) AND | ||
"Block".slot > :min_slot! AND | ||
"Block".slot <= :max_slot! | ||
ORDER BY ("Block".height, "Transaction".tx_index) ASC; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import type { PoolClient } from 'pg'; | ||
import { ISqlStakeDelegationForAddressResult, sqlStakeDelegationForAddress } from '../models/delegation/delegationForAddress.queries'; | ||
|
||
export async function delegationForAddress(request: { | ||
address: Buffer, | ||
until: { absoluteSlot: number }, | ||
dbTx: PoolClient, | ||
}): Promise<ISqlStakeDelegationForAddressResult> { | ||
return (await sqlStakeDelegationForAddress.run({ credential: request.address, slot: request.until.absoluteSlot }, request.dbTx))[0]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import type { PoolClient } from 'pg'; | ||
import { ISqlStakeDelegationByPoolResult, sqlStakeDelegationByPool } from '../models/delegation/delegationsForPool.queries'; | ||
|
||
|
||
export async function delegationsForPool(request: { | ||
range: { minSlot: number, maxSlot: number }, | ||
pools: Buffer[], | ||
dbTx: PoolClient, | ||
}): Promise<ISqlStakeDelegationByPoolResult[]> { | ||
return (await sqlStakeDelegationByPool.run({ | ||
min_slot: request.range.minSlot, | ||
max_slot: request.range.maxSlot, | ||
pools: request.pools | ||
}, request.dbTx)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Address } from "./Address"; | ||
import { Pool } from "./Pool"; | ||
|
||
export type DelegationForPoolRequest = { | ||
pools: Pool[]; | ||
range: { minSlot: number, maxSlot: number } | ||
}; | ||
|
||
export type DelegationForPoolResponse = { | ||
credential: Address; | ||
isDelegation: boolean, | ||
txId: string | null; | ||
}[]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* @pattern [0-9a-fA-F]{56} | ||
* @example "8200581c8baf48931c5187cd59fde553f4e7da2e1a2aa9202ec6e67815cb3f8a" | ||
*/ | ||
export type PoolHex = string; | ||
|
||
export type Pool = | ||
| PoolHex |
Oops, something went wrong.