Skip to content

Commit

Permalink
Added build method for TransactionV1Payload
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmyshchyshyn committed Nov 21, 2024
1 parent 332a002 commit 484bbdd
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 31 deletions.
43 changes: 16 additions & 27 deletions src/types/Transaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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')!
Expand Down
5 changes: 2 additions & 3 deletions src/types/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down Expand Up @@ -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.
Expand All @@ -416,7 +415,7 @@ export class TransactionBody {
target: TransactionTarget,
entryPoint: TransactionEntryPoint,
scheduling: TransactionScheduling,
category: number
category?: number
) {
this.args = args;
this.target = target;
Expand Down
49 changes: 48 additions & 1 deletion src/types/TransactionPayload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number, Uint8Array> = new Map();

Expand Down Expand Up @@ -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();
Expand All @@ -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;
}
}

0 comments on commit 484bbdd

Please sign in to comment.