# Withdrawals 2.0

Auto-withdrawals 2.0 allow you to make withdrawals from **payout** and **collect** addresses.

Opportunities:

* direct withdrawal
* withdrawal between networks
* withdrawal with swap coins

## API Interaction Scheme

{% @mermaid/diagram content="sequenceDiagram
Merchant ->> Apollopayment: Creating withdrawal
Apollopayment ->> Merchant: Withdrawal body

```
Note over Apollopayment: Processing operation

Apollopayment -->> Merchant: Withdrawal result webhook" %}
```

### Address selection

The most appropriate address is selected for the operation

Example of address selection:

You have several addresses

| Coin | Network  | Balance |
| ---- | -------- | ------- |
| USDT | tron     | 100     |
| USDT | ethereum | 100     |
| BNB  | bsc      | 100     |

* You want to withdraw **10 USDT tron**
  * Direct withdrawal. The **USDT tron** address will be taken since you already have an address with this coin on this network, and there is enough money on it
* You want to withdraw **10 BNB bsc**
  * Direct withdrawal. The **BNB bsc address will be taken** since you already have an address with this coin on this network, and there is enough money on it
* You want to withdraw **10 USDT bsc**
  * Withdrawal between networks. The **USDT tron address will be taken** since you don't have a suitable address
* You want to withdraw **1 BTC bitcoin**
  * Withdrawal with swap coins. The **USDT tron address will be taken** since you don't have a suitable address

> Please note
>
> Operations *withdrawal between networks* and *withdrawal with swap coins* work only with token addresses (USDT, USDC)\
> Addresses of native coins will be taken only for *direct withdrawal*

The address is taken from all **PAY\_OUT** (payouts) and **COLLECT** (collecting) addresses.\
We are looking for an address with a balance covering the requested amount and with the lowest network fee.\
Priority of operations: direct withdrawal, withdrawal between networks, withdrawal with swap coins.

### Commissions

During the operation, the tariff will be charged depending on the type of operation

| Type of operation           | Tariff                  |
| --------------------------- | ----------------------- |
| Direct withdrawal           | Withdrawal from pay-out |
| Withdrawal between networks | Blockchain bridge API   |
| Withdrawal with swap coins  | Swap API                |

> Operations **withdrawal between networks**, **withdrawal with swap coins** are carried out through\
> service provider, the network commission for sending coins to the provider is compensated by the service
>
> The service fee for the operation is always taken from the advance balance
>
> The network fee for the withdrawal from the provider is charged **from the amount if the outgoing native coins**,**from the advance balance if the outgoing coin is a token**

### The `feeInAmount` parameter

This parameter allows you to specify that the network fee for sending coins from the provider to the destination address\
it will be taken from the amount (the user will receive an amount less than the specified amount of the network fee)

> If the outgoing coin is native, and the parameter `feeInAmount=false` is specified, then the network commission will be\
> added to the amount so that the user receives the specified amount

### Description of fields

In response to the request, you will receive a body with the following fields

| Name                    | Type                                                     | Description                                                                                     |
| ----------------------- | -------------------------------------------------------- | ----------------------------------------------------------------------------------------------- |
| `id`                    | `string`                                                 | Operation ID                                                                                    |
| `organizationId`        | `string`                                                 | Organization ID                                                                                 |
| `type`                  | `enum(WITHDRAWAL, BRIDGE, SWAP)`                         | Type                                                                                            |
| `status`                | `enum(PENDING, WITHDRAWING, PROCESSED, REJECTED, ERROR)` | Status                                                                                          |
| `message`               | `string or null`                                         | Rejection message                                                                               |
| `addressRiskLevel`      | `enum(Low, Medium, High, Severe) or null`                | The risk level of the end address                                                               |
| `addressFromId`         | `string`                                                 | ID of the outgoing address                                                                      |
| `addressFrom`           | `string`                                                 | Outgoing address                                                                                |
| `addressTo`             | `string`                                                 | Destination address                                                                             |
| `amountFrom`            | `string`                                                 | Outgoing amount                                                                                 |
| `amountFromUSD`         | `string`                                                 | Outgoing amount in USD                                                                          |
| `amountTo`              | `string`                                                 | Amount after the operation                                                                      |
| `amountToUSD`           | `string`                                                 | Amount after the operation in USD                                                               |
| `amountToReceive`       | `string`                                                 | The amount that will be sent to the destination address                                         |
| `amountToReceiveUSD`    | `string`                                                 | The amount that will be sent to the destination address in USD                                  |
| `rate`                  | `string`                                                 | Swap rate                                                                                       |
| `blockchainFeeFrom`     | `string`                                                 | Network fee for sending to the provider                                                         |
| `blockchainFeeFromUSD`  | `string`                                                 | Network fee for sending to the provider in USD                                                  |
| `blockchainFeeToSource` | `enum(ADVANCED, AMOUNT)`                                 | The source of the network's fee charge for sending from the provider to the destination address |
| `blockchainFeeTo`       | `string`                                                 | Network fee for sending from the provider to the destination address                            |
| `blockchainFeeToUSD`    | `string`                                                 | Network fee for sending from the provider to the destination address in USD                     |
| `serviceFee`            | `string`                                                 | Service fee for the operation                                                                   |
| `webhookUrl`            | `string or null`                                         | URL for sending the webhook                                                                     |
| `txId`                  | `string or null`                                         | Hash of the transaction of sending coins to the destination address                             |
| `createdAt`             | `string (Date in ISO 8601)`                              | Date of creation                                                                                |
| `updatedAt`             | `string (Date in ISO 8601)`                              | Date of last update                                                                             |

***

The `type` field

| Name         | Description                 |
| ------------ | --------------------------- |
| `WITHDRAWAL` | Withdrawal                  |
| `BRIDGE`     | Withdrawal between networks |
| `SWAP`       | Withdrawal with swap coin   |

***

The `status` field

| Name          | Description                                      |
| ------------- | ------------------------------------------------ |
| `PENDING`     | In processing                                    |
| `WITHDRAWING` | In process of sending to the destination address |
| `PROCESSED`   | Completed successfully                           |
| `REJECTED`    | Rejected by the system                           |
| `ERROR`       | Processing error                                 |

***

The `blockchainFeeToSource` field

| Name       | Description     |
| ---------- | --------------- |
| `ADVANCED` | Advance balance |
| `AMOUNT`   | Amount          |

***

Example of the response body

```json
{
  "success": true,
  "response": {
    "id": "31a3b86b-e350-4906-9d3e-cc2fca054821",
    "organizationId": "1f07eb01-5fd8-4e05-89b5-bebcd1d1fc39",
    "userId": null,
    "type": "WITHDRAWAL",
    "status": "PENDING",
    "message": null,
    "addressRiskLevel": "Low",
    "addressFromId": "25a8de42-a359-47f1-bb82-bc9f6c20f1b9",
    "addressFrom": "0xD65D24ABCd85165a243C33Cf8133ffBaaa98255D",
    "addressTo": "0x22aECc7ff5b435E38be5457C8538256918783F67",
    "amountFrom": "3",
    "amountFromUSD": "3.00",
    "amountTo": "3",
    "amountToUSD": "3.00",
    "amountToReceive": "3",
    "amountToReceiveUSD": "3.00",
    "rate": "0",
    "blockchainFeeFrom": "0",
    "blockchainFeeFromUSD": "0",
    "blockchainFeeToSource": "ADVANCED",
    "blockchainFeeTo": "0.12",
    "blockchainFeeToUSD": "0.12",
    "serviceFee": "0.09",
    "webhookUrl": "https://example.com/webhook-url",
    "txId": null,
    "createdAt": "2024-09-09T15:43:32.986Z",
    "updatedAt": "2024-09-09T15:43:34.073Z"
  }
}
```

### Webhook

When the operation status is changed, a webhook will be sent to the specified URL

Example of a webhook body

```json
{
  "id": "31a3b86b-e350-4906-9d3e-cc2fca054821",
  "organizationId": "1f07eb01-5fd8-4e05-89b5-bebcd1d1fc39",
  "userId": null,
  "type": "WITHDRAWAL",
  "status": "PENDING",
  "message": null,
  "addressRiskLevel": "Low",
  "addressFromId": "25a8de42-a359-47f1-bb82-bc9f6c20f1b9",
  "addressFrom": "0xD65D24ABCd85165a243C33Cf8133ffBaaa98255D",
  "addressTo": "0x22aECc7ff5b435E38be5457C8538256918783F67",
  "amountFrom": "3",
  "amountFromUSD": "3.00",
  "amountTo": "3",
  "amountToUSD": "3.00",
  "amountToReceive": "3",
  "amountToReceiveUSD": "3.00",
  "rate": "0",
  "blockchainFeeFrom": "0",
  "blockchainFeeFromUSD": "0",
  "blockchainFeeToSource": "ADVANCED",
  "blockchainFeeTo": "0.12",
  "blockchainFeeToUSD": "0.12",
  "serviceFee": "0.09",
  "webhookUrl": "https://example.com/webhook-url",
  "txId": null,
  "createdAt": "2024-09-09T15:43:32.986Z",
  "updatedAt": "2024-09-09T15:43:34.073Z"
}
```
