78 lines
2.2 KiB
TypeScript
78 lines
2.2 KiB
TypeScript
import { useCallback } from 'react'
|
|
import { useBlindBox } from 'hooks/useContract'
|
|
import { getAddress, getBlindBoxAddress } from 'utils/addressHelpers'
|
|
import { getOfficialPage, getOfficialPurchase, getPurchaseRecord, getNftDetail } from 'services/bazaar'
|
|
import { ethers, Contract } from 'ethers'
|
|
|
|
export const useGetOfficialPage = () => {
|
|
const data = async (params) => {
|
|
const result = await getOfficialPage(params)
|
|
return result
|
|
}
|
|
return data
|
|
}
|
|
|
|
// 授权usdt
|
|
export const useApproveUsdt = (tokenContract: Contract) => {
|
|
const handleApprove = useCallback(async () => {
|
|
try {
|
|
const tx = await tokenContract.approve(getBlindBoxAddress(), ethers.constants.MaxUint256)
|
|
const receipt = await tx.wait()
|
|
return receipt.status
|
|
} catch (e) {
|
|
return false
|
|
}
|
|
}, [tokenContract])
|
|
|
|
return { onApprove: handleApprove }
|
|
}
|
|
export const useApproveHcc = (tokenContract: Contract) => {
|
|
const handleApprove = useCallback(async () => {
|
|
try {
|
|
const tx = await tokenContract.approve(getBlindBoxAddress(), ethers.constants.MaxUint256)
|
|
const receipt = await tx.wait()
|
|
return receipt.status
|
|
} catch (e) {
|
|
return false
|
|
}
|
|
}, [tokenContract])
|
|
|
|
return { onApprove: handleApprove }
|
|
}
|
|
|
|
// 交易记录
|
|
export const useGetPurchaseRecord = () => {
|
|
const data = async (page, size) => {
|
|
const result = await getPurchaseRecord({ page, size })
|
|
console.log(result)
|
|
return result
|
|
}
|
|
return data
|
|
}
|
|
|
|
// 购买合约
|
|
export const useBuyTransaction = () => {
|
|
const blindBox = useBlindBox()
|
|
const address = getBlindBoxAddress()
|
|
const transaction = async (id, params) => {
|
|
const result = await getOfficialPurchase(id, params)
|
|
const { num, hccPrice, otherPaymentPrice, timestamp, code, type, sign } = result
|
|
const mintParams = [address, num, hccPrice, otherPaymentPrice, timestamp, code, type, sign]
|
|
const res = await blindBox.mint(...mintParams)
|
|
return res
|
|
}
|
|
return transaction
|
|
}
|
|
|
|
// 交易详情
|
|
export const useGetNftDetail = () => {
|
|
const data = async (token, params) => {
|
|
const result = await getNftDetail(token, params)
|
|
console.log(result)
|
|
return result
|
|
}
|
|
return data
|
|
}
|
|
|
|
export default useGetOfficialPage
|