feat: 添加余额校验
This commit is contained in:
parent
eacfbf4406
commit
34ad3fe932
|
|
@ -8,8 +8,8 @@ import { getAddress, getBoardAddress } from 'utils/addressHelpers'
|
|||
export const fetchBoardUserAllowances = async (account: string) => {
|
||||
const calls = boardsConfig.map((board) => {
|
||||
const tokenAddresses = getAddress(board.tokenAddresses)
|
||||
const boardChefAdress = getAddress(board.contractAddress)
|
||||
return { address: tokenAddresses, name: 'allowance', params: [account, boardChefAdress] }
|
||||
const boardChefAddress = getAddress(board.contractAddress)
|
||||
return { address: tokenAddresses, name: 'allowance', params: [account, boardChefAddress] }
|
||||
})
|
||||
|
||||
const rawLpAllowances = await multicall(erc20ABI, calls)
|
||||
|
|
@ -39,15 +39,15 @@ export const fetchBoardUserTokenBalances = async (account: string) => {
|
|||
export const fetchBoardUserInfo = async (account: string) => {
|
||||
const data = await Promise.all(
|
||||
boardsConfig.map(async (board) => {
|
||||
const boardChefAdress = getAddress(board.contractAddress)
|
||||
const boardChefAddress = getAddress(board.contractAddress)
|
||||
const calls = [
|
||||
{
|
||||
address: boardChefAdress,
|
||||
address: boardChefAddress,
|
||||
name: 'userInfo',
|
||||
params: [account],
|
||||
},
|
||||
{
|
||||
address: boardChefAdress,
|
||||
address: boardChefAddress,
|
||||
name: 'pendingHCC',
|
||||
params: [account],
|
||||
},
|
||||
|
|
|
|||
|
|
@ -8,7 +8,14 @@ export interface ReferralConfigInfo {
|
|||
dividendSecond?: number
|
||||
receiveLimit?: number
|
||||
id?: number
|
||||
properties?: Record<string, unknown>
|
||||
properties?: {
|
||||
cover?: string
|
||||
name?: string
|
||||
price?: {
|
||||
HCC: string
|
||||
USDT: string
|
||||
}
|
||||
}
|
||||
type?: string
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import React, { useState, useEffect } from 'react'
|
||||
import { Flex, Button, Modal, Image } from '@pancakeswap/uikit'
|
||||
import { Flex, Button } from '@pancakeswap/uikit'
|
||||
import styled from 'styled-components'
|
||||
import { useTranslation } from 'contexts/Localization'
|
||||
import { useAccount } from 'state/userInfo/hooks'
|
||||
|
|
@ -10,6 +10,7 @@ import useToast from 'hooks/useToast'
|
|||
import { checkBuyResult } from 'services/referral'
|
||||
import useRefresh from 'hooks/useRefresh'
|
||||
import tokens from 'config/constants/tokens'
|
||||
import { useReferralCommanderConfigInfo } from 'state/referral/hooks'
|
||||
import { useCheckTokenBalance, useApproveReferral, useBuyTransaction } from '../hooks'
|
||||
|
||||
const Btn = styled(Button)`
|
||||
|
|
@ -22,6 +23,7 @@ const Btn = styled(Button)`
|
|||
const BuyActions: React.FC = () => {
|
||||
const { t } = useTranslation()
|
||||
const account = useAccount()
|
||||
const referralCommanderConfigInfo = useReferralCommanderConfigInfo()
|
||||
const [allowanceList, setAllowanceList] = useState({ usdt: 0, hcc: 0 })
|
||||
const usdtContract = useERC20(getAddress(tokens.usdt.address))
|
||||
const [loading, setLoading] = useState(false)
|
||||
|
|
@ -32,6 +34,7 @@ const BuyActions: React.FC = () => {
|
|||
const { onApprove: onUsdtApprove } = useApproveReferral(usdtContract)
|
||||
const { onApprove: onHccApprove } = useApproveReferral(hccContract)
|
||||
const buyTransaction = useBuyTransaction()
|
||||
const [onCheckBalance] = useCheckTokenBalance()
|
||||
const handleApprove = async (approve) => {
|
||||
try {
|
||||
setLoading(true)
|
||||
|
|
@ -52,8 +55,16 @@ const BuyActions: React.FC = () => {
|
|||
const handleBuy = async () => {
|
||||
setLoading(true)
|
||||
try {
|
||||
const res = await buyTransaction()
|
||||
setTxId(res.hash)
|
||||
const enoughBalance = onCheckBalance(
|
||||
referralCommanderConfigInfo.properties?.price?.HCC,
|
||||
referralCommanderConfigInfo.properties?.price?.USDT,
|
||||
)
|
||||
if (enoughBalance) {
|
||||
const res = await buyTransaction()
|
||||
setTxId(res.hash)
|
||||
} else {
|
||||
setLoading(false)
|
||||
}
|
||||
} catch (e: any) {
|
||||
setLoading(false)
|
||||
toastError(e?.error?.message || e.message)
|
||||
|
|
|
|||
|
|
@ -87,10 +87,10 @@ const RegimentalCom: React.FC = () => {
|
|||
<ContentDiv>
|
||||
<MainDiv>
|
||||
<HeadingDiv scale="xl" mb="24px" textAlign="center">
|
||||
{referralCommanderConfigInfo.properties.name}
|
||||
{referralCommanderConfigInfo?.properties?.name}
|
||||
</HeadingDiv>
|
||||
<LogoImage>
|
||||
<Image src={referralCommanderConfigInfo.properties.cover as string} width={249} height={249} />
|
||||
<Image src={referralCommanderConfigInfo?.properties?.cover} width={249} height={249} />
|
||||
{/* <Image src="/images/recommend/logo.svg" width={249} height={249} /> */}
|
||||
</LogoImage>
|
||||
|
||||
|
|
|
|||
|
|
@ -9,21 +9,20 @@ import { ethers, Contract } from 'ethers'
|
|||
import { getBalanceNumber, getDecimalAmountNumber } from 'utils/formatBalance'
|
||||
import { getWithdrawRewardParams, getBuyReferralParams } from 'services/referral'
|
||||
|
||||
export const useCheckTokenBalance = ({ isBnb, isHcc, isUsdt }) => {
|
||||
export const useCheckTokenBalance = () => {
|
||||
const { balance: usdtTokenBalance } = useTokenBalance(getAddress(tokensList.usdt.address))
|
||||
const { balance: hccTokenBalance } = useTokenBalance(getAddress(tokensList.hcc.address))
|
||||
const { toastWarning } = useToast()
|
||||
const { t } = useTranslation()
|
||||
const onCheck = useCallback(
|
||||
(hccAmount, usdtAmount) => {
|
||||
if (
|
||||
(isHcc && getBalanceNumber(hccTokenBalance) === hccAmount) ||
|
||||
(isUsdt && getBalanceNumber(usdtTokenBalance) === usdtAmount)
|
||||
) {
|
||||
if (getBalanceNumber(hccTokenBalance) <= hccAmount || getBalanceNumber(usdtTokenBalance) <= usdtAmount) {
|
||||
toastWarning(t('Insufficient Balance'))
|
||||
return false
|
||||
}
|
||||
return true
|
||||
},
|
||||
[isBnb, isHcc, isUsdt],
|
||||
[usdtTokenBalance, hccTokenBalance],
|
||||
)
|
||||
return [onCheck]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -41,8 +41,6 @@ const Nft: React.FC = () => {
|
|||
const account = useAccount()
|
||||
|
||||
const referralIsCommander = useReferralIsCommander()
|
||||
// const referralIsCommander = false
|
||||
console.log('referralIsCommander:', referralIsCommander)
|
||||
useEffect(() => {
|
||||
dispatch(fetchReferralInfoAsync(account))
|
||||
}, [account])
|
||||
|
|
|
|||
Loading…
Reference in New Issue