-
Notifications
You must be signed in to change notification settings - Fork 213
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: Add new routes components #2279
Open
chrstph-dvx
wants to merge
15
commits into
master
Choose a base branch
from
add-new-routes-flow
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
1844746
feat: Add new routes components
chrstph-dvx b95b371
Fix modals and MoveFundsButton logic
chrstph-dvx 988c5eb
Clear route when changing networks
chrstph-dvx 8181ce1
Fix MoveFundsButton disabled state
chrstph-dvx e0b1ad2
Fix onClose in USDC modals
chrstph-dvx b18e265
Review comments
chrstph-dvx 8156b48
Address comments
chrstph-dvx 4e11398
Fix selected route logic
chrstph-dvx ba1b11a
Add batch transfer, remove fast withdrawal
chrstph-dvx 9438585
Fix selected state
chrstph-dvx c493cbe
Fix check for amount2
chrstph-dvx 2915077
Fix border for selected state
chrstph-dvx bd51add
Update design
chrstph-dvx ba27b26
Fix transfer for oft
chrstph-dvx b96cedb
Remove shadow on check svg
chrstph-dvx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
280 changes: 280 additions & 0 deletions
280
packages/arb-token-bridge-ui/src/components/TransferPanel/Routes/ArbitrumRoute.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,280 @@ | ||
import { useNetworks } from '../../../hooks/useNetworks' | ||
import { useNetworksRelationship } from '../../../hooks/useNetworksRelationship' | ||
import { constants, utils } from 'ethers' | ||
import { Route, Token } from './Route' | ||
import { useAmountBigNumber } from '../hooks/useAmountBigNumber' | ||
import { | ||
UseGasSummaryResult, | ||
useGasSummary | ||
} from '../../../hooks/TransferPanel/useGasSummary' | ||
import { | ||
NativeCurrency, | ||
useNativeCurrency | ||
} from '../../../hooks/useNativeCurrency' | ||
import { CommonAddress } from '../../../util/CommonAddressUtils' | ||
import { | ||
getOrbitDepositDuration, | ||
getStandardDepositDuration, | ||
getWithdrawalDuration | ||
} from '../../../hooks/useTransferDuration' | ||
import { isNetwork } from '../../../util/networks' | ||
import dayjs from 'dayjs' | ||
import { useSelectedToken } from '../../../hooks/useSelectedToken' | ||
import { isTokenNativeUSDC } from '../../../util/TokenUtils' | ||
import { useRouteStore } from '../hooks/useRouteStore' | ||
import { useMemo } from 'react' | ||
import { ERC20BridgeToken } from '../../../hooks/arbTokenBridge.types' | ||
|
||
const commonUsdcToken = { | ||
decimals: 6, | ||
address: CommonAddress.Ethereum.USDC | ||
} | ||
|
||
const bridgedUsdcToken = { | ||
...commonUsdcToken, | ||
name: 'Bridged USDC', | ||
symbol: 'USDC.e', | ||
l2Address: CommonAddress.ArbitrumOne['USDC.e'] | ||
} | ||
|
||
const nativeUsdcToken = { | ||
...commonUsdcToken, | ||
name: 'USDC', | ||
symbol: 'USDC', | ||
l2Address: CommonAddress.ArbitrumOne.USDC | ||
} | ||
|
||
function getDuration({ | ||
isTestnet, | ||
sourceChainId, | ||
isTeleportMode, | ||
isWithdrawal, | ||
isOrbitChain | ||
}: { | ||
isTestnet: boolean | ||
sourceChainId: number | ||
isTeleportMode: boolean | ||
isWithdrawal: boolean | ||
isOrbitChain: boolean | ||
}) { | ||
if (isTeleportMode) { | ||
return ( | ||
getStandardDepositDuration(isTestnet) + getOrbitDepositDuration(isTestnet) | ||
) | ||
} | ||
|
||
if (isWithdrawal) { | ||
return getWithdrawalDuration({ | ||
createdAt: dayjs().valueOf(), | ||
sourceChainId: sourceChainId | ||
}) | ||
} | ||
|
||
if (isOrbitChain) { | ||
return getOrbitDepositDuration(isTestnet) | ||
} | ||
|
||
return getStandardDepositDuration(isTestnet) | ||
} | ||
|
||
function getGasCostAndToken({ | ||
childChainNativeCurrency, | ||
parentChainNativeCurrency, | ||
gasSummaryStatus, | ||
estimatedChildChainGasFees, | ||
estimatedParentChainGasFees, | ||
isDepositMode, | ||
selectedToken | ||
}: { | ||
childChainNativeCurrency: NativeCurrency | ||
parentChainNativeCurrency: NativeCurrency | ||
gasSummaryStatus: UseGasSummaryResult['status'] | ||
estimatedChildChainGasFees: UseGasSummaryResult['estimatedChildChainGasFees'] | ||
estimatedParentChainGasFees: UseGasSummaryResult['estimatedParentChainGasFees'] | ||
isDepositMode: boolean | ||
selectedToken: ERC20BridgeToken | null | ||
}): { | ||
isLoading: boolean | ||
gasCost: { gasCost: number; gasToken: Token }[] | null | ||
} { | ||
const sameNativeCurrency = | ||
childChainNativeCurrency.isCustom === parentChainNativeCurrency.isCustom | ||
const estimatedTotalGasFees = | ||
gasSummaryStatus === 'loading' || | ||
typeof estimatedChildChainGasFees == 'undefined' || | ||
typeof estimatedParentChainGasFees == 'undefined' | ||
? undefined | ||
: estimatedParentChainGasFees + estimatedChildChainGasFees | ||
|
||
const childChainNativeCurrencyWithAddress: Token = | ||
'address' in childChainNativeCurrency | ||
? childChainNativeCurrency | ||
: { ...childChainNativeCurrency, address: constants.AddressZero } | ||
|
||
const parentChainNativeCurrencyWithAddress: Token = | ||
'address' in parentChainNativeCurrency | ||
? parentChainNativeCurrency | ||
: { ...parentChainNativeCurrency, address: constants.AddressZero } | ||
|
||
if (typeof estimatedTotalGasFees === 'undefined') { | ||
return { | ||
gasCost: null, | ||
isLoading: true | ||
} | ||
} | ||
|
||
/** | ||
* Same Native Currencies between Parent and Child chains | ||
* 1. ETH/ER20 deposit: L1->L2 | ||
* 2. ETH/ERC20 withdrawal: L2->L1 | ||
* 3. ETH/ER20 deposit: L2->L3 (ETH as gas token) | ||
* 4. ETH/ERC20 withdrawal: L3 (ETH as gas token)->L2 | ||
* | ||
* x ETH | ||
*/ | ||
if (sameNativeCurrency) { | ||
return { | ||
isLoading: false, | ||
gasCost: [ | ||
{ | ||
gasCost: estimatedTotalGasFees, | ||
gasToken: childChainNativeCurrencyWithAddress | ||
} | ||
] | ||
} | ||
} | ||
|
||
/** Different Native Currencies between Parent and Child chains | ||
* | ||
* Custom gas token deposit: L2->Xai | ||
* x ETH | ||
* | ||
* ERC20 deposit: L2->Xai | ||
* x ETH and x XAI | ||
* | ||
* Custom gas token/ERC20 withdrawal: L3->L2 | ||
* only show child chain native currency | ||
* x XAI | ||
*/ | ||
if (isDepositMode) { | ||
const gasCost: { gasCost: number; gasToken: Token }[] = [ | ||
{ | ||
gasCost: estimatedParentChainGasFees!, | ||
gasToken: parentChainNativeCurrencyWithAddress | ||
} | ||
] | ||
|
||
if (selectedToken) { | ||
gasCost.push({ | ||
gasCost: estimatedChildChainGasFees!, | ||
gasToken: childChainNativeCurrencyWithAddress | ||
}) | ||
} | ||
|
||
return { | ||
gasCost, | ||
isLoading: false | ||
} | ||
} | ||
|
||
return { | ||
isLoading: false, | ||
gasCost: [ | ||
{ | ||
gasCost: estimatedChildChainGasFees!, | ||
gasToken: childChainNativeCurrencyWithAddress | ||
} | ||
] | ||
} | ||
} | ||
|
||
export function ArbitrumRoute() { | ||
const amount = useAmountBigNumber() | ||
const [networks] = useNetworks() | ||
const { | ||
childChain, | ||
isTeleportMode, | ||
childChainProvider, | ||
parentChainProvider, | ||
isDepositMode | ||
} = useNetworksRelationship(networks) | ||
const { | ||
status: gasSummaryStatus, | ||
estimatedParentChainGasFees, | ||
estimatedChildChainGasFees | ||
} = useGasSummary() | ||
const childChainNativeCurrency = useNativeCurrency({ | ||
provider: childChainProvider | ||
}) | ||
const parentChainNativeCurrency = useNativeCurrency({ | ||
provider: parentChainProvider | ||
}) | ||
const { isTestnet, isOrbitChain } = isNetwork(childChain.id) | ||
|
||
const selectedRoute = useRouteStore(state => state.selectedRoute) | ||
const [selectedToken] = useSelectedToken() | ||
|
||
const { gasCost, isLoading } = useMemo( | ||
() => | ||
getGasCostAndToken({ | ||
childChainNativeCurrency, | ||
parentChainNativeCurrency, | ||
gasSummaryStatus, | ||
estimatedChildChainGasFees, | ||
estimatedParentChainGasFees, | ||
isDepositMode, | ||
selectedToken | ||
}), | ||
[ | ||
childChainNativeCurrency, | ||
estimatedChildChainGasFees, | ||
estimatedParentChainGasFees, | ||
gasSummaryStatus, | ||
isDepositMode, | ||
parentChainNativeCurrency, | ||
selectedToken | ||
] | ||
) | ||
|
||
/** | ||
* For USDC: | ||
* - Withdrawing USDC.e, we receive USDC on Mainnet | ||
* - Depositing USDC, we receive USDC.e on Arbitrum | ||
*/ | ||
const isUsdcTransfer = isTokenNativeUSDC(selectedToken?.address) | ||
const overrideToken = isDepositMode ? bridgedUsdcToken : nativeUsdcToken | ||
const durationMs = | ||
getDuration({ | ||
isTestnet, | ||
isWithdrawal: !isDepositMode, | ||
sourceChainId: networks.sourceChain.id, | ||
isTeleportMode, | ||
isOrbitChain | ||
}) * | ||
60 * | ||
1_000 | ||
|
||
return ( | ||
<Route | ||
type="arbitrum" | ||
bridge={'Arbitrum Bridge'} | ||
bridgeIconURI={'/icons/arbitrum.svg'} | ||
durationMs={durationMs} | ||
amountReceived={amount.toString()} | ||
isLoadingGasEstimate={isLoading} | ||
overrideToken={isUsdcTransfer ? overrideToken : undefined} | ||
gasCost={ | ||
gasCost && gasCost.length > 0 | ||
? gasCost.map(({ gasCost, gasToken }) => ({ | ||
gasCost: utils | ||
.parseUnits(gasCost.toFixed(18), gasToken.decimals) | ||
.toString(), | ||
gasToken | ||
})) | ||
: [] | ||
} | ||
tag={'security-guaranteed'} | ||
selected={selectedRoute === 'arbitrum'} | ||
/> | ||
) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function is accepting lots of parameters rather than being a hook with lots of internal calls to hook to make it easier to test