# Merge

With the merge function, it is possible to combine two STAS UTXO inputs into one output. However, please keep in mind that this function can only be used by tokens that have the splittable property set to true. For more information on which token templates have this property, please see [Token Properties](/library/token-properties.md).

{% hint style="warning" %}
Merging can only occur for STAS UTXOs that contain identical script values with exception of the owner address in the script.
{% endhint %}

The merge function has different parameters compared to the split or transfer functions. It necessitates the previous transaction hex value of the UTXOs being merged, and the required format is as follows:

```javascript
const stasInput1 = {
    txHex : 'previous transaction hex string',
    vout : 'output index of the UTXO being spent'
}

const stasInput2 = {
    txHex : 'previous transaction hex string',
    vout : 'output index of the UTXO being spent'
}
```

The previous transaction hex is required as part of the unlocking script to complete the merge functionality. To create a merge transaction, the following additional parameters are necessary: private keys for both UTXO inputs, a destination address, and the payment UTXO and its corresponding private key.

```javascript
const mergeHex = await stasMerge.signed(
    ownerPrivateKey1,
    stasInput1,
    ownerPrivateKey2,
    stasInput2,
    destinationAddr,
    paymentPrivateKey,
    paymentUtxo
)

```

{% hint style="success" %}
After executing the function, you will receive a transaction hexadecimal representation that is now ready to be broadcasted to the miner.
{% endhint %}

{% hint style="warning" %}
NOTE: It is important to keep in mind that the size of the merge transaction will increase after each subsequent merge transaction due to its design nature. To optimize the transaction size, it is recommended to consider ways to mitigate the compounding effects of the merge transactions. One possible solution is to use interval transfer functions for each UTXO being merged, which resets the previous transaction hexadecimal to the size of a transfer transaction. It is recommended to transfer the UTXO after every two merge transactions as a means of resetting the transaction size before continuing with additional merge transactions.
{% endhint %}

{% hint style="info" %}
The MergeSplit function works in a similar way to merge, with the exception that instead of a single destination address, it accepts an array of splitDestinations, just like in the split function.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.stastoken.com/library/merge.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
