Skip to content

Commit

Permalink
v4 token redemption (#4554)
Browse files Browse the repository at this point in the history
  • Loading branch information
wraeth-eth authored Dec 4, 2024
1 parent 499edfd commit 99604b9
Show file tree
Hide file tree
Showing 11 changed files with 623 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export const RedeemConfiguration: React.FC<RedeemConfigurationProps> = ({
const balance = userTokenBalance ?? 0
return amount > balance
}, [redeemAmount, userTokenBalance])

const tokenTicker = tokenSymbol || 'TOKENS'

// 0.5% slippage for USD-denominated tokens
Expand Down
34 changes: 34 additions & 0 deletions src/packages/v4/contexts/V4UserTotalTokensBalanceProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { useWallet } from 'hooks/Wallet'
import {
useJBContractContext,
useReadJbTokensTotalBalanceOf,
} from 'juice-sdk-react'
import React, { PropsWithChildren } from 'react'
import { zeroAddress } from 'viem'

const V4UserTotalTokensBalanceContext = React.createContext<{
data: bigint | undefined
isLoading: boolean
}>({
data: undefined,
isLoading: false,
})

export const V4UserTotalTokensBalanceProvider: React.FC<PropsWithChildren> = ({
children,
}) => {
const { userAddress } = useWallet()
const { projectId } = useJBContractContext()
const value = useReadJbTokensTotalBalanceOf({
args: [userAddress ?? zeroAddress, projectId],
})

return (
<V4UserTotalTokensBalanceContext.Provider value={value}>
{children}
</V4UserTotalTokensBalanceContext.Provider>
)
}

export const useV4UserTotalTokensBalance = () =>
React.useContext(V4UserTotalTokensBalanceContext)
31 changes: 20 additions & 11 deletions src/packages/v4/hooks/useETHReceivedFromTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,28 @@ export function useETHReceivedFromTokens(
const redemptionRate = rulesetMetadata.data?.redemptionRate?.value

if (
!redemptionRate ||
!totalSupply ||
!tokensReserved ||
!tokenAmountWei ||
!nativeTokenSurplus
redemptionRate === undefined ||
totalSupply === undefined ||
tokensReserved === undefined ||
tokenAmountWei === undefined ||
nativeTokenSurplus === undefined
) {
return
}

return getTokenRedemptionQuoteEth(tokenAmountWei, {
redemptionRate: Number(redemptionRate),
totalSupply,
tokensReserved,
overflowWei: nativeTokenSurplus,
})
try {
return getTokenRedemptionQuoteEth(tokenAmountWei, {
redemptionRate: Number(redemptionRate),
totalSupply,
tokensReserved,
overflowWei: nativeTokenSurplus,
})
} catch (e) {
// Division by zero can cause a RangeError
if (e instanceof RangeError) {
return
} else {
throw e
}
}
}
4 changes: 3 additions & 1 deletion src/packages/v4/utils/math.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { feeForAmount } from 'utils/math'

export const MAX_PAYOUT_LIMIT = BigInt('26959946667150639794667015087019630673637144422540572481103610249215') // uint 224, probably a better way lol
export const MAX_PAYOUT_LIMIT = BigInt(
'26959946667150639794667015087019630673637144422540572481103610249215',
) // uint 224, probably a better way lol

export const amountSubFee = (
amountWad: bigint | undefined,
Expand Down
Loading

0 comments on commit 99604b9

Please sign in to comment.