Skip to content

Commit

Permalink
feat: chainflip explorer link (#8529)
Browse files Browse the repository at this point in the history
  • Loading branch information
gomesalexandre authored Jan 10, 2025
1 parent 8bd7ccf commit d690560
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 1 deletion.
5 changes: 4 additions & 1 deletion react-app-rewired/headers/csps/chainflip.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type { Csp } from '../types'

export const csp: Csp = {
'connect-src': ['https://chainflip-broker.io/'],
'connect-src': [
'https://explorer-service-processor.chainflip.io/graphql',
'https://chainflip-broker.io/',
],
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { MiddleEllipsis } from 'components/MiddleEllipsis/MiddleEllipsis'
import { useGetTradeQuotes } from 'components/MultiHopTrade/hooks/useGetTradeQuotes/useGetTradeQuotes'
import { RawText, Text } from 'components/Text'
import { getChainAdapterManager } from 'context/PluginProvider/chainAdapterSingleton'
import { useChainflipSwapIdQuery } from 'hooks/queries/useChainflipSwapIdQuery'
import { useSafeTxQuery } from 'hooks/queries/useSafeTx'
import { useLocaleFormatter } from 'hooks/useLocaleFormatter/useLocaleFormatter'
import { bn, bnOrZero } from 'lib/bignumber/bignumber'
Expand Down Expand Up @@ -118,6 +119,11 @@ export const HopTransactionStep = ({
accountId: sellAssetAccountId,
})

const { data: maybeChainflipSwapId } = useChainflipSwapIdQuery({
txHash: sellTxHash,
swapperName,
})

const txLinks = useMemo(() => {
const txLinks = []
if (buyTxHash) {
Expand All @@ -141,6 +147,7 @@ export const HopTransactionStep = ({
defaultExplorerBaseUrl: tradeQuoteStep.sellAsset.explorerTxLink,
accountId: sellAssetAccountId,
maybeSafeTx,
maybeChainflipSwapId,
...(tradeQuoteStep.source === SwapperName.CowSwap
? {
tradeId: sellTxHash,
Expand All @@ -156,6 +163,7 @@ export const HopTransactionStep = ({
return txLinks
}, [
buyTxHash,
maybeChainflipSwapId,
maybeSafeTx,
sellAssetAccountId,
sellTxHash,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Link } from '@chakra-ui/react'
import type { AccountId } from '@shapeshiftoss/caip'
import type { SwapSource } from '@shapeshiftoss/swapper'
import { MiddleEllipsis } from 'components/MiddleEllipsis/MiddleEllipsis'
import { useChainflipSwapIdQuery } from 'hooks/queries/useChainflipSwapIdQuery'
import { useSafeTxQuery } from 'hooks/queries/useSafeTx'
import { getTxLink } from 'lib/getTxLink'

Expand All @@ -23,12 +24,18 @@ export const TxLabel = ({
accountId,
})

const { data: maybeChainflipSwapId } = useChainflipSwapIdQuery({
txHash,
swapperName,
})

const txLink = getTxLink({
defaultExplorerBaseUrl: explorerBaseUrl,
maybeSafeTx,
accountId,
name: swapperName,
...(isBuyTxHash ? { txId: txHash } : { tradeId: txHash }),
maybeChainflipSwapId,
})

return txLink ? (
Expand Down
59 changes: 59 additions & 0 deletions src/hooks/queries/useChainflipSwapIdQuery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import type { SwapSource } from '@shapeshiftoss/swapper'
import { SwapperName } from '@shapeshiftoss/swapper'
import type { UseQueryResult } from '@tanstack/react-query'
import { skipToken, useQuery } from '@tanstack/react-query'
import axios from 'axios'

type UseChainflipSwapIdArgs = {
txHash: string | undefined
swapperName: SwapSource | undefined
}

type SwapIdResponse = {
data: {
txRefs: {
nodes: {
swap: {
nativeId: string | undefined
}
}[]
}
}
}

const nativeSwapIdQuery = `
query GetStringSearchResults($searchString: String!) {
txRefs: allTransactionRefs(filter: {ref: {equalTo: $searchString}}) {
nodes {
swap: swapRequestBySwapRequestId {
nativeId
}
}
}
}
`

export const useChainflipSwapIdQuery = ({
txHash,
swapperName,
}: UseChainflipSwapIdArgs): UseQueryResult<string | undefined, Error> => {
return useQuery({
queryKey: ['chainflipSwapId', { txHash }],
queryFn:
txHash && swapperName?.includes(SwapperName.Chainflip)
? () =>
// Yes, this is ugly, just another day in "we're doing inline raw GraphQL queries with http"
// Note, this is undocumented but is actually the exact same query fragment CF UI uses to go from Tx hash to swap ID (though only a fragment of it,
// actually stripped out to the bare minimum here vs. cf UI)
axios.post<SwapIdResponse>('https://explorer-service-processor.chainflip.io/graphql', {
query: nativeSwapIdQuery,
variables: {
searchString: txHash,
},
})
: skipToken,
select: ({ data: { data } }) => data.txRefs.nodes[0].swap.nativeId,
// Long-poll until swap by initiating Txid is indexed
refetchInterval: 10_000,
})
}
19 changes: 19 additions & 0 deletions src/lib/getTxLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import type { AccountId } from '@shapeshiftoss/caip'
import { fromAccountId } from '@shapeshiftoss/caip'
import type { SafeTxInfo, SwapSource } from '@shapeshiftoss/swapper'
import { SwapperName } from '@shapeshiftoss/swapper'
import {
CHAINFLIP_BOOST_SWAP_SOURCE,
CHAINFLIP_DCA_BOOST_SWAP_SOURCE,
CHAINFLIP_DCA_SWAP_SOURCE,
} from '@shapeshiftoss/swapper/dist/swappers/ChainflipSwapper/constants'
import {
THORCHAIN_LONGTAIL_STREAMING_SWAP_SOURCE,
THORCHAIN_LONGTAIL_SWAP_SOURCE,
Expand Down Expand Up @@ -33,6 +38,7 @@ type GetTxLink = GetTxBaseUrl &
({ txId: string; tradeId?: never } | { tradeId: string; txId?: never }) & {
accountId: AccountId | undefined
maybeSafeTx: SafeTxInfo | undefined
maybeChainflipSwapId?: string | undefined
}

export const getTxBaseUrl = ({ name, defaultExplorerBaseUrl, isOrder }: GetTxBaseUrl): string => {
Expand All @@ -46,6 +52,11 @@ export const getTxBaseUrl = ({ name, defaultExplorerBaseUrl, isOrder }: GetTxBas
case THORCHAIN_LONGTAIL_SWAP_SOURCE:
case THORCHAIN_LONGTAIL_STREAMING_SWAP_SOURCE:
return 'https://viewblock.io/thorchain/tx/'
case SwapperName.Chainflip:
case CHAINFLIP_BOOST_SWAP_SOURCE:
case CHAINFLIP_DCA_SWAP_SOURCE:
case CHAINFLIP_DCA_BOOST_SWAP_SOURCE:
return 'https://scan.chainflip.io/swaps/'
default:
return defaultExplorerBaseUrl
}
Expand All @@ -58,6 +69,7 @@ export const getTxLink = ({
tradeId,
maybeSafeTx,
accountId,
maybeChainflipSwapId,
}: GetTxLink): string => {
const isSafeTxHash = maybeSafeTx?.isSafeTxHash
const id = txId ?? tradeId
Expand All @@ -72,6 +84,13 @@ export const getTxLink = ({
case THORCHAIN_LONGTAIL_SWAP_SOURCE:
case THORCHAIN_LONGTAIL_STREAMING_SWAP_SOURCE:
return `${baseUrl}${id.replace(/^0x/, '')}`
case SwapperName.Chainflip:
case CHAINFLIP_BOOST_SWAP_SOURCE:
case CHAINFLIP_DCA_SWAP_SOURCE:
case CHAINFLIP_DCA_BOOST_SWAP_SOURCE:
return maybeChainflipSwapId
? `${baseUrl}${maybeChainflipSwapId}`
: `${defaultExplorerBaseUrl}${id}`
default:
return `${baseUrl}${id}`
}
Expand Down

0 comments on commit d690560

Please sign in to comment.