Skip to content
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
16 changes: 3 additions & 13 deletions packages/chain-adapters/src/cosmossdk/CosmosSdkBaseAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { bech32 } from 'bech32'
import PQueue from 'p-queue'

import type { ChainAdapter as IChainAdapter } from '../api'
import { ErrorHandler } from '../error/ErrorHandler'
import { ErrorHandler, handleBroadcastTransactionError } from '../error/ErrorHandler'
import type {
Account,
BroadcastTransactionInput,
Expand Down Expand Up @@ -382,19 +382,9 @@ export abstract class CosmosSdkBaseAdapter<T extends CosmosSdkChainId> implement

return txHash
} catch (err) {
if ((err as Error).name === 'ResponseError') {
const response = await ((err as any).response as Response).json()

return handleBroadcastTransactionError(err, response => {
const match = response.message.match(/description:\s*([^,]+)$/)

return ErrorHandler(JSON.stringify(response), {
translation: 'chainAdapters.errors.broadcastTransactionWithMessage',
options: { message: match && match[1] ? match[1].trim() : response.message },
})
}

return ErrorHandler(err, {
translation: 'chainAdapters.errors.broadcastTransaction',
return match && match[1] ? match[1].trim() : response.message
})
}
}
Expand Down
46 changes: 42 additions & 4 deletions packages/chain-adapters/src/error/ErrorHandler.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type * as unchained from '@shapeshiftoss/unchained-client'
import type { AxiosError } from 'axios'
import type { InterpolationOptions } from 'node-polyglot'

Expand All @@ -6,6 +7,15 @@ type ErrorMetadata = {
options?: InterpolationOptions
}

const isResponseError = (err: unknown): err is unchained.ResponseError => {
return (
err instanceof Error &&
err.name === 'ResponseError' &&
'response' in err &&
err.response instanceof Response
)
}

export class ChainAdapterError extends Error {
metadata: ErrorMetadata

Expand All @@ -25,6 +35,36 @@ export class ChainAdapterError extends Error {
}
}

export const handleBroadcastTransactionError = async (
err: unknown,
customMessage?: (response: any) => string,
) => {
try {
if (!isResponseError(err)) throw err

const response = await err.response.clone().json()

const message = (() => {
if (customMessage) return customMessage(response)

try {
return JSON.parse(response.message).message
} catch {
return response.message
}
})()

return ErrorHandler(JSON.stringify(response), {
translation: 'chainAdapters.errors.broadcastTransactionWithMessage',
options: { message },
})
} catch (err) {
return ErrorHandler(err, {
translation: 'chainAdapters.errors.broadcastTransaction',
})
}
}

export const ErrorHandler = async (err: unknown, metadata?: ErrorMetadata): Promise<never> => {
if (err instanceof Error && err.message.toLowerCase().includes('blind sign')) {
throw new ChainAdapterError(err, {
Expand Down Expand Up @@ -60,10 +100,8 @@ export const ErrorHandler = async (err: unknown, metadata?: ErrorMetadata): Prom
const response = JSON.stringify((err as AxiosError).response?.data)
if (metadata) throw new ChainAdapterError(response, metadata)
throw new Error(response)
} else if ((err as Error).name === 'ResponseError') {
// handle fetch api error coming from generated typescript client
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const response = JSON.stringify(await ((err as any).response as Response).json())
} else if (isResponseError(err)) {
const response = JSON.stringify(await err.response.clone().json())
if (metadata) throw new ChainAdapterError(response, metadata)
throw new Error(response)
} else if (err instanceof Error || err instanceof ChainAdapterError) {
Expand Down
20 changes: 6 additions & 14 deletions packages/chain-adapters/src/evm/EvmBaseAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ import PQueue from 'p-queue'
import { isAddress, toHex } from 'viem'

import type { ChainAdapter as IChainAdapter } from '../api'
import { ChainAdapterError, ErrorHandler } from '../error/ErrorHandler'
import {
ChainAdapterError,
ErrorHandler,
handleBroadcastTransactionError,
} from '../error/ErrorHandler'
import type {
Account,
BroadcastTransactionInput,
Expand Down Expand Up @@ -523,19 +527,7 @@ export abstract class EvmBaseAdapter<T extends EvmChainId> implements IChainAdap

return txHash
} catch (err) {
if ((err as Error).name === 'ResponseError') {
const response = await ((err as any).response as Response).json()
const error = JSON.parse(response.message)

return ErrorHandler(JSON.stringify(response), {
translation: 'chainAdapters.errors.broadcastTransactionWithMessage',
options: { message: error.message },
})
}

return ErrorHandler(err, {
translation: 'chainAdapters.errors.broadcastTransaction',
})
return handleBroadcastTransactionError(err)
}
}

Expand Down
19 changes: 6 additions & 13 deletions packages/chain-adapters/src/solana/SolanaChainAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ import { isUndefined } from 'lodash'
import PQueue from 'p-queue'

import type { ChainAdapter as IChainAdapter } from '../api'
import { ChainAdapterError, ErrorHandler } from '../error/ErrorHandler'
import {
ChainAdapterError,
ErrorHandler,
handleBroadcastTransactionError,
} from '../error/ErrorHandler'
import type {
Account,
BroadcastTransactionInput,
Expand Down Expand Up @@ -411,18 +415,7 @@ export class ChainAdapter implements IChainAdapter<KnownChainIds.SolanaMainnet>

return txHash
} catch (err) {
if ((err as Error).name === 'ResponseError') {
const response = await ((err as any).response as Response).json()

return ErrorHandler(JSON.stringify(response), {
translation: 'chainAdapters.errors.broadcastTransactionWithMessage',
options: { message: response.message },
})
}

return ErrorHandler(err, {
translation: 'chainAdapters.errors.broadcastTransaction',
})
return handleBroadcastTransactionError(err)
}
}

Expand Down
20 changes: 6 additions & 14 deletions packages/chain-adapters/src/utxo/UtxoBaseAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ import WAValidator from 'multicoin-address-validator'
import PQueue from 'p-queue'

import type { ChainAdapter as IChainAdapter } from '../api'
import { ChainAdapterError, ErrorHandler } from '../error/ErrorHandler'
import {
ChainAdapterError,
ErrorHandler,
handleBroadcastTransactionError,
} from '../error/ErrorHandler'
import type {
Account,
BroadcastTransactionInput,
Expand Down Expand Up @@ -591,19 +595,7 @@ export abstract class UtxoBaseAdapter<T extends UtxoChainId> implements IChainAd

return txHash
} catch (err) {
if ((err as Error).name === 'ResponseError') {
const response = await ((err as any).response as Response).json()
const error = JSON.parse(response.message)

return ErrorHandler(JSON.stringify(response), {
translation: 'chainAdapters.errors.broadcastTransactionWithMessage',
options: { message: error.message },
})
}

return ErrorHandler(err, {
translation: 'chainAdapters.errors.broadcastTransaction',
})
return handleBroadcastTransactionError(err)
}
}

Expand Down
2 changes: 2 additions & 0 deletions packages/unchained-client/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export * from './types'

export { ResponseError } from './generated/ethereum/runtime'

export * as ws from './websocket'

export * as evm from './evm'
Expand Down