Skip to content

Commit 0386983

Browse files
committed
refactor(core): do not add some undefined keys in deserialization
Some deserialization functions add some keys with undefined value. The DbSyncChainHistoryProvider has a slightly different behavior not adding those keys. In order to unify the output of BlockfrostChainHistoryProvider (which deserializes transactions CBOR) with the one from DbSyncChainHistoryProvider these keys are no longer added from deserialization as well.
1 parent d758234 commit 0386983

File tree

5 files changed

+36
-17
lines changed

5 files changed

+36
-17
lines changed

packages/core/src/Serialization/AuxiliaryData/AuxiliaryData.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,13 @@ export class AuxiliaryData {
228228
*/
229229
toCore(): Cardano.AuxiliaryData {
230230
const scripts = this.#getCoreScripts();
231-
return {
232-
blob: this.#metadata ? this.#metadata.toCore() : undefined,
233-
scripts: scripts.length > 0 ? scripts : undefined
231+
const auxiliaryData: Cardano.AuxiliaryData = {
232+
blob: this.#metadata ? this.#metadata.toCore() : undefined
234233
};
234+
235+
if (scripts.length > 0) auxiliaryData.scripts = scripts;
236+
237+
return auxiliaryData;
235238
}
236239

237240
/**

packages/core/src/Serialization/Certificates/PoolParams/PoolParams.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,10 @@ export class PoolParams {
194194
toCore(): Cardano.PoolParameters {
195195
const rewardAccountAddress = this.#rewardAccount.toAddress();
196196

197-
return {
197+
const poolParams: Cardano.PoolParameters = {
198198
cost: this.#cost,
199199
id: PoolId.fromKeyHash(this.#operator),
200200
margin: this.#margin.toCore(),
201-
metadataJson: this.#poolMetadata?.toCore(),
202201
owners: this.#poolOwners
203202
.toCore()
204203
.map((keyHash) => createRewardAccount(keyHash, rewardAccountAddress.getNetworkId())),
@@ -207,6 +206,10 @@ export class PoolParams {
207206
rewardAccount: this.#rewardAccount.toAddress().toBech32() as Cardano.RewardAccount,
208207
vrf: this.#vrfKeyHash
209208
};
209+
210+
if (this.#poolMetadata) poolParams.metadataJson = this.#poolMetadata.toCore();
211+
212+
return poolParams;
210213
}
211214

212215
/**

packages/core/src/Serialization/Transaction.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,18 @@ export class Transaction {
122122
* @returns The Core Tx object.
123123
*/
124124
toCore(): Cardano.Tx {
125-
return {
126-
auxiliaryData: this.#auxiliaryData ? this.#auxiliaryData.toCore() : undefined,
125+
const tx: Cardano.Tx = {
127126
body: this.#body.toCore(),
128127
id: this.getId(),
129128
isValid: this.#isValid,
130129
witness: this.#witnessSet.toCore()
131130
};
131+
132+
if (this.#auxiliaryData) {
133+
tx.auxiliaryData = this.#auxiliaryData.toCore();
134+
}
135+
136+
return tx;
132137
}
133138

134139
/**

packages/core/src/Serialization/TransactionBody/TransactionOutput.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,16 +208,21 @@ export class TransactionOutput {
208208
* @returns The Core TransactionOutput object.
209209
*/
210210
toCore(): Cardano.TxOut {
211-
return {
211+
const value = this.#amount.toCore();
212+
if (!value.assets) delete value.assets;
213+
214+
const txOut: Cardano.TxOut = {
212215
address: this.#address.asByron()
213216
? this.#address.toBase58()
214217
: (this.#address.toBech32() as unknown as Cardano.PaymentAddress),
215-
datum:
216-
this.#datum && this.#datum.kind() === DatumKind.InlineData ? this.#datum.asInlineData()?.toCore() : undefined,
217-
datumHash: this.#datum && this.#datum.kind() === DatumKind.DataHash ? this.#datum.asDataHash() : undefined,
218-
scriptReference: this.#scriptRef ? this.#scriptRef.toCore() : undefined,
219-
value: this.#amount.toCore()
218+
value
220219
};
220+
221+
if (this.#datum && this.#datum.kind() === DatumKind.InlineData) txOut.datum = this.#datum.asInlineData()?.toCore();
222+
if (this.#datum && this.#datum.kind() === DatumKind.DataHash) txOut.datumHash = this.#datum.asDataHash();
223+
if (this.#scriptRef) txOut.scriptReference = this.#scriptRef.toCore();
224+
225+
return txOut;
221226
}
222227

223228
/**

packages/core/src/Serialization/Update/ProtocolParamUpdate.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -412,17 +412,15 @@ export class ProtocolParamUpdate {
412412
* @returns The Core ProtocolParamUpdate object.
413413
*/
414414
toCore(): Cardano.ProtocolParametersUpdate {
415-
return {
415+
const protocolParametersUpdate: Cardano.ProtocolParametersUpdate = {
416416
coinsPerUtxoByte: this.#adaPerUtxoByte ? Number(this.#adaPerUtxoByte) : undefined,
417417
collateralPercentage: this.#collateralPercentage,
418418
committeeTermLimit: this.#committeeTermLimit ? EpochNo(this.#committeeTermLimit) : undefined,
419419
costModels: this.#costModels?.toCore(),
420420
dRepDeposit: this.#drepDeposit,
421421
dRepInactivityPeriod: this.#drepInactivityPeriod ? EpochNo(this.#drepInactivityPeriod) : undefined,
422422
dRepVotingThresholds: this.#drepVotingThresholds?.toCore(),
423-
decentralizationParameter: this.#d ? this.#d.toFloat().toString() : undefined,
424423
desiredNumberOfPools: this.#nOpt,
425-
extraEntropy: this.#extraEntropy,
426424
governanceActionDeposit: this.#governanceActionDeposit,
427425
governanceActionValidityPeriod: this.#governanceActionValidityPeriod
428426
? EpochNo(this.#governanceActionValidityPeriod)
@@ -447,10 +445,15 @@ export class ProtocolParamUpdate {
447445
poolRetirementEpochBound: this.#maxEpoch,
448446
poolVotingThresholds: this.#poolVotingThresholds?.toCore(),
449447
prices: this.#executionCosts?.toCore(),
450-
protocolVersion: this.#protocolVersion?.toCore(),
451448
stakeKeyDeposit: this.#keyDeposit ? Number(this.#keyDeposit) : undefined,
452449
treasuryExpansion: this.#treasuryGrowthRate ? this.#treasuryGrowthRate.toFloat().toString() : undefined
453450
};
451+
452+
if (this.#d) protocolParametersUpdate.decentralizationParameter = this.#d.toFloat().toString();
453+
if (this.#extraEntropy !== undefined) protocolParametersUpdate.extraEntropy = this.#extraEntropy;
454+
if (this.#protocolVersion) protocolParametersUpdate.protocolVersion = this.#protocolVersion.toCore();
455+
456+
return protocolParametersUpdate;
454457
}
455458

456459
/**

0 commit comments

Comments
 (0)