|
1 |
| -import { Cardano, ChainSyncEventType, TxBodyCBOR } from '@cardano-sdk/core'; |
| 1 | +import { Cardano, ChainSyncEventType } from '@cardano-sdk/core'; |
2 | 2 | import { Hash28ByteBase16 } from '@cardano-sdk/crypto';
|
3 | 3 | import { Mappers } from '@cardano-sdk/projection';
|
4 | 4 | import { credentialsFromAddress } from '@cardano-sdk/projection/src/operators/Mappers/util';
|
5 | 5 | import { CredentialEntity, CredentialType, OutputEntity } from '../entity';
|
6 | 6 | import { typeormOperator } from './util';
|
7 | 7 |
|
8 |
| -export const storeCredentials = typeormOperator<Mappers.WithUtxo & Mappers.WithAddresses>(async (evt) => { |
9 |
| - const { |
10 |
| - block: { body: txs }, |
11 |
| - eventType, |
12 |
| - queryRunner |
13 |
| - } = evt; |
| 8 | +export interface WithTxCredentials { |
| 9 | + txToCredentials: Map<Cardano.TransactionId, Map<Hash28ByteBase16, CredentialType>>; |
| 10 | +} |
14 | 11 |
|
15 |
| - // produced credentials will be automatically deleted via block cascade |
16 |
| - if (txs.length === 0 || eventType !== ChainSyncEventType.RollForward) return; |
| 12 | +export const storeCredentials = typeormOperator<Mappers.WithUtxo & Mappers.WithAddresses, WithTxCredentials>( |
| 13 | + async (evt) => { |
| 14 | + const { |
| 15 | + block: { body: txs }, |
| 16 | + eventType, |
| 17 | + queryRunner |
| 18 | + } = evt; |
17 | 19 |
|
18 |
| - const utxoRepository = queryRunner.manager.getRepository(OutputEntity); |
19 |
| - const txIdToCredentials = new Map<Cardano.TransactionId, Map<Hash28ByteBase16, CredentialType>>(); |
| 20 | + const txToCredentials = new Map<Cardano.TransactionId, Map<Hash28ByteBase16, CredentialType>>(); |
20 | 21 |
|
21 |
| - const addCredential = ( |
22 |
| - { paymentCredentialHash, stakeCredential }: Mappers.Address, |
23 |
| - map: Map<Hash28ByteBase16, CredentialType> |
24 |
| - ) => { |
25 |
| - if (paymentCredentialHash) { |
26 |
| - map.set(paymentCredentialHash, CredentialType.PAYMENT); |
27 |
| - } |
28 |
| - if (stakeCredential && !('slot' in stakeCredential)) { |
29 |
| - // FIXME: Support pointer stake credentials |
30 |
| - map.set(stakeCredential, CredentialType.STAKE); |
31 |
| - } |
32 |
| - }; |
| 22 | + // produced credentials will be automatically deleted via block cascade |
| 23 | + if (txs.length > 0 && eventType === ChainSyncEventType.RollForward) { |
| 24 | + const utxoRepository = queryRunner.manager.getRepository(OutputEntity); |
33 | 25 |
|
34 |
| - // get input & output credentials by tx |
35 |
| - for (const tx of evt.block.body) { |
36 |
| - const txCredentials = new Map<Hash28ByteBase16, CredentialType>(); |
| 26 | + // get input & output credentials by tx |
| 27 | + for (const tx of evt.block.body) { |
| 28 | + const txCredentials = new Map<Hash28ByteBase16, CredentialType>(); |
| 29 | + const addCredential = (source: OutputEntity | null | Cardano.TxOut) => { |
| 30 | + if (source && source.address) { |
| 31 | + const { paymentCredentialHash, stakeCredential } = credentialsFromAddress(source.address); |
| 32 | + if (paymentCredentialHash) { |
| 33 | + txCredentials.set(paymentCredentialHash, CredentialType.PAYMENT); |
| 34 | + } |
| 35 | + if (stakeCredential && !('slot' in stakeCredential)) { |
| 36 | + // FIXME: Support pointer stake credentials |
| 37 | + txCredentials.set(stakeCredential, CredentialType.STAKE); |
| 38 | + } |
| 39 | + } |
| 40 | + }; |
37 | 41 |
|
38 |
| - // get tx input address |
39 |
| - const txInOutputs = await Promise.all( |
40 |
| - tx.body.inputs.map( |
41 |
| - async ({ txId, index: outputIndex }) => |
42 |
| - await utxoRepository.findOne({ select: { address: true }, where: { outputIndex, txId } }) |
43 |
| - ) |
44 |
| - ); |
| 42 | + // get tx input address |
| 43 | + const txInOutputEntities = await Promise.all( |
| 44 | + tx.body.inputs.map( |
| 45 | + async ({ txId, index: outputIndex }) => |
| 46 | + await utxoRepository.findOne({ select: { address: true }, where: { outputIndex, txId } }) |
| 47 | + ) |
| 48 | + ); |
45 | 49 |
|
46 |
| - // add tx input credentials to involved tx credentials |
47 |
| - for (const txOut of txInOutputs) { |
48 |
| - if (txOut && txOut.address) { |
49 |
| - addCredential(credentialsFromAddress(txOut.address), txCredentials); |
50 |
| - } |
51 |
| - } |
| 50 | + // add tx input credentials to involved tx credentials |
| 51 | + txInOutputEntities.forEach(addCredential); |
| 52 | + // add tx output credentials to involved tx credentials |
| 53 | + tx.body.outputs.forEach(addCredential); |
52 | 54 |
|
53 |
| - // add tx output credentials to involved tx credentials |
54 |
| - for (const txOut of tx.body.outputs) { |
55 |
| - addCredential(credentialsFromAddress(txOut.address), txCredentials); |
56 |
| - } |
| 55 | + const credentialEntities = Array.from(txCredentials).map( |
| 56 | + ([credentialHash, credentialType]): CredentialEntity => ({ |
| 57 | + credentialHash: Buffer.from(credentialHash, 'hex'), |
| 58 | + credentialType |
| 59 | + }) |
| 60 | + ); |
57 | 61 |
|
58 |
| - const credentialEntities = Array.from(txCredentials).map( |
59 |
| - ([credentialHash, credentialType]): CredentialEntity => ({ |
60 |
| - credentialHash: Buffer.from(credentialHash, 'hex'), |
61 |
| - credentialType |
62 |
| - }) |
63 |
| - ); |
| 62 | + await queryRunner.manager |
| 63 | + .createQueryBuilder() |
| 64 | + .insert() |
| 65 | + .into(CredentialEntity) |
| 66 | + .values(credentialEntities) |
| 67 | + .orIgnore() |
| 68 | + .execute(); |
64 | 69 |
|
65 |
| - await queryRunner.manager |
66 |
| - .createQueryBuilder() |
67 |
| - .insert() |
68 |
| - .into(CredentialEntity) |
69 |
| - .values(credentialEntities) |
70 |
| - .orIgnore() |
71 |
| - .execute(); |
| 70 | + // store tx credentials for transaction entities |
| 71 | + txToCredentials.set(tx.id, txCredentials); |
| 72 | + } |
| 73 | + } |
72 | 74 |
|
73 |
| - // store tx credentials for transaction entities |
74 |
| - txIdToCredentials.set(tx.id, txCredentials); |
| 75 | + return { txToCredentials: txToCredentials }; |
75 | 76 | }
|
76 |
| - |
77 |
| - return { txIdToCredentials }; |
78 |
| -}); |
| 77 | +); |
0 commit comments