Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export type MeshTxBuilderBody = {
referenceInputs: RefTxIn[];
mints: MintParam[];
changeAddress: string;
changeAddressOutputIndex?: number;
metadata: TxMetadata;
scriptMetadata: ScriptMetadata[];
validityRange: ValidityRange;
Expand Down
16 changes: 16 additions & 0 deletions packages/mesh-transaction/src/mesh-tx-builder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,22 @@ export class MeshTxBuilder extends MeshTxBuilderCore {

this.meshTxBuilderBody.fee = selectionSkeleton.fee.toString();
this.queueAllLastItem();

const changeLen = selectionSkeleton.change.length;
if (this.meshTxBuilderBody.changeAddressOutputIndex !== undefined && changeLen > 0) {
let idx = this.meshTxBuilderBody.changeAddressOutputIndex;
const changes = this.meshTxBuilderBody.outputs.splice(-changeLen, changeLen);

const maxIdx = this.meshTxBuilderBody.outputs.length;
if (idx > maxIdx) {
idx = maxIdx;
} else if (idx < 0) {
idx = 0;
}

this.meshTxBuilderBody.outputs.splice(idx, 0, ...changes);
}

this.removeDuplicateInputs();
this.sortTxParts();
this.updateRedeemer(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1519,10 +1519,14 @@ export class MeshTxBuilderCore {
/**
* Configure the address to accept change UTxO
* @param addr The address to accept change UTxO
* @param outputIndex The index at which the change output should be placed
* @returns The MeshTxBuilder instance
*/
changeAddress = (addr: string) => {
changeAddress = (addr: string, outputIndex?: number) => {
this.meshTxBuilderBody.changeAddress = addr;
if (outputIndex !== undefined) {
this.meshTxBuilderBody.changeAddressOutputIndex = outputIndex;
}
return this;
};

Expand Down
39 changes: 39 additions & 0 deletions packages/mesh-transaction/test/mesh-tx-builder/tx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,45 @@ describe("MeshTxBuilder transactions", () => {
expect(txHex !== "").toBeTruthy();
});

it("Basic send tx with change output index", async () => {
let mesh = new MeshTxBuilder();
let txHex = await mesh
.txIn(
"2cb57168ee66b68bd04a0d595060b546edf30c04ae1031b883c9ac797967dd85",
3,
[{ unit: "lovelace", quantity: "9891607895" }],
"addr_test1vru4e2un2tq50q4rv6qzk7t8w34gjdtw3y2uzuqxzj0ldrqqactxh",
0
)
.txOut(
"addr_test1vru4e2un2tq50q4rv6qzk7t8w34gjdtw3y2uzuqxzj0ldrqqactxh",
[{ unit: "lovelace", quantity: "2000000" }],
)
.txOut(
"addr_test1vru4e2un2tq50q4rv6qzk7t8w34gjdtw3y2uzuqxzj0ldrqqactxh",
[{ unit: "lovelace", quantity: "3000000" }],
)
.changeAddress(
"addr_test1wql6cyymfrmqe9cjeyfh5d4h945nfszy3yup8d74kkrhsks4dkk0y",
1
)
.complete();

const cardanoTx = Serialization.Transaction.fromCbor(
Serialization.TxCBOR(txHex),
);
const outputs = cardanoTx.body().outputs();
// 3 outputs expected: 2 explicit, 1 change
expect(outputs.length).toBe(3);

// The change output should be at index 1 (between the two explicit outputs)
const changeOutput = outputs[1];
expect(changeOutput!.address().toBech32()).toBe("addr_test1wql6cyymfrmqe9cjeyfh5d4h945nfszy3yup8d74kkrhsks4dkk0y");
// Verify explicit outputs are pushed to indices 0 and 2
expect(outputs[0]!.amount().coin().toString()).toBe("2000000");
expect(outputs[2]!.amount().coin().toString()).toBe("3000000");
});

it("Basic send tx with set fee", () => {
let mesh = new MeshTxBuilder();
let txHex = mesh
Expand Down
Loading