Skip to content

Commit

Permalink
fixed rounding errors in promotion deploys
Browse files Browse the repository at this point in the history
  • Loading branch information
Ncookiez committed Nov 30, 2024
1 parent b3a7eab commit 40cfb0b
Showing 1 changed file with 25 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import {
promotionTokenAmountAtom,
promotionVaultAddressAtom
} from 'src/atoms'
import { SupportedNetwork } from 'src/types'
import { Address } from 'viem'
import { useAccount } from 'wagmi'
import { PROMOTION_FILTERS } from '@constants/config'
import { useSendApproveTransaction } from '@hooks/useSendApproveTransaction'
Expand All @@ -46,7 +44,7 @@ export const DeployPromotionButton = (props: DeployPromotionButtonProps) => {
const numEpochs = useAtomValue(promotionEpochsAtom)
const epochDuration = useAtomValue(promotionEpochLengthAtom)
const rewardTokenAddress = useAtomValue(promotionTokenAddressAtom)
const rewardTokenAmount = useAtomValue(promotionTokenAmountAtom)
const _rewardTokenAmount = useAtomValue(promotionTokenAmountAtom)

const { openConnectModal } = useConnectModal()
const { openChainModal } = useChainModal()
Expand All @@ -58,16 +56,22 @@ export const DeployPromotionButton = (props: DeployPromotionButtonProps) => {
}
}, [chainId, vaultAddress, publicClients])

const rewardTokensPerEpoch = useMemo(() => {
if (!!numEpochs && !!rewardTokenAmount) {
return calculatePercentageOfBigInt(rewardTokenAmount, 1 / numEpochs)
}
}, [numEpochs, rewardTokenAmount])
const { data: rewardToken } = useToken(chainId!, rewardTokenAddress!)

const rewardTokenAmount = useMemo(() => {
if (!!numEpochs && !!_rewardTokenAmount && !!rewardToken) {
const perEpoch = calculatePercentageOfBigInt(
_rewardTokenAmount,
Math.floor((1 / numEpochs) * 1e8) / 1e8
)
const total = perEpoch * BigInt(numEpochs)

const { data: rewardToken } = useToken(chainId as SupportedNetwork, rewardTokenAddress as Address)
return { perEpoch, total }
}
}, [numEpochs, _rewardTokenAmount, rewardToken])

const { refetch: refetchPromotionCreatedEvents } = usePromotionCreatedEvents(
chainId as SupportedNetwork,
chainId!,
!!chainId ? PROMOTION_FILTERS[chainId] : undefined
)

Expand All @@ -76,32 +80,24 @@ export const DeployPromotionButton = (props: DeployPromotionButtonProps) => {
data: allowance,
isFetched: isFetchedAllowance,
refetch: refetchTokenAllowance
} = useTokenAllowance(
chainId as SupportedNetwork,
userAddress as Address,
twabRewardsAddress as Address,
rewardTokenAddress as Address
)
} = useTokenAllowance(chainId!, userAddress!, twabRewardsAddress!, rewardTokenAddress!)

const {
isWaiting: isWaitingApproval,
isConfirming: isConfirmingApproval,
isSuccess: isSuccessfulApproval,
txHash: approvalTxHash,
sendApproveTransaction
} = useSendApproveTransaction(
chainId as SupportedNetwork,
rewardTokenAddress as Address,
rewardTokenAmount as bigint,
{ onSuccess: () => refetchTokenAllowance() }
)
} = useSendApproveTransaction(chainId!, rewardTokenAddress!, rewardTokenAmount?.total!, {
onSuccess: () => refetchTokenAllowance()
})

const { isWaiting, isConfirming, isSuccess, txHash, sendCreatePromotionTransaction } =
useSendCreatePromotionTransaction(
vault as Vault,
rewardTokenAddress as Address,
numEpochs as number,
rewardTokensPerEpoch as bigint,
vault!,
rewardTokenAddress!,
numEpochs!,
rewardTokenAmount?.perEpoch!,
{
startTimestamp,
epochDuration,
Expand All @@ -117,21 +113,21 @@ export const DeployPromotionButton = (props: DeployPromotionButtonProps) => {
)

const approvalEnabled =
!!chainId && !!rewardTokenAddress && !!rewardTokenAmount && !!sendApproveTransaction
!!chainId && !!rewardTokenAddress && !!rewardTokenAmount?.total && !!sendApproveTransaction

const deployPromotionEnabled =
!!vault &&
!!numEpochs &&
!!epochDuration &&
!!rewardToken &&
!!rewardTokensPerEpoch &&
!!rewardTokenAmount?.perEpoch &&
!!sendCreatePromotionTransaction

if (!chainId || !rewardTokenAmount) {
return <></>
}

if (isFetchedAllowance && allowance !== undefined && allowance < rewardTokenAmount) {
if (isFetchedAllowance && allowance !== undefined && allowance < rewardTokenAmount.total) {
return (
<TransactionButton
chainId={chainId}
Expand Down

0 comments on commit 40cfb0b

Please sign in to comment.