From 6bea7238f1856d8400047d3ee3ecc229d3a30ddb Mon Sep 17 00:00:00 2001 From: JuanRdBO Date: Sat, 22 Jun 2024 17:51:05 -0600 Subject: [PATCH 1/3] added clearing and adding operations at a specified index --- src/transaction_builder.js | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/transaction_builder.js b/src/transaction_builder.js index efd09505..070040d0 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -226,17 +226,42 @@ export class TransactionBuilder { addOperation(operation) { this.operations.push(operation); return this; + } + + /** + * Adds an operation to the transaction at a specific index. + * + * @param {xdr.Operation} operation - The xdr operation object to add, use {@link Operation} static methods. + * @param {number} index - The index at which to insert the operation. + * + * @returns {TransactionBuilder} - The TransactionBuilder instance for method chaining. + */ + addOperationAtIndex(operation, index) { + this.operations.splice(index, 0, operation); + return this; } /** - * Removes the operations from the builder (useful when cloning). - * @returns {TransactionBuilder} this builder instance - */ + * Removes the operations from the builder (useful when cloning). + * @returns {TransactionBuilder} this builder instance + */ clearOperations() { this.operations = []; return this; } + /** + * Removes the operation at the specified index from the transaction. + * + * @param {number} index - The index of the operation to remove. + * + * @returns {TransactionBuilder} The TransactionBuilder instance for method chaining. + */ + clearOperationAtIndex(index) { + this.operations.splice(index, 1); + return this; + } + /** * Adds a memo to the transaction. * @param {Memo} memo {@link Memo} object From 55b0ba06ded4c44e2ecc809f51f67c1c9ffeab15 Mon Sep 17 00:00:00 2001 From: JuanRdBO Date: Sat, 22 Jun 2024 17:52:16 -0600 Subject: [PATCH 2/3] format docs --- src/transaction_builder.js | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/transaction_builder.js b/src/transaction_builder.js index 070040d0..26b64234 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -229,34 +229,34 @@ export class TransactionBuilder { } /** - * Adds an operation to the transaction at a specific index. - * - * @param {xdr.Operation} operation - The xdr operation object to add, use {@link Operation} static methods. - * @param {number} index - The index at which to insert the operation. - * - * @returns {TransactionBuilder} - The TransactionBuilder instance for method chaining. - */ + * Adds an operation to the transaction at a specific index. + * + * @param {xdr.Operation} operation - The xdr operation object to add, use {@link Operation} static methods. + * @param {number} index - The index at which to insert the operation. + * + * @returns {TransactionBuilder} - The TransactionBuilder instance for method chaining. + */ addOperationAtIndex(operation, index) { this.operations.splice(index, 0, operation); return this; } /** - * Removes the operations from the builder (useful when cloning). - * @returns {TransactionBuilder} this builder instance - */ + * Removes the operations from the builder (useful when cloning). + * @returns {TransactionBuilder} this builder instance + */ clearOperations() { this.operations = []; return this; } /** - * Removes the operation at the specified index from the transaction. - * - * @param {number} index - The index of the operation to remove. - * - * @returns {TransactionBuilder} The TransactionBuilder instance for method chaining. - */ + * Removes the operation at the specified index from the transaction. + * + * @param {number} index - The index of the operation to remove. + * + * @returns {TransactionBuilder} The TransactionBuilder instance for method chaining. + */ clearOperationAtIndex(index) { this.operations.splice(index, 1); return this; From 53c97c6c595ccfa14faa974c3f7db8364ec5036f Mon Sep 17 00:00:00 2001 From: JuanRdBO Date: Fri, 28 Jun 2024 10:22:45 -0600 Subject: [PATCH 3/3] Adds test; updates naming --- src/transaction_builder.js | 4 +-- test/unit/transaction_builder_test.js | 48 +++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/transaction_builder.js b/src/transaction_builder.js index 26b64234..2437a02c 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -236,7 +236,7 @@ export class TransactionBuilder { * * @returns {TransactionBuilder} - The TransactionBuilder instance for method chaining. */ - addOperationAtIndex(operation, index) { + addOperationAt(operation, index) { this.operations.splice(index, 0, operation); return this; } @@ -257,7 +257,7 @@ export class TransactionBuilder { * * @returns {TransactionBuilder} The TransactionBuilder instance for method chaining. */ - clearOperationAtIndex(index) { + clearOperationAt(index) { this.operations.splice(index, 1); return this; } diff --git a/test/unit/transaction_builder_test.js b/test/unit/transaction_builder_test.js index 3c790c7a..d2cda555 100644 --- a/test/unit/transaction_builder_test.js +++ b/test/unit/transaction_builder_test.js @@ -954,5 +954,53 @@ describe('TransactionBuilder', function () { .build(); expect(cloneTx.operations).to.be.empty; }); + + it('adds operations at a specific index', function () { + const builder = new StellarBase.TransactionBuilder(source, { + fee: '100', + timebounds: { minTime: 0, maxTime: 0 }, + memo: new StellarBase.Memo(StellarBase.MemoText, 'Testing adding op at index'), + networkPassphrase + }); + + builder.addOperationAt(StellarBase.Operation.payment({ + source: source.accountId(), + destination: destination, + amount: '1', + asset: asset + }), 0); + + builder.addOperationAt(StellarBase.Operation.payment({ + source: source.accountId(), + destination: destination, + amount: '2', + asset: asset + }), 1); + + const tx = builder.build(); + // Assert operations + expect(tx.operations.length).to.equal(2); + expect(tx.operations[0].source).to.equal(source.accountId()); + expect(parseInt(tx.operations[0].amount)).to.equal(1); + expect(tx.operations[1].source).to.equal(source.accountId()); + expect(parseInt(tx.operations[1].amount)).to.equal(2); + + const clonedTx = StellarBase.TransactionBuilder.cloneFrom(tx) + clonedTx.clearOperationAt(0); + const newOperation = StellarBase.Operation.payment({ + source: source.accountId(), + destination: destination, + amount: '3', + asset: asset + }) + clonedTx.addOperationAt(newOperation, 0); + const newTx = clonedTx.build() + // Assert that the operations are the same, but the first one is updated + expect(newTx.operations.length).to.equal(2); + expect(newTx.operations[0].source).to.equal(source.accountId()); + expect(parseInt(newTx.operations[0].amount)).to.equal(3); + expect(newTx.operations[1].source).to.equal(source.accountId()); + expect(parseInt(newTx.operations[1].amount)).to.equal(2); + }); }); });