Skip to content

Commit 0bb5de4

Browse files
Flatten TransactionHeader and TransactionBody into single Transaction class
1 parent 242b3fe commit 0bb5de4

File tree

3 files changed

+75
-132
lines changed

3 files changed

+75
-132
lines changed

src/types/Deploy.ts

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,10 @@ import { jsonArrayMember, jsonMember, jsonObject, TypedJSON } from 'typedjson';
22
import { concat } from '@ethersproject/bytes';
33

44
import { Hash } from './key';
5-
import { PrivateKey } from './keypair/PrivateKey';
65
import { HexBytes } from './HexBytes';
7-
import { PublicKey } from './keypair';
6+
import { PublicKey, PrivateKey } from './keypair';
87
import { Duration, Timestamp } from './Time';
9-
import {
10-
Approval,
11-
Transaction,
12-
TransactionBody,
13-
TransactionCategory,
14-
TransactionHeader
15-
} from './Transaction';
8+
import { Approval, Transaction, TransactionCategory } from './Transaction';
169
import {
1710
TransactionEntryPoint,
1811
TransactionEntryPointEnum
@@ -411,21 +404,17 @@ export class Deploy {
411404

412405
return new Transaction(
413406
deploy.hash,
414-
new TransactionHeader(
415-
deploy.header.chainName,
416-
deploy.header.timestamp,
417-
deploy.header.ttl,
418-
new InitiatorAddr(deploy.header.account),
419-
pricingMode
420-
),
421-
new TransactionBody(
422-
deploy.session.getArgs(),
423-
TransactionTarget.newTransactionTargetFromSession(deploy.session),
424-
transactionEntryPoint,
425-
new TransactionScheduling({ standard: {} }),
426-
transactionCategory
427-
),
407+
deploy.header.chainName,
408+
deploy.header.timestamp,
409+
deploy.header.ttl,
410+
new InitiatorAddr(deploy.header.account),
411+
pricingMode,
412+
deploy.session.getArgs(),
413+
TransactionTarget.newTransactionTargetFromSession(deploy.session),
414+
transactionEntryPoint,
415+
new TransactionScheduling({ standard: {} }),
428416
deploy.approvals,
417+
transactionCategory,
429418
undefined,
430419
deploy
431420
);

src/types/ExecutableDeployItem.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ export class TransferDeployItem {
365365
amount: BigNumber | string,
366366
target: URef | PublicKey,
367367
sourcePurse: URef | null = null,
368-
id?: BigNumberish,
368+
id?: BigNumberish
369369
): TransferDeployItem {
370370
const runtimeArgs = Args.fromMap({});
371371
runtimeArgs.insert('amount', CLValueUInt512.newCLUInt512(amount));
@@ -386,7 +386,9 @@ export class TransferDeployItem {
386386

387387
runtimeArgs.insert(
388388
'id',
389-
id ? CLValueOption.newCLOption(CLValueUInt64.newCLUint64(id)) : defaultClValue
389+
id
390+
? CLValueOption.newCLOption(CLValueUInt64.newCLUint64(id))
391+
: defaultClValue
390392
);
391393

392394
return new TransferDeployItem(runtimeArgs);

src/types/Transaction.ts

Lines changed: 59 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ import { PricingMode } from './PricingMode';
88
import { TransactionTarget } from './TransactionTarget';
99
import { TransactionEntryPoint } from './TransactionEntryPoint';
1010
import { TransactionScheduling } from './TransactionScheduling';
11-
import { PublicKey } from './keypair';
11+
import { PublicKey, PrivateKey } from './keypair';
1212
import { HexBytes } from './HexBytes';
13-
import { PrivateKey } from './keypair/PrivateKey';
1413
import { Args } from './Args';
1514
import { deserializeArgs, serializeArgs } from './SerializationUtils';
1615
import { byteHash } from './ByteConverters';
@@ -101,7 +100,7 @@ export class Approval {
101100
}
102101

103102
/**
104-
* Represents a TransactionV1 object, including its header, body, and approvals.
103+
* Represents a TransactionV1 object, including its hash, payload, and approvals.
105104
*/
106105
@jsonObject
107106
export class TransactionV1 {
@@ -272,11 +271,21 @@ export class TransactionV1 {
272271
}
273272

274273
/**
275-
* Represents the header of a transaction, including details like chain name, timestamp,
276-
* time-to-live (TTL), initiator address, and pricing mode.
274+
* A wrapper for a TransactionV1 or Deploy.
277275
*/
278276
@jsonObject
279-
export class TransactionHeader {
277+
export class Transaction {
278+
/**
279+
* The hash of the transaction.
280+
*/
281+
@jsonMember({
282+
name: 'hash',
283+
constructor: Hash,
284+
deserializer: json => Hash.fromJSON(json),
285+
serializer: value => value.toJSON()
286+
})
287+
public hash: Hash;
288+
280289
/**
281290
* The name of the blockchain chain associated with this transaction.
282291
*/
@@ -322,35 +331,6 @@ export class TransactionHeader {
322331
@jsonMember({ name: 'pricing_mode', constructor: PricingMode })
323332
public pricingMode: PricingMode;
324333

325-
/**
326-
* Creates a new `TransactionHeader` instance with the given properties.
327-
* @param chainName The name of the blockchain chain.
328-
* @param timestamp The timestamp of the transaction.
329-
* @param ttl The TTL (Time-To-Live) for the transaction.
330-
* @param initiatorAddr The address of the transaction initiator.
331-
* @param pricingMode The pricing mode for the transaction.
332-
*/
333-
constructor(
334-
chainName: string,
335-
timestamp: Timestamp,
336-
ttl: Duration,
337-
initiatorAddr: InitiatorAddr,
338-
pricingMode: PricingMode
339-
) {
340-
this.chainName = chainName;
341-
this.timestamp = timestamp;
342-
this.ttl = ttl;
343-
this.initiatorAddr = initiatorAddr;
344-
this.pricingMode = pricingMode;
345-
}
346-
}
347-
348-
/**
349-
* Represents the body of a transaction, containing the arguments, target,
350-
* entry point, scheduling information, and transaction category.
351-
*/
352-
@jsonObject
353-
export class TransactionBody {
354334
/**
355335
* The arguments for the transaction, which can be a map of values required by the entry point.
356336
*/
@@ -395,62 +375,11 @@ export class TransactionBody {
395375

396376
/**
397377
* The category of the transaction, indicating its type (e.g., minting, auction).
378+
* Using TransactionCategory as enum
398379
*/
399380
@jsonMember({ name: 'transaction_category', constructor: Number })
400381
public category?: number;
401382

402-
/**
403-
* Constructs a `TransactionBody` with the given arguments, target, entry point, scheduling, and category.
404-
* @param args The arguments for the transaction.
405-
* @param target The target of the transaction (e.g., a contract or account).
406-
* @param entryPoint The entry point to specify the method or action of the transaction.
407-
* @param scheduling The scheduling information for the transaction's execution.
408-
* @param category The category/type of the transaction (e.g., mint, auction).
409-
*/
410-
constructor(
411-
args: Args,
412-
target: TransactionTarget,
413-
entryPoint: TransactionEntryPoint,
414-
scheduling: TransactionScheduling,
415-
category?: number
416-
) {
417-
this.args = args;
418-
this.target = target;
419-
this.entryPoint = entryPoint;
420-
this.scheduling = scheduling;
421-
this.category = category;
422-
}
423-
}
424-
425-
/**
426-
* Represents a transaction in the system, containing information such as its hash,
427-
* header, body, approvals, and optionally its associated deployment and transaction details.
428-
*/
429-
@jsonObject
430-
export class Transaction {
431-
/**
432-
* The hash of the transaction.
433-
*/
434-
@jsonMember({
435-
name: 'hash',
436-
constructor: Hash,
437-
deserializer: json => Hash.fromJSON(json),
438-
serializer: value => value.toJSON()
439-
})
440-
public hash: Hash;
441-
442-
/**
443-
* The header of the transaction, which includes metadata about the transaction.
444-
*/
445-
@jsonMember({ name: 'header', constructor: TransactionHeader })
446-
public header: TransactionHeader;
447-
448-
/**
449-
* The body of the transaction, containing details such as the target, entry point, and arguments.
450-
*/
451-
@jsonMember({ name: 'body', constructor: TransactionBody })
452-
public body: TransactionBody;
453-
454383
/**
455384
* The list of approvals for this transaction.
456385
*/
@@ -472,24 +401,49 @@ export class Transaction {
472401
/**
473402
* Creates a new `Transaction` instance with the specified values.
474403
* @param hash The hash of the transaction.
475-
* @param header The header of the transaction.
476-
* @param body The body of the transaction.
404+
* @param chainName The blockchain chain name associated with this transaction.
405+
* @param timestamp The timestamp of transaction creation.
406+
* @param ttl The time-to-live duration of the transaction.
407+
* @param initiatorAddr The address of the transaction initiator.
408+
* @param pricingMode The pricing mode for this transaction.
409+
* @param args The arguments for the transaction.
410+
* @param target The target of the transaction.
411+
* @param entryPoint The entry point of the transaction.
412+
* @param scheduling The scheduling information for the transaction.
477413
* @param approvals The list of approvals for this transaction.
414+
* @param category The category of the transaction, indicating its type (e.g., minting, auction).
478415
* @param originTransactionV1 The original TransactionV1, if applicable.
479416
* @param originDeployV1 The original deploy, if applicable.
480417
*/
481418
constructor(
482419
hash: Hash,
483-
header: TransactionHeader,
484-
body: TransactionBody,
420+
chainName: string,
421+
timestamp: Timestamp,
422+
ttl: Duration,
423+
initiatorAddr: InitiatorAddr,
424+
pricingMode: PricingMode,
425+
args: Args,
426+
target: TransactionTarget,
427+
entryPoint: TransactionEntryPoint,
428+
scheduling: TransactionScheduling,
485429
approvals: Approval[],
430+
category?: TransactionCategory,
486431
originTransactionV1?: TransactionV1,
487432
originDeployV1?: Deploy
488433
) {
489434
this.hash = hash;
490-
this.header = header;
491-
this.body = body;
435+
this.chainName = chainName;
436+
this.timestamp = timestamp;
437+
this.ttl = ttl;
438+
this.initiatorAddr = initiatorAddr;
439+
this.pricingMode = pricingMode;
440+
this.args = args;
441+
this.target = target;
442+
this.entryPoint = entryPoint;
443+
this.scheduling = scheduling;
492444
this.approvals = approvals;
445+
this.category = category;
446+
493447
this.originDeployV1 = originDeployV1;
494448
this.originTransactionV1 = originTransactionV1;
495449
}
@@ -518,21 +472,19 @@ export class Transaction {
518472
static fromTransactionV1(v1: TransactionV1): Transaction {
519473
return new Transaction(
520474
v1.hash,
521-
new TransactionHeader(
522-
v1.payload.chainName,
523-
v1.payload.timestamp,
524-
v1.payload.ttl,
525-
v1.payload.initiatorAddr,
526-
v1.payload.pricingMode
527-
),
528-
new TransactionBody(
529-
v1.payload.fields.args,
530-
v1.payload.fields.target,
531-
v1.payload.fields.entryPoint,
532-
v1.payload.fields.scheduling
533-
),
475+
v1.payload.chainName,
476+
v1.payload.timestamp,
477+
v1.payload.ttl,
478+
v1.payload.initiatorAddr,
479+
v1.payload.pricingMode,
480+
v1.payload.fields.args,
481+
v1.payload.fields.target,
482+
v1.payload.fields.entryPoint,
483+
v1.payload.fields.scheduling,
534484
v1.approvals,
535-
v1
485+
undefined,
486+
v1, // originTransactionV1
487+
undefined // originDeployV1 is not applicable for this method
536488
);
537489
}
538490
}

0 commit comments

Comments
 (0)