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

feat: add xcm dry run api #257

Merged
merged 9 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from 6 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
43 changes: 40 additions & 3 deletions app/src/hooks/useParaspellApi.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { getSenderAddress } from '@/utils/address'
import { trackTransferMetrics } from '@/utils/analytics'
import { isProduction } from '@/utils/env'
import { handleObservableEvents } from '@/utils/papi'
import { createTx, moonbeamTransfer } from '@/utils/paraspell'
import { createTx, dryRun, moonbeamTransfer } from '@/utils/paraspell'
import { txWasCancelled } from '@/utils/transfer'
import { captureException } from '@sentry/nextjs'
import { switchChain } from '@wagmi/core'
Expand Down Expand Up @@ -48,8 +48,6 @@ const useParaspellApi = () => {
const date = new Date()
await addToOngoingTransfers(hash, params, senderAddress, tokenUSDValue, date, setStatus)

// TODO: figure out how to add crosschain event stuff

// We intentionally track the transfer on submit. The intention was clear, and if it fails somehow we see it in sentry and fix it.
if (params.environment === Environment.Mainnet && isProduction) {
trackTransferMetrics({
Expand Down Expand Up @@ -79,6 +77,13 @@ const useParaspellApi = () => {
if (!account.pjsSigner?.signPayload || !account.pjsSigner?.signRaw)
throw new Error('Signer not found')

// Validate the transfer
setStatus('Validating')

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

const tx = await createTx(params, params.sourceChain.rpcConnection)
setStatus('Signing')

Expand Down Expand Up @@ -152,6 +157,38 @@ const useParaspellApi = () => {
}
}

interface DryRunResult {
dryRunSupported: boolean
success: boolean
error?: string
fee?: bigint
}
noahjoeris marked this conversation as resolved.
Show resolved Hide resolved

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

const res: DryRunResult = {
dryRunSupported: true,
success: dryRunResult.success,
}

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}`))
}

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

return { dryRunSupported: true, success: false, error: (e as Error).message }
}
}

const addToOngoingTransfers = async (
txHash: string,
params: TransferParams,
Expand Down
1 change: 0 additions & 1 deletion app/src/hooks/useSnowbridgeApi.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ const useSnowbridgeApi = () => {
onComplete,
} = params

setStatus('Loading')
try {
if (snowbridgeContext === undefined) {
addNotification({
Expand Down
31 changes: 31 additions & 0 deletions app/src/utils/paraspell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
getAllAssetsSymbols,
getTNode,
TCurrencyCore,
TDryRunResult,
TNodeDotKsmWithRelayChains,
type TPapiTransaction,
} from '@paraspell/sdk'
Expand Down Expand Up @@ -79,6 +80,36 @@ export const moonbeamTransfer = async (
.build()
noahjoeris marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Dry run a transfer using Paraspell.
*
* @param params - The transfer parameters
* @param wssEndpoint - An optional wss chain endpoint to connect to a specific blockchain.
* @returns - A Promise that resolves a dry run result.
* @throws - An error if the dry run api is not available.
*/
export const dryRun = async (
params: TransferParams,
wssEndpoint?: string,
): Promise<TDryRunResult> => {
const { environment, sourceChain, destinationChain, token, amount, recipient } = params

const relay = getRelayNode(environment)
const sourceChainFromId = getTNode(sourceChain.chainId, relay)
const destinationChainFromId = getTNode(destinationChain.chainId, relay)
if (!sourceChainFromId || !destinationChainFromId)
throw new Error('Dry Run failed: chain id not found.')

const currencyId = getCurrencyId(environment, sourceChainFromId, sourceChain.uid, token)

return await Builder(wssEndpoint)
.from(sourceChainFromId)
.to(destinationChainFromId)
.currency({ ...currencyId, amount })
.address(recipient)
.dryRun()
}

export const getTokenSymbol = (sourceChain: TNodeDotKsmWithRelayChains, token: Token) => {
const supportedAssets = getAllAssetsSymbols(sourceChain)

Expand Down
Loading