Skip to content

Commit

Permalink
refactor(cardano-services): align chain history provider tx outputs t…
Browse files Browse the repository at this point in the history
…o uxtos provider ones
  • Loading branch information
iccicci committed Sep 27, 2024
1 parent 0d39420 commit 619e972
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
WithCertType,
WithdrawalModel
} from './types';
import { Cardano } from '@cardano-sdk/core';
import { Cardano, jsonToNativeScript } from '@cardano-sdk/core';
import { DB_MAX_SAFE_INTEGER, findTxsByAddresses } from './queries';
import { Hash28ByteBase16 } from '@cardano-sdk/crypto';
import { Logger } from 'ts-log';
Expand Down Expand Up @@ -226,11 +226,12 @@ export class ChainHistoryBuilder {
values: [[model.reference_script_id]]
});

if (result.rows.length === 0) continue;
if (result.rows[0].type === 'timelock') continue; // Shouldn't happen.
if (result.rowCount === 0) continue;

const [row] = result.rows;

// There can only be one refScript per output.
txScriptMap.set(model.id, mapPlutusScript(result.rows[0]));
txScriptMap.set(model.id, row.bytes ? mapPlutusScript(row) : jsonToNativeScript(row.json));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
WithCertType,
WithdrawalModel
} from './types';
import { Cardano, NotImplementedError } from '@cardano-sdk/core';
import { Cardano, NotImplementedError, Serialization } from '@cardano-sdk/core';
import { Hash28ByteBase16, Hash32ByteBase16 } from '@cardano-sdk/crypto';
import {
isAuthorizeCommitteeHotCertModel,
Expand Down Expand Up @@ -91,29 +91,36 @@ export const mapTxInModel = (txInModel: TxInputModel): TxInput => ({
txSourceId: txInModel.tx_source_id.toString('hex') as unknown as Cardano.TransactionId
});

export const mapTxOut = (txOut: TxOutput): Cardano.TxOut => ({
address: txOut.address,
datum: txOut.datum,
datumHash: txOut.datumHash,
scriptReference: txOut.scriptReference,
value: txOut.value
});
export const mapTxOut = (txOut: TxOutput) => {
const result: Cardano.TxOut = { address: txOut.address, value: txOut.value };

if (txOut.datum) result.datum = txOut.datum;
if (txOut.datumHash) result.datumHash = txOut.datumHash;
if (txOut.scriptReference) result.scriptReference = txOut.scriptReference;

return result;
};

export const mapTxOutModel = (
txOutModel: TxOutputModel,
props: { assets?: Cardano.TokenMap; script?: Cardano.Script }
): TxOutput => ({
address: txOutModel.address as unknown as Cardano.PaymentAddress,
// Inline datums are missing, but for now it's ok on ChainHistoryProvider
datumHash: txOutModel.datum ? (txOutModel.datum.toString('hex') as unknown as Hash32ByteBase16) : undefined,
index: txOutModel.index,
scriptReference: props.script,
txId: txOutModel.tx_id.toString('hex') as unknown as Cardano.TransactionId,
value: {
assets: props.assets && props.assets.size > 0 ? props.assets : undefined,
coins: BigInt(txOutModel.coin_value)
}
});
) => {
const result: TxOutput = {
address: txOutModel.address as unknown as Cardano.PaymentAddress,
datum: txOutModel.bytes
? Serialization.PlutusData.fromCbor(HexBlob(txOutModel.bytes.toString('hex'))).toCore()
: undefined,
datumHash: txOutModel.datum ? (txOutModel.datum.toString('hex') as unknown as Hash32ByteBase16) : undefined,
index: txOutModel.index,
scriptReference: props.script,
txId: txOutModel.tx_id.toString('hex') as unknown as Cardano.TransactionId,
value: { coins: BigInt(txOutModel.coin_value) }
};

if (props.assets && props.assets.size > 0) result.value.assets = props.assets;

return result;
};

export const mapWithdrawal = (withdrawalModel: WithdrawalModel): Cardano.Withdrawal => ({
quantity: BigInt(withdrawalModel.quantity),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ const selectTxOutput = (collateral = false) => `
tx_out.value AS coin_value,
tx_out.data_hash AS datum,
tx_out.reference_script_id as reference_script_id,
bytes,
tx.hash AS tx_id
FROM ${collateral ? 'collateral_tx_out' : 'tx_out'} AS tx_out
LEFT JOIN datum ON datum.id = inline_datum_id
JOIN tx ON tx_out.tx_id = tx.id`;

export const findTxInputsByIds = `
Expand Down Expand Up @@ -125,9 +127,10 @@ export const findMultiAssetByTxOut = `

export const findReferenceScriptsById = `
SELECT
script.type AS type,
script.bytes AS bytes,
script.serialised_size AS serialized_size
type AS type,
bytes AS bytes,
serialised_size AS serialized_size,
json
FROM script AS script
WHERE id = ANY($1)`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export interface TxOutputModel {
id: string;
index: number;
reference_script_id: number | null;
bytes: Buffer | null;
tx_id: Buffer;
}

Expand All @@ -97,6 +98,8 @@ export interface ScriptModel {
bytes: Buffer;
hash: Buffer;
serialised_size: number;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
json: any;
}

export interface WithdrawalModel {
Expand Down
9 changes: 1 addition & 8 deletions packages/cardano-services/src/ChainHistory/openApi.json
Original file line number Diff line number Diff line change
Expand Up @@ -452,14 +452,7 @@
"$ref": "#/components/schemas/Value"
},
"datum": {
"anyOf": [
{
"$ref": "#/components/schemas/Undefined"
},
{
"type": "string"
}
]
"type": "object"
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ export const utxosToCore = (utxosModels: UtxoModel[]): Cardano.Utxo[] => {
};
if (isNotNil(current.inline_datum)) {
txOut.datum = Serialization.PlutusData.fromCbor(HexBlob(current.inline_datum)).toCore();
} else if (isNotNil(current.data_hash)) {
}
if (isNotNil(current.data_hash)) {
txOut.datumHash = current.data_hash as unknown as Hash32ByteBase16;
}
if (isNotNil(current.reference_script_type)) txOut.scriptReference = parseReferenceScript(current);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const txOutput: TxOutput = {

const txOutModel: TxOutputModel = {
address,
bytes: null,
coin_value: '20000000',
datum: Buffer.from(hash32ByteBase16, 'hex'),
id: '1',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ describe('UtxoHttpService', () => {

expect(res.length).toBeGreaterThan(0);
expect(res[0]).toMatchShapeOf([DataMocks.Tx.input, DataMocks.Tx.outputWithInlineDatum]);
expect(res[0][1].datumHash).toBeUndefined();
});

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

0 comments on commit 619e972

Please sign in to comment.