-
-
Notifications
You must be signed in to change notification settings - Fork 127
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
8823ae0
commit 8402e3f
Showing
12 changed files
with
318 additions
and
70 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
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 |
---|---|---|
|
@@ -110,5 +110,5 @@ export default function FormattedNumberInput({ | |
{accessory && <div>{accessory}</div>} | ||
</div> | ||
</div> | ||
); | ||
) | ||
} |
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
137 changes: 137 additions & 0 deletions
137
...3Project/ProjectDashboard/components/PayRedeemCard/PayProjectModal/hooks/usePayAmounts.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,137 @@ | ||
import { useCurrencyConverter } from 'hooks/useCurrencyConverter' | ||
import { useWallet } from 'hooks/Wallet' | ||
import { useNftCredits } from 'packages/v2v3/hooks/JB721Delegate/useNftCredits' | ||
import { | ||
V2V3_CURRENCY_ETH, | ||
V2V3_CURRENCY_USD, | ||
} from 'packages/v2v3/utils/currency' | ||
import { formatCurrencyAmount } from 'packages/v2v3/utils/formatCurrencyAmount' | ||
import React from 'react' | ||
import { fromWad, parseWad } from 'utils/format/formatNumber' | ||
import { useProjectSelector } from '../../../../redux/hooks' | ||
import { usePayProjectModal } from './usePayProjectModal/usePayProjectModal' | ||
|
||
export const usePayAmounts = () => { | ||
const converter = useCurrencyConverter() | ||
const { payAmount } = useProjectSelector(state => state.projectCart) | ||
const { primaryAmount, secondaryAmount } = usePayProjectModal() | ||
const { userAddress } = useWallet() | ||
const { data: nftCreditsData } = useNftCredits(userAddress) | ||
|
||
const payAmountRaw = React.useMemo(() => { | ||
if (!payAmount) return | ||
|
||
switch (payAmount.currency) { | ||
case V2V3_CURRENCY_ETH: | ||
return { | ||
eth: parseWad(payAmount.amount), | ||
usd: converter.weiToUsd(parseWad(payAmount.amount))!, | ||
} | ||
case V2V3_CURRENCY_USD: | ||
return { | ||
eth: converter.usdToWei(payAmount.amount), | ||
usd: parseWad(payAmount.amount), | ||
} | ||
} | ||
}, [converter, payAmount]) | ||
|
||
const appliedNFTCreditsRaw = React.useMemo(() => { | ||
if (!payAmountRaw || !nftCreditsData) return | ||
|
||
const nftCreditsApplied = payAmountRaw.eth.lt(nftCreditsData) | ||
? payAmountRaw.eth | ||
: nftCreditsData | ||
|
||
const eth = nftCreditsApplied | ||
const usd = parseWad(converter.weiToUsd(nftCreditsApplied))! | ||
|
||
return { | ||
eth, | ||
usd, | ||
} | ||
}, [converter, nftCreditsData, payAmountRaw]) | ||
|
||
const formattedNftCredits = React.useMemo(() => { | ||
if (!appliedNFTCreditsRaw || !payAmount) return | ||
|
||
switch (payAmount.currency) { | ||
case V2V3_CURRENCY_ETH: | ||
return { | ||
primaryAmount: formatCurrencyAmount({ | ||
amount: fromWad(appliedNFTCreditsRaw.eth), | ||
currency: V2V3_CURRENCY_ETH, | ||
}), | ||
secondaryAmount: formatCurrencyAmount({ | ||
amount: fromWad(appliedNFTCreditsRaw.usd), | ||
currency: V2V3_CURRENCY_USD, | ||
}), | ||
} | ||
case V2V3_CURRENCY_USD: | ||
return { | ||
primaryAmount: formatCurrencyAmount({ | ||
amount: fromWad(appliedNFTCreditsRaw.usd), | ||
currency: V2V3_CURRENCY_USD, | ||
}), | ||
secondaryAmount: formatCurrencyAmount({ | ||
amount: fromWad(appliedNFTCreditsRaw.eth), | ||
currency: V2V3_CURRENCY_ETH, | ||
}), | ||
} | ||
} | ||
}, [appliedNFTCreditsRaw, payAmount]) | ||
|
||
const formattedTotalAmount = React.useMemo(() => { | ||
if (!payAmountRaw || !payAmount) return | ||
|
||
if (!appliedNFTCreditsRaw) { | ||
return { | ||
primaryAmount: primaryAmount, | ||
secondaryAmount: secondaryAmount, | ||
} | ||
} | ||
|
||
const totalEth = payAmountRaw.eth.sub(appliedNFTCreditsRaw.eth) | ||
const totalUsd = converter.weiToUsd(parseWad(totalEth)) | ||
|
||
const formattedEth = formatCurrencyAmount({ | ||
amount: fromWad(totalEth), | ||
currency: V2V3_CURRENCY_ETH, | ||
}) | ||
const formattedUsd = formatCurrencyAmount({ | ||
amount: fromWad(totalUsd), | ||
currency: V2V3_CURRENCY_USD, | ||
}) | ||
|
||
switch (payAmount?.currency) { | ||
case V2V3_CURRENCY_ETH: | ||
return { | ||
primaryAmount: formattedEth, | ||
secondaryAmount: formattedUsd, | ||
} | ||
case V2V3_CURRENCY_USD: | ||
return { | ||
primaryAmount: formattedUsd, | ||
secondaryAmount: formattedEth, | ||
} | ||
} | ||
}, [ | ||
appliedNFTCreditsRaw, | ||
converter, | ||
payAmount, | ||
payAmountRaw, | ||
primaryAmount, | ||
secondaryAmount, | ||
]) | ||
|
||
return { | ||
formattedAmount: { primaryAmount, secondaryAmount }, | ||
formattedNftCredits: { | ||
primaryAmount: formattedNftCredits?.primaryAmount, | ||
secondaryAmount: formattedNftCredits?.secondaryAmount, | ||
}, | ||
formattedTotalAmount: { | ||
primaryAmount: formattedTotalAmount?.primaryAmount, | ||
secondaryAmount: formattedTotalAmount?.secondaryAmount, | ||
}, | ||
} | ||
} |
Oops, something went wrong.