Commit 43931dd6 authored by Zach Pomerantz's avatar Zach Pomerantz Committed by GitHub

feat: chain-specific ttls (#3228)

parent efa3d552
import { Trans } from '@lingui/macro' import { Trans } from '@lingui/macro'
import { useAtom } from 'jotai' import { useDefaultTransactionTtl, useTransactionTtl } from 'lib/hooks/useTransactionDeadline'
import { TRANSACTION_TTL_DEFAULT, transactionTtlAtom } from 'lib/state/settings'
import styled, { ThemedText } from 'lib/theme' import styled, { ThemedText } from 'lib/theme'
import { useRef } from 'react' import { useRef } from 'react'
...@@ -11,14 +10,14 @@ import { Label } from './components' ...@@ -11,14 +10,14 @@ import { Label } from './components'
const tooltip = <Trans>Your transaction will revert if it has been pending for longer than this period of time.</Trans> const tooltip = <Trans>Your transaction will revert if it has been pending for longer than this period of time.</Trans>
const placeholder = TRANSACTION_TTL_DEFAULT.toString()
const Input = styled(Row)` const Input = styled(Row)`
${inputCss} ${inputCss}
` `
export default function TransactionTtlInput() { export default function TransactionTtlInput() {
const [transactionTtl, setTransactionTtl] = useAtom(transactionTtlAtom) const [ttl, setTtl] = useTransactionTtl()
const defaultTtl = useDefaultTransactionTtl()
const placeholder = defaultTtl.toString()
const input = useRef<HTMLInputElement>(null) const input = useRef<HTMLInputElement>(null)
return ( return (
<Column gap={0.75}> <Column gap={0.75}>
...@@ -27,9 +26,9 @@ export default function TransactionTtlInput() { ...@@ -27,9 +26,9 @@ export default function TransactionTtlInput() {
<Input justify="start" onClick={() => input.current?.focus()}> <Input justify="start" onClick={() => input.current?.focus()}>
<IntegerInput <IntegerInput
placeholder={placeholder} placeholder={placeholder}
value={transactionTtl?.toString() ?? ''} value={ttl?.toString() ?? ''}
onChange={(value) => setTransactionTtl(value ? parseFloat(value) : 0)} onChange={(value) => setTtl(value ? parseFloat(value) : 0)}
size={Math.max(transactionTtl?.toString().length || 0, placeholder.length)} size={Math.max(ttl?.toString().length || 0, placeholder.length)}
ref={input} ref={input}
/> />
<Trans>minutes</Trans> <Trans>minutes</Trans>
......
import { BigNumber } from '@ethersproject/bignumber' import { BigNumber } from '@ethersproject/bignumber'
import { L2_CHAIN_IDS } from 'constants/chains' import { L2_CHAIN_IDS } from 'constants/chains'
import { L2_DEADLINE_FROM_NOW } from 'constants/misc' import { DEFAULT_DEADLINE_FROM_NOW, L2_DEADLINE_FROM_NOW } from 'constants/misc'
import useCurrentBlockTimestamp from 'hooks/useCurrentBlockTimestamp' import useCurrentBlockTimestamp from 'hooks/useCurrentBlockTimestamp'
import { useAtomValue } from 'jotai/utils' import { useAtom } from 'jotai'
import { TRANSACTION_TTL_DEFAULT, transactionTtlAtom } from 'lib/state/settings' import { transactionTtlAtom } from 'lib/state/settings'
import { useMemo } from 'react' import { useMemo } from 'react'
import useActiveWeb3React from './useActiveWeb3React' import useActiveWeb3React from './useActiveWeb3React'
/** Returns the default transaction TTL for the chain, in minutes. */
export function useDefaultTransactionTtl(): number {
const { chainId } = useActiveWeb3React()
if (chainId && L2_CHAIN_IDS.includes(chainId)) return L2_DEADLINE_FROM_NOW / 60
return DEFAULT_DEADLINE_FROM_NOW / 60
}
/** Returns the user-inputted transaction TTL, in minutes. */
export function useTransactionTtl(): [number | undefined, (ttl?: number) => void] {
return useAtom(transactionTtlAtom)
}
// combines the block timestamp with the user setting to give the deadline that should be used for any submitted transaction // combines the block timestamp with the user setting to give the deadline that should be used for any submitted transaction
export default function useTransactionDeadline(): BigNumber | undefined { export default function useTransactionDeadline(): BigNumber | undefined {
const { chainId } = useActiveWeb3React() const [ttl] = useTransactionTtl()
const userDeadline: number = useAtomValue(transactionTtlAtom) || TRANSACTION_TTL_DEFAULT const defaultTtl = useDefaultTransactionTtl()
const blockTimestamp = useCurrentBlockTimestamp() const blockTimestamp = useCurrentBlockTimestamp()
return useMemo(() => { return useMemo(() => {
if (blockTimestamp && chainId && L2_CHAIN_IDS.includes(chainId)) return blockTimestamp.add(L2_DEADLINE_FROM_NOW) if (!blockTimestamp) return undefined
//@TODO(ianlapham): update this to be stored as seconds return blockTimestamp.add((ttl || defaultTtl) /* in seconds */ * 60)
if (blockTimestamp && userDeadline) return blockTimestamp.add(userDeadline * 60) // adjust for seconds }, [blockTimestamp, defaultTtl, ttl])
return undefined
}, [blockTimestamp, chainId, userDeadline])
} }
...@@ -6,13 +6,9 @@ import { pickAtom, setTogglable } from './atoms' ...@@ -6,13 +6,9 @@ import { pickAtom, setTogglable } from './atoms'
export const MAX_VALID_SLIPPAGE = new Percent(1, 2) export const MAX_VALID_SLIPPAGE = new Percent(1, 2)
export const MIN_HIGH_SLIPPAGE = new Percent(1, 100) export const MIN_HIGH_SLIPPAGE = new Percent(1, 100)
// transaction deadline in minutes, needs to be adjusted to seconds before use
// @TODO(ianlapham): update this to be stored as seconds
export const TRANSACTION_TTL_DEFAULT = 30
interface Settings { interface Settings {
maxSlippage: Percent | 'auto' // auto will cause slippage to resort to default calculation maxSlippage: Percent | 'auto' // auto will cause slippage to resort to default calculation
transactionTtl: number | undefined transactionTtl: number | undefined // transaction ttl in minutes
integratorFee: number | undefined integratorFee: number | undefined
mockTogglable: boolean mockTogglable: boolean
clientSideRouter: boolean // whether to use the client-side router or query the remote API clientSideRouter: boolean // whether to use the client-side router or query the remote API
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment