Skip to content

Commit

Permalink
Add TransactionBuilder.addOperationAt|clearOperationAt methods (#757)
Browse files Browse the repository at this point in the history
  • Loading branch information
JuanRdBO authored Jun 28, 2024
1 parent 9abd8bd commit c4f9989
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/transaction_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,19 @@ 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.
*/
addOperationAt(operation, index) {
this.operations.splice(index, 0, operation);
return this;
}

/**
Expand All @@ -237,6 +250,18 @@ export class TransactionBuilder {
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.
*/
clearOperationAt(index) {
this.operations.splice(index, 1);
return this;
}

/**
* Adds a memo to the transaction.
* @param {Memo} memo {@link Memo} object
Expand Down
48 changes: 48 additions & 0 deletions test/unit/transaction_builder_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});

0 comments on commit c4f9989

Please sign in to comment.