Skip to content

Commit

Permalink
feat!: added new babbage era types in Transactions and Outputs
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Alonzo transaction outputs will now contain a datumHash field, carrying the datum hash digest. However, they will also contain a datum field with the exact same value for backward compatibility reason. In Babbage however, transaction outputs will carry either datum or datumHash depending on the case; and datum will only contain inline datums.
  • Loading branch information
AngelCastilloB committed Dec 21, 2022
1 parent d531844 commit 0b1f2ff
Show file tree
Hide file tree
Showing 19 changed files with 612 additions and 217 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,14 @@ export const mapTxInModel = (txInModel: TxInputModel): TxInput => ({
export const mapTxOut = (txOut: TxOutput): Cardano.TxOut => ({
address: txOut.address,
datum: txOut.datum,
datumHash: txOut.datumHash,
scriptReference: txOut.scriptReference,
value: txOut.value
});

export const mapTxOutModel = (txOutModel: TxOutputModel, assets?: Cardano.TokenMap): TxOutput => ({
address: Cardano.Address(txOutModel.address),
datum: txOutModel.datum ? Cardano.util.Hash32ByteBase16(txOutModel.datum.toString('hex')) : undefined,
datumHash: txOutModel.datum ? Cardano.util.Hash32ByteBase16(txOutModel.datum.toString('hex')) : undefined,
index: txOutModel.index,
txId: Cardano.TransactionId(txOutModel.tx_id.toString('hex')),
value: {
Expand Down
56 changes: 55 additions & 1 deletion packages/cardano-services/src/Utxo/DbSyncUtxoProvider/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,57 @@ import { UtxoModel } from './types';
import { generateAssetId } from './util';
import { isNotNil } from '@cardano-sdk/util';

/**
* Parse the reference script from the UtxoModel
*
* @param model The UtxoModel from dbSync.
* @returns The reference Script.
*/
const parseReferenceScript = (model: UtxoModel): Cardano.Script => {
let script: Cardano.Script;
switch (model.reference_script_type) {
case ReferenceScriptType.Timelock:
case ReferenceScriptType.Multisig:
if (!isNotNil(model.reference_script_json))
throw new SerializationError(
SerializationFailure.InvalidScript,
'Unexpected error deserializing Native script. Data is null'
);
script = jsonToNativeScript(model.reference_script_json);
break;
case ReferenceScriptType.PlutusV1:
if (!isNotNil(model.reference_script_bytes))
throw new SerializationError(
SerializationFailure.InvalidScript,
'Unexpected error deserializing PlutusV1 script. Data is null'
);
script = {
__type: Cardano.ScriptType.Plutus,
bytes: Cardano.util.HexBlob(model.reference_script_bytes),
version: Cardano.PlutusLanguageVersion.V1
};
break;
case ReferenceScriptType.PlutusV2:
if (!isNotNil(model.reference_script_bytes))
throw new SerializationError(
SerializationFailure.InvalidScript,
'Unexpected error deserializing PlutusV2 script. Data is null'
);
script = {
__type: Cardano.ScriptType.Plutus,
bytes: Cardano.util.HexBlob(model.reference_script_bytes),
version: Cardano.PlutusLanguageVersion.V2
};
break;
default:
throw new SerializationError(
SerializationFailure.InvalidScriptType,
`Unknown script type ${model.reference_script_type}`
);
}
return script;
};

/**
* Transform DB results into indexed core UTxO
*
Expand Down Expand Up @@ -30,7 +81,10 @@ export const utxosToCore = (utxosModels: UtxoModel[]): Cardano.Utxo[] => {
coins: BigInt(current.coins)
}
};
if (current.data_hash) txOut.datum = Cardano.util.Hash32ByteBase16(current.data_hash);
if (isNotNil(current.data_hash)) txOut.datumHash = Cardano.util.Hash32ByteBase16(current.data_hash);
if (isNotNil(current.inline_datum)) txOut.datum = Cardano.util.HexBlob(current.inline_datum);
if (isNotNil(current.reference_script_type)) txOut.scriptReference = parseReferenceScript(current);

if (isNotNil(current.asset_name) && current.asset_policy && current.asset_quantity) {
txOut.value.assets = new Map<Cardano.AssetId, bigint>([
[generateAssetId(current.asset_policy, current.asset_name), BigInt(current.asset_quantity)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const txInputModel: TxInputModel = {

const txOutput: TxOutput = {
address: Cardano.Address(address),
datum: Cardano.util.Hash32ByteBase16(hash32ByteBase16),
datumHash: Cardano.util.Hash32ByteBase16(hash32ByteBase16),
index: 1,
txId: Cardano.TransactionId(transactionHash),
value: { coins: 20_000_000n }
Expand Down Expand Up @@ -388,7 +388,7 @@ describe('chain history mappers', () => {
const result = mappers.mapTxOutModel(txOutModel, assets);
expect(result).toEqual<TxOutput>({
address: Cardano.Address(address),
datum: Cardano.util.Hash32ByteBase16(hash32ByteBase16),
datumHash: Cardano.util.Hash32ByteBase16(hash32ByteBase16),
index: 1,
txId: Cardano.TransactionId(transactionHash),
value: { assets, coins: 20_000_000n }
Expand All @@ -398,7 +398,7 @@ describe('chain history mappers', () => {
const result = mappers.mapTxOutModel(txOutModel);
expect(result).toEqual<TxOutput>({
address: Cardano.Address(address),
datum: Cardano.util.Hash32ByteBase16(hash32ByteBase16),
datumHash: Cardano.util.Hash32ByteBase16(hash32ByteBase16),
index: 1,
txId: Cardano.TransactionId(transactionHash),
value: { coins: 20_000_000n }
Expand All @@ -419,7 +419,7 @@ describe('chain history mappers', () => {
const result = mappers.mapTxOut(txOutput);
expect(result).toEqual<Cardano.TxOut>({
address: txOutput.address,
datum: txOutput.datum,
datumHash: txOutput.datumHash,
value: txOutput.value
});
});
Expand Down
4 changes: 2 additions & 2 deletions packages/cardano-services/test/data-mocks/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const txOutBase: Omit<Cardano.TxOut, 'value' | 'datum'> = {

export const txOutBaseWithDatum: Omit<Cardano.TxOut, 'value'> = {
...txOutBase,
datum: Cardano.util.Hash32ByteBase16('c5dfa8c3cbd5a959829618a7b46e163078cb3f1b39f152514d0c3686d553529a')
datumHash: Cardano.util.Hash32ByteBase16('c5dfa8c3cbd5a959829618a7b46e163078cb3f1b39f152514d0c3686d553529a')
};

export const txOutWithCoinOnly: Cardano.TxOut = { ...txOutBase, value: valueWithCoinOnly };
Expand Down Expand Up @@ -183,7 +183,7 @@ export const txOut = {
address:
// eslint-disable-next-line max-len
'addr_test1qpv5muwgjmmtqh2ta0kq9pmz0nurg9kmw7dryueqt57mncynjnzmk67fvy2unhzydrgzp2v6hl625t0d4qd5h3nxt04qu0ww7k',
datum: undefined,
datumHash: undefined,
index: 0,
txId: '59f3ea1bb67b39447aad523f35daa1950c833472bf9232b6c0abac968f45bad9',
value: { assets: undefined, coins: 3_061_089_499_500n }
Expand Down
Loading

0 comments on commit 0b1f2ff

Please sign in to comment.