From 43aec46f8733d37cbdbe726ea4febd3a60781c4e Mon Sep 17 00:00:00 2001 From: myf <> Date: Tue, 17 May 2022 21:00:56 +0800 Subject: [PATCH] ido --- src/App.tsx | 6 +- src/components/Menu/config.ts | 2 +- .../abi/{purchase.json => idoPurchase.json} | 0 src/config/constants/contracts.ts | 4 +- src/services/idoPurchase.ts | 10 + src/state/ido/index.ts | 66 +++++++ src/utils/contractHelpers.ts | 2 +- .../components/BoardCard/StakeAction.tsx | 1 + .../Exchange/components/ExchangeCard.tsx | 81 -------- .../components/IdoInput.tsx} | 5 +- src/views/Ido/components/IdoPurchaseCard.tsx | 182 ++++++++++++++++++ src/views/Ido/hooks/index.ts | 43 +++++ src/views/{Exchange => Ido}/index.tsx | 2 +- src/views/Referral/hooks/index.ts | 1 + 14 files changed, 314 insertions(+), 91 deletions(-) rename src/config/abi/{purchase.json => idoPurchase.json} (100%) create mode 100644 src/services/idoPurchase.ts create mode 100644 src/state/ido/index.ts delete mode 100644 src/views/Exchange/components/ExchangeCard.tsx rename src/views/{Exchange/components/ExchangeInput.tsx => Ido/components/IdoInput.tsx} (81%) create mode 100644 src/views/Ido/components/IdoPurchaseCard.tsx create mode 100644 src/views/Ido/hooks/index.ts rename src/views/{Exchange => Ido}/index.tsx (89%) diff --git a/src/App.tsx b/src/App.tsx index db999e0..28f686b 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -54,7 +54,7 @@ const Referral = lazy(() => import('./views/Referral')) const Board = lazy(() => import('./views/Board')) const Nft = lazy(() => import('./views/Nft')) const Announcement = lazy(() => import('./views/Announcement')) -const Exchange = lazy(() => import('./views/Exchange')) +const Ido = lazy(() => import('./views/Ido')) // This config is required for number formatting BigNumber.config({ @@ -100,8 +100,8 @@ const App: React.FC = () => { - - + + diff --git a/src/components/Menu/config.ts b/src/components/Menu/config.ts index 4c4874d..de4f67d 100644 --- a/src/components/Menu/config.ts +++ b/src/components/Menu/config.ts @@ -28,7 +28,7 @@ const config: (t: ContextApi['t']) => MenuEntry[] = (t) => [ { label: t('IDO Exchange'), icon: 'FarmIcon', - href: '/exchange', + href: '/Ido', }, { label: t('Exchange'), diff --git a/src/config/abi/purchase.json b/src/config/abi/idoPurchase.json similarity index 100% rename from src/config/abi/purchase.json rename to src/config/abi/idoPurchase.json diff --git a/src/config/constants/contracts.ts b/src/config/constants/contracts.ts index 8e9c3dc..d4137fc 100644 --- a/src/config/constants/contracts.ts +++ b/src/config/constants/contracts.ts @@ -32,8 +32,8 @@ export default { 56: '0x88F46EF2Ee08494D84942DCA3bd24cDEf7C88Ae2', // NEED CHANGE 邀请或则军团长收益 }, idoPurchase: { - 97: '0x3d437b81f8080c11d0e7c6b8a6c8185d8b92afee', - 56: '0x3d437b81f8080c11d0e7c6b8a6c8185d8b92afee', // NEED CHANGE IDO兑换 + 97: '0x2bAA17bDeC5cDF2943309B245EF5310FF9c46B01', + 56: '0x2bAA17bDeC5cDF2943309B245EF5310FF9c46B01', // NEED CHANGE IDO兑换 }, lotteryV2: { 97: '0x5790c3534F30437641541a0FA04C992799602998', diff --git a/src/services/idoPurchase.ts b/src/services/idoPurchase.ts new file mode 100644 index 0000000..eef2752 --- /dev/null +++ b/src/services/idoPurchase.ts @@ -0,0 +1,10 @@ +import request from 'utils/request' + +export const getPurchaseActivity = () => { + return request.request({ + url: '/high_city/app/api/purchasr/activity/current', + method: 'get', + }) +} + +export default getPurchaseActivity diff --git a/src/state/ido/index.ts b/src/state/ido/index.ts new file mode 100644 index 0000000..68668da --- /dev/null +++ b/src/state/ido/index.ts @@ -0,0 +1,66 @@ +import { createSlice } from '@reduxjs/toolkit' +import { getReferralRewardInfo, getReferralConfigInfo } from 'services/referral' +import { ReferralInfo, ReferralConfigType, ReferralConfigInfo } from 'types/referral' +import BigNumber from 'bignumber.js' +import erc20ABI from 'config/abi/erc20.json' +import multicall from 'utils/multicall' +import tokens from 'config/constants/tokens' +import { getAddress, getIdoPurchaseAddress } from 'utils/addressHelpers' +import { ReferralState } from '../types' + +const initialState: ReferralState = { + commanderConfigInfo: {}, + normalConfigInfo: {}, + isCommander: false, + rewardInfo: {}, +} +const payWayList = { + usdt: tokens.usdt, +} +export const referralSlice = createSlice({ + name: 'Ido', + initialState, + reducers: { + setReferralInfo: (state, action) => { + console.log(action) + }, + }, +}) + +// Actions +export const { setReferralInfo } = referralSlice.actions +// Thunks +// export const fetchReferralInfoAsync = (account) => async (dispatch) => { +// if (!account) { +// dispatch(setReferralInfo({})) +// return +// } +// const data: ReferralInfo = await getReferralRewardInfo() +// const configInfo: ReferralConfigInfo[] = await getReferralConfigInfo() +// dispatch( +// setReferralInfo({ +// commanderConfigInfo: configInfo?.find((item) => item.type === ReferralConfigType.COMMANDER) || {}, +// normalConfigInfo: configInfo?.find((item) => item.type === ReferralConfigType.NORMAL) || {}, +// isCommander: data.isCommander, +// rewardInfo: data.reward || {}, +// }), +// ) +// } + +export const fetchIdoUserAllowances = async (account: string) => { + const calls = Object.values(payWayList).map((payWayItem) => { + const tokenAddresses = getAddress(payWayItem.address) + const idoPurchaseAddress = getIdoPurchaseAddress() + return { address: tokenAddresses, name: 'allowance', params: [account, idoPurchaseAddress] } + }) + + const rawLpAllowances = await multicall(erc20ABI, calls) + const parsedLpAllowances = rawLpAllowances.map((balance) => { + return new BigNumber(balance).toNumber() + }) + return { + usdt: parsedLpAllowances[0], + } +} + +export default referralSlice.reducer diff --git a/src/utils/contractHelpers.ts b/src/utils/contractHelpers.ts index 4c07a56..e48fb78 100644 --- a/src/utils/contractHelpers.ts +++ b/src/utils/contractHelpers.ts @@ -60,7 +60,7 @@ import chainlinkOracleAbi from 'config/abi/chainlinkOracle.json' import MultiCallAbi from 'config/abi/Multicall.json' import bunnySpecialCakeVaultAbi from 'config/abi/bunnySpecialCakeVault.json' import bunnySpecialPredictionAbi from 'config/abi/bunnySpecialPrediction.json' -import idoPurchase from 'config/abi/purchase.json' +import idoPurchase from 'config/abi/idoPurchase.json' import { ChainLinkOracleContract, PredictionsContract } from './types' const getContract = (abi: any, address: string, signer?: ethers.Signer | ethers.providers.Provider) => { diff --git a/src/views/Board/components/BoardCard/StakeAction.tsx b/src/views/Board/components/BoardCard/StakeAction.tsx index 26ef3da..70a84b6 100644 --- a/src/views/Board/components/BoardCard/StakeAction.tsx +++ b/src/views/Board/components/BoardCard/StakeAction.tsx @@ -39,6 +39,7 @@ const StakeAction: React.FC = ({ stakedBalance, tokenBalan const { t } = useTranslation() const { toastWarning } = useToast() const { onStake } = useStakeBoard(pid) + console.log(onStake) const { onUnstake } = useUnstakeBoard(pid) const { onUnstake: onUnstakeForce } = useUnstakeForceBoard(pid) const { tokenDecimals = 18, minStakeAmount, userData } = useBoardsFromPid(pid) diff --git a/src/views/Exchange/components/ExchangeCard.tsx b/src/views/Exchange/components/ExchangeCard.tsx deleted file mode 100644 index 74651d2..0000000 --- a/src/views/Exchange/components/ExchangeCard.tsx +++ /dev/null @@ -1,81 +0,0 @@ -import React, { useMemo, useState, useEffect } from 'react' -import styled, { keyframes } from 'styled-components' -import { useTranslation } from 'contexts/Localization' -import { Flex, Text, Button } from '@pancakeswap/uikit' -import { useIdoPurchase } from 'hooks/useContract' - -import { useAccount } from 'state/userInfo/hooks' -import UnlockButton from 'components/UnlockButton' -import ExchangeInput from './ExchangeInput' - -const FCard = styled.div` - width: 650px; - background: ${(props) => props.theme.card.background}; - border-radius: 32px; - box-shadow: 0px 2px 30px 0px rgba(0, 0, 0, 0.1); - display: flex; - flex-direction: column; - position: relative; - text-align: center; - padding: 50px 70px; -` -const HeaderText = styled(Text)` - font-size: 48px; - color: #280d5f; -` -const TimeText = styled(Text)` - color: #7a6eaa; - font-size: 22px; -` - -const RateText = styled(Text)` - font-size: 30px; - color: #280d5f; -` -const RateNumber = styled(Text)` - font-size: 30px; - color: #1fc7d4; -` -const FooterButton = styled.div` - margin-top: 50px; - width: 100%; -` -const UnlockButtonDiv = styled(UnlockButton)` - width: 100%; -` - -const ExchangeCard: React.FC = () => { - const { t } = useTranslation() - - const account = useAccount() - - const useBuyTransaction = () => { - return useIdoPurchase() - } - console.log(useBuyTransaction()) - // useEffect(() => {}, []) - - return ( - - {t('IDO exchange in the first phase')} - {t('Opening time of next exchange period:')}2022.5.16 00:00 - - - {t('Exchange rate')}: - 1USDT=100HCC - - - {t('gross')}:100000HCC - - {t('remaining quantity')}:100000HCC - - - - - {account ? : } - - - ) -} - -export default ExchangeCard diff --git a/src/views/Exchange/components/ExchangeInput.tsx b/src/views/Ido/components/IdoInput.tsx similarity index 81% rename from src/views/Exchange/components/ExchangeInput.tsx rename to src/views/Ido/components/IdoInput.tsx index 89e28d9..312b72b 100644 --- a/src/views/Exchange/components/ExchangeInput.tsx +++ b/src/views/Ido/components/IdoInput.tsx @@ -22,15 +22,16 @@ const CoinText = styled(Text)` interface InputProps { name: string value?: number | string + onChange: (e: React.FormEvent) => void } -const ExchangeInput: React.FC = ({ name, value }) => { +const ExchangeInput: React.FC = ({ name, value, onChange }) => { const { t } = useTranslation() return ( {name} - + ) } diff --git a/src/views/Ido/components/IdoPurchaseCard.tsx b/src/views/Ido/components/IdoPurchaseCard.tsx new file mode 100644 index 0000000..73454d2 --- /dev/null +++ b/src/views/Ido/components/IdoPurchaseCard.tsx @@ -0,0 +1,182 @@ +import React, { useMemo, useState, useEffect, useCallback } from 'react' +import styled, { keyframes } from 'styled-components' +import { useTranslation } from 'contexts/Localization' +import { Flex, Text, Button, Input } from '@pancakeswap/uikit' +import { getAddress, getIdoPurchaseAddress } from 'utils/addressHelpers' +import { getPurchaseActivity } from 'services/idoPurchase' +// import { useIdoPurchase } from 'hooks/useContract' +import { fetchIdoUserAllowances } from 'state/ido' +import multicall from 'utils/multicall' +import { useERC20, useIdoPurchase } from 'hooks/useContract' +import idoPurchaseABI from 'config/abi/idoPurchase.json' + +import tokens from 'config/constants/tokens' +import { useAccount } from 'state/userInfo/hooks' +import UnlockButton from 'components/UnlockButton' +import ExchangeInput from './IdoInput' +import { useApproveIdo, useBuyTransaction } from '../hooks' + +const FCard = styled.div` + width: 650px; + background: ${(props) => props.theme.card.background}; + border-radius: 32px; + box-shadow: 0px 2px 30px 0px rgba(0, 0, 0, 0.1); + display: flex; + flex-direction: column; + position: relative; + text-align: center; + padding: 50px 70px; +` +const HeaderText = styled(Text)` + font-size: 48px; + color: #280d5f; +` +const TimeText = styled(Text)` + color: #7a6eaa; + font-size: 22px; +` + +const RateText = styled(Text)` + font-size: 30px; + color: #280d5f; +` +const RateNumber = styled(Text)` + font-size: 30px; + color: #1fc7d4; +` +const FooterButton = styled.div` + margin-top: 50px; + width: 100%; +` +const UnlockButtonDiv = styled(UnlockButton)` + width: 100%; +` + +const ExchangeCard: React.FC = () => { + const { t } = useTranslation() + + const account = useAccount() + + const [allowanceList, setAllowanceList] = useState({ usdt: 0 }) + const getAllowances = async () => { + const allowances = await fetchIdoUserAllowances(account) + setAllowanceList({ + usdt: allowances.usdt, + }) + } + + const getPurchasrInfo = async () => { + const data = await getPurchaseActivity() + console.log(data) + } + + useEffect(() => { + if (account) { + getAllowances() + getPurchasrInfo() + } + }, [account]) + + const usdtContract = useERC20(getAddress(tokens.usdt.address)) + const { onApprove: onUsdtApprove } = useApproveIdo(usdtContract) + + const [loading, setLoading] = useState(false) + const handleApprove = async (approve) => { + try { + setLoading(true) + await approve() + setLoading(false) + getAllowances() + } catch (e) { + console.error(e) + } + } + + const [usdtPrice, setUsdtPrice] = useState('') + const [hccPrice, setHccPrice] = useState('') + + const handleUsdtChange = (e: React.FormEvent) => { + const price = Number(e.currentTarget.value) * Number(hccPrice) + console.log(price) + setUsdtPrice(e.currentTarget.value) + setHccPrice(price.toString()) + } + const handleHccChange = (e: React.FormEvent) => { + setHccPrice(e.currentTarget.value) + } + const buyTransaction = useBuyTransaction() + // 立即兑换 + const immediatelyChange = async () => { + console.log('立即兑换') + // "inputs": [ + // { "internalType": "uint256", "name": "round", "type": "uint256" }, + // { "internalType": "uint256", "name": "amount", "type": "uint256" } + // ], + // "name": "purchase", + const params = [ + '0x8ec8610ef88cdd222d45a22dd98c55a98b979664', + '50000000000000000000', + '0', + '1652787744', + '1525022104880209921', + '0x18e77720bdfb43fc6b0a9a9050b7001fcd8f859041bdb0986d053366238e41582ceacaaca5e8c9ac815f6cc054c7135638b38acf5cd0aeb8fe6112effe7901d81b', + ] + const res = await buyTransaction() + console.log(res) + // setTxId(res.hash) + const params1 = [1, 2] + // const tx = await useIdoPurchase.purchase(...params) + // console.log(idoPurchaseABI) + // const Info = await multicall(idoPurchaseABI, [ + // { + // address: getIdoPurchaseAddress(), + // name: 'purchase', + // params: params1, + // }, + // ]) + // console.log(Info) + } + + return ( + + {t('IDO exchange in the first phase')} + {t('Opening time of next exchange period:')}2022.5.16 00:00 + + + {t('Exchange rate')}: + 1USDT=100HCC + + + {t('gross')}:100000HCC + + {t('remaining quantity')}:100000HCC + + + + + {account ? ( + allowanceList.usdt ? ( + + ) : ( + + ) + ) : ( + + )} + + + ) +} + +export default ExchangeCard diff --git a/src/views/Ido/hooks/index.ts b/src/views/Ido/hooks/index.ts new file mode 100644 index 0000000..276528a --- /dev/null +++ b/src/views/Ido/hooks/index.ts @@ -0,0 +1,43 @@ +import { useState, useCallback } from 'react' +import { useIdoPurchase, useReferralchef } from 'hooks/useContract' +import useTokenBalance from 'hooks/useTokenBalance' +import erc20ABI from 'config/abi/erc20.json' +import { getAddress, getIdoPurchaseAddress } from 'utils/addressHelpers' +import { getPurchaseActivity } from 'services/idoPurchase' +import BigNumber from 'bignumber.js' +import multicall from 'utils/multicall' +import tokensList from 'config/constants/tokens' +import useToast from 'hooks/useToast' +import { useTranslation } from 'contexts/Localization' +import { ethers, Contract } from 'ethers' +import { getBalanceNumber, getDecimalAmountNumber } from 'utils/formatBalance' + +import { getWithdrawRewardParams, getBuyReferralParams } from 'services/referral' + +export const useApproveIdo = (tokenContract: Contract) => { + const handleApprove = useCallback(async () => { + try { + const tx = await tokenContract.approve(getIdoPurchaseAddress(), ethers.constants.MaxUint256) + const receipt = await tx.wait() + return receipt.status + } catch (e) { + return false + } + }, [tokenContract]) + + return { onApprove: handleApprove } +} + +export const useBuyTransaction = () => { + const idoPurchase = useIdoPurchase() + const transaction = async () => { + const data = await getPurchaseActivity() + const { id } = data + const params = [id, 1110000000000] + const res = await idoPurchase.purchase(...params) + return res + } + return transaction +} + +export default useApproveIdo diff --git a/src/views/Exchange/index.tsx b/src/views/Ido/index.tsx similarity index 89% rename from src/views/Exchange/index.tsx rename to src/views/Ido/index.tsx index e4ec50c..0c10796 100644 --- a/src/views/Exchange/index.tsx +++ b/src/views/Ido/index.tsx @@ -1,6 +1,6 @@ import React, { useState, useEffect } from 'react' import styled from 'styled-components' -import ExchangeCard from './components/ExchangeCard' +import ExchangeCard from './components/IdoPurchaseCard' const PageContent = styled.div` min-height: calc(100vh - 64px); diff --git a/src/views/Referral/hooks/index.ts b/src/views/Referral/hooks/index.ts index 9f99c47..112d584 100644 --- a/src/views/Referral/hooks/index.ts +++ b/src/views/Referral/hooks/index.ts @@ -33,6 +33,7 @@ export const useBuyTransaction = () => { const data = await getBuyReferralParams() const { to, hccPrice, otherPaymentPrice, timestamp, code, sign } = data const params = [to, hccPrice, otherPaymentPrice, timestamp, code, sign] + console.log(referralContract) const res = await referralContract.mint(...params) return res }