diff --git a/README.md b/README.md index 96c083028..36061cc67 100644 --- a/README.md +++ b/README.md @@ -119,8 +119,7 @@ import { NativeTransferBuilder, PrivateKey, KeyAlgorithm, - PublicKey, - Transaction + PublicKey } from 'casper-js-sdk'; const rpcHandler = new HttpHandler('http://:7777/rpc'); @@ -128,7 +127,7 @@ const rpcClient = new RpcClient(rpcHandler); const privateKey = await PrivateKey.generate(KeyAlgorithm.ED25519); -const transactionV1 = new NativeTransferBuilder() +const transaction = new NativeTransferBuilder() .from(privateKey.publicKey) .target( PublicKey.fromHex( @@ -141,12 +140,10 @@ const transactionV1 = new NativeTransferBuilder() .payment(100_000_000) .build(); -await transactionV1.sign(privateKey); - -const tx = Transaction.fromTransactionV1(transactionV1); +await transaction.sign(privateKey); try { - const result = await rpcClient.putTransaction(tx); + const result = await rpcClient.putTransaction(transaction); console.log(`Transaction Hash: ${result.transactionHash}`); } catch (e) { console.error(e); diff --git a/migration-guide-v2-v5.md b/migration-guide-v2-v5.md index df863e1c4..956ff2eda 100644 --- a/migration-guide-v2-v5.md +++ b/migration-guide-v2-v5.md @@ -100,13 +100,14 @@ const nativeTarget = new TransactionTarget({}); const stored = new StoredTarget(); const invocationTarget = new TransactionInvocationTarget(); invocationTarget.byHash = new Hash(Uint8Array.from([])); // an example of hash -storedTarget.runtime = 'VmCasperV1'; // an example of runtime +storedTarget.runtime = TransactionRuntime.vmCasperV1(); // an example of runtime storedTarget.id = invocationTarget; const storedTransactionTarget = new TransactionTarget( undefined, stored ); + // OR const storedTransactionTarget1 = new TransactionTarget(); storedTransactionTarget1.stored = stored; @@ -117,16 +118,15 @@ storedTransactionTarget1.stored = stored; ```typescript const sessionTarget = new SessionTarget(); sessionTarget.moduleBytes = Uint8Array.from([]); // an example of module bytes -sessionTarget.runtime = 'VmCasperV1'; // an example of runtime -sessionTarget.transferredValue = 1000; // an example of transferredValue +sessionTarget.runtime = TransactionRuntime.vmCasperV1(); // an example of runtime sessionTarget.isInstallUpgrade = true; // an example of isInstallUpgrade -sessionTarget.seed = new Hash(Uint8Array.from([])); // an example of seed const sessionTransactionTarget = new TransactionTarget( undefined, undefined, sessionTarget ); + // OR const sessionTransactionTarget1 = new TransactionTarget(); sessionTransactionTarget1.session = sessionTarget; @@ -158,19 +158,20 @@ Specifies how transaction fees are calculated. Supports three modes: #### Examples: -- **FixedMode**: +- **PaymentLimitedMode**: ```typescript -const fixedMode = new FixedMode(); -fixedMode.gasPriceTolerance = 2; -fixedMode.additionalComputationFactor = 1; +const paymentLimited = new PaymentLimitedMode(); +paymentLimited.standardPayment = true; +paymentLimited.paymentAmount = '250000000'; +paymentLimited.gasPriceTolerance = 1; ``` - **Assign Pricing Mode**: ```typescript const pricingMode = new PricingMode(); -pricingMode.fixed = fixedMode; +pricingMode.paymentLimited = paymentLimited; ``` --- @@ -268,7 +269,7 @@ console.log(`Transaction Hash: ${result.transactionHash}`); ## Using [TransactionBuilder](./src/types/TransactionBuilder.md) -The TransactionV1Builder is a base class used to create various types of transactions. By extending this class, you can build custom transaction types with specific methods and properties. +The `TransactionBuilder` is a base class used to create various types of transactions. By extending this class, you can build custom transaction types with specific methods and properties. #### Example of how to construct a transaction with TransactionBuilder and push it to the network: @@ -279,8 +280,7 @@ import { NativeTransferBuilder, PrivateKey, KeyAlgorithm, - PublicKey, - Transaction + PublicKey } from 'casper-js-sdk'; const rpcHandler = new HttpHandler('http://:7777/rpc'); @@ -288,7 +288,7 @@ const rpcClient = new RpcClient(rpcHandler); const privateKey = await PrivateKey.generate(KeyAlgorithm.ED25519); -const transactionV1 = new NativeTransferBuilder() +const transaction = new NativeTransferBuilder() .from(sender.publicKey) .target( PublicKey.fromHex( @@ -301,12 +301,10 @@ const transactionV1 = new NativeTransferBuilder() .payment(100_000_000) .build(); -await transactionV1.sign(privateKey); - -const tx = Transaction.fromTransactionV1(transactionV1); +await transaction.sign(privateKey); try { - const result = await rpcClient.putTransaction(tx); + const result = await rpcClient.putTransaction(transaction); console.log(`Transaction Hash: ${result.transactionHash}`); } catch (e) { console.error(e); diff --git a/src/types/Transaction.test.ts b/src/types/Transaction.test.ts index 8d152147b..fff24f67f 100644 --- a/src/types/Transaction.test.ts +++ b/src/types/Transaction.test.ts @@ -110,12 +110,13 @@ describe('Test Transaction', () => { await transaction.sign(sender); - const transactionPaymentAmount = transaction.payload.fields.args.args + const transactionV2 = transaction.getTransactionV1()!; + const transactionPaymentAmount = transactionV2.payload.fields.args.args .get('amount')! .toString(); assert.deepEqual(transaction.approvals[0].signer, sender.publicKey); assert.deepEqual(parseInt(transactionPaymentAmount, 10), 25000000000); - expect(transaction.payload.chainName).to.deep.equal('casper-net-1'); + expect(transactionV2.payload.chainName).to.deep.equal('casper-net-1'); }); }); diff --git a/src/types/TransactionBuilder.md b/src/types/TransactionBuilder.md index 2a936fceb..d98e3de61 100644 --- a/src/types/TransactionBuilder.md +++ b/src/types/TransactionBuilder.md @@ -1,12 +1,12 @@ -# TransactionV1 Builders +# Transaction Builders -TransactionBuilder contains classes and methods for building and managing Casper blockchain transactions. The provided classes extend the `TransactionV1Builder` base class to create specific types of transactions, such as transfers, bids, delegations, and contract calls. +TransactionBuilder contains classes and methods for building and managing Casper blockchain transactions. The provided classes extend the `TransactionBuilder` base class to create specific types of transactions, such as transfers, bids, delegations, and contract calls. ## Table of Contents - [Overview](#overview) - [Classes](#classes) - - [TransactionV1Builder](#transactionv1builder) + - [TransactionBuilder](#transactionv1builder) - [NativeTransferBuilder](#nativetransferbuilder) - [NativeAddBidBuilder](#nativeaddbidbuilder) - [NativeWithdrawBidBuilder](#nativewithdrawbidbuilder) @@ -20,11 +20,11 @@ TransactionBuilder contains classes and methods for building and managing Casper ## Overview -The provided classes allow developers to build, customize, and execute transactions on the Casper blockchain. By using builder patterns, developers can chain methods to configure each aspect of the transaction before calling `build()` to create the final `TransactionV1` instance. +The provided classes allow developers to build, customize, and execute transactions on the Casper blockchain. By using builder patterns, developers can chain methods to configure each aspect of the transaction before calling `build()` to create the final [Transaction](./Transaction.ts) instance. ## Classes -### TransactionV1Builder +### TransactionBuilder The abstract base class for all transaction builders. This class provides common methods for setting basic transaction parameters such as: @@ -137,12 +137,18 @@ Used to create session transactions. ## Usage Example ```typescript -import { PublicKey, Args, PrivateKey } from 'casper-js-sdk'; +import { + PublicKey, + Args, + PrivateKey, + NativeTransferBuilder, + ContractCallBuilder +} from 'casper-js-sdk'; const sender = await PrivateKey.generate(KeyAlgorithm.ED25519); // Create a simple native transfer -const transactionV1 = new NativeTransferBuilder() +const transaction = new NativeTransferBuilder() .from(sender.publicKey) .target(PublicKey.fromHex('abcdef0123456789')) .amount('25000000000') // Amount in motes @@ -151,10 +157,10 @@ const transactionV1 = new NativeTransferBuilder() .payment(100_000_000) .build(); -await transactionV1.sign(sender); +await transaction.sign(sender); // Create a contract call -const transactionV1ContractCall = new ContractCallBuilder() +const contractCallTransaction = new ContractCallBuilder() .from(sender.publicKey) .byHash('example_contract') .entryPoint('unstake') @@ -163,5 +169,5 @@ const transactionV1ContractCall = new ContractCallBuilder() .chainName('casper-net-1') .build(); -await transactionV1ContractCall.sign(sender); +await contractCallTransaction.sign(sender); ``` diff --git a/src/types/TransactionBuilder.ts b/src/types/TransactionBuilder.ts index e3441b625..a6bed25d3 100644 --- a/src/types/TransactionBuilder.ts +++ b/src/types/TransactionBuilder.ts @@ -19,7 +19,7 @@ import { TransactionScheduling } from './TransactionScheduling'; import { Args } from './Args'; import { PublicKey } from './keypair'; import { AccountHash, Hash } from './key'; -import { TransactionV1 } from './Transaction'; +import { Transaction, TransactionV1 } from './Transaction'; import { TransactionV1Payload } from './TransactionV1Payload'; import { Duration, Timestamp } from './Time'; import { @@ -35,7 +35,7 @@ import { /** * Abstract base class for building Transaction V1 instances. */ -abstract class TransactionV1Builder> { +abstract class TransactionBuilder> { protected _initiatorAddr!: InitiatorAddr; protected _chainName!: string; protected _timestamp = new Timestamp(new Date()); @@ -102,9 +102,9 @@ abstract class TransactionV1Builder> { } /** - * Builds and returns the TransactionV1 instance. + * Builds and returns the Transaction instance. */ - public build(): TransactionV1 { + public build(): Transaction { const transactionPayload = TransactionV1Payload.build({ initiatorAddr: this._initiatorAddr, timestamp: this._timestamp, @@ -117,14 +117,15 @@ abstract class TransactionV1Builder> { scheduling: this._scheduling }); - return TransactionV1.makeTransactionV1(transactionPayload); + const transactionV1 = TransactionV1.makeTransactionV1(transactionPayload); + return Transaction.fromTransactionV1(transactionV1); } } /** * Builder for creating Native Transfer transactions. */ -export class NativeTransferBuilder extends TransactionV1Builder< +export class NativeTransferBuilder extends TransactionBuilder< NativeTransferBuilder > { private _target!: CLValue; @@ -174,7 +175,7 @@ export class NativeTransferBuilder extends TransactionV1Builder< /** * Builds and returns the Native Transfer transaction. */ - public build(): TransactionV1 { + public build(): Transaction { const runtimeArgs = Args.fromMap({}); runtimeArgs.insert('target', this._target); @@ -192,7 +193,7 @@ export class NativeTransferBuilder extends TransactionV1Builder< } } -export class NativeAddBidBuilder extends TransactionV1Builder< +export class NativeAddBidBuilder extends TransactionBuilder< NativeAddBidBuilder > { private _validator!: CLValue; @@ -248,7 +249,7 @@ export class NativeAddBidBuilder extends TransactionV1Builder< return this; } - public build(): TransactionV1 { + public build(): Transaction { const runtimeArgs = Args.fromMap({}); runtimeArgs.insert('public_key', this._validator); @@ -279,7 +280,7 @@ export class NativeAddBidBuilder extends TransactionV1Builder< } } -export class NativeWithdrawBidBuilder extends TransactionV1Builder< +export class NativeWithdrawBidBuilder extends TransactionBuilder< NativeWithdrawBidBuilder > { private _validator!: CLValue; @@ -303,7 +304,7 @@ export class NativeWithdrawBidBuilder extends TransactionV1Builder< return this; } - public build(): TransactionV1 { + public build(): Transaction { this._runtimeArgs = Args.fromMap({ public_key: this._validator, amount: this._amount @@ -313,7 +314,7 @@ export class NativeWithdrawBidBuilder extends TransactionV1Builder< } } -export class NativeDelegateBuilder extends TransactionV1Builder< +export class NativeDelegateBuilder extends TransactionBuilder< NativeDelegateBuilder > { private _validator!: CLValue; @@ -337,7 +338,7 @@ export class NativeDelegateBuilder extends TransactionV1Builder< return this; } - public build(): TransactionV1 { + public build(): Transaction { if (!this._initiatorAddr.publicKey) { throw new Error('Initiator addr is not specified'); } @@ -352,7 +353,7 @@ export class NativeDelegateBuilder extends TransactionV1Builder< } } -export class NativeUndelegateBuilder extends TransactionV1Builder< +export class NativeUndelegateBuilder extends TransactionBuilder< NativeUndelegateBuilder > { private _validator!: CLValue; @@ -376,7 +377,7 @@ export class NativeUndelegateBuilder extends TransactionV1Builder< return this; } - public build(): TransactionV1 { + public build(): Transaction { if (!this._initiatorAddr.publicKey) { throw new Error('Initiator addr is not specified'); } @@ -391,7 +392,7 @@ export class NativeUndelegateBuilder extends TransactionV1Builder< } } -export class NativeRedelegateBuilder extends TransactionV1Builder< +export class NativeRedelegateBuilder extends TransactionBuilder< NativeRedelegateBuilder > { private _validator!: CLValue; @@ -421,7 +422,7 @@ export class NativeRedelegateBuilder extends TransactionV1Builder< return this; } - public build(): TransactionV1 { + public build(): Transaction { if (!this._initiatorAddr.publicKey) { throw new Error('Initiator addr is not specified'); } @@ -437,7 +438,7 @@ export class NativeRedelegateBuilder extends TransactionV1Builder< } } -export class NativeActivateBidBuilder extends TransactionV1Builder< +export class NativeActivateBidBuilder extends TransactionBuilder< NativeActivateBidBuilder > { private _validator!: CLValue; @@ -455,7 +456,7 @@ export class NativeActivateBidBuilder extends TransactionV1Builder< return this; } - public build(): TransactionV1 { + public build(): Transaction { this._runtimeArgs = Args.fromMap({ validator: this._validator }); @@ -464,7 +465,7 @@ export class NativeActivateBidBuilder extends TransactionV1Builder< } } -export class NativeChangeBidPublicKeyBuilder extends TransactionV1Builder< +export class NativeChangeBidPublicKeyBuilder extends TransactionBuilder< NativeChangeBidPublicKeyBuilder > { private _public_key!: CLValue; @@ -490,7 +491,7 @@ export class NativeChangeBidPublicKeyBuilder extends TransactionV1Builder< return this; } - public build(): TransactionV1 { + public build(): Transaction { this._runtimeArgs = Args.fromMap({ public_key: this._public_key, new_public_key: this._new_public_key @@ -500,7 +501,7 @@ export class NativeChangeBidPublicKeyBuilder extends TransactionV1Builder< } } -export class ContractCallBuilder extends TransactionV1Builder< +export class ContractCallBuilder extends TransactionBuilder< ContractCallBuilder > { constructor() { @@ -581,7 +582,7 @@ export class ContractCallBuilder extends TransactionV1Builder< } } -export class SessionBuilder extends TransactionV1Builder { +export class SessionBuilder extends TransactionBuilder { private _isInstallOrUpgrade = false; constructor() {