-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: generalise useRoute hook (#2491)
- Loading branch information
1 parent
cf35f95
commit 281e100
Showing
9 changed files
with
386 additions
and
344 deletions.
There are no files selected for viewing
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,17 @@ | ||
export class RouteError extends Error { | ||
constructor( | ||
public message: string, | ||
public data?: { | ||
fromToken?: string; | ||
toToken?: string; | ||
fromChain?: string | number; | ||
toChain?: string | number; | ||
errorStatus?: number; | ||
errorMessage?: string; | ||
errorStack?: string; | ||
}, | ||
) { | ||
super(message); | ||
Object.setPrototypeOf(this, RouteError.prototype); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/checkout/widgets-lib/src/lib/squid/functions/findToken.ts
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,10 @@ | ||
import { Token } from '../types'; | ||
|
||
export const findToken = ( | ||
tokens: Token[], | ||
address: string, | ||
chainId: string, | ||
): Token | undefined => tokens.find( | ||
(value) => value.address.toLowerCase() === address.toLowerCase() | ||
&& value.chainId === chainId, | ||
); |
14 changes: 14 additions & 0 deletions
14
packages/checkout/widgets-lib/src/lib/squid/functions/isRouteToAmountGreaterThanToAmount.ts
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,14 @@ | ||
import { RouteResponse } from '@0xsquid/squid-types'; | ||
import { BigNumber, utils } from 'ethers'; | ||
|
||
export const isRouteToAmountGreaterThanToAmount = ( | ||
routeResponse: RouteResponse, | ||
toAmount: string, | ||
) => { | ||
if (!routeResponse?.route?.estimate?.toAmount || !routeResponse?.route?.estimate?.toToken?.decimals) { | ||
throw new Error('Invalid route response or token decimals'); | ||
} | ||
const toAmountInBaseUnits = utils.parseUnits(toAmount, routeResponse?.route.estimate.toToken.decimals); | ||
const routeToAmountInBaseUnits = BigNumber.from(routeResponse.route.estimate.toAmount); | ||
return routeToAmountInBaseUnits.gt(toAmountInBaseUnits); | ||
}; |
60 changes: 60 additions & 0 deletions
60
packages/checkout/widgets-lib/src/lib/squid/hooks/useRouteCalculation.ts
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,60 @@ | ||
import { useMemo } from 'react'; | ||
import { utils } from 'ethers'; | ||
import { useSlippage } from './useSlippage'; | ||
import { Token } from '../types'; | ||
|
||
/** | ||
* Hook to handle route amount calculations. | ||
*/ | ||
export const useRouteCalculation = () => { | ||
const { calculateAdjustedAmount } = useSlippage(); | ||
|
||
/** | ||
* Calculate the fromAmount based on USD prices and slippage. | ||
*/ | ||
const calculateFromAmount = ( | ||
fromToken: Token, | ||
toToken: Token, | ||
toAmount: string, | ||
additionalBuffer: number = 0, | ||
) => { | ||
const toAmountNumber = parseFloat(toAmount); | ||
// Calculate the USD value of the toAmount | ||
const toAmountInUsd = toAmountNumber * toToken.usdPrice; | ||
// Calculate the amount of fromToken needed to match this USD value | ||
const baseFromAmount = toAmountInUsd / fromToken.usdPrice; | ||
// Add a buffer for price fluctuations and fees | ||
const fromAmountWithBuffer = calculateAdjustedAmount(baseFromAmount, toAmountInUsd, additionalBuffer); | ||
|
||
return fromAmountWithBuffer.toString(); | ||
}; | ||
|
||
/** | ||
* Calculate the fromAmount using exchange rate returned from the route. | ||
*/ | ||
const calculateFromAmountFromRoute = ( | ||
exchangeRate: string, | ||
toAmount: string, | ||
toAmountUSD?: string, | ||
) => { | ||
const toAmountUSDNumber = toAmountUSD ? parseFloat(toAmountUSD) : 0; | ||
const fromAmount = parseFloat(toAmount) / parseFloat(exchangeRate); | ||
const fromAmountWithBuffer = calculateAdjustedAmount(fromAmount, toAmountUSDNumber); | ||
return fromAmountWithBuffer.toString(); | ||
}; | ||
|
||
/** | ||
* Convert a string amount to a formatted amount with the specified number of decimals. | ||
*/ | ||
const convertToFormattedFromAmount = (amount: string, decimals: number) => { | ||
const parsedFromAmount = parseFloat(amount).toFixed(decimals); | ||
const formattedFromAmount = utils.parseUnits(parsedFromAmount, decimals); | ||
return formattedFromAmount.toString(); | ||
}; | ||
|
||
return useMemo(() => ({ | ||
calculateFromAmount, | ||
calculateFromAmountFromRoute, | ||
convertToFormattedFromAmount, | ||
}), []); | ||
}; |
Oops, something went wrong.