Skip to content

Commit

Permalink
fix tvl and user deposit calculations (#375)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamoskvin authored Jul 28, 2024
1 parent 6403ded commit 073b7a0
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 16 deletions.
14 changes: 9 additions & 5 deletions src/components/vault/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ export function useUserShares(

const { token0All, token1All } = useMemo(() => {
const result: any = data
if (!result || isError || !result[0] || !result[1]) return { token0All: undefined, token1All: undefined }
// if (!result || isError || !result[0] || !result[1]) return { token0All: undefined, token1All: undefined }
if (!result || isError || result[0] === undefined || result[1] === undefined)
//we can have zero values, so we can't do !result[0]
return { token0All: undefined, token1All: undefined }
return {
token0All: result[0],
token1All: result[1],
Expand All @@ -98,7 +101,8 @@ export function useUserShares(
}, [supply, supplyError])

const token0: CurrencyAmount<Currency> | undefined = useMemo(() => {
if (!typedValue || !totalSupply || !token0All || !currencyA) {
if (!typedValue || !totalSupply || token0All === undefined || !currencyA) {
// token0All could be zero
return undefined
}
const token0Amount =
Expand All @@ -110,7 +114,7 @@ export function useUserShares(
}, [typedValue, totalSupply, token0All, currencyA])

const token1: CurrencyAmount<Currency> | undefined = useMemo(() => {
if (!typedValue || !totalSupply || !token1All || !currencyB) {
if (!typedValue || !totalSupply || token1All === undefined || !currencyB) {
return undefined
}
const token1Amount =
Expand All @@ -121,7 +125,7 @@ export function useUserShares(
}, [typedValue, totalSupply, token1All, currencyB])

const totalToken0Amount: CurrencyAmount<Currency> | undefined = useMemo(() => {
if (!shares || !totalSupply || !token0All || !currencyA) {
if (!shares || !totalSupply || token0All === undefined || !currencyA) {
return undefined
}
const token0Amount =
Expand All @@ -131,7 +135,7 @@ export function useUserShares(
}, [shares, totalSupply, token0All, currencyA])

const totalToken1Amount: CurrencyAmount<Currency> | undefined = useMemo(() => {
if (!shares || !totalSupply || !token1All || !currencyB) {
if (!shares || !totalSupply || token1All === undefined || !currencyB) {
return undefined
}
const token1Amount =
Expand Down
10 changes: 5 additions & 5 deletions src/pages/Vault/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ export default function Vault({ className }: { className?: string }) {
}
}, [separatedFiatValueofLiquidity, totalToken0Amount, totalToken1Amount])

const sharesUSDPrice = token0usdPrice && token1usdPrice ? token0usdPrice + token1usdPrice : 0
const sharesUSDPrice = (token0usdPrice || 0) + (token1usdPrice || 0)

const getContent = () => {
switch (true) {
Expand Down Expand Up @@ -576,10 +576,10 @@ export default function Vault({ className }: { className?: string }) {
<MyDeposits>
<span>My Deposits</span>
<span>
{token0usdPrice && token0usdPrice
? sharesUSDPrice
? `~$${sharesUSDPrice.toFixed(2)}`
: 'NA'
{sharesUSDPrice
? `~$${sharesUSDPrice.toFixed(2)}`
: totalToken0Amount?.greaterThan('0') || totalToken1Amount?.greaterThan('0')
? 'NA'
: 0}
</span>
</MyDeposits>
Expand Down
56 changes: 50 additions & 6 deletions src/pages/Vaults/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,10 @@ const getVaultDetails = ({
currency0: any
currency1: any
token0usdPrice: any
token1usdPrice: any
sharesUSDPrice: any
totalToken0Amount: any
totalToken1Amount: any
} => {
const { chainId: chainIdConnected } = useAccountDetails()
const chainId = chainIdConnected || DEFAULT_CHAIN_ID
Expand Down Expand Up @@ -461,7 +464,8 @@ const getVaultDetails = ({
}
}, [separatedFiatValueofLiquidity, totalToken0Amount, totalToken1Amount])

const sharesUSDPrice = token0usdPrice && token1usdPrice ? token0usdPrice + token1usdPrice : 0
// const sharesUSDPrice = token0usdPrice && token1usdPrice ? token0usdPrice + token1usdPrice : 0
const sharesUSDPrice = (token0usdPrice || 0) + (token1usdPrice || 0)

let tvl
let apr
Expand All @@ -485,11 +489,31 @@ const getVaultDetails = ({
apr = (feeApr + vaultData.aprStarknet * 100).toFixed(2)
shareTokenPriceUsd = shareTokenPriceInUnits * tokenPrice
}
return { tvl, apr, currency0, currency1, token0usdPrice, sharesUSDPrice }
return {
tvl,
apr,
currency0,
currency1,
token0usdPrice,
token1usdPrice,
sharesUSDPrice,
totalToken0Amount,
totalToken1Amount,
}
}

const MobileVaultListItem = ({ index, vaultAddress, vaultData, getUserBalance = noop }: ListItemProps) => {
const { tvl, apr, currency0, currency1, token0usdPrice, sharesUSDPrice } = getVaultDetails({
const {
tvl,
apr,
currency0,
currency1,
token0usdPrice,
token1usdPrice,
sharesUSDPrice,
totalToken0Amount,
totalToken1Amount,
} = getVaultDetails({
index,
vaultAddress,
vaultData,
Expand Down Expand Up @@ -517,7 +541,12 @@ const MobileVaultListItem = ({ index, vaultAddress, vaultData, getUserBalance =
<MobileValues>{tvl ? formatUsdPrice(tvl) : '-'}</MobileValues>
<MobileValues>{apr ? `${apr}%` : '-'}</MobileValues>
<MobileValues style={{ color: '#2AAAFE' }}>
{token0usdPrice && token0usdPrice ? (sharesUSDPrice ? `~$${sharesUSDPrice.toFixed(2)}` : 'NA') : 0}
{/* {token0usdPrice || token1usdPrice ? (sharesUSDPrice ? `~$${sharesUSDPrice.toFixed(2)}` : 'NA') : 0} */}
{sharesUSDPrice
? `~$${sharesUSDPrice.toFixed(2)}`
: totalToken0Amount?.greaterThan('0') || totalToken1Amount?.greaterThan('0')
? 'NA'
: 0}
</MobileValues>
</PageTitleRow>
</VaultMobileContainer>
Expand All @@ -529,7 +558,17 @@ const ListItem = ({ index, vaultAddress, vaultData, getUserBalance = noop }: Lis
const below600 = useMedia('(max-width: 600px)')
const below768 = useMedia('(max-width: 768px)')

const { tvl, apr, currency0, currency1, token0usdPrice, sharesUSDPrice } = getVaultDetails({
const {
tvl,
apr,
currency0,
currency1,
token0usdPrice,
token1usdPrice,
sharesUSDPrice,
totalToken0Amount,
totalToken1Amount,
} = getVaultDetails({
index,
vaultAddress,
vaultData,
Expand Down Expand Up @@ -564,7 +603,12 @@ const ListItem = ({ index, vaultAddress, vaultData, getUserBalance = noop }: Lis
<DataText area="deposite">
<ThemedText.BodySmall>
<span>
{token0usdPrice && token0usdPrice ? (sharesUSDPrice ? `~$${sharesUSDPrice.toFixed(2)}` : 'NA') : 0}
{/* {token0usdPrice || token1usdPrice ? (sharesUSDPrice ? `~$${sharesUSDPrice.toFixed(2)}` : 'NA') : 0} */}
{sharesUSDPrice
? `~$${sharesUSDPrice.toFixed(2)}`
: totalToken0Amount?.greaterThan('0') || totalToken1Amount?.greaterThan('0')
? 'NA'
: 0}
</span>
</ThemedText.BodySmall>
</DataText>
Expand Down

0 comments on commit 073b7a0

Please sign in to comment.