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(synapse-interface): bridge gas handler #2437

Merged
merged 151 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from 141 commits
Commits
Show all changes
151 commits
Select commit Hold shift + click to select a range
86d769a
Initial gas data slice state
bigboydiamonds Apr 2, 2024
57866b3
Async thunk fetchGasData
bigboydiamonds Apr 2, 2024
b2a2ec0
Add gas data reducers
bigboydiamonds Apr 2, 2024
2eeb3ab
useFetchGasDataOnInterval added to Bridge Listener
bigboydiamonds Apr 2, 2024
c0db7cf
Explicitly declare fetching gas data in gwei
bigboydiamonds Apr 2, 2024
365c017
Calculate max bridgeable amount
bigboydiamonds Apr 2, 2024
c75c77f
On max balance factoring in gas cost
bigboydiamonds Apr 2, 2024
c16b276
Add comment
bigboydiamonds Apr 2, 2024
0bf73d3
Differentiate max balance vs max bridgeable balance
bigboydiamonds Apr 3, 2024
e821ef8
Add error toaster for when max balance less than bridge fee
bigboydiamonds Apr 3, 2024
9f09239
refactor; calculateGasFeeInGwei()
bigboydiamonds Apr 3, 2024
6d12c41
Move to util
bigboydiamonds Apr 3, 2024
9758112
Fix lint error
bigboydiamonds Apr 3, 2024
1496842
Fetch gas when fromChainId on bridge card changes
bigboydiamonds Apr 3, 2024
c01109f
Use exact token balance when calculate max bridgeable amount
bigboydiamonds Apr 3, 2024
eee9c50
Conditions for showing max button
bigboydiamonds Apr 4, 2024
8092671
Merge branch 'master' into fe/bridge-gas
bigboydiamonds Apr 4, 2024
93d2047
showMaxOption to determine display
bigboydiamonds Apr 4, 2024
c5e222c
Disable max button if gas fees more than gas balance
bigboydiamonds Apr 4, 2024
f580a80
Clean jsx
bigboydiamonds Apr 4, 2024
561a62b
AvailableBalance component to track subscript detail in bridge origin…
bigboydiamonds Apr 4, 2024
b4c91bf
Return raw and formatted gas cost in calculateGasCost
bigboydiamonds Apr 5, 2024
85c4a7f
Update comments
bigboydiamonds Apr 5, 2024
cd7ae73
Fix imports based on name cange
bigboydiamonds Apr 5, 2024
a109662
Use parsed
bigboydiamonds Apr 5, 2024
e9266ba
calculateMaxBridgeableGas
bigboydiamonds Apr 5, 2024
79d540c
Clean
bigboydiamonds Apr 5, 2024
28a8811
refactor: clean InputContainer
bigboydiamonds Apr 5, 2024
c6754d7
Replace onMaxBalance() with onMaxBridgeableBalance()`
bigboydiamonds Apr 5, 2024
8867be6
Allow undefined object when destructuring
bigboydiamonds Apr 5, 2024
1d00de2
Display when token balance is trace balance
bigboydiamonds Apr 5, 2024
a9d3446
Typing
bigboydiamonds Apr 5, 2024
1f7ec54
showGasReserved
bigboydiamonds Apr 5, 2024
8d9c491
Update avail balance text and color when gas cost greater than balance
bigboydiamonds Apr 8, 2024
a8a5d78
Detect when input value is less than required estimated gas
bigboydiamonds Apr 8, 2024
4d3a0bd
onAvailableBalance applied
bigboydiamonds Apr 8, 2024
104866e
Make opacity greater when Max button disabled
bigboydiamonds Apr 8, 2024
9ab631d
Fix calculations for when gas input is enough to cover gas
bigboydiamonds Apr 8, 2024
76037f4
Clean
bigboydiamonds Apr 8, 2024
3427616
Add hover tooltip for warning gas states
bigboydiamonds Apr 8, 2024
44edc57
Use tooltip to describe gas status
bigboydiamonds Apr 8, 2024
3052e57
Update tooltip text
bigboydiamonds Apr 8, 2024
86a3cfb
Add buffer to calculate gas cost, use 1.5x
bigboydiamonds Apr 9, 2024
be6bcda
-mMerge
bigboydiamonds Apr 11, 2024
b0e5e52
AvailableBalance component to replace label in AmountInput
bigboydiamonds Apr 11, 2024
06e1044
Display trace balances in AvailableBalance
bigboydiamonds Apr 11, 2024
52d65ce
Fix naming
bigboydiamonds Apr 11, 2024
d7b073b
Add HoverTooltip to Available Balance
bigboydiamonds Apr 11, 2024
e34a363
Add conditions for displaying gas error based on input
bigboydiamonds Apr 11, 2024
677a8fc
Use full parsed balance when making comparisons for gas checks
bigboydiamonds Apr 11, 2024
6864b5b
Show gas reserved in Available Balance
bigboydiamonds Apr 11, 2024
2d48a9d
hasOnlyZeroes to return true if string contains only zeroes and periods
bigboydiamonds Apr 11, 2024
d0e7116
Display estimated gas cost in tool tip
bigboydiamonds Apr 11, 2024
e813eea
Remove Available Balance elements from AmountInput, migrated to Avail…
bigboydiamonds Apr 11, 2024
d8fabef
Display trace amount for input when displaying how much is reserved f…
bigboydiamonds Apr 11, 2024
89a386f
Fetch estimated gas limit based on real bridge quote using max gas ba…
bigboydiamonds Apr 11, 2024
f3bd4f1
Fetching accurate gasLimit
bigboydiamonds Apr 11, 2024
7d75555
Fix Trace balance
bigboydiamonds Apr 11, 2024
ec90bd0
Set gasLimit to 0 if not valid bridge data avail
bigboydiamonds Apr 11, 2024
a2b8f64
...
bigboydiamonds Apr 11, 2024
509f66a
Fix when gas covered msg appears
bigboydiamonds Apr 11, 2024
ef9d11c
Available Balance
bigboydiamonds Apr 11, 2024
8b23f18
Clean available balance flow
bigboydiamonds Apr 11, 2024
cb16d7a
useGasEstimator hook to encapsulate logic to clean InputContainer
bigboydiamonds Apr 12, 2024
990afcc
Merge branch 'master' of https://github.com/synapsecns/sanguine into …
bigboydiamonds Apr 12, 2024
4db337f
Merge branch 'master' into fe/bridge-gas
bigboydiamonds Apr 17, 2024
7a54739
Merge branch 'master' into fe/bridge-gas
bigboydiamonds Apr 17, 2024
914d237
Fix prop issue
bigboydiamonds Apr 17, 2024
fb72b99
Remove duplicate HoverTooltip
bigboydiamonds Apr 17, 2024
8377d7a
Move HoverTooltip to shared component folder
bigboydiamonds Apr 17, 2024
3954eee
Update gas fetch for wagmi v2, up limit to 1.7
bigboydiamonds Apr 17, 2024
2f618cc
Estimated gas bridge quote fetched on load
bigboydiamonds Apr 18, 2024
e25cd3e
Update var naming
bigboydiamonds Apr 18, 2024
340496e
Improve Available Balance flow
bigboydiamonds Apr 18, 2024
a6517e7
Display estimated bridgeable balance on hover
bigboydiamonds Apr 18, 2024
5529577
Show gas estimate when estimated gas cost available
bigboydiamonds Apr 18, 2024
a0e333c
Do not show negative bridgable balance
bigboydiamonds Apr 18, 2024
c234f50
Separate out bridge gas limit calculations into smaller functions
bigboydiamonds Apr 19, 2024
2db63d1
Wrap async functions with try catch
bigboydiamonds Apr 19, 2024
8f30fd9
Implement updated gas estimation flow
bigboydiamonds Apr 19, 2024
f5a85f0
Move gas estimate logic to useGasEstimator
bigboydiamonds Apr 19, 2024
135e4b0
Move async sdk fetch functions to useGasEstimator file
bigboydiamonds Apr 19, 2024
fde785b
Remove unused vars
bigboydiamonds Apr 19, 2024
f52c0bd
Shorten est gas cost
bigboydiamonds Apr 19, 2024
9dae803
Improve code legibility in AvailableBalance
bigboydiamonds Apr 19, 2024
6e30bb7
Assign conditions to vars to reduce clutter
bigboydiamonds Apr 19, 2024
c517245
Reset gas price data when fromChainId changes
bigboydiamonds Apr 19, 2024
ccb4e5c
Reset fetched gas limit when fromChainId changes
bigboydiamonds Apr 19, 2024
a6b6fa2
Clean
bigboydiamonds Apr 19, 2024
02366ae
Fix old import
bigboydiamonds Apr 19, 2024
44c4915
Merge branch 'master' into fe/bridge-gas
bigboydiamonds Apr 19, 2024
e11dc66
Merge branch 'master' into fe/bridge-gas
bigboydiamonds Apr 29, 2024
225efad
Fix max button placement
bigboydiamonds Apr 30, 2024
ab5503d
Add loading state for useGasEstimator
bigboydiamonds Apr 30, 2024
c6ab2a9
Remove available balance states for gas est
bigboydiamonds Apr 30, 2024
a3d31fa
Remove unused props
bigboydiamonds Apr 30, 2024
fa4109f
Clean gas token detection in PortfolioTokenAsset
bigboydiamonds Apr 30, 2024
8da8876
Simplify onMaxBalance()
bigboydiamonds Apr 30, 2024
13e54af
Clean constants in InputContainer
bigboydiamonds Apr 30, 2024
4987688
Clean token detail destructuring
bigboydiamonds Apr 30, 2024
7d68957
Include gas estimate when updating input from Portfolio token selection
bigboydiamonds Apr 30, 2024
83a87f8
Fire error toaster if gas fees exceed balance
bigboydiamonds Apr 30, 2024
eafb8ec
Render error toaster when firing onMaxBalance callback instead of a s…
bigboydiamonds Apr 30, 2024
dd76066
Clean useGasEstimator
bigboydiamonds Apr 30, 2024
651b5ea
Ensure available balance does not show if wallet not connected, set d…
bigboydiamonds Apr 30, 2024
3110540
Replicate onMax behavior in Portfolio
bigboydiamonds Apr 30, 2024
ecfe813
Use fetched gas
bigboydiamonds Apr 30, 2024
6b6a3f0
Improve error handling in gas estimator
bigboydiamonds Apr 30, 2024
a59a2a8
...
bigboydiamonds Apr 30, 2024
31e9e13
Remove tooltip on AvailableBalance
bigboydiamonds May 1, 2024
5040a59
Remove balance prefill from Portfolio Token selection
bigboydiamonds May 1, 2024
6303bb3
Update available balance in input based on max bridgeable gas toke
bigboydiamonds May 1, 2024
870bccb
Destructure estimateGasLimit callback from useGasEstimator to retrigg…
bigboydiamonds May 1, 2024
db53502
Refetch gas ata in estimateGasLimit callback
bigboydiamonds May 1, 2024
26e4c8f
Display total vs bridgeable balance on hover in Portfolio gas token w…
bigboydiamonds May 1, 2024
e1b87cc
Update balance shown on hover
bigboydiamonds May 1, 2024
7b33d3b
Clean
bigboydiamonds May 1, 2024
8fe478b
Simplify useFetchGasDataOnInterval
bigboydiamonds May 1, 2024
e0ebedc
Prevent unnecessary fetches, fix chain update old gas issue
bigboydiamonds May 1, 2024
095d985
Fetch estimated bridgeable max gas amount onClick token max input, fi…
bigboydiamonds May 2, 2024
723ae59
Clean
bigboydiamonds May 2, 2024
2972908
onMaxBalance will use default balanceif fetched bridgeable balance es…
bigboydiamonds May 2, 2024
1383bfc
Show dust gas amounts on hover in Portfolio
bigboydiamonds May 2, 2024
04a430b
Clean
bigboydiamonds May 2, 2024
cd90fef
Available balance including gas estimates moved to within Input to sy…
bigboydiamonds May 2, 2024
db75b99
Move gasLimit to global store to share data between Bridge components
bigboydiamonds May 2, 2024
7a46279
Display max bridgeable balance on hover in Portfolio, add onMaxBalanc…
bigboydiamonds May 2, 2024
9bb0901
Portfolio and Input available balance onClick behave the same
bigboydiamonds May 2, 2024
96815b6
Fetch gas prices to provide most accurate gas cost
bigboydiamonds May 2, 2024
ee8d3ac
Remove hover on AvailableBalance
bigboydiamonds May 3, 2024
3d901d2
Fix Token Selector width
bigboydiamonds May 3, 2024
e5e7a1d
Remove onClick Portfolio Token Balance, allow Token selection from Po…
bigboydiamonds May 3, 2024
b220371
Cursor default on Portfolio Token Balance
bigboydiamonds May 3, 2024
a83aceb
Show available/max buttons when bridge valid selections
bigboydiamonds May 6, 2024
cc9f96d
Swap input to have available balance
bigboydiamonds May 6, 2024
bc5eca7
Clean
bigboydiamonds May 6, 2024
63f2849
Basic MaxButton onClick and hidden when loading gas data
bigboydiamonds May 7, 2024
9f20c05
Show MaxButton when input is not full balance or bridgeable balance
bigboydiamonds May 7, 2024
429a023
Implement Max button and placement in Bridge/Swap
bigboydiamonds May 7, 2024
1d36a72
Hide MaxButton until connected
bigboydiamonds May 7, 2024
a350cf9
Hide MaxButton conditions
bigboydiamonds May 7, 2024
973fc67
Show Max when all input selections are made
bigboydiamonds May 7, 2024
962f372
Remove click states for AvailableBalance on Bridge/Swap
bigboydiamonds May 7, 2024
744c42e
Style Input / Max
bigboydiamonds May 7, 2024
11f31ee
Max mobile size
bigboydiamonds May 8, 2024
f274044
Fe/format amount (#2598)
bigboydiamonds May 8, 2024
2f6b360
Clean imports
bigboydiamonds May 8, 2024
e8ca7e6
Swap Max spacing
bigboydiamonds May 8, 2024
38669bd
Improve html element composition
bigboydiamonds May 8, 2024
6b70424
Merge branch 'master' into fe/bridge-gas
bigboydiamonds May 9, 2024
0cbe497
Prevent layout shift in Swap UI
bigboydiamonds May 9, 2024
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
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
import React, { useState } from 'react'

export const HoverTooltip = ({ children, hoverContent }) => {
export const HoverTooltip = ({
children,
hoverContent,
isActive = true,
}: {
children: React.ReactNode
hoverContent: React.ReactNode
isActive?: boolean
}) => {
const [showTooltip, setShowTooltip] = useState(false)

const activateTooltip = () => setShowTooltip(true)
const hideTooltip = () => setShowTooltip(false)

return (
<div
onMouseEnter={activateTooltip}
onMouseLeave={hideTooltip}
className="relative"
>
{children}
<Tooltip isHovered={showTooltip}>{hoverContent}</Tooltip>
</div>
)
if (!isActive) {
return <div>{children}</div>
} else {
return (
<div
onMouseEnter={activateTooltip}
onMouseLeave={hideTooltip}
className="relative w-fit"
>
{children}
{hoverContent ? (
<Tooltip isHovered={showTooltip}>{hoverContent}</Tooltip>
) : null}
</div>
)
}
}

const Tooltip = ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react'
import Image from 'next/image'
import { Token } from '@/utils/types'
import { getParsedBalance } from '@/utils/getParsedBalance'
import { HoverTooltip } from './HoverTooltip'
import { HoverTooltip } from '../../HoverTooltip'
import GasIcon from '@/components/icons/GasIcon'

export const GasTokenAsset = ({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import React, { useCallback } from 'react'
import _ from 'lodash'
import { zeroAddress } from 'viem'
import { isNumber } from 'lodash'
import Image from 'next/image'
import { useAppDispatch } from '@/store/hooks'
import {
setFromChainId,
setFromToken,
updateFromValue,
} from '@/slices/bridge/reducer'
import { setFromChainId, setFromToken } from '@/slices/bridge/reducer'
import { Token } from '@/utils/types'
import { inputRef } from '../../StateManagedBridge/InputContainer'
import Image from 'next/image'
import { useBridgeState } from '@/slices/bridge/hooks'
import { PortfolioAssetActionButton } from './PortfolioAssetActionButton'
import { trimTrailingZeroesAfterDecimal } from '@/utils/trimTrailingZeroesAfterDecimal'
import { zeroAddress } from 'viem'
import GasIcon from '@/components/icons/GasIcon'
import { HoverTooltip } from './HoverTooltip'
import { HoverTooltip } from '@/components/HoverTooltip'
import { getParsedBalance } from '@/utils/getParsedBalance'
import { useGasEstimator } from '@/utils/hooks/useGasEstimator'
import GasIcon from '@/components/icons/GasIcon'
import { trimTrailingZeroesAfterDecimal } from '@/utils/trimTrailingZeroesAfterDecimal'

const handleFocusOnBridgeInput = () => {
inputRef.current?.focus()
Expand All @@ -37,30 +34,26 @@ export const PortfolioTokenAsset = ({
const dispatch = useAppDispatch()
const { fromChainId, fromToken } = useBridgeState()
const { icon, symbol, decimals, addresses } = token

const tokenDecimals = _.isNumber(decimals)
const tokenAddress = addresses[portfolioChainId]
const tokenDecimals = isNumber(decimals)
? decimals
: decimals[portfolioChainId]

const parsedBalance = getParsedBalance(balance, tokenDecimals, 3)
const parsedBalance = getParsedBalance(balance, tokenDecimals, 4)
const parsedBalanceLong = getParsedBalance(balance, tokenDecimals, 8)

const isDisabled = false
const isTokenSelected =
fromToken === token && fromChainId === portfolioChainId
const isPortfolioChainSelected = fromChainId === portfolioChainId
const isTokenSelected = isPortfolioChainSelected && fromToken === token
const isGasToken = tokenAddress === zeroAddress

const handleFromSelectionCallback = useCallback(() => {
const { maxBridgeableGas } = useGasEstimator()

const handleFromSelectionCallback = useCallback(async () => {
dispatch(setFromChainId(portfolioChainId))
dispatch(setFromToken(token))
handleFocusOnBridgeInput()
dispatch(
updateFromValue(
trimTrailingZeroesAfterDecimal(getParsedBalance(balance, tokenDecimals))
)
)
}, [token, balance, portfolioChainId])

const isBridgeableGasToken = addresses[portfolioChainId] === zeroAddress
}, [token, portfolioChainId])

return (
<div
Expand All @@ -70,14 +63,7 @@ export const PortfolioTokenAsset = ({
${isTokenSelected ? 'bg-tint border-surface' : 'border-transparent'}
`}
>
<div
onClick={handleFromSelectionCallback}
className={`
flex items-center gap-2
pl-2 pr-4 py-2 cursor-pointer rounded
hover:bg-surface active:opacity-70
`}
>
<div className="flex items-center gap-2 py-2 pl-2 pr-4 rounded cursor-default">
<Image
loading="lazy"
alt={`${symbol} img`}
Expand All @@ -86,23 +72,31 @@ export const PortfolioTokenAsset = ({
/>
<HoverTooltip
hoverContent={
<div className="whitespace-nowrap">
{parsedBalanceLong} {symbol}
</div>
isPortfolioChainSelected && isGasToken && maxBridgeableGas ? (
<div className="whitespace-nowrap">
Available:{' '}
{trimTrailingZeroesAfterDecimal(maxBridgeableGas.toFixed(8))}{' '}
{symbol}
</div>
) : (
<div className="whitespace-nowrap">
{parsedBalanceLong} {symbol}
</div>
)
}
>
<div>
{parsedBalance} {symbol}
</div>
</HoverTooltip>

{isBridgeableGasToken ? (
{isGasToken && (
<HoverTooltip
hoverContent={<div className="whitespace-nowrap">Gas token</div>}
>
<GasIcon className="pt-0.5 m-auto fill-secondary" />
</HoverTooltip>
) : null}
)}
</div>
<PortfolioAssetActionButton
selectCallback={handleFromSelectionCallback}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Image from 'next/image'
import { TokenAndBalance } from '@/utils/actions/fetchPortfolioBalances'
import { HoverTooltip } from './HoverTooltip'
import { HoverTooltip } from '../../HoverTooltip'

export const PortfolioTokenVisualizer = ({
portfolioTokens,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import React from 'react'
import { joinClassNames } from '@/utils/joinClassNames'
import { HoverTooltip } from '../HoverTooltip'
import { Token } from '@/utils/types'
import { trimTrailingZeroesAfterDecimal } from '@/utils/trimTrailingZeroesAfterDecimal'

export const AvailableBalance = ({
balance,
maxBridgeableBalance,
isGasToken,
isGasEstimateLoading,
isDisabled,
onMaxBalance,
}: {
balance?: string
maxBridgeableBalance?: number
gasCost?: string
isGasToken: boolean
isGasEstimateLoading: boolean
isDisabled: boolean
onMaxBalance?: () => void
}) => {
const labelClassName = joinClassNames({
space: 'block',
textColor: 'text-xxs md:text-xs',
animation: 'transition-all duration-150 transform-gpu',
hover: 'hover:opacity-70 cursor-pointer',
})

if (isDisabled) {
return null
} else if (isGasToken && isGasEstimateLoading) {
return (
<label className={labelClassName} htmlFor="inputRow">
<span className="animate-pulse text-zinc-500 dark:text-zinc-400">
calculating gas...
</span>
</label>
)
} else {
return (
<label
onClick={onMaxBalance}
className={labelClassName}
htmlFor="inputRow"
>
<span className="text-zinc-500 dark:text-zinc-400">Available: </span>
{maxBridgeableBalance?.toFixed(4) ?? balance ?? '0.0'}
</label>
)
}
}
Loading
Loading