Skip to content

Commit 59afa9d

Browse files
committed
refactor: centralize control for pre and post conwa era
1 parent 180dc92 commit 59afa9d

File tree

9 files changed

+34
-32
lines changed

9 files changed

+34
-32
lines changed

packages/core/src/Cardano/types/Certificate.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ export const isCertType = <K extends keyof CertificateTypeMap>(
241241
certificateTypes: readonly K[]
242242
): certificate is CertificateTypeMap[K] => certificateTypes.includes(certificate.__typename as K);
243243

244+
// LW-10773 add the deposit and change StakeRegistration certificate into Registration certificate
244245
/**
245246
* Creates a stake key registration certificate from a given reward account.
246247
*
@@ -254,6 +255,7 @@ export const createStakeRegistrationCert = (rewardAccount: RewardAccount): Certi
254255
}
255256
});
256257

258+
// LW-10773 change deposit from optional to required and remove StakeDeregistration certificate
257259
/**
258260
* Creates a stake key de-registration certificate from a given reward account.
259261
*

packages/core/src/Serialization/Common/CborSet.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { CborReader, CborReaderState, CborTag, CborWriter } from '../CBOR';
22
import { HexBlob } from '@cardano-sdk/util';
3+
import { inConwayEra } from '../../util';
34

45
/** Represents a cbor serialization wrapper for a Core type <{@link C}> */
56
interface CborSerializable<C> {
@@ -16,8 +17,6 @@ interface CborSerializable<C> {
1617
export class CborSet<C, T extends CborSerializable<C>> {
1718
#values: T[];
1819

19-
static useConwaySerialization = false;
20-
2120
// Prevent users from directly creating an instance. Only allow creating via fromCore or fromCbor.
2221
private constructor(values: T[]) {
2322
this.#values = [...values];
@@ -50,13 +49,13 @@ export class CborSet<C, T extends CborSerializable<C>> {
5049
/**
5150
* Serializes a CborSet<T> into CBOR format.
5251
*
53-
* @returns The CborSet in CBOR format, using the `258` tag representation if {@link useConwaySerialization} flag is set,
52+
* @returns The CborSet in CBOR format, using the `258` tag representation if {@link inConwayEra} flag is set,
5453
* or as an the array.
5554
*/
5655
toCbor(): HexBlob {
5756
const writer = new CborWriter();
5857

59-
if (CborSet.useConwaySerialization) writer.writeTag(CborTag.Set);
58+
if (inConwayEra) writer.writeTag(CborTag.Set);
6059

6160
writer.writeStartArray(this.size());
6261

packages/core/src/Serialization/TransactionWitnessSet/Redeemer/Redeemers.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Cardano } from '../../..';
1+
import { Cardano, inConwayEra } from '../../..';
22
import { CborReader, CborReaderState, CborWriter } from '../../CBOR';
33
import { ExUnits } from '../../Common';
44
import { HexBlob, InvalidArgumentError } from '@cardano-sdk/util';
@@ -13,27 +13,22 @@ const MAP_VALUE_EMBEDDED_GROUP_SIZE = 2;
1313
export class Redeemers {
1414
#values: Redeemer[];
1515

16-
// TODO: set to true or remove once mainnet is hardforked to mainnet
17-
/**
18-
* Enable encoding `redeemers` as `Map`:
19-
* https://github.com/IntersectMBO/cardano-ledger/blob/master/eras/conway/impl/cddl-files/conway.cddl#L480
20-
*/
21-
static useConwaySerialization = false;
22-
2316
private constructor(redeemers: Redeemer[]) {
2417
this.#values = [...redeemers];
2518
}
2619

2720
/**
2821
* Serializes Redeemers into CBOR format.
29-
* Redeemers are encoded as array when {@link useConwaySerialization} is false, and as map when
30-
* {@link useConwaySerialization} is true.
22+
* Redeemers are encoded as array when {@link inConwayEra} is false, and as map when
23+
* {@link inConwayEra} is true.
3124
*
3225
* @returns The Redeemers in CBOR format.
3326
*/
3427
toCbor(): HexBlob {
3528
const writer = new CborWriter();
36-
if (Redeemers.useConwaySerialization) {
29+
// Encoding `redeemers` as `Map`:
30+
// https://github.com/IntersectMBO/cardano-ledger/blob/master/eras/conway/impl/cddl-files/conway.cddl#L480
31+
if (inConwayEra) {
3732
// { + [ tag: redeemer_tag, index: uint ] => [ data: plutus_data, ex_units: ex_units ] }
3833
const redeemersMap = new Map(this.#values.map((redeemer) => [`${redeemer.tag()}:${redeemer.index()}`, redeemer]));
3934

packages/core/src/util/conwayEra.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// LW-10773 remove this file
2+
3+
/** Specifies if SDK should behave as in Conway era. */
4+
// eslint-disable-next-line import/no-mutable-exports
5+
export let inConwayEra = false as const;
6+
7+
export const setInConwayEra = (value: boolean) => (inConwayEra = value as false);

packages/core/src/util/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * as util from './misc';
2+
export * from './conwayEra';
23
export * from './slotCalc';
34
export * from './txInspector';
45
export * from './tokenTransferInspector';

packages/core/test/Serialization/Common/CborSet.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { CborReader, CborSet, CborTag, CborWriter } from '../../../src/Serialization';
22
import { HexBlob } from '@cardano-sdk/util';
3+
import { setInConwayEra } from '../../../src';
34

45
class TestNumber {
56
#value: number;
@@ -54,14 +55,14 @@ describe('CborSet', () => {
5455
testCborConway = writer.encodeAsHex();
5556
});
5657

57-
afterEach(() => (CborSet.useConwaySerialization = false));
58+
afterEach(() => setInConwayEra(false));
5859

5960
it('can serialize as array', () => {
6061
expect(testCbor).toEqual(set.toCbor());
6162
});
6263

6364
it('can serialize as 258 tag set', () => {
64-
CborSet.useConwaySerialization = true;
65+
setInConwayEra(true);
6566
expect(testCborConway).toEqual(set.toCbor());
6667
});
6768

@@ -84,7 +85,7 @@ describe('CborSet', () => {
8485
const emptyArrayCbor = writer.encodeAsHex();
8586

8687
const emptySet = CborSet.fromCore<number, TestNumber>([], TestNumber.fromCore);
87-
CborSet.useConwaySerialization = useConwaySerialization;
88+
setInConwayEra(useConwaySerialization);
8889
expect(emptySet.toCbor()).toEqual(emptyArrayCbor);
8990
});
9091

packages/core/test/Serialization/TransactionWitnessSet/Redeemers.test.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Cardano } from '../../../src';
1+
import { Cardano, setInConwayEra } from '../../../src';
22
import { HexBlob } from '@cardano-sdk/util';
33
import { RedeemerPurpose } from '../../../src/Cardano';
44
import { Redeemers } from '../../../src/Serialization';
@@ -33,7 +33,7 @@ const cborInvalidMapIndex = HexBlob('a28308000082d8799f0102030405ff821821182c820
3333
const cborInvalidMapValue = HexBlob('a28200008300d8799f0102030405ff821821182c82040082d8799f0102030405ff8218371842');
3434

3535
describe('Redeemers', () => {
36-
afterEach(() => (Redeemers.useConwaySerialization = false));
36+
afterEach(() => setInConwayEra(false));
3737

3838
it('can decode Redeemers from CBOR', () => {
3939
const redeemers = Redeemers.fromCbor(cbor);
@@ -47,13 +47,12 @@ describe('Redeemers', () => {
4747

4848
it('can decode Redeemers from map encoded CBOR', () => {
4949
const redeemers = Redeemers.fromCbor(cborConway);
50-
// Redeemers.useConwaySerialization = true;
5150
expect(redeemers.toCore()).toEqual(core);
5251
});
5352

5453
it('can encode Redeemers as map in CBOR', () => {
5554
const redeemers = Redeemers.fromCore(core);
56-
Redeemers.useConwaySerialization = true;
55+
setInConwayEra(true);
5756
expect(redeemers.toCbor()).toEqual(cborConway);
5857
});
5958

packages/e2e/test/wallet_epoch_0/PersonalWallet/conwayTransactions.test.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as Crypto from '@cardano-sdk/crypto';
22
import { BaseWallet } from '@cardano-sdk/wallet';
3-
import { Cardano, Serialization } from '@cardano-sdk/core';
3+
import { Cardano, setInConwayEra } from '@cardano-sdk/core';
44
import { logger } from '@cardano-sdk/util-dev';
55

66
import { firstValueFrom, map } from 'rxjs';
@@ -161,9 +161,7 @@ describe.skip('PersonalWallet/conwayTransactions', () => {
161161
};
162162

163163
beforeAll(async () => {
164-
// TODO: remove once mainnet hardforks to conway-era, and this becomes "the norm"
165-
Serialization.CborSet.useConwaySerialization = true;
166-
Serialization.Redeemers.useConwaySerialization = true;
164+
setInConwayEra(true);
167165

168166
// TODO LW-10555: Revert this so that wallet is at account 0 and drepWallet at account 1, once the dbsync bug is fixed
169167
// https://github.com/IntersectMBO/cardano-db-sync/issues/1702
@@ -191,9 +189,7 @@ describe.skip('PersonalWallet/conwayTransactions', () => {
191189
wallet.shutdown();
192190
dRepWallet.shutdown();
193191

194-
// TODO: remove once mainnet hardforks to conway-era, and this becomes "the norm"
195-
Serialization.CborSet.useConwaySerialization = false;
196-
Serialization.Redeemers.useConwaySerialization = false;
192+
setInConwayEra(false);
197193
});
198194

199195
it('can register a stake key and delegate stake using a combo certificate', async () => {

packages/tx-construction/src/tx-builder/TxBuilder.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
WitnessedTx,
99
util
1010
} from '@cardano-sdk/key-management';
11-
import { Cardano, HandleProvider, HandleResolution, Serialization, metadatum } from '@cardano-sdk/core';
11+
import { Cardano, HandleProvider, HandleResolution, Serialization, inConwayEra, metadatum } from '@cardano-sdk/core';
1212
import {
1313
CustomizeCb,
1414
InsufficientRewardAccounts,
@@ -570,6 +570,7 @@ export class GenericTxBuilder implements TxBuilder {
570570
return rewardAccountsWithWeights;
571571
}
572572

573+
// eslint-disable-next-line sonarjs/cognitive-complexity
573574
async #delegatePortfolio(): Promise<RewardAccountsAndWeights> {
574575
const rewardAccountsWithWeights: RewardAccountsAndWeights = new Map();
575576
if (!this.#requestedPortfolio) {
@@ -641,8 +642,9 @@ export class GenericTxBuilder implements TxBuilder {
641642
this.#logger.debug(`De-registering ${availableRewardAccounts.length} stake keys`);
642643
for (const rewardAccount of availableRewardAccounts) {
643644
if (rewardAccount.credentialStatus === Cardano.StakeCredentialStatus.Registered) {
644-
// TODO: re-enable conway stake deregistration cert, after the conway hardfork
645-
certificates.push(Cardano.createStakeDeregistrationCert(rewardAccount.address /* , rewardAccount.deposit*/));
645+
certificates.push(
646+
Cardano.createStakeDeregistrationCert(rewardAccount.address, inConwayEra ? rewardAccount.deposit : undefined)
647+
);
646648
}
647649
}
648650
this.partialTxBody = { ...this.partialTxBody, certificates };

0 commit comments

Comments
 (0)