diff --git a/src/types/Transaction.test.ts b/src/types/Transaction.test.ts index d5978e4b..f8e5e2e6 100644 --- a/src/types/Transaction.test.ts +++ b/src/types/Transaction.test.ts @@ -19,12 +19,11 @@ import { CLValueUInt64 } from './clvalue'; import { PublicKey } from './keypair'; -import { PayloadFields, TransactionV1Payload } from './TransactionPayload'; +import { TransactionV1Payload } from './TransactionPayload'; describe('Test Transaction', () => { it('should create a Transaction from TransactionV1', async () => { const keys = await PrivateKey.generate(KeyAlgorithm.ED25519); - const timestamp = new Timestamp(new Date()); const paymentAmount = 20000000000000; const pricingMode = new PricingMode(); @@ -43,36 +42,26 @@ describe('Test Transaction', () => { id: CLValueOption.newCLOption(CLValueUInt64.newCLUint64(3)) }); - const transactionTarget = new TransactionTarget(new SessionTarget()); - const entryPoint = new TransactionEntryPoint(undefined, {}); - const scheduling = new TransactionScheduling({}); - - const payloadFields = new PayloadFields(); - payloadFields.addField(0, args.toBytes()); - payloadFields.addField(1, transactionTarget.toBytes()); - payloadFields.addField(2, entryPoint.bytes()); - payloadFields.addField(3, scheduling.bytes()); - - const transactionPayload = new TransactionV1Payload(); - transactionPayload.initiatorAddr = new InitiatorAddr(keys.publicKey); - transactionPayload.ttl = new Duration(1800000); - transactionPayload.args = args; - transactionPayload.entryPoint = entryPoint; - transactionPayload.pricingMode = pricingMode; - transactionPayload.timestamp = timestamp; - transactionPayload.category = 2; - transactionPayload.target = transactionTarget; - transactionPayload.scheduling = scheduling; - transactionPayload.chainName = 'casper-net-1'; - transactionPayload.fields = payloadFields; + const transactionPayload = TransactionV1Payload.build({ + initiatorAddr: new InitiatorAddr(keys.publicKey), + ttl: new Duration(1800000), + args, + timestamp: new Timestamp(new Date()), + category: 2, + entryPoint: new TransactionEntryPoint(undefined, {}), + scheduling: new TransactionScheduling({}), + transactionTarget: new TransactionTarget(new SessionTarget()), + chainName: 'casper-net-1', + pricingMode + }); const transaction = TransactionV1.makeTransactionV1(transactionPayload); await transaction.sign(keys); - // const toJson = TransactionV1.toJson(transaction); - // const parsed = TransactionV1.fromJSON(toJson); + const toJson = TransactionV1.toJson(transaction); + console.log(toJson); - // console.log(Args.fromBytes(transaction.payload.fields.fields.get(0)!)); + // const parsed = TransactionV1.fromJSON(toJson); // const transactionPaymentAmount = parsed.body.args.args // .get('amount')! diff --git a/src/types/Transaction.ts b/src/types/Transaction.ts index bed56ddb..506e7eee 100644 --- a/src/types/Transaction.ts +++ b/src/types/Transaction.ts @@ -148,7 +148,6 @@ export class TransactionV1 { * @throws {TransactionError} Throws errors if validation fails. */ public validate(): boolean { - console.log(this.payload); const payloadBytes = this.payload!.toBytes(); const calculatedHash = new Hash(byteHash(payloadBytes)); @@ -401,7 +400,7 @@ export class TransactionBody { * The category of the transaction, indicating its type (e.g., minting, auction). */ @jsonMember({ name: 'transaction_category', constructor: Number }) - public category: number; + public category?: number; /** * Constructs a `TransactionBody` with the given arguments, target, entry point, scheduling, and category. @@ -416,7 +415,7 @@ export class TransactionBody { target: TransactionTarget, entryPoint: TransactionEntryPoint, scheduling: TransactionScheduling, - category: number + category?: number ) { this.args = args; this.target = target; diff --git a/src/types/TransactionPayload.ts b/src/types/TransactionPayload.ts index e1450d2d..41c05a13 100644 --- a/src/types/TransactionPayload.ts +++ b/src/types/TransactionPayload.ts @@ -19,6 +19,19 @@ import { byteArrayJsonSerializer } from './SerializationUtils'; +interface ITransactionPayloadBuildParams { + initiatorAddr: InitiatorAddr; + args: Args; + ttl: Duration; + entryPoint: TransactionEntryPoint; + pricingMode: PricingMode; + timestamp: Timestamp; + category?: number; + transactionTarget: TransactionTarget; + scheduling: TransactionScheduling; + chainName: string; +} + export class PayloadFields { public fields: Map = new Map(); @@ -125,7 +138,7 @@ export class TransactionV1Payload { public target: TransactionTarget; public entryPoint: TransactionEntryPoint; public scheduling: TransactionScheduling; - public category: number; + public category?: number; public toBytes(): Uint8Array { const calltableSerialization = new CalltableSerialization(); @@ -147,4 +160,38 @@ export class TransactionV1Payload { return calltableSerialization.toBytes(); } + + public static build({ + initiatorAddr, + args, + ttl, + entryPoint, + pricingMode, + timestamp, + category, + transactionTarget, + scheduling, + chainName + }: ITransactionPayloadBuildParams): TransactionV1Payload { + const payloadFields = new PayloadFields(); + payloadFields.addField(0, args.toBytes()); + payloadFields.addField(1, transactionTarget.toBytes()); + payloadFields.addField(2, entryPoint.bytes()); + payloadFields.addField(3, scheduling.bytes()); + + const transactionPayload = new TransactionV1Payload(); + transactionPayload.initiatorAddr = initiatorAddr; + transactionPayload.ttl = ttl; + transactionPayload.args = args; + transactionPayload.entryPoint = entryPoint; + transactionPayload.pricingMode = pricingMode; + transactionPayload.timestamp = timestamp; + transactionPayload.category = category; + transactionPayload.target = transactionTarget; + transactionPayload.scheduling = scheduling; + transactionPayload.chainName = chainName; + transactionPayload.fields = payloadFields; + + return transactionPayload; + } }