-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathmappers.test.ts
493 lines (484 loc) · 16.9 KB
/
mappers.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
import * as mappers from '../../../src/ChainHistory/DbSyncChainHistory/mappers';
import { AssetId } from '@cardano-sdk/util-dev';
import {
BlockModel,
BlockOutputModel,
CertificateModel,
DelegationCertModel,
MirCertModel,
MultiAssetModel,
PoolRegisterCertModel,
PoolRetireCertModel,
RedeemerModel,
StakeCertModel,
TipModel,
TxInput,
TxInputModel,
TxModel,
TxOutMultiAssetModel,
TxOutTokenMap,
TxOutput,
TxOutputModel,
TxTokenMap,
WithCertIndex,
WithCertType,
WithdrawalModel
} from '../../../src/ChainHistory/DbSyncChainHistory/types';
import { Cardano } from '@cardano-sdk/core';
import { Hash28ByteBase16, Hash32ByteBase16 } from '@cardano-sdk/crypto';
const blockHash = '7a48b034645f51743550bbaf81f8a14771e58856e031eb63844738ca8ad72298';
const poolId = 'pool1zuevzm3xlrhmwjw87ec38mzs02tlkwec9wxpgafcaykmwg7efhh';
const datetime = '2022-05-10T19:22:43.620Z';
const vrfKey = 'vrf_vk19j362pkr4t9y0m3qxgmrv0365vd7c4ze03ny4jh84q8agjy4ep4s99zvg8';
const genesisLeaderHash = 'eff1b5b26e65b791d6f236c7c0264012bd1696759d22bdb4dd0f6f56';
const transactionHash = 'cefd2fcf657e5e5d6c35975f4e052f427819391b153ebb16ad8aa107ba5a3819';
const sourceTransactionHash = 'cefd2fcf657e5e5d6c35975f4e052f427819391b153ebb16ad8aa107ba5a3812';
const hash28ByteBase16 = '8293d319ef5b3ac72366dd28006bd315b715f7e7cfcbd3004129b80d';
const hash32ByteBase16 = '3e33018e8293d319ef5b3ac72366dd28006bd315b715f7e7cfcbd3004129b80d';
const address = 'addr_test1wphyve8r76kvfr5yn6k0fcmq0mn2uf6c6mvtsrafmr7awcg0vnzpg';
const stakeAddress = 'stake_test1up7pvfq8zn4quy45r2g572290p9vf99mr9tn7r9xrgy2l2qdsf58d';
const assetName = '6e7574636f696e';
const policyId = 'b0d07d45fe9514f80213f4020e5a61241458be626841cde717cb38a7';
const fingerprint = 'asset1pkpwyknlvul7az0xx8czhl60pyel45rpje4z8w';
const baseCertModel: CertificateModel = {
cert_index: 0,
tx_id: Buffer.from(transactionHash, 'hex')
};
const txInput: TxInput = {
address: Cardano.PaymentAddress(address),
id: '1',
index: 1,
txInputId: Cardano.TransactionId(transactionHash),
txSourceId: Cardano.TransactionId(sourceTransactionHash)
};
const txInputModel: TxInputModel = {
address,
id: '1',
index: 1,
tx_input_id: Buffer.from(transactionHash, 'hex'),
tx_source_id: Buffer.from(sourceTransactionHash, 'hex')
};
const txOutput: TxOutput = {
address: Cardano.PaymentAddress(address),
datumHash: Hash32ByteBase16(hash32ByteBase16),
index: 1,
txId: Cardano.TransactionId(transactionHash),
value: { coins: 20_000_000n }
};
const txOutModel: TxOutputModel = {
address,
coin_value: '20000000',
datum: Buffer.from(hash32ByteBase16, 'hex'),
id: '1',
index: 1,
tx_id: Buffer.from(transactionHash, 'hex')
};
const assets: Cardano.TokenMap = new Map([
[AssetId.TSLA, 500n],
[AssetId.PXL, 500n]
]);
const multiAssetModel: MultiAssetModel = {
asset_name: Buffer.from(assetName, 'hex'),
fingerprint,
policy_id: Buffer.from(policyId, 'hex'),
quantity: '12000',
tx_id: Buffer.from(transactionHash, 'hex')
};
const txOutMaModel: TxOutMultiAssetModel = {
...multiAssetModel,
tx_out_id: '1'
};
const withdrawalModel: WithdrawalModel = {
quantity: '20000000',
stake_address: stakeAddress,
tx_id: Buffer.from(transactionHash, 'hex')
};
const txModel: TxModel = {
block_hash: Buffer.from(blockHash, 'hex'),
block_no: 200,
block_slot_no: 250,
fee: '170000',
id: Buffer.from(transactionHash, 'hex'),
index: 1,
invalid_before: '300',
invalid_hereafter: '500',
size: 20,
valid_contract: true
};
describe('chain history mappers', () => {
describe('mapBlock', () => {
const blockModel: BlockModel = {
block_no: 200,
epoch_no: 12,
epoch_slot_no: 202,
hash: Buffer.from(blockHash, 'hex'),
next_block: Buffer.from(blockHash, 'hex'),
previous_block: Buffer.from(blockHash, 'hex'),
size: 50,
slot_leader_hash: Buffer.from(genesisLeaderHash, 'hex'),
slot_leader_pool: poolId,
slot_no: '100',
time: datetime,
tx_count: '3000',
vrf: vrfKey
};
const blockOutputModel: BlockOutputModel = {
fees: '170000',
hash: Buffer.from(blockHash, 'hex'),
output: '100000000'
};
const tipModel: TipModel = {
block_no: 300,
hash: Buffer.from(blockHash, 'hex'),
slot_no: 400
};
test('map BlockModel to Cardano.Block', () => {
const result = mappers.mapBlock(blockModel, blockOutputModel, tipModel);
expect(result).toEqual<Cardano.ExtendedBlockInfo>({
confirmations: 100,
date: new Date(datetime),
epoch: Cardano.EpochNo(12),
epochSlot: 202,
fees: 170_000n,
header: {
blockNo: Cardano.BlockNo(200),
hash: Cardano.BlockId(blockHash),
slot: Cardano.Slot(100)
},
nextBlock: Cardano.BlockId(blockHash),
previousBlock: Cardano.BlockId(blockHash),
size: Cardano.BlockSize(50),
slotLeader: Cardano.SlotLeader(poolId),
totalOutput: 100_000_000n,
txCount: 3000,
vrf: Cardano.VrfVkBech32(vrfKey)
});
});
test('map BlockModel with nulls to Cardano.Block', () => {
const result = mappers.mapBlock(
{ ...blockModel, next_block: null, previous_block: null, slot_leader_pool: null },
blockOutputModel,
tipModel
);
expect(result).toEqual<Cardano.ExtendedBlockInfo>({
confirmations: 100,
date: new Date(datetime),
epoch: Cardano.EpochNo(12),
epochSlot: 202,
fees: 170_000n,
header: {
blockNo: Cardano.BlockNo(200),
hash: Cardano.BlockId(blockHash),
slot: Cardano.Slot(100)
},
size: Cardano.BlockSize(50),
slotLeader: Cardano.SlotLeader(genesisLeaderHash),
totalOutput: 100_000_000n,
txCount: 3000,
vrf: Cardano.VrfVkBech32(vrfKey)
});
});
});
describe('mapCertificate', () => {
test('map PoolRetireCertModel to Cardano.PoolRetirementCertificate', () => {
const result = mappers.mapCertificate({
...baseCertModel,
pool_id: poolId,
retiring_epoch: 1,
type: 'retire'
} as WithCertType<PoolRetireCertModel>);
expect(result).toEqual<WithCertIndex<Cardano.PoolRetirementCertificate>>({
__typename: Cardano.CertificateType.PoolRetirement,
cert_index: 0,
epoch: Cardano.EpochNo(1),
poolId: Cardano.PoolId(poolId)
});
});
test('map PoolUpdateCertModel to Cardano.PoolRegistrationCertificate', () => {
const result = mappers.mapCertificate({
...baseCertModel,
pool_id: poolId,
type: 'register'
} as WithCertType<PoolRegisterCertModel>);
expect(result).toEqual<WithCertIndex<Cardano.PoolRegistrationCertificate>>({
__typename: Cardano.CertificateType.PoolRegistration,
cert_index: 0,
poolParameters: null as unknown as Cardano.PoolParameters
});
});
test('map MirCertModel to Cardano.MirCertificate', () => {
const mirCert: WithCertType<MirCertModel> = {
...baseCertModel,
address: stakeAddress,
amount: '500000',
pot: 'reserve',
type: 'mir'
};
const reservePotResult = mappers.mapCertificate(mirCert);
const treasuryPotResult = mappers.mapCertificate({ ...mirCert, pot: 'treasury' } as WithCertType<MirCertModel>);
expect(reservePotResult).toEqual<WithCertIndex<Cardano.MirCertificate>>({
__typename: Cardano.CertificateType.MIR,
cert_index: 0,
kind: Cardano.MirCertificateKind.ToStakeCreds,
pot: Cardano.MirCertificatePot.Reserves,
quantity: 500_000n,
stakeCredential: Cardano.Address.fromString(stakeAddress)!.asReward()!.getPaymentCredential()
});
expect(treasuryPotResult).toEqual<WithCertIndex<Cardano.MirCertificate>>({
__typename: Cardano.CertificateType.MIR,
cert_index: 0,
kind: Cardano.MirCertificateKind.ToStakeCreds,
pot: Cardano.MirCertificatePot.Treasury,
quantity: 500_000n,
stakeCredential: Cardano.Address.fromString(stakeAddress)!.asReward()!.getPaymentCredential()
});
});
test('map StakeCertModel to Cardano.StakeAddressCertificate', () => {
const stakeCert: WithCertType<StakeCertModel> = {
...baseCertModel,
address: stakeAddress,
deposit: '0',
registration: true,
type: 'stake'
};
const registrationResult = mappers.mapCertificate(stakeCert);
const deregistrationResult = mappers.mapCertificate({
...stakeCert,
registration: false
} as WithCertType<StakeCertModel>);
expect(registrationResult).toEqual<WithCertIndex<Cardano.NewStakeAddressCertificate>>({
__typename: Cardano.CertificateType.Registration,
cert_index: 0,
deposit: 0n,
stakeCredential: {
hash: Hash28ByteBase16.fromEd25519KeyHashHex(
Cardano.RewardAccount.toHash(Cardano.RewardAccount(stakeAddress))
),
type: Cardano.CredentialType.KeyHash
}
});
expect(deregistrationResult).toEqual<WithCertIndex<Cardano.NewStakeAddressCertificate>>({
__typename: Cardano.CertificateType.Unregistration,
cert_index: 0,
deposit: 0n,
stakeCredential: {
hash: Hash28ByteBase16.fromEd25519KeyHashHex(
Cardano.RewardAccount.toHash(Cardano.RewardAccount(stakeAddress))
),
type: Cardano.CredentialType.KeyHash
}
});
});
test('map DelegationCertModel to Cardano.StakeDelegationCertificate', () => {
const result = mappers.mapCertificate({
...baseCertModel,
address: stakeAddress,
pool_id: poolId,
type: 'delegation'
} as WithCertType<DelegationCertModel>);
expect(result).toEqual<WithCertIndex<Cardano.StakeDelegationCertificate>>({
__typename: Cardano.CertificateType.StakeDelegation,
cert_index: 0,
poolId: Cardano.PoolId(poolId),
stakeCredential: {
hash: Hash28ByteBase16.fromEd25519KeyHashHex(
Cardano.RewardAccount.toHash(Cardano.RewardAccount(stakeAddress))
),
type: Cardano.CredentialType.KeyHash
}
});
});
});
describe('mapRedeemer', () => {
const redeemerModel: RedeemerModel = {
index: 1,
purpose: 'mint',
script_hash: Buffer.from(hash28ByteBase16, 'hex'),
tx_id: Buffer.from(transactionHash, 'hex'),
unit_mem: '2000',
unit_steps: '5000'
};
test('map RedeemerModel to Cardano.Redeemer', () => {
const result = mappers.mapRedeemer(redeemerModel);
expect(result).toEqual<Cardano.Redeemer>({
data: Buffer.from('not implemented'),
executionUnits: {
memory: 2000,
steps: 5000
},
index: 1,
purpose: Cardano.RedeemerPurpose.mint
});
});
});
describe('mapTxAlonzo', () => {
const inputs: Cardano.HydratedTxIn[] = [
{ address: Cardano.PaymentAddress(address), index: 1, txId: Cardano.TransactionId(transactionHash) }
];
const outputs: Cardano.TxOut[] = [
{ address: Cardano.PaymentAddress(address), value: { assets, coins: 20_000_000n } }
];
const redeemers: Cardano.Redeemer[] = [
{
data: Buffer.from('not implemented'),
executionUnits: { memory: 1, steps: 2 },
index: 1,
purpose: Cardano.RedeemerPurpose.spend
}
];
const withdrawals: Cardano.Withdrawal[] = [{ quantity: 200n, stakeAddress: Cardano.RewardAccount(stakeAddress) }];
const metadata: Cardano.TxMetadata = new Map([[1n, 'data']]);
const inputSource = Cardano.InputSource.inputs;
const certificates: Cardano.Certificate[] = [
{
__typename: Cardano.CertificateType.StakeRegistration,
stakeCredential: {
hash: Hash28ByteBase16.fromEd25519KeyHashHex(
Cardano.RewardAccount.toHash(Cardano.RewardAccount(stakeAddress))
),
type: Cardano.CredentialType.KeyHash
}
}
];
const expected: Cardano.HydratedTx = {
blockHeader: { blockNo: Cardano.BlockNo(200), hash: Cardano.BlockId(blockHash), slot: Cardano.Slot(250) },
body: {
fee: 170_000n,
inputs,
outputs,
validityInterval: { invalidBefore: Cardano.Slot(300), invalidHereafter: Cardano.Slot(500) }
},
id: Cardano.TransactionId(transactionHash),
index: 1,
inputSource: Cardano.InputSource.inputs,
txSize: 20,
witness: { signatures: new Map() }
};
test('map TxModel to Cardano.HydratedTx with minimal data', () => {
const result = mappers.mapTxAlonzo(txModel, { inputSource, inputs, outputs });
expect(result).toEqual<Cardano.HydratedTx>(expected);
});
test('map TxModel with null fields to Cardano.HydratedTx', () => {
const result = mappers.mapTxAlonzo(
{ ...txModel, invalid_before: null, invalid_hereafter: null },
{ inputSource, inputs, outputs }
);
expect(result).toEqual<Cardano.HydratedTx>({ ...expected, body: { ...expected.body, validityInterval: {} } });
});
test('map TxModel to Cardano.HydratedTx with extra data', () => {
const result = mappers.mapTxAlonzo(txModel, {
certificates,
collateralOutputs: [txOutput],
collaterals: inputs,
inputSource,
inputs,
metadata,
mint: assets,
outputs,
redeemers,
withdrawals
});
expect(result).toEqual<Cardano.HydratedTx>({
...expected,
auxiliaryData: { blob: metadata },
body: {
...expected.body,
certificates,
collateralReturn: txOutput,
collaterals: inputs,
mint: assets,
withdrawals
},
witness: { ...expected.witness, redeemers }
});
});
});
describe('mapTxInModel', () => {
test('map txInputModel to TxInput', () => {
const result = mappers.mapTxInModel(txInputModel);
expect(result).toEqual<TxInput>({
address: Cardano.PaymentAddress(txInputModel.address),
id: txInputModel.id,
index: txInputModel.index,
txInputId: Cardano.TransactionId(transactionHash),
txSourceId: Cardano.TransactionId(sourceTransactionHash)
});
});
});
describe('mapTxIn', () => {
test('map TxInput to Cardano.HydratedTxIn', () => {
const result = mappers.mapTxIn(txInput);
expect(result).toEqual<Cardano.HydratedTxIn>({
address: Cardano.PaymentAddress(address),
index: 1,
txId: Cardano.TransactionId(sourceTransactionHash)
});
});
});
describe('mapTxOutModel', () => {
test('map TxOutputModel with assets to TxOutput', () => {
const result = mappers.mapTxOutModel(txOutModel, assets);
expect(result).toEqual<TxOutput>({
address: Cardano.PaymentAddress(address),
datumHash: Hash32ByteBase16(hash32ByteBase16),
index: 1,
txId: Cardano.TransactionId(transactionHash),
value: { assets, coins: 20_000_000n }
});
});
test('map TxOutputModel with no assets to TxOutput', () => {
const result = mappers.mapTxOutModel(txOutModel);
expect(result).toEqual<TxOutput>({
address: Cardano.PaymentAddress(address),
datumHash: Hash32ByteBase16(hash32ByteBase16),
index: 1,
txId: Cardano.TransactionId(transactionHash),
value: { coins: 20_000_000n }
});
});
test('map TxOutputModel with nulls to TxOutput', () => {
const result = mappers.mapTxOutModel({ ...txOutModel, datum: null });
expect(result).toEqual<TxOutput>({
address: Cardano.PaymentAddress(address),
index: 1,
txId: Cardano.TransactionId(transactionHash),
value: { coins: 20_000_000n }
});
});
});
describe('mapTxOut', () => {
test('map TxOutput to Cardano.TxOut', () => {
const result = mappers.mapTxOut(txOutput);
expect(result).toEqual<Cardano.TxOut>({
address: txOutput.address,
datumHash: txOutput.datumHash,
value: txOutput.value
});
});
});
describe('mapTxOutTokenMap', () => {
test('map TxOutMultiAssetModel to TxOutTokenMap', () => {
const result = mappers.mapTxOutTokenMap([txOutMaModel, txOutMaModel]);
expect(result).toEqual<TxOutTokenMap>(
new Map([['1', new Map([[Cardano.AssetId(policyId + assetName), 24_000n]])]])
);
});
});
describe('mapTxTokenMap', () => {
test('map MultiAssetModel to TxTokenMap', () => {
const result = mappers.mapTxTokenMap([multiAssetModel, multiAssetModel]);
expect(result).toEqual<TxTokenMap>(
new Map([[Cardano.TransactionId(transactionHash), new Map([[Cardano.AssetId(policyId + assetName), 24_000n]])]])
);
});
});
describe('mapWithdrawal', () => {
test('map WithdrawalModel to Cardano.Withdrawal', () => {
const result = mappers.mapWithdrawal(withdrawalModel);
expect(result).toEqual<Cardano.Withdrawal>({
quantity: 20_000_000n,
stakeAddress: Cardano.RewardAccount(stakeAddress)
});
});
});
});