From 8c20bcbffb171b164a63938f812b13893228ba20 Mon Sep 17 00:00:00 2001 From: Dust Date: Mon, 16 Dec 2024 18:31:13 -0300 Subject: [PATCH] CU-86a5xtcvh - BS Swap - Throw error message returned by SimpleSwap --- .../CU-86a5xtcvh_2024-12-16-21-27.json | 10 + packages/bs-swap/src/apis/SimpleSwapApi.ts | 174 +++++++++++------- 2 files changed, 121 insertions(+), 63 deletions(-) create mode 100644 common/changes/@cityofzion/bs-swap/CU-86a5xtcvh_2024-12-16-21-27.json diff --git a/common/changes/@cityofzion/bs-swap/CU-86a5xtcvh_2024-12-16-21-27.json b/common/changes/@cityofzion/bs-swap/CU-86a5xtcvh_2024-12-16-21-27.json new file mode 100644 index 0000000..769a0e8 --- /dev/null +++ b/common/changes/@cityofzion/bs-swap/CU-86a5xtcvh_2024-12-16-21-27.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@cityofzion/bs-swap", + "comment": "Throw error message returned by SimpleSwap instead of default Axios error", + "type": "patch" + } + ], + "packageName": "@cityofzion/bs-swap" +} \ No newline at end of file diff --git a/packages/bs-swap/src/apis/SimpleSwapApi.ts b/packages/bs-swap/src/apis/SimpleSwapApi.ts index 9c19d4c..5fade16 100644 --- a/packages/bs-swap/src/apis/SimpleSwapApi.ts +++ b/packages/bs-swap/src/apis/SimpleSwapApi.ts @@ -72,66 +72,98 @@ export class SimpleSwapApi { } async getCurrencies(options: SimpleSwapServiceInitParams): Promise[]> { - if (this.#allCurrenciesMap.size) { - return Array.from(this.#allCurrenciesMap.values()) - } + try { + if (this.#allCurrenciesMap.size) { + return Array.from(this.#allCurrenciesMap.values()) + } - const response = await this.#axios.get('/currencies') + const response = await this.#axios.get('/currencies') - const tokens: SimpleSwapApiCurrency[] = [] + const tokens: SimpleSwapApiCurrency[] = [] - response.data.result.forEach(currency => { - const token = this.#getTokenFromCurrency(currency, options) - if (!token) return + response.data.result.forEach(currency => { + const token = this.#getTokenFromCurrency(currency, options) + if (!token) return - this.#allCurrenciesMap.set(`${token.ticker}:${token.network}`, token) + this.#allCurrenciesMap.set(`${token.ticker}:${token.network}`, token) - if (!token.blockchain) return + if (!token.blockchain) return - tokens.push(token) - }) + tokens.push(token) + }) - return tokens + return tokens + } catch (error) { + if (axios.isAxiosError(error) && error.response?.data.message) { + throw new Error(error.response.data.message) + } + + throw error + } } async getPairs(ticker: string, network: string) { - const response = await this.#axios.get(`/pairs/${ticker}/${network}`) - const pairs = response.data.result[`${ticker}:${network}`] ?? [] + try { + const response = await this.#axios.get(`/pairs/${ticker}/${network}`) + const pairs = response.data.result[`${ticker}:${network}`] ?? [] - const tokens: SimpleSwapApiCurrency[] = [] + const tokens: SimpleSwapApiCurrency[] = [] - pairs.forEach(pair => { - const token = this.#allCurrenciesMap.get(pair) - if (token) tokens.push(token) - }) + pairs.forEach(pair => { + const token = this.#allCurrenciesMap.get(pair) + if (token) tokens.push(token) + }) - return tokens + return tokens + } catch (error) { + if (axios.isAxiosError(error) && error.response?.data.message) { + throw new Error(error.response.data.message) + } + + throw error + } } async getRange(currencyFrom: SimpleSwapApiCurrency, currencyTo: SimpleSwapApiCurrency) { - const response = await this.#axios.get('/ranges', { - params: { - tickerFrom: currencyFrom.ticker, - tickerTo: currencyTo.ticker, - networkFrom: currencyFrom.network, - networkTo: currencyTo.network, - }, - }) - return response.data.result + try { + const response = await this.#axios.get('/ranges', { + params: { + tickerFrom: currencyFrom.ticker, + tickerTo: currencyTo.ticker, + networkFrom: currencyFrom.network, + networkTo: currencyTo.network, + }, + }) + return response.data.result + } catch (error) { + if (axios.isAxiosError(error) && error.response?.data.message) { + throw new Error(error.response.data.message) + } + + throw error + } } async getEstimate(currencyFrom: SimpleSwapApiCurrency, currencyTo: SimpleSwapApiCurrency, amount: string) { - const response = await this.#axios.get('/estimates', { - params: { - tickerFrom: currencyFrom.ticker, - tickerTo: currencyTo.ticker, - networkFrom: currencyFrom.network, - networkTo: currencyTo.network, - amount, - }, - }) + try { + const response = await this.#axios.get('/estimates', { + params: { + tickerFrom: currencyFrom.ticker, + tickerTo: currencyTo.ticker, + networkFrom: currencyFrom.network, + networkTo: currencyTo.network, + amount, + }, + }) + + return response.data.result.estimatedAmount + } catch (error) { + if (axios.isAxiosError(error) && error.response?.data.message) { + throw new Error(error.response.data.message) + } - return response.data.result.estimatedAmount + throw error + } } async createExchange( @@ -141,35 +173,51 @@ export class SimpleSwapApi { address: string, refundAddress: string ) { - const { - data: { result }, - } = await this.#axios.post('/exchanges', { - tickerFrom: currencyFrom.ticker, - tickerTo: currencyTo.ticker, - networkFrom: currencyFrom.network, - networkTo: currencyTo.network, - amount, - addressTo: address, - userRefundAddress: refundAddress, - }) + try { + const { + data: { result }, + } = await this.#axios.post('/exchanges', { + tickerFrom: currencyFrom.ticker, + tickerTo: currencyTo.ticker, + networkFrom: currencyFrom.network, + networkTo: currencyTo.network, + amount, + addressTo: address, + userRefundAddress: refundAddress, + }) + + return { + id: result.id, + depositAddress: result.addressFrom, + log: JSON.stringify(result), + } + } catch (error) { + if (axios.isAxiosError(error) && error.response?.data.message) { + throw new Error(error.response.data.message) + } - return { - id: result.id, - depositAddress: result.addressFrom, - log: JSON.stringify(result), + throw error } } async getExchange(id: string) { - const { - data: { result }, - } = await this.#axios.get(`/exchanges/${id}`) + try { + const { + data: { result }, + } = await this.#axios.get(`/exchanges/${id}`) + + return { + status: result.status, + txFrom: result.txFrom, + txTo: result.txTo, + log: JSON.stringify(result), + } + } catch (error) { + if (axios.isAxiosError(error) && error.response?.data.message) { + throw new Error(error.response.data.message) + } - return { - status: result.status, - txFrom: result.txFrom, - txTo: result.txTo, - log: JSON.stringify(result), + throw error } } }