summaryrefslogtreecommitdiff
path: root/bank/src/main/kotlin/tech/libeufin/bank/ConversionApi.kt
blob: 168b20348a312b724212d1273e06b6c9f4844020 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
 * This file is part of LibEuFin.
 * Copyright (C) 2023 Taler Systems S.A.

 * LibEuFin is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation; either version 3, or
 * (at your option) any later version.

 * LibEuFin is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General
 * Public License for more details.

 * You should have received a copy of the GNU Affero General Public
 * License along with LibEuFin; see the file COPYING.  If not, see
 * <http://www.gnu.org/licenses/>
 */
package tech.libeufin.bank

import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import tech.libeufin.bank.auth.authAdmin
import tech.libeufin.bank.db.ConversionDAO
import tech.libeufin.bank.db.ConversionDAO.ConversionResult
import tech.libeufin.bank.db.Database
import tech.libeufin.common.TalerAmount
import tech.libeufin.common.TalerErrorCode

fun Routing.conversionApi(db: Database, ctx: BankConfig) = conditional(ctx.allowConversion) {
    get("/conversion-info/config") {
        val config = db.conversion.getConfig(ctx.regionalCurrency, ctx.fiatCurrency!!)
        if (config == null) {
            throw libeufinError(
                HttpStatusCode.NotImplemented, 
                "conversion rate not configured yet", 
                TalerErrorCode.END
            )
        }
        call.respond(
            ConversionConfig(
                regional_currency = ctx.regionalCurrency,
                regional_currency_specification = ctx.regionalCurrencySpec,
                fiat_currency = ctx.fiatCurrency,
                fiat_currency_specification = ctx.fiatCurrencySpec!!,
                conversion_rate = config
            )
        )
    }
    suspend fun ApplicationCall.convert(
        input: TalerAmount, 
        conversion: suspend ConversionDAO.(TalerAmount) -> ConversionResult,
        output: (TalerAmount) -> ConversionResponse
    ) {
        when (val res = db.conversion.(conversion)(input)) {
            is ConversionResult.Success -> respond(output(res.converted))
            is ConversionResult.ToSmall -> throw conflict(
                "$input is too small to be converted",
                TalerErrorCode.BANK_BAD_CONVERSION
            )
            is ConversionResult.MissingConfig -> throw libeufinError(
                HttpStatusCode.NotImplemented, 
                "conversion rate not configured yet", 
                TalerErrorCode.END
            )
        }
    }
    get("/conversion-info/cashout-rate") {
        val params = RateParams.extract(call.request.queryParameters)

        params.debit?.let { ctx.checkRegionalCurrency(it) }
        params.credit?.let { ctx.checkFiatCurrency(it) }

        if (params.debit != null) {
            call.convert(params.debit, ConversionDAO::toCashout) {
                ConversionResponse(params.debit, it)
            }
        } else {
            call.convert(params.credit!!, ConversionDAO::fromCashout) {
                ConversionResponse(it, params.credit)
            }
        }
    }
    get("/conversion-info/cashin-rate") {
        val params = RateParams.extract(call.request.queryParameters)

        params.debit?.let { ctx.checkFiatCurrency(it) }
        params.credit?.let { ctx.checkRegionalCurrency(it) }

        if (params.debit != null) {
            call.convert(params.debit, ConversionDAO::toCashin) {
                ConversionResponse(params.debit, it)
            }
        } else {
            call.convert(params.credit!!, ConversionDAO::fromCashin) {
                ConversionResponse(it, params.credit)
            }
        }
    }
    authAdmin(db, TokenScope.readwrite) {
        post("/conversion-info/conversion-rate") {
            val req = call.receive<ConversionRate>()
            for (regionalAmount in sequenceOf(req.cashin_fee, req.cashin_tiny_amount, req.cashout_min_amount)) {
                ctx.checkRegionalCurrency(regionalAmount)
            }
            for (fiatAmount in sequenceOf(req.cashout_fee, req.cashout_tiny_amount, req.cashin_min_amount)) {
                ctx.checkFiatCurrency(fiatAmount)
            }
            db.conversion.updateConfig(req)
            call.respond(HttpStatusCode.NoContent)
        }
    }
}