Skip to content

Commit

Permalink
Bugfix/profile creation mint (pancakeswap#468)
Browse files Browse the repository at this point in the history
* fix(profile): Unmount React error

* fix(profile): Check minimum CAKE balance
  • Loading branch information
hachiojidev authored Feb 17, 2021
1 parent a98a462 commit b8f7212
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/hooks/useHasCakeBalance.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import BigNumber from 'bignumber.js'
import { getCakeAddress } from 'utils/addressHelpers'
import useTokenBalance from './useTokenBalance'

/**
* A hook to check if a wallet's CAKE balance is at least the amount passed in
*/
const useHasCakeBalance = (minimumBalance) => {
const useHasCakeBalance = (minimumBalance: BigNumber) => {
const cakeBalance = useTokenBalance(getCakeAddress())
return cakeBalance.gte(minimumBalance)
}
Expand Down
18 changes: 10 additions & 8 deletions src/views/Profile/ProfileCreation/Mint.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@ import useProfileCreation from './contexts/hook'

const starterBunnyIds = [5, 6, 7, 8, 9]
const nfts = nftList.filter((nft) => starterBunnyIds.includes(nft.bunnyId))
const minimumCakeBalance = 4
const cakeCostToMint = 4
const minimumCakeBalanceToMint = new BigNumber(cakeCostToMint).multipliedBy(new BigNumber(10).pow(18))

const Mint: React.FC = () => {
const [bunnyId, setBunnyId] = useState(null)
const { actions, minimumCakeRequired, allowance } = useProfileCreation()

const { account } = useWallet()
const cakeContract = useCake()
const bunnyFactoryContract = useBunnyFactory()
const TranslateString = useI18n()
const hasMinimumCakeRequired = useHasCakeBalance(minimumCakeBalance)
const hasMinimumCakeRequired = useHasCakeBalance(minimumCakeBalanceToMint)
const {
isApproving,
isApproved,
Expand Down Expand Up @@ -94,19 +96,19 @@ const Mint: React.FC = () => {
</SelectionCard>
)
})}
{!hasMinimumCakeRequired && (
<Text color="failure" mb="16px">
{TranslateString(1098, `A minimum of ${cakeCostToMint} CAKE is required`)}
</Text>
)}
<ApproveConfirmButtons
isApproveDisabled={bunnyId === null || isConfirmed || isConfirming || isApproved}
isApproving={isApproving}
isConfirmDisabled={!isApproved || isConfirmed}
isConfirmDisabled={!isApproved || isConfirmed || !hasMinimumCakeRequired}
isConfirming={isConfirming}
onApprove={handleApprove}
onConfirm={handleConfirm}
/>
{!hasMinimumCakeRequired && (
<Text color="failure" mt="16px">
{TranslateString(1098, `A minimum of ${minimumCakeBalance} CAKE is required`)}
</Text>
)}
</CardBody>
</Card>
<NextStepButton onClick={actions.nextStep} disabled={!isConfirmed}>
Expand Down
8 changes: 5 additions & 3 deletions src/views/Profile/ProfileCreation/UserName.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { ChangeEvent, useEffect, useState } from 'react'
import styled from 'styled-components'
import BigNumber from 'bignumber.js'
import {
Card,
CardBody,
Expand Down Expand Up @@ -34,7 +35,8 @@ enum ExistingUserState {
const MIN_LENGTH = 3
const MAX_LENGTH = 15
const profileApiUrl = process.env.REACT_APP_API_PROFILE
const minimumCakeBalance = 1
const cakeCostToRegister = 1
const minimumCakeToRegister = new BigNumber(cakeCostToRegister).multipliedBy(new BigNumber(10).pow(18))

const InputWrap = styled.div`
position: relative;
Expand Down Expand Up @@ -67,7 +69,7 @@ const UserName: React.FC = () => {
const [isValid, setIsValid] = useState(false)
const [isLoading, setIsLoading] = useState(false)
const [message, setMessage] = useState('')
const hasMinimumCakeRequired = useHasCakeBalance(minimumCakeBalance)
const hasMinimumCakeRequired = useHasCakeBalance(minimumCakeToRegister)
const [onPresentConfirmProfileCreation] = useModal(
<ConfirmProfileCreationModal
userName={userName}
Expand Down Expand Up @@ -243,7 +245,7 @@ const UserName: React.FC = () => {
</Button>
{!hasMinimumCakeRequired && (
<Text color="failure" mt="16px">
{TranslateString(1098, `A minimum of ${minimumCakeBalance} CAKE is required`)}
{TranslateString(1098, `A minimum of ${cakeCostToRegister} CAKE is required`)}
</Text>
)}
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,26 @@ const ProfileCreationProvider: React.FC = ({ children }) => {

// Initial checks
useEffect(() => {
let isSubscribed = true

const fetchData = async () => {
const bunnyFactoryContract = getBunnyFactoryContract()
const canMint = await bunnyFactoryContract.methods.canMint(account).call()
dispatch({ type: 'initialize', step: canMint ? 0 : 1 })

// When changing wallets quickly unmounting before the hasClaim finished causes a React error
if (isSubscribed) {
dispatch({ type: 'initialize', step: canMint ? 0 : 1 })
}
}

if (account) {
fetchData()
}

return () => {
isSubscribed = false
}
}, [account, dispatch])

const actions: ContextType['actions'] = useMemo(
Expand Down

0 comments on commit b8f7212

Please sign in to comment.