Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CU-86a5xtcvh - BS Swap - Throw error message returned by SimpleSwap #124

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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"
}
174 changes: 111 additions & 63 deletions packages/bs-swap/src/apis/SimpleSwapApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,66 +72,98 @@ export class SimpleSwapApi<BSName extends string = string> {
}

async getCurrencies(options: SimpleSwapServiceInitParams<BSName>): Promise<SimpleSwapApiCurrency<BSName>[]> {
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<SimpleSwapApiGetCurrenciesResponse>('/currencies')
const response = await this.#axios.get<SimpleSwapApiGetCurrenciesResponse>('/currencies')

const tokens: SimpleSwapApiCurrency<BSName>[] = []
const tokens: SimpleSwapApiCurrency<BSName>[] = []

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<SimpleSwapApiGetPairsResponse>(`/pairs/${ticker}/${network}`)
const pairs = response.data.result[`${ticker}:${network}`] ?? []
try {
const response = await this.#axios.get<SimpleSwapApiGetPairsResponse>(`/pairs/${ticker}/${network}`)
const pairs = response.data.result[`${ticker}:${network}`] ?? []

const tokens: SimpleSwapApiCurrency<BSName>[] = []
const tokens: SimpleSwapApiCurrency<BSName>[] = []

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<SimpleSwapApiGetRangeResponse>('/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<SimpleSwapApiGetRangeResponse>('/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<SimpleSwapApiGetEstimateResponse>('/estimates', {
params: {
tickerFrom: currencyFrom.ticker,
tickerTo: currencyTo.ticker,
networkFrom: currencyFrom.network,
networkTo: currencyTo.network,
amount,
},
})
try {
const response = await this.#axios.get<SimpleSwapApiGetEstimateResponse>('/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(
Expand All @@ -141,35 +173,51 @@ export class SimpleSwapApi<BSName extends string = string> {
address: string,
refundAddress: string
) {
const {
data: { result },
} = await this.#axios.post<SimpleSwapApiCreateExchangeResponse>('/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<SimpleSwapApiCreateExchangeResponse>('/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<SimpleSwapApiGetExchangeResponse>(`/exchanges/${id}`)
try {
const {
data: { result },
} = await this.#axios.get<SimpleSwapApiGetExchangeResponse>(`/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
}
}
}
Loading