# unsafe_mergeCoins

> For the complete documentation index, see [llms.txt](/docs/llms.txt).

POST https://sui-mainnet.g.alchemy.com/v2/{apiKey}

Constructs a BCS-encoded transaction block that merges the balance of `coin_to_merge` into `primary_coin`. This transaction must be signed and then submitted using a separate execute call. The gas object must be owned by the signer.


Reference: https://www.alchemy.com/docs/chains/sui/sui-api-endpoints/unsafe-merge-coins

## Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| signer | string | Yes | The transaction signer's Sui address. |
| primary_coin | string | Yes | The coin object to merge into. This coin will remain after the transaction. |
| coin_to_merge | string | Yes | The coin object to be merged. It will be destroyed and its balance added to `primary_coin`. |
| gas | string | No | The gas object to be used. If omitted, the node will auto-select one from the signer's owned coins. |
| gas_budget | string | Yes | The gas budget for the transaction. The transaction fails if gas used exceeds this. |

## Result

**result** (object): The unsigned transaction block that merges coins.

## Example

### Request

```json
{
  "jsonrpc": "2.0",
  "method": "unsafe_mergeCoins",
  "params": [
    "0x4c05f4f76ed81d210e9a29ac0756c7a3129e4b9ecacbbb9fc1579505947630cf",
    "0x5b14e6b640d6254959803aafff56b3d42c559760f38c3f98a91f557233b8ebc9",
    "0x47011883c7c75aaeb5beec48ade6eebb428bb8a89a340936e9b24fcbacdc67ea",
    "0x8aPfVmE7k1h3BFju4WFhGJ59oWpMFSiRqsCa1EsyJxtS",
    "3000"
  ],
  "id": 1
}
```

### Response

```json
{
  "jsonrpc": "2.0",
  "result": {
    "gas": [
      {
        "objectId": "0x8aPfVmE7k1h3BFju4WFhGJ59oWpMFSiRqsCa1EsyJxtS",
        "version": "1",
        "digest": "BhbWpBeESxuRWvmvLMyb2JNUuFa6j4aG1T4WUiPgKAHm"
      }
    ],
    "inputObjects": [
      {
        "objectId": "0x5b14e6b640d6254959803aafff56b3d42c559760f38c3f98a91f557233b8ebc9",
        "version": "1",
        "digest": "BhbWpBeESxuRWvmvLMyb2JNUuFa6j4aG1T4WUiPgKAHm",
        "objectType": "Coin"
      },
      {
        "objectId": "0x47011883c7c75aaeb5beec48ade6eebb428bb8a89a340936e9b24fcbacdc67ea",
        "version": "1",
        "digest": "BhbWpBeESxuRWvmvLMyb2JNUuFa6j4aG1T4WUiPgKAHm",
        "objectType": "Coin"
      }
    ],
    "txBytes": "BASE64_ENCODED_BCS_BYTES_HERE"
  },
  "id": 1
}
```

## Code Examples

### cURL

```bash
curl --request POST \
  --url https://sui-mainnet.g.alchemy.com/v2/docs-demo \
  --header 'Content-Type: application/json' \
  --data '{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "unsafe_mergeCoins",
  "params": [
    "0x4c05f4f76ed81d210e9a29ac0756c7a3129e4b9ecacbbb9fc1579505947630cf",
    "0x5b14e6b640d6254959803aafff56b3d42c559760f38c3f98a91f557233b8ebc9",
    "0x47011883c7c75aaeb5beec48ade6eebb428bb8a89a340936e9b24fcbacdc67ea",
    "0x8aPfVmE7k1h3BFju4WFhGJ59oWpMFSiRqsCa1EsyJxtS",
    "3000"
  ]
}'
```

### JavaScript

```javascript
const options = {
  method: 'POST',
  headers: {'Content-Type': 'application/json'},
  body: JSON.stringify({
    jsonrpc: '2.0',
    id: 1,
    method: 'unsafe_mergeCoins',
    params: [
      '0x4c05f4f76ed81d210e9a29ac0756c7a3129e4b9ecacbbb9fc1579505947630cf',
      '0x5b14e6b640d6254959803aafff56b3d42c559760f38c3f98a91f557233b8ebc9',
      '0x47011883c7c75aaeb5beec48ade6eebb428bb8a89a340936e9b24fcbacdc67ea',
      '0x8aPfVmE7k1h3BFju4WFhGJ59oWpMFSiRqsCa1EsyJxtS',
      '3000'
    ]
  })
};

fetch('https://sui-mainnet.g.alchemy.com/v2/docs-demo', options)
  .then(res => res.json())
  .then(res => console.log(res))
  .catch(err => console.error(err));
```

### Python

```python
import requests

url = "https://sui-mainnet.g.alchemy.com/v2/docs-demo"

payload = {
    "jsonrpc": "2.0",
    "id": 1,
    "method": "unsafe_mergeCoins",
    "params": ["0x4c05f4f76ed81d210e9a29ac0756c7a3129e4b9ecacbbb9fc1579505947630cf", "0x5b14e6b640d6254959803aafff56b3d42c559760f38c3f98a91f557233b8ebc9", "0x47011883c7c75aaeb5beec48ade6eebb428bb8a89a340936e9b24fcbacdc67ea", "0x8aPfVmE7k1h3BFju4WFhGJ59oWpMFSiRqsCa1EsyJxtS", "3000"]
}
headers = {"Content-Type": "application/json"}

response = requests.post(url, json=payload, headers=headers)

print(response.text)
```

### Go

```go
package main

import (
	"fmt"
	"strings"
	"net/http"
	"io"
)

func main() {

	url := "https://sui-mainnet.g.alchemy.com/v2/docs-demo"

	payload := strings.NewReader("{\n  \"jsonrpc\": \"2.0\",\n  \"id\": 1,\n  \"method\": \"unsafe_mergeCoins\",\n  \"params\": [\n    \"0x4c05f4f76ed81d210e9a29ac0756c7a3129e4b9ecacbbb9fc1579505947630cf\",\n    \"0x5b14e6b640d6254959803aafff56b3d42c559760f38c3f98a91f557233b8ebc9\",\n    \"0x47011883c7c75aaeb5beec48ade6eebb428bb8a89a340936e9b24fcbacdc67ea\",\n    \"0x8aPfVmE7k1h3BFju4WFhGJ59oWpMFSiRqsCa1EsyJxtS\",\n    \"3000\"\n  ]\n}")

	req, _ := http.NewRequest("POST", url, payload)

	req.Header.Add("Content-Type", "application/json")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := io.ReadAll(res.Body)

	fmt.Println(string(body))

}
```

### Java

```java
HttpResponse<String> response = Unirest.post("https://sui-mainnet.g.alchemy.com/v2/docs-demo")
  .header("Content-Type", "application/json")
  .body("{\n  \"jsonrpc\": \"2.0\",\n  \"id\": 1,\n  \"method\": \"unsafe_mergeCoins\",\n  \"params\": [\n    \"0x4c05f4f76ed81d210e9a29ac0756c7a3129e4b9ecacbbb9fc1579505947630cf\",\n    \"0x5b14e6b640d6254959803aafff56b3d42c559760f38c3f98a91f557233b8ebc9\",\n    \"0x47011883c7c75aaeb5beec48ade6eebb428bb8a89a340936e9b24fcbacdc67ea\",\n    \"0x8aPfVmE7k1h3BFju4WFhGJ59oWpMFSiRqsCa1EsyJxtS\",\n    \"3000\"\n  ]\n}")
  .asString();
```

### C#

```csharp
using RestSharp;


var options = new RestClientOptions("https://sui-mainnet.g.alchemy.com/v2/docs-demo");
var client = new RestClient(options);
var request = new RestRequest("");
request.AddJsonBody("{\n  \"jsonrpc\": \"2.0\",\n  \"id\": 1,\n  \"method\": \"unsafe_mergeCoins\",\n  \"params\": [\n    \"0x4c05f4f76ed81d210e9a29ac0756c7a3129e4b9ecacbbb9fc1579505947630cf\",\n    \"0x5b14e6b640d6254959803aafff56b3d42c559760f38c3f98a91f557233b8ebc9\",\n    \"0x47011883c7c75aaeb5beec48ade6eebb428bb8a89a340936e9b24fcbacdc67ea\",\n    \"0x8aPfVmE7k1h3BFju4WFhGJ59oWpMFSiRqsCa1EsyJxtS\",\n    \"3000\"\n  ]\n}", false);
var response = await client.PostAsync(request);

Console.WriteLine("{0}", response.Content);

```


## OpenRPC Method Specification

```yaml
name: unsafe_mergeCoins
summary: Create an unsigned transaction to merge multiple coins into one coin
description: |
  Constructs a BCS-encoded transaction block that merges the balance of `coin_to_merge` into `primary_coin`. This transaction must be signed and then submitted using a separate execute call. The gas object must be owned by the signer.
params:
  - name: signer
    required: true
    description: The transaction signer's Sui address.
    schema:
      type: string
  - name: primary_coin
    required: true
    description: The coin object to merge into. This coin will remain after the transaction.
    schema:
      type: string
  - name: coin_to_merge
    required: true
    description: The coin object to be merged. It will be destroyed and its balance added to `primary_coin`.
    schema:
      type: string
  - name: gas
    required: false
    description: The gas object to be used. If omitted, the node will auto-select one from the signer's owned coins.
    schema:
      type: string
  - name: gas_budget
    required: true
    description: The gas budget for the transaction. The transaction fails if gas used exceeds this.
    schema:
      type: string
result:
  name: result
  description: The unsigned transaction block that merges coins.
  schema:
    type: object
    properties:
      gas:
        type: array
        description: The gas object(s) selected for this transaction.
        items:
          type: object
          properties:
            objectId:
              type: string
            version:
              type: string
            digest:
              type: string
      inputObjects:
        type: array
        description: List of objects the transaction references.
        items:
          type: object
          properties:
            objectId:
              type: string
            version:
              type: string
            digest:
              type: string
            objectType:
              type: string
      txBytes:
        type: string
        description: Base64-encoded BCS transaction data, ready for signing.
examples:
  - name: Merge coins
    params:
      - name: signer
        value: '0x4c05f4f76ed81d210e9a29ac0756c7a3129e4b9ecacbbb9fc1579505947630cf'
      - name: primary_coin
        value: '0x5b14e6b640d6254959803aafff56b3d42c559760f38c3f98a91f557233b8ebc9'
      - name: coin_to_merge
        value: '0x47011883c7c75aaeb5beec48ade6eebb428bb8a89a340936e9b24fcbacdc67ea'
      - name: gas
        value: 0x8aPfVmE7k1h3BFju4WFhGJ59oWpMFSiRqsCa1EsyJxtS
      - name: gas_budget
        value: '3000'
    result:
      name: result
      value:
        gas:
          - objectId: 0x8aPfVmE7k1h3BFju4WFhGJ59oWpMFSiRqsCa1EsyJxtS
            version: '1'
            digest: BhbWpBeESxuRWvmvLMyb2JNUuFa6j4aG1T4WUiPgKAHm
        inputObjects:
          - objectId: '0x5b14e6b640d6254959803aafff56b3d42c559760f38c3f98a91f557233b8ebc9'
            version: '1'
            digest: BhbWpBeESxuRWvmvLMyb2JNUuFa6j4aG1T4WUiPgKAHm
            objectType: Coin
          - objectId: '0x47011883c7c75aaeb5beec48ade6eebb428bb8a89a340936e9b24fcbacdc67ea'
            version: '1'
            digest: BhbWpBeESxuRWvmvLMyb2JNUuFa6j4aG1T4WUiPgKAHm
            objectType: Coin
        txBytes: BASE64_ENCODED_BCS_BYTES_HERE
```
