-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit introduces the structure for the redeem page where users will be able to retrieve their deposit from the course. Included are a number of new contract functions used to check the status of a user's stake, the block in which they registered, the course length and finally send a transaction to retrieve their deposit. Still needed is the ability for a user to select whether or not they'd like to retrieve their deposit or mint $LEARN instead.
- Loading branch information
Anthony M
committed
May 27, 2022
1 parent
de6f46a
commit c6611bd
Showing
10 changed files
with
360 additions
and
30 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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** @jsx jsx */ | ||
|
||
import { Flex, jsx } from 'theme-ui' | ||
import { useConnect } from 'wagmi' | ||
import { useState } from 'react' | ||
|
||
import { Button as Web3Button } from '@src/modules/web3' | ||
import { Connector } from '@src/course/connect' | ||
|
||
const ConnectButton = () => { | ||
const { connect, connectors } = useConnect() | ||
const [connector] = useState(connectors[Connector.INJECTED]) | ||
|
||
return ( | ||
<Flex> | ||
<Web3Button | ||
descriptionText="Connect your wallet to continue" | ||
buttonText="Metamask" | ||
isDisabled={!connector.ready} | ||
onClickButton={() => { | ||
connect(connector) | ||
}} | ||
/> | ||
</Flex> | ||
) | ||
} | ||
|
||
export default ConnectButton |
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,37 @@ | ||
/** @jsx jsx */ | ||
|
||
import { jsx } from 'theme-ui' | ||
import { useAccount, useProvider } from 'wagmi' | ||
import { useEffect, useState } from 'react' | ||
|
||
import { hasStakeInCourse } from '@src/course/contracts' | ||
import RegisterButton from './RegisterButton' | ||
import RedemptionDetails from './RedemptionDetails' | ||
|
||
const RedemptionConnector = () => { | ||
const { data: accountData } = useAccount() | ||
const provider = useProvider() | ||
const [hasStakeStakeToRedeem, setHasStakeToRedeem] = useState(false) | ||
|
||
useEffect(() => { | ||
const retrieveAndSetStakeState = async () => { | ||
const _hasStake = await hasStakeInCourse(provider) | ||
setHasStakeToRedeem(_hasStake) | ||
} | ||
|
||
if (accountData?.address && provider) { | ||
retrieveAndSetStakeState() | ||
} | ||
}, [accountData?.address, provider]) | ||
|
||
return ( | ||
<div> | ||
{hasStakeStakeToRedeem && ( | ||
<RedemptionDetails address={accountData?.address} /> | ||
)} | ||
{!hasStakeStakeToRedeem && <RegisterButton />} | ||
</div> | ||
) | ||
} | ||
|
||
export default RedemptionConnector |
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,110 @@ | ||
/** @jsx jsx */ | ||
|
||
import { Flex, jsx } from 'theme-ui' | ||
import { useConnect, useProvider, useBlockNumber, useSigner } from 'wagmi' | ||
import { useEffect, useState } from 'react' | ||
|
||
import { Button as Web3Button } from '@src/modules/web3' | ||
import { Connector } from '@src/course/connect' | ||
import { | ||
getBlockRegistered, | ||
getCourseLength, | ||
redeemDeposit, | ||
} from '@src/course/contracts' | ||
|
||
const isRedemptionTime = (currentBlockNumber, redemptionBlockNumber) => { | ||
return currentBlockNumber >= redemptionBlockNumber | ||
} | ||
|
||
const getTimeUntilRedemptionText = ( | ||
currentBlockNumber, | ||
redemptionBlockNumber | ||
) => { | ||
if (isRedemptionTime(currentBlockNumber, redemptionBlockNumber)) { | ||
return 'You can redeem your deposit now' | ||
} else { | ||
const averageBlockTimeInDays = 14 / 60 / 60 / 24 | ||
const daysUntilRedemption = | ||
(redemptionBlockNumber - currentBlockNumber) * averageBlockTimeInDays | ||
|
||
if (daysUntilRedemption <= 1) { | ||
return `You can redeem your deposit today at block number ${redemptionBlockNumber}` | ||
} else { | ||
const roundedDays = Math.round(daysUntilRedemption) | ||
const dayText = roundedDays == 1 ? 'day' : 'days' | ||
return `You can redeem your deposit in ${roundedDays} ${dayText} at block number ${redemptionBlockNumber}` | ||
} | ||
} | ||
} | ||
|
||
const RedemptionDetails = ({ address }) => { | ||
const provider = useProvider() | ||
const { data: signer } = useSigner() | ||
const { data: currentBlockNumber } = useBlockNumber({ watch: true }) | ||
const { connectors } = useConnect() | ||
|
||
const [connector] = useState(connectors[Connector.INJECTED]) | ||
const [blockRegistered, setBlockRegistered] = useState(0) | ||
const [courseLength, setCourseLength] = useState(0) | ||
const [canRedeemDeposit, setCanRedeemDeposit] = useState(false) | ||
|
||
const redemptionBlockNumber = blockRegistered + courseLength | ||
const timeUntilRedemptionText = getTimeUntilRedemptionText( | ||
currentBlockNumber, | ||
redemptionBlockNumber | ||
) | ||
|
||
useEffect(() => { | ||
const retrieveBlockRegistered = async () => { | ||
const blockNumber = await getBlockRegistered(address, provider) | ||
setBlockRegistered(blockNumber || 0) | ||
} | ||
|
||
const retrieveCourseLength = async () => { | ||
const lengthOfCourse = await getCourseLength(provider) | ||
setCourseLength(lengthOfCourse || 0) | ||
} | ||
|
||
if (address && provider) { | ||
retrieveBlockRegistered() | ||
retrieveCourseLength() | ||
} | ||
}, [address, provider]) | ||
|
||
useEffect(() => { | ||
if (currentBlockNumber && blockRegistered) { | ||
setCanRedeemDeposit( | ||
isRedemptionTime(currentBlockNumber, redemptionBlockNumber) | ||
) | ||
} | ||
}, [blockRegistered, currentBlockNumber]) | ||
|
||
const handleOnPressRedeem = async () => { | ||
await redeemDeposit(signer) | ||
} | ||
|
||
return ( | ||
<Flex sx={styles.container}> | ||
<p sx={styles.blockNumberText}>Current block: {currentBlockNumber}</p> | ||
<Web3Button | ||
descriptionText={timeUntilRedemptionText} | ||
buttonText="Redeem" | ||
isDisabled={!connector.ready || !canRedeemDeposit} | ||
onClickButton={handleOnPressRedeem} | ||
/> | ||
</Flex> | ||
) | ||
} | ||
|
||
const styles = { | ||
container: { | ||
flexDirection: 'column', | ||
}, | ||
blockNumberText: { | ||
textAlign: 'center', | ||
fontWeight: 'bold', | ||
marginX: 'auto', | ||
}, | ||
} | ||
|
||
export default RedemptionDetails |
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,28 @@ | ||
/** @jsx jsx */ | ||
|
||
import { Flex, jsx } from 'theme-ui' | ||
import { useConnect } from 'wagmi' | ||
import { useState } from 'react' | ||
|
||
import { Button as Web3Button, Modal as Web3Modal } from '@src/modules/web3' | ||
import { Connector } from '@src/course/connect' | ||
|
||
const RegisterButton = () => { | ||
const { connectors } = useConnect() | ||
const [connector] = useState(connectors[Connector.INJECTED]) | ||
const [isModalVisible, setIsModalVisible] = useState(false) | ||
|
||
return ( | ||
<Flex> | ||
{isModalVisible && <Web3Modal setIsVisible={setIsModalVisible} />} | ||
<Web3Button | ||
descriptionText="You don't have any deposits in the course" | ||
buttonText="Register" | ||
isDisabled={!connector.ready} | ||
onClickButton={() => setIsModalVisible(true)} | ||
/> | ||
</Flex> | ||
) | ||
} | ||
|
||
export default RegisterButton |
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,4 @@ | ||
import ConnectButton from './ConnectButton' | ||
import RedemptionConnector from './RedemptionConnector' | ||
|
||
export { ConnectButton, RedemptionConnector } |
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,37 @@ | ||
/** @jsx jsx */ | ||
|
||
import { jsx, Text, Box } from 'theme-ui' | ||
import { Button } from '@modules/ui' | ||
|
||
const Web3Button = ({ | ||
children, | ||
onClickButton, | ||
buttonText, | ||
descriptionText, | ||
isDisabled, | ||
}) => { | ||
return ( | ||
<div> | ||
<Box sx={{ padding: '0.5rem' }}> | ||
<Text sx={styles.connectText}>{descriptionText}</Text> | ||
</Box> | ||
<Button | ||
sx={{ marginX: 'auto' }} | ||
disabled={isDisabled} | ||
onClick={onClickButton}> | ||
{buttonText} | ||
</Button> | ||
{children} | ||
</div> | ||
) | ||
} | ||
|
||
const styles = { | ||
connectText: { | ||
textAlign: 'center', | ||
fontWeight: 'bold', | ||
marginX: 'auto', | ||
}, | ||
} | ||
|
||
export default Web3Button |
Oops, something went wrong.