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 8 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
33 changes: 30 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, DryRunResult, 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.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 @@ -152,6 +157,28 @@ const useParaspellApi = () => {
}
}

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

const dryRunResult: DryRunResult = {
type: 'Supported',
...result,
}

return dryRunResult
noahjoeris marked this conversation as resolved.
Show resolved Hide resolved
} catch (e: unknown) {
if (e instanceof Error && e.message.includes('DryRunApi is not available'))
return { type: 'Unsupported', success: false, failureReason: e.message } as DryRunResult

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

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
42 changes: 33 additions & 9 deletions app/src/utils/paraspell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ import {
getAllAssetsSymbols,
getTNode,
TCurrencyCore,
TDryRunResult,
TNodeDotKsmWithRelayChains,
type TPapiTransaction,
} from '@paraspell/sdk'
import { captureException } from '@sentry/nextjs'

export type DryRunResult = { type: 'Supported' | 'Unsupported' } & TDryRunResult

/**
* Creates a submittable PAPI transaction using Paraspell Builder.
*
Expand Down Expand Up @@ -61,15 +64,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 All @@ -79,6 +73,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