> For the complete documentation index, see [llms.txt](https://mercuryo.gitbook.io/mobilesdk-en/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mercuryo.gitbook.io/mobilesdk-en/operations.md).

# Operations (buy/sell/withdraw crypto)

### Available operations

* [Buying process](/mobilesdk-en/operations.md#buying-crypto)
  * [Getting token for buying](/mobilesdk-en/operations.md#getting-token-for-buying)
  * [Buying](/mobilesdk-en/operations.md#buying)
  * [Confirmation of operation](/mobilesdk-en/operations.md#confirmation-of-operation)
  * [Getting rate for buying](/mobilesdk-en/operations.md#getting-rate-for-buying)
* [Selling process](/mobilesdk-en/operations.md#selling-process)
  * [Getting token for selling](/mobilesdk-en/operations.md#getting-token-for-selling)
  * [Selling](/mobilesdk-en/operations.md#selling)
  * [Getting rate for buying](/mobilesdk-en/operations.md#getting-exchange-rate-for-selling)
* [Withdrawal to another wallet](/mobilesdk-en/operations.md#withdrawal-to-another-wallet)
  * [Currency converter](/mobilesdk-en/operations.md#currency-converter)
  * [Withdrawal fees](/mobilesdk-en/operations.md#withdrawal-fees)
  * [Make a withdrawal](/mobilesdk-en/operations.md#make-a-withdrawal)
  * [Withdrawal confirmation](/mobilesdk-en/operations.md#withdrawal-confirmation)

## Buying crypto

`Buy` object needs to be obtained first to start the process of buying:

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
val mercuryo: Mercuryo = Mercuryo.create(...)  
val buy: Buy = mercuryo.operations.buy
```

{% endtab %}

{% tab title="Swift" %}

```swift
var mercuryoSDK: IMercuryo = IMercuryo(isDebug: true, baseHost: "HOST_HERE")
var wallet = mercuryoSDK.operations.buy
```

{% endtab %}
{% endtabs %}

### Getting token for buying

Before buying, you need to get a unique token designated to this specific operation, for this you can use the `.convert` method:

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
buy.convert(fromCurrency: String, toCurrency: String, amount: String) : ConverterResult
```

{% endtab %}

{% tab title="Swift" %}

```swift
var token: String?

buy.convert(fromCurrency: "BTC", toCurrency: "EUR", amount: "1") { converterResult, error in 
    token = converterResult?.token
    //do some magic here
}
```

{% endtab %}
{% endtabs %}

### Buying

After you obtained a [buying token](/mobilesdk-en/operations.md#getting-token-for-buying) you can call buy method passing parameters of operation.

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
buy.commit(cardId: String, cvv: String, buyToken: String, redirectUrl: String): TransactionStatus
```

{% endtab %}

{% tab title="Swift" %}

```swift
var transactionID: String?

buy.commit(cardId: "CARD_ID HERE", cvv: "CARD_CVV HERE", buyToken: token, redirectUrl: "REDIRECT_URL HERE") { transactionStatus, error in 
    transactionID = transactionStatus?.id
    //do some magic here
}
```

{% endtab %}
{% endtabs %}

| Parameter   | Description                                                                                                                                                             |
| ----------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| cardId      | ID of card in Mercuryo                                                                                                                                                  |
| cvv         | CVV code of card                                                                                                                                                        |
| buyToken    | Buying token [`buy.convert(...)`](/mobilesdk-en/operations.md#getting-token-for-buying)                                                                                 |
| redirectUrl | URL where user will be redirected after operation. Example: [http://my.mercuryo.io/orders?invoice={invoice\_id}](http://my.mercuryo.io/orders?invoice=%7Binvoice_id%7D) |

### Confirmation of operation

To finalize an operation user may need to confirm ownership of bank account by providing a descriptor sent as a part of transaction.

{% tabs %}
{% tab title="Kotlin" %}

```
buy.verifyDescriptor(transactionId: String, code: String): TransactionStatus
```

{% endtab %}

{% tab title="Swift" %}

```swift
buy.verifyDescriptor(transactionId: transactionID, code: "TRANSACTION_CODE HERE") { transactionStatus, error in 
    //do some magic here
}
```

{% endtab %}
{% endtabs %}

| Parameter     | Description                                                                                                |
| ------------- | ---------------------------------------------------------------------------------------------------------- |
| transactionId | ID of transaction obtained after [`buy.commit(...)`](/mobilesdk-en/operations.md#getting-token-for-buying) |
| code          | Descriptor code                                                                                            |

### Getting rate for buying

User may want to know the rate of fiat-crypto conversion which may be obtained using the following method:

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
buy.rate(fromCurrency: String, toCurrency: String): Rate
```

{% endtab %}

{% tab title="Swift" %}

```swift
buy.getRate(fromCurrency: "BTC", toCurrency: "EUR") { rate, error in 
    //do soma magic here
}
```

{% endtab %}
{% endtabs %}

## Selling process

`Sell` object needs to be obtained first to start the process of buying:

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
val mercuryo: Mercuryo = Mercuryo.create(...)  
val sell: Sell = mercuryo.operations.sell
```

{% endtab %}

{% tab title="Swift" %}

```swift
var mercuryoSDK: IMercuryo = IMercuryo(isDebug: true, baseHost: "HOST_HERE")
var wallet = mercuryoSDK.operations.sell
```

{% endtab %}
{% endtabs %}

### Getting token for selling

Before selling, you need to get a unique token designated to this specific operation, for this you can use the `.convert` method:

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
sell.convert(fromCurrency: String, toCurrency: String, amount: String) : ConverterResult
```

{% endtab %}

{% tab title="Swift" %}

```swift
var token: String?

sell.convert(fromCurrency: "BTC", toCurrency: "EUR", amount: "1") { converterResult, error in 
    token = converterResult?.token
    //do some magic here
}
```

{% endtab %}
{% endtabs %}

### Selling

To sell you need to [get token for selling](/mobilesdk-en/operations.md#getting-token-for-selling) first call method as shown below.

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
sell.commit(cardId: String, sellToken: String): TransactionStatus
```

{% endtab %}

{% tab title="Swift" %}

```swift
sell.commit(cardId: "CARD_ID HERE", sellToken: token) { transactionStatus, error in 
    //do some magic here
}
```

{% endtab %}
{% endtabs %}

| Parameter | Description                                                                                      |
| --------- | ------------------------------------------------------------------------------------------------ |
| cardId    | ID of card in Mercuryo                                                                           |
| sellToken | Token obtained from [`sell.convert(...)`](/mobilesdk-en/operations.md#getting-token-for-selling) |

### Getting exchange rate for selling

Rate includes a fee.

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
sell.rate(fromCurrency: String, toCurrency: String): Rate
```

{% endtab %}

{% tab title="Swift" %}

```swift
sell.getRate(fromCurrency: "BTC", toCurrency: "EUR") { rate, error in 
    //do soma magic here
}
```

{% endtab %}
{% endtabs %}

## Withdrawal to another wallet

`Withdraw` object needs to be obtained first to start the process of buying:

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
val mercuryo: Mercuryo = Mercuryo.create(...)  
val withdraw: Withdraw = mercuryo.operations.withdraw
```

{% endtab %}

{% tab title="Swift" %}

```swift
var mercuryoSDK: IMercuryo = IMercuryo(isDebug: true, baseHost: "HOST_HERE")
var wallet = mercuryoSDK.operations.buy
```

{% endtab %}
{% endtabs %}

### Currency converter

Before sending crypto it may be beneficial for user to know how amount converts to fiat or vice versa. Mercuryo SDK provides a method for that allowing to understand transaction better.

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
withdraw.convert(fromCurrency: String, toCurrency: String, amount: String) : ConverterResult
```

{% endtab %}

{% tab title="Swift" %}

```swift
var token: String?

withdraw.convert(fromCurrency: "BTC", toCurrency: "EUR", amount: "1") { converterResult, error in 
    token = converterResult?.token
    //do some magic here
}
```

{% endtab %}
{% endtabs %}

### Withdrawal fees

To make withdrawal you need to obtain available blockchain fees first. In case there are no fees this method will throw an `CurrencyNotSupportFeeException` exception.

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
withdraw.getEstimateFee(currency: String, fiatCurrency: String, address: String, amount: String): EstimateWithdrawFee
```

{% endtab %}

{% tab title="Swift" %}

```swift
var firstEstimateId: String?
withdraw.getEstimateFee(currency: "BTC", fiatCurrency: "EUR", address: "ADDRESS HERE", amount: "1") { fee, error in 
    firstEstimateId = fee?.fees.first?.id
    //do soma magic here
}
```

{% endtab %}
{% endtabs %}

### Make a withdrawal

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
withdraw.commit(currency: String, address: String, amount: String, estimateId: String?): VerifyMetaData
```

{% endtab %}

{% tab title="Swift" %}

```swift
var nextStep: VerifyMetaData.NextStep?
var key: String?

withdraw.commit(currency: "BTC", address: "ADDRESS HERE", amount: "1", estimateId: firstEstimateId) { verifyData, error in 
    nextStep = verifyData?.step
    key = verifyData?.key
    //do some magic here
}
```

{% endtab %}
{% endtabs %}

| Fields     | Description                                                                                                                             |
| ---------- | --------------------------------------------------------------------------------------------------------------------------------------- |
| currency   | Crypto                                                                                                                                  |
| address    | Wallet address                                                                                                                          |
| amount     | Amount of withdrawal in crypto                                                                                                          |
| estimateId | ID of estimation fee obtained on the previous step, see method [`withdraw.getEstimateFee`](/mobilesdk-en/operations.md#withdrawal-fees) |

### Withdrawal confirmation

User may need to confirm withdrawal using text message or email code.

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
withdraw.verify(next: VerifyMetaData.NextStep, key: String, code: String): TransactionStatus
```

{% endtab %}

{% tab title="Swift" %}

```swift
withdraw.verify(next: nextStep, key: key, code: "SMS/EMAIL CODE") { transactionStatus, error in 
    //do some magic here
}
```

{% endtab %}
{% endtabs %}
