Skip to content

Commit dc90eeb

Browse files
committed
refactor(cardano-services): align chain history provider tx outputs to uxtos provider ones
1 parent cc1fde1 commit dc90eeb

File tree

8 files changed

+45
-37
lines changed

8 files changed

+45
-37
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import {
3535
WithCertType,
3636
WithdrawalModel
3737
} from './types';
38-
import { Cardano } from '@cardano-sdk/core';
38+
import { Cardano, jsonToNativeScript } from '@cardano-sdk/core';
3939
import { DB_MAX_SAFE_INTEGER, findTxsByAddresses } from './queries';
4040
import { Hash28ByteBase16 } from '@cardano-sdk/crypto';
4141
import { Logger } from 'ts-log';
@@ -226,11 +226,12 @@ export class ChainHistoryBuilder {
226226
values: [[model.reference_script_id]]
227227
});
228228

229-
if (result.rows.length === 0) continue;
230-
if (result.rows[0].type === 'timelock') continue; // Shouldn't happen.
229+
if (result.rowCount === 0) continue;
230+
231+
const [row] = result.rows;
231232

232233
// There can only be one refScript per output.
233-
txScriptMap.set(model.id, mapPlutusScript(result.rows[0]));
234+
txScriptMap.set(model.id, row.bytes ? mapPlutusScript(row) : jsonToNativeScript(row.json));
234235
}
235236
}
236237

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

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
WithCertType,
2525
WithdrawalModel
2626
} from './types';
27-
import { Cardano, NotImplementedError } from '@cardano-sdk/core';
27+
import { Cardano, NotImplementedError, Serialization } from '@cardano-sdk/core';
2828
import { Hash28ByteBase16, Hash32ByteBase16 } from '@cardano-sdk/crypto';
2929
import {
3030
isAuthorizeCommitteeHotCertModel,
@@ -91,29 +91,36 @@ export const mapTxInModel = (txInModel: TxInputModel): TxInput => ({
9191
txSourceId: txInModel.tx_source_id.toString('hex') as unknown as Cardano.TransactionId
9292
});
9393

94-
export const mapTxOut = (txOut: TxOutput): Cardano.TxOut => ({
95-
address: txOut.address,
96-
datum: txOut.datum,
97-
datumHash: txOut.datumHash,
98-
scriptReference: txOut.scriptReference,
99-
value: txOut.value
100-
});
94+
export const mapTxOut = (txOut: TxOutput) => {
95+
const result: Cardano.TxOut = { address: txOut.address, value: txOut.value };
96+
97+
if (txOut.datum) result.datum = txOut.datum;
98+
if (txOut.datumHash) result.datumHash = txOut.datumHash;
99+
if (txOut.scriptReference) result.scriptReference = txOut.scriptReference;
100+
101+
return result;
102+
};
101103

102104
export const mapTxOutModel = (
103105
txOutModel: TxOutputModel,
104106
props: { assets?: Cardano.TokenMap; script?: Cardano.Script }
105-
): TxOutput => ({
106-
address: txOutModel.address as unknown as Cardano.PaymentAddress,
107-
// Inline datums are missing, but for now it's ok on ChainHistoryProvider
108-
datumHash: txOutModel.datum ? (txOutModel.datum.toString('hex') as unknown as Hash32ByteBase16) : undefined,
109-
index: txOutModel.index,
110-
scriptReference: props.script,
111-
txId: txOutModel.tx_id.toString('hex') as unknown as Cardano.TransactionId,
112-
value: {
113-
assets: props.assets && props.assets.size > 0 ? props.assets : undefined,
114-
coins: BigInt(txOutModel.coin_value)
115-
}
116-
});
107+
) => {
108+
const result: TxOutput = {
109+
address: txOutModel.address as unknown as Cardano.PaymentAddress,
110+
datum: txOutModel.bytes
111+
? Serialization.PlutusData.fromCbor(HexBlob(txOutModel.bytes.toString('hex'))).toCore()
112+
: undefined,
113+
datumHash: txOutModel.datum ? (txOutModel.datum.toString('hex') as unknown as Hash32ByteBase16) : undefined,
114+
index: txOutModel.index,
115+
scriptReference: props.script,
116+
txId: txOutModel.tx_id.toString('hex') as unknown as Cardano.TransactionId,
117+
value: { coins: BigInt(txOutModel.coin_value) }
118+
};
119+
120+
if (props.assets && props.assets.size > 0) result.value.assets = props.assets;
121+
122+
return result;
123+
};
117124

118125
export const mapWithdrawal = (withdrawalModel: WithdrawalModel): Cardano.Withdrawal => ({
119126
quantity: BigInt(withdrawalModel.quantity),

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ const selectTxOutput = (collateral = false) => `
2525
tx_out.value AS coin_value,
2626
tx_out.data_hash AS datum,
2727
tx_out.reference_script_id as reference_script_id,
28+
bytes,
2829
tx.hash AS tx_id
2930
FROM ${collateral ? 'collateral_tx_out' : 'tx_out'} AS tx_out
31+
LEFT JOIN datum ON datum.id = inline_datum_id
3032
JOIN tx ON tx_out.tx_id = tx.id`;
3133

3234
export const findTxInputsByIds = `
@@ -125,9 +127,10 @@ export const findMultiAssetByTxOut = `
125127

126128
export const findReferenceScriptsById = `
127129
SELECT
128-
script.type AS type,
129-
script.bytes AS bytes,
130-
script.serialised_size AS serialized_size
130+
type AS type,
131+
bytes AS bytes,
132+
serialised_size AS serialized_size,
133+
json
131134
FROM script AS script
132135
WHERE id = ANY($1)`;
133136

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export interface TxOutputModel {
7272
id: string;
7373
index: number;
7474
reference_script_id: number | null;
75+
bytes: Buffer | null;
7576
tx_id: Buffer;
7677
}
7778

@@ -97,6 +98,8 @@ export interface ScriptModel {
9798
bytes: Buffer;
9899
hash: Buffer;
99100
serialised_size: number;
101+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
102+
json: any;
100103
}
101104

102105
export interface WithdrawalModel {

packages/cardano-services/src/ChainHistory/openApi.json

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -452,14 +452,7 @@
452452
"$ref": "#/components/schemas/Value"
453453
},
454454
"datum": {
455-
"anyOf": [
456-
{
457-
"$ref": "#/components/schemas/Undefined"
458-
},
459-
{
460-
"type": "string"
461-
}
462-
]
455+
"type": "object"
463456
}
464457
}
465458
},

packages/cardano-services/src/Utxo/DbSyncUtxoProvider/mappers.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ export const utxosToCore = (utxosModels: UtxoModel[]): Cardano.Utxo[] => {
104104
};
105105
if (isNotNil(current.inline_datum)) {
106106
txOut.datum = Serialization.PlutusData.fromCbor(HexBlob(current.inline_datum)).toCore();
107-
} else if (isNotNil(current.data_hash)) {
107+
}
108+
if (isNotNil(current.data_hash)) {
108109
txOut.datumHash = current.data_hash as unknown as Hash32ByteBase16;
109110
}
110111
if (isNotNil(current.reference_script_type)) txOut.scriptReference = parseReferenceScript(current);

packages/cardano-services/test/ChainHistory/DbSyncChainHistoryProvider/mappers.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ const txOutput: TxOutput = {
7777

7878
const txOutModel: TxOutputModel = {
7979
address,
80+
bytes: null,
8081
coin_value: '20000000',
8182
datum: Buffer.from(hash32ByteBase16, 'hex'),
8283
id: '1',

packages/cardano-services/test/Utxo/UtxoHttpService.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ describe('UtxoHttpService', () => {
172172

173173
expect(res.length).toBeGreaterThan(0);
174174
expect(res[0]).toMatchShapeOf([DataMocks.Tx.input, DataMocks.Tx.outputWithInlineDatum]);
175-
expect(res[0][1].datumHash).toBeUndefined();
176175
});
177176

178177
it('return UTxO with time lock reference script', async () => {

0 commit comments

Comments
 (0)