Skip to content
Open
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
2 changes: 1 addition & 1 deletion packages/public-api/src/lib/quoteStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export type StoredQuote = {
createdAt: number
expiresAt: number
metadata: {
chainflipSwapId?: number
chainflipSwapId?: number | string
nearIntentsDepositAddress?: string
nearIntentsDepositMemo?: string
relayId?: string
Expand Down
20 changes: 16 additions & 4 deletions packages/swapper/src/swappers/ChainflipSwapper/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,18 @@ export const chainflipApi: SwapperApi = {
chainSpecific: { from, tokenId },
})

// Chainflip deposit addresses are program-owned accounts that require more compute
// than a regular SOL transfer. Apply a safety margin to avoid "Computational budget exceeded".
const computeUnits = Math.ceil(Math.max(Number(fast.chainSpecific.computeUnits), 50_000) * 1.6)

return adapter.buildSendApiTransaction({
to,
from,
value,
accountNumber,
chainSpecific: {
tokenId,
computeUnitLimit: fast.chainSpecific.computeUnits,
computeUnitLimit: String(computeUnits),
computeUnitPrice: fast.chainSpecific.priorityFee,
},
})
Expand Down Expand Up @@ -207,11 +211,17 @@ export const chainflipApi: SwapperApi = {
},
})

return fast.txFee
// Mirror the same compute budget boost as getUnsignedSolanaTransaction
// to avoid underquoting fees for Chainflip deposit addresses
const simulatedUnits = Number(fast.chainSpecific.computeUnits)
const boostedUnits = Math.ceil(Math.max(simulatedUnits, 50_000) * 1.6)
const ratio = simulatedUnits > 0 ? boostedUnits / simulatedUnits : 1

return String(Math.ceil(Number(fast.txFee) * ratio))
},
checkTradeStatus: async ({ config, swap }) => {
const chainflipSwapId = swap?.metadata.chainflipSwapId
if (!chainflipSwapId) throw Error(`chainflipSwapId is required`)
if (chainflipSwapId == null) throw Error(`chainflipSwapId is required`)

const brokerUrl = config.VITE_CHAINFLIP_API_URL
const apiKey = config.VITE_CHAINFLIP_API_KEY
Expand All @@ -231,7 +241,7 @@ export const chainflipApi: SwapperApi = {

const { data: statusResponse } = maybeStatusResponse.unwrap()
const {
status: { swapEgress },
status: { swapEgress, swapId },
} = statusResponse

// Assume no outbound Tx is a pending Tx
Expand All @@ -240,6 +250,7 @@ export const chainflipApi: SwapperApi = {
buyTxHash: undefined,
status: TxStatus.Pending,
message: getLatestChainflipStatusMessage(statusResponse),
chainflipSwapId: swapId ?? undefined,
}
}

Expand All @@ -249,6 +260,7 @@ export const chainflipApi: SwapperApi = {
buyTxHash: swapEgress.transactionReference,
status: TxStatus.Confirmed,
message: undefined,
chainflipSwapId: swapId ?? undefined,
}
},
}
1 change: 1 addition & 0 deletions packages/swapper/src/swappers/ChainflipSwapper/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { ChainflipBaasStatusSwap } from './models/ChainflipBaasStatusSwap'
export type ChainFlipStatus = {
status: {
state: 'waiting' | 'receiving' | 'swapping' | 'sending' | 'sent' | 'completed' | 'failed'
swapId?: string
swap?: ChainflipBaasStatusSwap
swapEgress?: ChainflipBaasStatusEgress
}
Expand Down
5 changes: 3 additions & 2 deletions packages/swapper/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ export type TradeQuoteStep = {
}
cowswapQuoteResponse?: OrderQuoteResponse
chainflipSpecific?: {
chainflipSwapId?: number
chainflipSwapId?: number | string
chainflipDepositAddress?: string
chainflipNumberOfChunks?: number
chainflipChunkIntervalBlocks?: number
Expand Down Expand Up @@ -567,7 +567,7 @@ export type SwapExecutionMetadata = {
}

export type SwapperSpecificMetadata = {
chainflipSwapId: number | undefined
chainflipSwapId: number | string | undefined
nearIntentsSpecific?: {
depositAddress: string
depositMemo?: string
Expand Down Expand Up @@ -821,6 +821,7 @@ export type TradeStatus = {
relayerExplorerTxLink?: string | undefined
message: string | [string, InterpolationOptions] | undefined
actualBuyAmountCryptoBaseUnit?: string
chainflipSwapId?: number | string
}

// a result containing all routes that were successfully generated, or an error in the case where
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ const DEFAULT_STREAMING_SWAP_METADATA: StreamingSwapMetadata = {
}

const getChainflipStreamingSwap = async (
swapId: number | undefined,
swapId: number | string | undefined,
): Promise<ChainflipStreamingSwapResponseSuccess | undefined> => {
if (!swapId) return
if (swapId == null) return

const config = getConfig()
const brokerUrl = config.VITE_CHAINFLIP_API_URL
Expand Down Expand Up @@ -105,7 +105,7 @@ export const useChainflipStreamingProgress = ({
useQuery({
queryKey: ['streamingSwapData', chainflipSwapId, SwapperName.Chainflip],
queryFn:
chainflipSwapId &&
chainflipSwapId != null &&
swap &&
swap.swapperName === SwapperName.Chainflip &&
sellTxHash &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ export const useTradeExecution = (
)
execution.on(
TradeExecutionEvent.Status,
({ buyTxHash, message, actualBuyAmountCryptoBaseUnit }) => {
({ buyTxHash, message, actualBuyAmountCryptoBaseUnit, chainflipSwapId }) => {
dispatch(
tradeQuoteSlice.actions.setSwapTxMessage({
hopIndex,
Expand All @@ -280,9 +280,9 @@ export const useTradeExecution = (
)
}

// Update the swap with the actual buy amount if available
// Update the swap with the actual buy amount and/or real Chainflip swap ID if available
// Read fresh state to avoid stale closure - swapsById captured at render time may have outdated status
if (actualBuyAmountCryptoBaseUnit) {
if (actualBuyAmountCryptoBaseUnit || chainflipSwapId) {
const freshActiveSwapId = swapSlice.selectors.selectActiveSwapId(store.getState())
if (freshActiveSwapId) {
const currentSwap = swapSlice.selectors.selectSwapsById(store.getState())[
Expand All @@ -292,7 +292,10 @@ export const useTradeExecution = (
dispatch(
swapSlice.actions.upsertSwap({
...currentSwap,
actualBuyAmountCryptoBaseUnit,
...(actualBuyAmountCryptoBaseUnit && { actualBuyAmountCryptoBaseUnit }),
...(chainflipSwapId && {
metadata: { ...currentSwap.metadata, chainflipSwapId },
}),
}),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ export const useSwapActionSubscriber = () => {
if (!swap.sellTxHash) return
if (!swap.receiveAddress) return

const { status, message, buyTxHash, actualBuyAmountCryptoBaseUnit } =
const { status, message, buyTxHash, actualBuyAmountCryptoBaseUnit, chainflipSwapId } =
await queryClient.fetchQuery({
queryKey: tradeStatusQueryKey(swap.id, swap.sellTxHash),
queryFn: () =>
Expand Down Expand Up @@ -252,7 +252,7 @@ export const useSwapActionSubscriber = () => {
defaultExplorerBaseUrl,
maybeSafeTx,
stepSource: status && status !== TxStatus.Unknown ? swap.source : undefined,
maybeChainflipSwapId: `${swap.metadata.chainflipSwapId}`,
maybeChainflipSwapId: chainflipSwapId?.toString(),
maybeNearIntentsDepositAddress: swap.metadata.nearIntentsSpecific?.depositAddress,
...(swap.swapperName === SwapperName.CowSwap ? { tradeId: txHash } : { txId: txHash }),
...(swap.metadata.relayerTxHash && {
Expand All @@ -272,6 +272,9 @@ export const useSwapActionSubscriber = () => {
buyTxHash,
txLink,
actualBuyAmountCryptoBaseUnit,
...(chainflipSwapId && {
metadata: { ...swap.metadata, chainflipSwapId },
}),
}),
)

Expand Down
2 changes: 2 additions & 0 deletions src/lib/tradeExecution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export const fetchTradeStatus = async ({
relayerTxHash,
relayerExplorerTxLink,
actualBuyAmountCryptoBaseUnit,
chainflipSwapId,
} = await swapper.checkTradeStatus({
txHash: sellTxHash,
chainId: sellAssetChainId,
Expand All @@ -114,6 +115,7 @@ export const fetchTradeStatus = async ({
relayerTxHash,
relayerExplorerTxLink,
actualBuyAmountCryptoBaseUnit,
chainflipSwapId,
}
}

Expand Down
Loading