summaryrefslogtreecommitdiff
path: root/wallet/src/main/java/net/taler/wallet/exchanges/ExchangeManager.kt
blob: fa357b5dd195c0fa72ea51648cc00e546d7e2596 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
 * This file is part of GNU Taler
 * (C) 2020 Taler Systems S.A.
 *
 * GNU Taler is free software; you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 3, or (at your option) any later version.
 *
 * GNU Taler 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */

package net.taler.wallet.exchanges

import android.util.Log
import androidx.annotation.WorkerThread
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.launch
import kotlinx.serialization.Serializable
import net.taler.common.Event
import net.taler.common.toEvent
import net.taler.wallet.TAG
import net.taler.wallet.backend.TalerErrorInfo
import net.taler.wallet.backend.WalletBackendApi

@Serializable
data class ExchangeListResponse(
    val exchanges: List<ExchangeItem>,
)

@Serializable
data class ExchangeDetailedResponse(
    val exchange: ExchangeItem,
)

class ExchangeManager(
    private val api: WalletBackendApi,
    private val scope: CoroutineScope,
) {

    private val mProgress = MutableLiveData<Boolean>()
    val progress: LiveData<Boolean> = mProgress

    private val mExchanges = MutableLiveData<List<ExchangeItem>>()
    val exchanges: LiveData<List<ExchangeItem>> get() = list()

    private val mAddError = MutableLiveData<Event<TalerErrorInfo>>()
    val addError: LiveData<Event<TalerErrorInfo>> = mAddError

    private val mListError = MutableLiveData<Event<TalerErrorInfo>>()
    val listError: LiveData<Event<TalerErrorInfo>> = mListError

    private val mDeleteError = MutableLiveData<Event<TalerErrorInfo>>()
    val deleteError: LiveData<Event<TalerErrorInfo>> = mDeleteError

    private val mReloadError = MutableLiveData<Event<TalerErrorInfo>>()
    val reloadError: LiveData<Event<TalerErrorInfo>> = mReloadError

    var withdrawalExchange: ExchangeItem? = null

    private fun list(): LiveData<List<ExchangeItem>> {
        mProgress.value = true
        scope.launch {
            val response = api.request("listExchanges", ExchangeListResponse.serializer())
            response.onError {
                mProgress.value = false
                mListError.value = it.toEvent()
            }.onSuccess {
                Log.d(TAG, "Exchange list: ${it.exchanges}")
                mProgress.value = false
                mExchanges.value = it.exchanges
            }
        }
        return mExchanges
    }

    fun add(exchangeUrl: String) = scope.launch {
        mProgress.value = true
        api.request<Unit>("addExchange") {
            put("exchangeBaseUrl", exchangeUrl)
        }.onError {
            Log.e(TAG, "Error adding exchange: $it")
            mProgress.value = false
            mAddError.value = it.toEvent()
        }.onSuccess {
            mProgress.value = false
            Log.d(TAG, "Exchange $exchangeUrl added")
            list()
        }
    }

    fun reload(exchangeUrl: String, force: Boolean = true) = scope.launch {
        mProgress.value = true
        api.request<Unit>("updateExchangeEntry") {
            put("exchangeBaseUrl", exchangeUrl)
            put("force", force)
        }.onError {
            Log.e(TAG, "Error reloading exchange: $it")
            mProgress.value = false
            mReloadError.value = it.toEvent()
        }.onSuccess {
            mProgress.value = false
            Log.d(TAG, "Exchange $exchangeUrl reloaded")
            list()
        }
    }

    fun delete(exchangeUrl: String, purge: Boolean = false) = scope.launch {
        mProgress.value = true
        api.request<Unit>("deleteExchange") {
            put("exchangeBaseUrl", exchangeUrl)
            put("purge", purge)
        }.onError {
            Log.e(TAG, "Error deleting exchange: $it")
            mProgress.value = false
            mDeleteError.value = it.toEvent()
        }.onSuccess {
            mProgress.value = false
            Log.d(TAG, "Exchange $exchangeUrl deleted")
            list()
        }
    }

    fun findExchangeForCurrency(currency: String): Flow<ExchangeItem?> = flow {
        emit(findExchange(currency))
    }

    @WorkerThread
    suspend fun findExchange(currency: String): ExchangeItem? {
        var exchange: ExchangeItem? = null
        api.request(
            operation = "listExchanges",
            serializer = ExchangeListResponse.serializer()
        ).onSuccess { exchangeListResponse ->
            // just pick the first for now
            exchange = exchangeListResponse.exchanges.find { it.currency == currency }
        }
        return exchange
    }

    @WorkerThread
    suspend fun findExchangeByUrl(exchangeUrl: String): ExchangeItem? {
        var exchange: ExchangeItem? = null
        api.request("getExchangeDetailedInfo", ExchangeDetailedResponse.serializer()) {
            put("exchangeBaseUrl", exchangeUrl)
        }.onError {
            Log.e(TAG, "Error getExchangeDetailedInfo: $it")
        }.onSuccess {
            exchange = it.exchange
        }
        return exchange
    }

    fun addDevExchanges() {
        scope.launch {
            listOf(
                "https://exchange.demo.taler.net/",
                "https://exchange.test.taler.net/",
                "https://exchange.head.taler.net/",
                "https://exchange.taler.ar/",
                "https://exchange.taler.fdold.eu/",
                "https://exchange.taler.grothoff.org/",
            ).forEach { exchangeUrl ->
                add(exchangeUrl)
                delay(100)
            }
            exchanges.value?.let { exs ->
                exs.find {
                    it.exchangeBaseUrl.startsWith("https://exchange.taler.fdold.eu")
                }?.let { fDoldExchange ->
                    api.request<Unit>("addGlobalCurrencyExchange") {
                        put("currency", fDoldExchange.currency)
                        put("exchangeBaseUrl", fDoldExchange.exchangeBaseUrl)
                        put("exchangeMasterPub",
                            "7ER30ZWJEXAG026H5KG9M19NGTFC2DKKFPV79GVXA6DK5DCNSWXG")
                    }.onError {
                        Log.e(TAG, "Error addGlobalCurrencyExchange: $it")
                    }.onSuccess {
                        Log.i(TAG, "fdold is global now!")
                    }
                }
            }
        }
    }

}