Skip to content

Commit 6b8c78b

Browse files
committed
feat!: add deposit to stake pool registration certificates
1 parent a0a2ead commit 6b8c78b

File tree

6 files changed

+34
-44
lines changed

6 files changed

+34
-44
lines changed

packages/cardano-services/src/ChainHistory/DbSyncChainHistory/mappers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ export const mapCertificate = (
168168
return {
169169
__typename: Cardano.CertificateType.PoolRegistration,
170170
cert_index: certModel.cert_index,
171+
deposit: BigInt(certModel.deposit),
171172
poolParameters: null as unknown as Cardano.PoolParameters
172173
} as WithCertIndex<Cardano.PoolRegistrationCertificate>;
173174

packages/cardano-services/src/ChainHistory/DbSyncChainHistory/queries.ts

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -238,13 +238,32 @@ export const findPoolRetireCertsTxIds = `
238238
ORDER BY tx.id ASC`;
239239

240240
export const findPoolRegisterCertsByTxIds = `
241-
SELECT
241+
SELECT
242242
cert.cert_index AS cert_index,
243243
pool."view" AS pool_id,
244-
tx.hash AS tx_id
244+
tx.hash AS tx_id,
245+
CASE
246+
WHEN (
247+
SELECT count(*) = 0
248+
FROM pool_update AS pu
249+
WHERE pu.registered_tx_id BETWEEN (
250+
CASE WHEN (
251+
SELECT count(*) FROM pool_retire AS pr
252+
WHERE pr.announced_tx_id < cert.registered_tx_id
253+
) > 0 THEN (
254+
SELECT max(pr.announced_tx_id) FROM pool_retire AS pr
255+
WHERE pr.announced_tx_id < cert.registered_tx_id
256+
)
257+
ELSE 0 END
258+
) AND cert.registered_tx_id - 1
259+
) THEN pool_deposit
260+
ELSE '0'
261+
END AS deposit
245262
FROM tx
246263
JOIN pool_update AS cert ON cert.registered_tx_id = tx.id
247264
JOIN pool_hash AS pool ON pool.id = cert.hash_id
265+
JOIN block ON block_id = block.id
266+
JOIN epoch_param ON epoch_param.epoch_no = (CASE WHEN block.epoch_no > 1 THEN block.epoch_no ELSE 1 END)
248267
WHERE tx.id = ANY($1)
249268
ORDER BY tx.id ASC`;
250269

@@ -413,42 +432,3 @@ FROM combined`,
413432
WITH: ''
414433
}
415434
} as const;
416-
417-
/*
418-
(SELECT
419-
tx.id,
420-
cert.cert_index AS cert_index,
421-
addr."view" AS address,
422-
TRUE AS registration,
423-
tx.hash AS tx_id,
424-
epoch_param.key_deposit AS deposit
425-
FROM tx
426-
JOIN stake_registration AS cert ON cert.tx_id = tx.id
427-
JOIN stake_address AS addr ON addr.id = cert.addr_id
428-
JOIN block ON block_id = block.id
429-
JOIN epoch_param ON block.epoch_no = epoch_param.epoch_no
430-
WHERE tx.id in (134,135,143,144)
431-
ORDER BY tx.id ASC)
432-
UNION
433-
(SELECT
434-
tx.id,
435-
cert.cert_index AS cert_index,
436-
addr."view" AS address,
437-
FALSE AS registration,
438-
tx.hash AS tx_id,
439-
-(SELECT key_deposit FROM stake_registration AS sr
440-
JOIN tx AS tx2 ON sr.tx_id = tx2.id
441-
JOIN block ON tx2.block_id = block.id
442-
JOIN epoch_param ON block.epoch_no = epoch_param.epoch_no
443-
WHERE sr.addr_id = cert.addr_id
444-
AND sr.tx_id < tx.id
445-
ORDER BY sr.tx_id DESC
446-
LIMIT 1
447-
) AS deposit
448-
FROM tx
449-
JOIN stake_deregistration AS cert ON cert.tx_id = tx.id
450-
JOIN stake_address AS addr ON addr.id = cert.addr_id
451-
WHERE tx.id in (134,135,143,144)
452-
ORDER BY tx.id ASC);
453-
454-
*/

packages/cardano-services/src/ChainHistory/DbSyncChainHistory/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,10 @@ export interface PoolRetireCertModel extends CertificateModel {
163163
retiring_epoch: number;
164164
pool_id: string;
165165
}
166+
166167
export interface PoolRegisterCertModel extends CertificateModel {
167168
pool_id: string;
169+
deposit: string;
168170
}
169171

170172
export interface MirCertModel extends CertificateModel {

packages/core/src/Cardano/types/Certificate.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ export interface StakeAddressCertificate {
115115

116116
export interface PoolRegistrationCertificate {
117117
__typename: CertificateType.PoolRegistration;
118+
deposit: Lovelace;
118119
poolParameters: PoolParameters;
119120
}
120121

packages/core/src/Serialization/Certificates/PoolRegistration.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const EMBEDDED_GROUP_SIZE = 10;
1313
* about the pool such as the pledge, costs, margin, reward account, and the pool's owners and relays.
1414
*/
1515
export class PoolRegistration {
16+
#deposit: Cardano.Lovelace;
1617
#params: PoolParams;
1718
#originalBytes: HexBlob | undefined = undefined;
1819

@@ -21,7 +22,8 @@ export class PoolRegistration {
2122
*
2223
* @param params The pool registration/update parameters.
2324
*/
24-
constructor(params: PoolParams) {
25+
constructor(params: PoolParams, deposit: Cardano.Lovelace) {
26+
this.#deposit = deposit;
2527
this.#params = params;
2628
}
2729

@@ -146,7 +148,8 @@ export class PoolRegistration {
146148

147149
reader.readEndArray();
148150

149-
const cert = new PoolRegistration(params);
151+
// TODO: the hard-coded 0 deposit should actually be read/computed from tx implicit coins (?)
152+
const cert = new PoolRegistration(params, 0n);
150153
cert.#originalBytes = cbor;
151154

152155
return cert;
@@ -160,6 +163,7 @@ export class PoolRegistration {
160163
toCore(): Cardano.PoolRegistrationCertificate {
161164
return {
162165
__typename: Cardano.CertificateType.PoolRegistration,
166+
deposit: this.#deposit,
163167
poolParameters: this.#params.toCore()
164168
};
165169
}
@@ -170,7 +174,7 @@ export class PoolRegistration {
170174
* @param cert core PoolRegistrationCertificate object.
171175
*/
172176
static fromCore(cert: Cardano.PoolRegistrationCertificate) {
173-
return new PoolRegistration(PoolParams.fromCore(cert.poolParameters)); // TODO: Core type does not support script hash as credential, fix?
177+
return new PoolRegistration(PoolParams.fromCore(cert.poolParameters), cert.deposit); // TODO: Core type does not support script hash as credential, fix?
174178
}
175179

176180
/**

packages/projection/src/operators/Mappers/certificates/withStakePools.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ export const withStakePools = unifiedProjectorOperator<WithCertificates & WithEp
3131
switch (certificate.__typename) {
3232
case Cardano.CertificateType.PoolRegistration:
3333
updates[certificate.poolParameters.id] = {
34+
// TODO: the hard-coded 0 deposit should actually be read/computed from tx implicit coins or from stake pool status (?)
35+
deposit: 0n,
3436
poolParameters: certificate.poolParameters,
3537
source
3638
};

0 commit comments

Comments
 (0)