Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
noahjoeris committed Jan 23, 2025
1 parent 3698671 commit 5bafb92
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 31 deletions.
37 changes: 15 additions & 22 deletions app/src/hooks/useParaspellApi.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { isProduction } from '@/utils/env'
import { handleObservableEvents } from '@/utils/papi'
import { createTx, dryRun, moonbeamTransfer } from '@/utils/paraspell'
import { txWasCancelled } from '@/utils/transfer'
import { TDryRunResult } from '@paraspell/sdk'
import { captureException } from '@sentry/nextjs'
import { switchChain } from '@wagmi/core'
import { InvalidTxError, TxEvent } from 'polkadot-api'
Expand Down Expand Up @@ -81,8 +82,8 @@ const useParaspellApi = () => {
setStatus('Validating')

const validationResult = await validate(params)
if (validationResult.dryRunSupported && !validationResult.success)
throw new Error(`Transfer dry run failed: ${validationResult.error}`)
if (validationResult.type === 'Supported' && !validationResult.success)
throw new Error(`Transfer dry run failed: ${validationResult.failureReason}`)

const tx = await createTx(params, params.sourceChain.rpcConnection)
setStatus('Signing')
Expand Down Expand Up @@ -157,35 +158,27 @@ const useParaspellApi = () => {
}
}

interface DryRunResult {
dryRunSupported: boolean
success: boolean
error?: string
fee?: bigint
}
type DryRunResult = { type: 'Supported' | 'Unsupported' } & TDryRunResult

const validate = async (params: TransferParams): Promise<DryRunResult> => {
try {
const dryRunResult = await dryRun(params, params.sourceChain.rpcConnection)

const res: DryRunResult = {
dryRunSupported: true,
success: dryRunResult.success,
}
const result = await dryRun(params, params.sourceChain.rpcConnection)

if (dryRunResult.success) res.fee = dryRunResult.fee
else {
res.error = dryRunResult.failureReason
console.error('Dry run failed:', dryRunResult.failureReason)
captureException(new Error(`Dry run failed: ${dryRunResult.failureReason}`))
const dryRunResult: DryRunResult = {
type: 'Supported',
...result,
}

return res
return dryRunResult
} catch (e: unknown) {
if (e instanceof Error && e.message.includes('DryRunApi is not available'))
return { dryRunSupported: false, success: false, error: e.message }
return { type: 'Unsupported', success: false, failureReason: e.message } as DryRunResult

return { dryRunSupported: true, success: false, error: (e as Error).message }
return {
type: 'Supported',
success: false,
failureReason: (e as Error).message,
} as DryRunResult
}
}

Expand Down
9 changes: 0 additions & 9 deletions app/src/utils/paraspell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,6 @@ export const moonbeamTransfer = async (
throw new Error('Transfer failed: chain id not found.')
const currencyId = getCurrencyId(environment, sourceChainFromId, sourceChain.uid, token)

console.log('Moonbeam transfer:', {
sourceChainFromId,
destinationChainFromId,
currencyId,
amount,
recipient,
viemClient,
})

return EvmBuilder()
.from('Moonbeam')
.to(destinationChainFromId)
Expand Down

0 comments on commit 5bafb92

Please sign in to comment.