-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bd09774
commit 34c3b8c
Showing
8 changed files
with
121 additions
and
119 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 |
---|---|---|
@@ -1,44 +1,16 @@ | ||
import { utils } from "@across-protocol/sdk-v2"; | ||
import { BigNumber } from "ethers"; | ||
import { useCallback } from "react"; | ||
import { useQuery } from "react-query"; | ||
import { fixedPointAdjustment, getTokenByAddress } from "utils"; | ||
import { ConvertDecimals } from "utils/convertdecimals"; | ||
import getApiEndpoint from "utils/serverless-api"; | ||
|
||
export function useCoingeckoPrice( | ||
l1Token: string, | ||
baseCurrency: string, | ||
enabled: boolean = true | ||
) { | ||
const query = useQuery( | ||
return useQuery( | ||
["price", l1Token, baseCurrency], | ||
async () => getApiEndpoint().coingecko(l1Token, baseCurrency), | ||
{ | ||
enabled, | ||
} | ||
); | ||
const convertTokenToBaseCurrency = useCallback( | ||
(amount?: BigNumber) => { | ||
// Resolve the price as an 18 decimal big number which takes | ||
// the form {PRICE} / 1 token | ||
const price = query.data?.price; | ||
// If the price is not defined yet, return undefined | ||
if (!utils.isDefined(price) || !utils.isDefined(amount)) { | ||
return undefined; | ||
} | ||
// Resolve token decimals | ||
const { decimals } = getTokenByAddress(l1Token); | ||
// Convert the amount to the base currency | ||
const convertedAmount = ConvertDecimals(decimals, 18)(amount); | ||
// Return the product of the price and the amount | ||
return price.mul(convertedAmount).div(fixedPointAdjustment); | ||
}, | ||
[l1Token, query.data] | ||
); | ||
|
||
return { | ||
...query, | ||
convertTokenToBaseCurrency, | ||
}; | ||
} |
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,32 @@ | ||
import { utils } from "@across-protocol/sdk-v2"; | ||
import { useCoingeckoPrice } from "./useCoingeckoPrice"; | ||
import { BigNumber } from "ethers"; | ||
import { useCallback } from "react"; | ||
import { fixedPointAdjustment, getToken, getTokenByAddress } from "utils"; | ||
import { ConvertDecimals } from "utils/convertdecimals"; | ||
|
||
export function useTokenConversion(symbol: string, baseCurrency: string) { | ||
const token = getToken(symbol); | ||
const l1Token = token.mainnetAddress!; | ||
const query = useCoingeckoPrice( | ||
l1Token, | ||
baseCurrency, | ||
utils.isDefined(l1Token) | ||
); | ||
const convertTokenToBaseCurrency = useCallback( | ||
(amount?: BigNumber) => { | ||
const price = query.data?.price; | ||
if (!utils.isDefined(price) || !utils.isDefined(amount)) { | ||
return undefined; | ||
} | ||
const { decimals } = getTokenByAddress(l1Token); | ||
const convertedAmount = ConvertDecimals(decimals, 18)(amount); | ||
return price.mul(convertedAmount).div(fixedPointAdjustment); | ||
}, | ||
[l1Token, query.data?.price] | ||
); | ||
return { | ||
convertTokenToBaseCurrency, | ||
baseCurrency, | ||
}; | ||
} |
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
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
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
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
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
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,74 @@ | ||
import { utils } from "@across-protocol/sdk-v2"; | ||
import { BigNumber } from "ethers"; | ||
import { useConnection } from "hooks"; | ||
import { useReferralSummary } from "hooks/useReferralSummary"; | ||
import { useTokenConversion } from "hooks/useTokenConversion"; | ||
import { useMemo, useState } from "react"; | ||
import { | ||
TokenInfo, | ||
fixedPointAdjustment, | ||
formatUnitsFnBuilder, | ||
parseUnits, | ||
} from "utils"; | ||
|
||
export function useEstimatedTable( | ||
token: TokenInfo, | ||
gasFee?: BigNumber, | ||
bridgeFee?: BigNumber | ||
) { | ||
const [isDetailedFeesAvailable, setIsDetailedFeesAvailable] = useState(false); | ||
|
||
const { account } = useConnection(); | ||
const { summary: referralSummary } = useReferralSummary(account); | ||
|
||
const { convertTokenToBaseCurrency: convertL1ToBaseCurrency } = | ||
useTokenConversion(token.symbol, "usd"); | ||
const { convertTokenToBaseCurrency: convertRewardToBaseCurrency } = | ||
useTokenConversion("ACX", "usd"); | ||
|
||
const depositReferralReward = useMemo(() => { | ||
if (!utils.isDefined(referralSummary) || !utils.isDefined(bridgeFee)) { | ||
return undefined; | ||
} | ||
const totalFeesUSD = convertL1ToBaseCurrency(bridgeFee); | ||
const acxExchangeRate = convertRewardToBaseCurrency(parseUnits("1", 18)); | ||
if (!utils.isDefined(totalFeesUSD) || !utils.isDefined(acxExchangeRate)) { | ||
return undefined; | ||
} | ||
const totalFeesACX = totalFeesUSD | ||
.mul(fixedPointAdjustment) | ||
.div(acxExchangeRate); | ||
const feePct = parseUnits(referralSummary.referralRate.toString(), 18); | ||
return totalFeesACX.mul(feePct).div(fixedPointAdjustment); | ||
}, [ | ||
referralSummary, | ||
bridgeFee, | ||
convertL1ToBaseCurrency, | ||
convertRewardToBaseCurrency, | ||
]); | ||
|
||
const referralRewardAsBaseCurrency = convertRewardToBaseCurrency( | ||
depositReferralReward | ||
); | ||
const gasFeeAsBaseCurrency = convertL1ToBaseCurrency(gasFee); | ||
const bridgeFeeAsBaseCurrency = convertL1ToBaseCurrency(bridgeFee); | ||
const netFeeAsBaseCurrency = | ||
gasFeeAsBaseCurrency && bridgeFeeAsBaseCurrency | ||
? gasFeeAsBaseCurrency.add(bridgeFeeAsBaseCurrency) | ||
: undefined; | ||
const formatUsd = formatUnitsFnBuilder(18); | ||
const hasDepositReferralReward = | ||
depositReferralReward && depositReferralReward.gt(0); | ||
|
||
return { | ||
isDetailedFeesAvailable, | ||
setIsDetailedFeesAvailable, | ||
referralRewardAsBaseCurrency, | ||
gasFeeAsBaseCurrency, | ||
bridgeFeeAsBaseCurrency, | ||
netFeeAsBaseCurrency, | ||
formatUsd, | ||
depositReferralReward, | ||
hasDepositReferralReward, | ||
}; | ||
} |