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

Adjust toasts for snowbridge #1901

Merged
merged 10 commits into from
Dec 6, 2024
Merged
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
6 changes: 3 additions & 3 deletions src/sections/transaction/ReviewTransaction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const ReviewTransaction = (props: Transaction) => {
txLink,
txHash,
bridge,
} = useSendTx()
} = useSendTx(props.xcallMeta)

if (!isLoaded) return null

Expand Down Expand Up @@ -145,9 +145,9 @@ export const ReviewTransaction = (props: Transaction) => {
props.onSubmitted?.()
sendEvmTx(data)
}}
onSigned={(tx, xcallMeta) => {
onSigned={(tx) => {
props.onSubmitted?.()
sendTx({ tx, xcallMeta })
sendTx({ tx })
}}
onPermitDispatched={(permit) => {
props.onSubmitted?.()
Expand Down
96 changes: 51 additions & 45 deletions src/sections/transaction/ReviewTransaction.utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import { isAnyParachain, Maybe } from "utils/helpers"
import { createSubscanLink } from "utils/formatting"
import { QUERY_KEYS } from "utils/queryKeys"
import { useIsTestnet } from "api/provider"
import { tags } from "@galacticcouncil/xcm-cfg"

const EVM_PERMIT_BLOCKTIME = 20_000

Expand Down Expand Up @@ -177,27 +178,23 @@ export const useSendEvmTransactionMutation = (
{
evmTx: TransactionResponse
tx?: SubmittableExtrinsic<"promise">
xcallMeta?: Record<string, string>
}
> = {},
xcallMeta?: Record<string, string>,
) => {
const [txState, setTxState] = useState<ExtrinsicStatus["type"] | null>(null)
const [txHash, setTxHash] = useState<string>("")
const [txData, setTxData] = useState<string>()
const [xcallMeta, setCallMeta] = useState<Record<string, string> | undefined>(
undefined,
)

const { account } = useEvmAccount()
const isTestnet = useIsTestnet()

const sendTx = useMutation(async ({ evmTx, xcallMeta }) => {
const sendTx = useMutation(async ({ evmTx }) => {
return await new Promise(async (resolve, reject) => {
try {
setTxState("Broadcast")
setTxHash(evmTx?.hash ?? "")
setTxData(evmTx?.data)
setCallMeta(xcallMeta)
const receipt = await evmTx.wait()
setTxState("InBlock")

Expand All @@ -208,9 +205,12 @@ export const useSendEvmTransactionMutation = (
})
}, options)

const isSnowBridge = xcallMeta?.tags === tags.Tag.Snowbridge
const chain = account?.chainId ? getEvmChainById(account.chainId) : null
const txLink =
txHash && chain ? getEvmTxLink(txHash, txData, chain.key, isTestnet) : ""
txHash && chain
? getEvmTxLink(txHash, txData, chain.key, isTestnet, isSnowBridge)
: ""

const isApproveTx = txData?.startsWith("0x095ea7b3")

Expand All @@ -226,11 +226,10 @@ export const useSendEvmTransactionMutation = (
txState,
txLink,
txHash,
bridge: isApproveTx ? undefined : bridge,
bridge: isApproveTx || isSnowBridge ? undefined : bridge,
reset: () => {
setTxState(null)
setTxHash("")
setCallMeta(undefined)
sendTx.reset()
},
}
Expand Down Expand Up @@ -301,21 +300,19 @@ export const useSendDispatchPermit = (
unknown,
{
permit: PermitResult
xcallMeta?: Record<string, string>
}
> = {},
xcallMeta?: Record<string, string>,
) => {
const { api } = useRpcProvider()
const { wallet } = useWallet()
const queryClient = useQueryClient()
const [txState, setTxState] = useState<ExtrinsicStatus["type"] | null>(null)
const [txHash, setTxHash] = useState<string>("")
const [xcallMeta, setCallMeta] = useState<Record<string, string> | undefined>(
undefined,
)

const isMounted = useMountedState()

const sendTx = useMutation(async ({ permit, xcallMeta }) => {
const sendTx = useMutation(async ({ permit }) => {
return await new Promise(async (resolve, reject) => {
try {
const extrinsic = api.tx.multiTransactionPayment.dispatchPermit(
Expand All @@ -337,7 +334,6 @@ export const useSendDispatchPermit = (
if (isMounted()) {
setTxHash(result.txHash.toHex())
setTxState(result.status.type)
setCallMeta(xcallMeta)
}

const account = new H160(permit.message.from).toAccount()
Expand Down Expand Up @@ -392,6 +388,8 @@ export const useSendDispatchPermit = (
})
}, options)

const isSnowBridge = xcallMeta?.tags === tags.Tag.Snowbridge

const destChain = xcallMeta?.dstChain
? chainsMap.get(xcallMeta.dstChain)
: undefined
Expand All @@ -403,7 +401,8 @@ export const useSendDispatchPermit = (
? createSubscanLink("extrinsic", txHash, srcChain.key)
: undefined

const bridge = destChain?.isEvmChain() ? "substrate" : undefined
const bridge =
destChain?.isEvmChain() && !isSnowBridge ? "substrate" : undefined

return {
...sendTx,
Expand All @@ -425,19 +424,16 @@ export const useSendTransactionMutation = (
unknown,
{
tx: SubmittableExtrinsic<"promise">
xcallMeta?: Record<string, string>
}
> = {},
xcallMeta?: Record<string, string>,
) => {
const { api } = useRpcProvider()
const isMounted = useMountedState()
const [txState, setTxState] = useState<ExtrinsicStatus["type"] | null>(null)
const [txHash, setTxHash] = useState<string>("")
const [xcallMeta, setCallMeta] = useState<Record<string, string> | undefined>(
undefined,
)

const sendTx = useMutation(async ({ tx, xcallMeta }) => {
const sendTx = useMutation(async ({ tx }) => {
return await new Promise(async (resolve, reject) => {
try {
const unsubscribe = await tx.send(async (result) => {
Expand All @@ -446,7 +442,6 @@ export const useSendTransactionMutation = (
if (isMounted()) {
setTxHash(result.txHash.toHex())
setTxState(result.status.type)
setCallMeta(xcallMeta)
}

const externalChain =
Expand Down Expand Up @@ -477,6 +472,8 @@ export const useSendTransactionMutation = (
})
}, options)

const isSnowBridge = xcallMeta?.tags === tags.Tag.Snowbridge

const destChain = xcallMeta?.dstChain
? chainsMap.get(xcallMeta.dstChain)
: undefined
Expand All @@ -488,7 +485,8 @@ export const useSendTransactionMutation = (
? createSubscanLink("extrinsic", txHash, srcChain.key)
: undefined

const bridge = destChain?.isEvmChain() ? "substrate" : undefined
const bridge =
destChain?.isEvmChain() && !isSnowBridge ? "substrate" : undefined

return {
...sendTx,
Expand All @@ -499,7 +497,6 @@ export const useSendTransactionMutation = (
reset: () => {
setTxState(null)
setTxHash("")
setCallMeta(undefined)
sendTx.reset()
},
}
Expand Down Expand Up @@ -620,39 +617,48 @@ const useStoreExternalAssetsOnSign = () => {
)
}

export const useSendTx = () => {
export const useSendTx = (xcallMeta?: Record<string, string>) => {
const [txType, setTxType] = useState<"default" | "evm" | "permit" | null>(
null,
)

const boundReferralToast = useBoundReferralToast()
const storeExternalAssetsOnSign = useStoreExternalAssetsOnSign()

const sendTx = useSendTransactionMutation({
onMutate: ({ tx }) => {
boundReferralToast.onLoading(tx)
storeExternalAssetsOnSign(getAssetIdsFromTx(tx))
setTxType("default")
},
onSuccess: boundReferralToast.onSuccess,
})

const sendEvmTx = useSendEvmTransactionMutation({
onMutate: ({ tx }) => {
if (tx) {
const sendTx = useSendTransactionMutation(
{
onMutate: ({ tx }) => {
boundReferralToast.onLoading(tx)
storeExternalAssetsOnSign(getAssetIdsFromTx(tx))
}
setTxType("evm")
setTxType("default")
},
onSuccess: boundReferralToast.onSuccess,
},
onSuccess: boundReferralToast.onSuccess,
})
xcallMeta,
)

const sendPermitTx = useSendDispatchPermit({
onMutate: () => {
setTxType("permit")
const sendEvmTx = useSendEvmTransactionMutation(
{
onMutate: ({ tx }) => {
if (tx) {
boundReferralToast.onLoading(tx)
storeExternalAssetsOnSign(getAssetIdsFromTx(tx))
}
setTxType("evm")
},
onSuccess: boundReferralToast.onSuccess,
},
})
xcallMeta,
)

const sendPermitTx = useSendDispatchPermit(
{
onMutate: () => {
setTxType("permit")
},
},
xcallMeta,
)

const activeMutation =
txType === "default" ? sendTx : txType === "evm" ? sendEvmTx : sendPermitTx
Expand Down
19 changes: 4 additions & 15 deletions src/sections/transaction/ReviewTransactionForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,12 @@ type TxProps = Omit<Transaction, "id" | "tx" | "xcall"> & {

type Props = TxProps & {
onCancel: () => void
onPermitDispatched: ({
permit,
xcallMeta,
}: {
permit: PermitResult
xcallMeta?: Record<string, string>
}) => void
onPermitDispatched: ({ permit }: { permit: PermitResult }) => void
onEvmSigned: (data: {
evmTx: TransactionResponse
tx: SubmittableExtrinsic<"promise">
xcallMeta?: Record<string, string>
}) => void
onSigned: (
signed: SubmittableExtrinsic<"promise">,
xcallMeta?: Record<string, string>,
) => void
onSigned: (signed: SubmittableExtrinsic<"promise">) => void
onSignError?: (error: unknown) => void
}

Expand Down Expand Up @@ -141,15 +131,14 @@ export const ReviewTransactionForm: FC<Props> = (props) => {
const permit = await wallet.signer.getPermit(txData, nonce)
return props.onPermitDispatched({
permit,
xcallMeta: props.xcallMeta,
})
}

const evmTx = await wallet.signer.sendDispatch(
txData,
props.xcallMeta?.srcChain,
)
return props.onEvmSigned({ evmTx, tx, xcallMeta: props.xcallMeta })
return props.onEvmSigned({ evmTx, tx })
}

const srcChain = props?.xcallMeta?.srcChain
Expand All @@ -171,7 +160,7 @@ export const ReviewTransactionForm: FC<Props> = (props) => {
withSignedTransaction: true,
})

return props.onSigned(signature, props.xcallMeta)
return props.onSigned(signature)
} catch (error) {
props.onSignError?.(error)
}
Expand Down
3 changes: 2 additions & 1 deletion src/utils/evm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export function getEvmTxLink(
txData: string | undefined,
chainKey = "hydration",
isTestnet = false,
isSnowbridge: boolean,
) {
const chain = chainsMap.get(chainKey)

Expand All @@ -86,7 +87,7 @@ export function getEvmTxLink(
if (chain.isEvmChain()) {
const isApproveTx = txData?.startsWith("0x095ea7b3")

return isApproveTx
return isApproveTx || isSnowbridge
? `https://etherscan.io/tx/${txHash}`
: `https://wormholescan.io/#/tx/${txHash}`
}
Expand Down
Loading