Skip to content

Commit 0839f57

Browse files
authored
Merge pull request #1101 from input-output-hk/fix/LW-9604-deposit-values
LW-9604 deposit values
2 parents 93f4776 + d5d00f1 commit 0839f57

File tree

8 files changed

+53
-16
lines changed

8 files changed

+53
-16
lines changed

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ export const mapAnchor = (anchorUrl: string, anchorDataHash: string): Cardano.An
155155
export const mapCertificate = (
156156
certModel: WithCertType<CertificateModel>
157157
// eslint-disable-next-line sonarjs/cognitive-complexity
158-
): WithCertIndex<Cardano.Certificate> | null => {
158+
): WithCertIndex<Cardano.HydratedCertificate> | null => {
159159
if (isPoolRetireCertModel(certModel))
160160
return {
161161
__typename: Cardano.CertificateType.PoolRetirement,
@@ -168,8 +168,9 @@ 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
172-
} as WithCertIndex<Cardano.PoolRegistrationCertificate>;
173+
} as WithCertIndex<Cardano.HydratedPoolRegistrationCertificate>;
173174

174175
if (isMirCertModel(certModel)) {
175176
const credential = Cardano.Address.fromString(certModel.address)?.asReward()?.getPaymentCredential();

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

+30-5
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

@@ -293,12 +312,18 @@ export const findStakeCertsByTxIds = `
293312
addr."view" AS address,
294313
FALSE AS registration,
295314
tx.hash AS tx_id,
296-
-epoch_param.key_deposit AS deposit
315+
-(SELECT key_deposit FROM stake_registration AS sr
316+
JOIN tx AS tx2 ON sr.tx_id = tx2.id
317+
JOIN block ON tx2.block_id = block.id
318+
JOIN epoch_param ON block.epoch_no = epoch_param.epoch_no
319+
WHERE sr.addr_id = cert.addr_id
320+
AND sr.tx_id < tx.id
321+
ORDER BY sr.tx_id DESC
322+
LIMIT 1
323+
) AS deposit
297324
FROM tx
298325
JOIN stake_deregistration AS cert ON cert.tx_id = tx.id
299326
JOIN stake_address AS addr ON addr.id = cert.addr_id
300-
JOIN block ON block_id = block.id
301-
JOIN epoch_param ON block.epoch_no = epoch_param.epoch_no
302327
WHERE tx.id = ANY($1)
303328
ORDER BY tx.id ASC)`;
304329

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

+2
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/cardano-services/test/ChainHistory/DbSyncChainHistoryProvider/mappers.test.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -201,15 +201,17 @@ describe('chain history mappers', () => {
201201
poolId: Cardano.PoolId(poolId)
202202
});
203203
});
204-
test('map PoolUpdateCertModel to Cardano.PoolRegistrationCertificate', () => {
204+
test('map PoolUpdateCertModel to Cardano.HydratedPoolRegistrationCertificate', () => {
205205
const result = mappers.mapCertificate({
206206
...baseCertModel,
207+
deposit: '500000000',
207208
pool_id: poolId,
208209
type: 'register'
209210
} as WithCertType<PoolRegisterCertModel>);
210-
expect(result).toEqual<WithCertIndex<Cardano.PoolRegistrationCertificate>>({
211+
expect(result).toEqual<WithCertIndex<Cardano.HydratedPoolRegistrationCertificate>>({
211212
__typename: Cardano.CertificateType.PoolRegistration,
212213
cert_index: 0,
214+
deposit: 500_000_000n,
213215
poolParameters: null as unknown as Cardano.PoolParameters
214216
});
215217
});

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

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as Crypto from '@cardano-sdk/crypto';
22
import { AuxiliaryData } from './AuxiliaryData';
33
import { Base64Blob, HexBlob, OpaqueString } from '@cardano-sdk/util';
4-
import { Certificate } from './Certificate';
4+
import { Certificate, PoolRegistrationCertificate } from './Certificate';
55
import { ExUnits, Update, ValidityInterval } from './ProtocolParameters';
66
import { HydratedTxIn, TxIn, TxOut } from './Utxo';
77
import { Lovelace, TokenMap } from './Value';
@@ -36,14 +36,20 @@ export interface Withdrawal {
3636
quantity: Lovelace;
3737
}
3838

39+
export type HydratedPoolRegistrationCertificate = PoolRegistrationCertificate & { deposit?: Lovelace };
40+
41+
export type HydratedCertificate =
42+
| Exclude<Certificate, PoolRegistrationCertificate>
43+
| HydratedPoolRegistrationCertificate;
44+
3945
export interface HydratedTxBody {
4046
inputs: HydratedTxIn[];
4147
collaterals?: HydratedTxIn[];
4248
outputs: TxOut[];
4349
fee: Lovelace;
4450
validityInterval?: ValidityInterval;
4551
withdrawals?: Withdrawal[];
46-
certificates?: Certificate[];
52+
certificates?: HydratedCertificate[];
4753
mint?: TokenMap;
4854
scriptIntegrityHash?: Crypto.Hash32ByteBase16;
4955
requiredExtraSignatures?: Crypto.Ed25519KeyHashHex[];
@@ -80,9 +86,10 @@ export interface HydratedTxBody {
8086
donation?: Lovelace;
8187
}
8288

83-
export interface TxBody extends Omit<HydratedTxBody, 'inputs' | 'collaterals' | 'referenceInputs'> {
84-
inputs: TxIn[];
89+
export interface TxBody extends Omit<HydratedTxBody, 'certificates' | 'inputs' | 'collaterals' | 'referenceInputs'> {
90+
certificates?: Certificate[];
8591
collaterals?: TxIn[];
92+
inputs: TxIn[];
8693
referenceInputs?: TxIn[];
8794
}
8895

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ export class PoolRegistration {
170170
* @param cert core PoolRegistrationCertificate object.
171171
*/
172172
static fromCore(cert: Cardano.PoolRegistrationCertificate) {
173-
return new PoolRegistration(PoolParams.fromCore(cert.poolParameters)); // TODO: Core type does not support script hash as credential, fix?
173+
return new PoolRegistration(PoolParams.fromCore(cert.poolParameters));
174174
}
175175

176176
/**

packages/projection/test/InMemory/storeStakePools.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { InMemory, Mappers, ProjectionEvent } from '../../src';
33
import { firstValueFrom, of } from 'rxjs';
44

55
describe('InMemory.storeStakePools', () => {
6-
it('adds pool updates and retirements on RollRorward, removes on RollBackward', async () => {
6+
it('adds pool updates and retirements on RollForward, removes on RollBackward', async () => {
77
const store = InMemory.createStore();
88
const project = async (
99
eventType: ChainSyncEventType,

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ describe('withStakePools', () => {
7070
expect(result.stakePools.retirements[0].epoch).toEqual(4);
7171
});
7272

73-
it("omits pool retirement if it's folowed by a pool registration", async () => {
73+
it("omits pool retirement if it's followed by a pool registration", async () => {
7474
const data: Mappers.WithCertificates & WithEpochNo = {
7575
certificates: [
7676
{

0 commit comments

Comments
 (0)