fix
Helius
2022-07-22 a39df92a62edee1cee74c8aee97489dd16de5b66
src/main/java/cc/mrbird/febs/dapp/service/impl/DappWalletServiceImpl.java
@@ -1,5 +1,6 @@
package cc.mrbird.febs.dapp.service.impl;
import cc.mrbird.febs.common.configure.i18n.MessageSourceUtils;
import cc.mrbird.febs.common.contants.AppContants;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.common.exception.FebsException;
@@ -9,12 +10,14 @@
import cc.mrbird.febs.dapp.chain.ChainEnum;
import cc.mrbird.febs.dapp.chain.ChainService;
import cc.mrbird.febs.dapp.chain.ContractChainService;
import cc.mrbird.febs.dapp.dto.PriceDto;
import cc.mrbird.febs.dapp.dto.RecordInPageDto;
import cc.mrbird.febs.dapp.dto.TransferDto;
import cc.mrbird.febs.dapp.dto.WalletOperateDto;
import cc.mrbird.febs.dapp.entity.*;
import cc.mrbird.febs.dapp.mapper.*;
import cc.mrbird.febs.dapp.service.DappWalletService;
import cc.mrbird.febs.dapp.utils.BoxUtil;
import cc.mrbird.febs.dapp.vo.WalletInfoVo;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
@@ -31,7 +34,7 @@
import java.util.Map;
/**
 * @author
 * @author
 * @date 2022-03-18
 **/
@Slf4j
@@ -42,24 +45,24 @@
    private final DappMemberDao dappMemberDao;
    private final DappWalletMineDao dappWalletMineDao;
    private final DappWalletCoinDao dappWalletCoinDao;
    private final DappIdoAssetsDao dappIdoAssetsDao;
    private final DappFundFlowDao dappFundFlowDao;
    private final DappAccountMoneyChangeDao dappAccountMoneyChangeDao;
    private final RedisUtils redisUtils;
    private final DataDictionaryCustomMapper dataDictionaryCustomMapper;
    @Override
    public WalletInfoVo walletInfo() {
//        DappMemberEntity member = LoginUserUtil.getAppUser();
//        DappWalletCoinEntity walletCoin = dappWalletCoinDao.selectByMemberId(member.getId());
//        DappWalletMineEntity walletMine = dappWalletMineDao.selectByMemberId(member.getId());
        DappMemberEntity member = LoginUserUtil.getAppUser();
        DappIdoAssetsEntity idoAssets = dappIdoAssetsDao.selectByMemberId(member.getId());
        WalletInfoVo walletInfo = new WalletInfoVo();
//        walletInfo.setInviteId(member.getInviteId());
//        walletInfo.setTotalMine(walletMine.getTotalAmount());
//        walletInfo.setAvailableMine(walletMine.getAvailableAmount());
//        walletInfo.setAvailableWallet(walletCoin.getAvailableAmount());
        walletInfo.setTotalChild(10000);
        walletInfo.setTotalChild(41451);
        walletInfo.setBoxCnt(idoAssets.getBoxCnt());
        walletInfo.setCoinAmount(idoAssets.getCoinAmount());
        walletInfo.setUsdtAmount(idoAssets.getUsdtAmount());
        walletInfo.setInviteId(member.getInviteId());
        List<DappMemberEntity> childList = dappMemberDao.selectChildMemberDirectOrNot(member.getInviteId(), 1);
        walletInfo.setDirectCnt(CollUtil.isNotEmpty(childList) ? childList.size() : 0);
        return walletInfo;
    }
@@ -136,8 +139,12 @@
        DappMemberEntity member = LoginUserUtil.getAppUser();
        DappFundFlowEntity dappFundFlowEntity = new DappFundFlowEntity();
        dappFundFlowEntity.setType(recordInPageDto.getType());
        if (recordInPageDto.getType() != null && recordInPageDto.getType() != 0) {
            dappFundFlowEntity.setType(recordInPageDto.getType());
        }
        dappFundFlowEntity.setMemberId(member.getId());
        dappFundFlowEntity.setStatus(2);
        IPage<DappFundFlowEntity> records = dappFundFlowDao.selectInPage(page, dappFundFlowEntity);
        return records.getRecords();
    }
@@ -202,34 +209,41 @@
        DappMemberEntity member = LoginUserUtil.getAppUser();
        if (transferDto.getId() == null) {
            if (DappFundFlowEntity.TYPE_SALE == transferDto.getType()) {
                String hasStart = redisUtils.getString(AppContants.SYSTEM_START_FLAG);
                if (!"start".equals(hasStart)) {
                    throw new FebsException("系统还未启动");
                }
            if (new BigDecimal(300).compareTo(transferDto.getAmount()) < 0) {
                throw new FebsException("Do Not More Than 300 USDT");
            }
            DappFundFlowEntity fundFlow = new DappFundFlowEntity(member.getId(), transferDto.getAmount(), transferDto.getType(), 1, transferDto.getFee(), transferDto.getTxHash());
            fundFlow.setNewestPrice(transferDto.getPrice());
            dappFundFlowDao.insert(fundFlow);
            return fundFlow.getId();
        }
        if ("success".equals(transferDto.getFlag())) {
            DappFundFlowEntity flow = dappFundFlowDao.selectById(transferDto.getId());
            flow.setFromHash(transferDto.getTxHash());
            dappFundFlowDao.updateById(flow);
        } else {
            dappFundFlowDao.deleteById(transferDto.getId());
        }
            if ("success".equals(transferDto.getFlag())) {
                DappFundFlowEntity flow = dappFundFlowDao.selectById(transferDto.getId());
                flow.setFromHash(transferDto.getTxHash());
                dappFundFlowDao.updateById(flow);
            } else {
                DappFundFlowEntity flow = dappFundFlowDao.selectById(transferDto.getId());
                if (flow.getStatus() == 1) {
                    dappFundFlowDao.deleteById(transferDto.getId());
                }
            }
        }
        return null;
    }
    @Override
    public Map<String, BigDecimal> calPrice() {
    public Map<String, BigDecimal> calPrice(PriceDto priceDto) {
        DappMemberEntity member = LoginUserUtil.getAppUser();
        String hasStart = redisUtils.getString(AppContants.SYSTEM_START_FLAG);
        if (!"start".equals(hasStart)) {
            HashMap<String, BigDecimal> map = new HashMap<>();
            map.put("x", new BigDecimal("0.075"));
            map.put("y", new BigDecimal("100000"));
            return map;
        }
        ContractChainService tfcInstance = ChainService.getInstance(ChainEnum.BSC_TFC.name());
        // u剩余数量
@@ -237,13 +251,54 @@
        // 源池代币剩余数量
        BigDecimal sourceCoin = tfcInstance.balanceOf(ChainEnum.BSC_USDT_SOURCE.getAddress());
        // 用户代币剩余数量
        BigDecimal coin = tfcInstance.balanceOf(member.getAddress());
        BigDecimal x = sourceU.divide(sourceCoin, tfcInstance.decimals(), RoundingMode.HALF_DOWN);
        BigDecimal y = sourceU.divide(sourceCoin.add(coin), tfcInstance.decimals(), RoundingMode.HALF_DOWN);
        BigDecimal coin = priceDto.getAmount();
        BigDecimal x = sourceU.divide(sourceCoin, tfcInstance.decimals(), RoundingMode.HALF_UP);
        BigDecimal y = sourceU.divide(sourceCoin.add(coin), tfcInstance.decimals(), RoundingMode.HALF_UP);
        log.info("购买价格:{}, 出卖价格:{}", x, y);
        HashMap<String, BigDecimal> map = new HashMap<>();
        map.put("x", BigDecimal.ONE);
        map.put("y", BigDecimal.valueOf(0.8D));
        map.put("x", x);
        map.put("y", y);
        return map;
    }
    @Override
    public int boxSurprise() {
        DappMemberEntity member = LoginUserUtil.getAppUser();
        DappIdoAssetsEntity idoAssets = dappIdoAssetsDao.selectByMemberId(member.getId());
        if (idoAssets.getBoxCnt() < 1) {
            throw new FebsException("盲盒数量不足");
        }
        idoAssets.setBoxCnt(idoAssets.getBoxCnt() - 1);
        dappIdoAssetsDao.updateById(idoAssets);
        BoxUtil.Box box = BoxUtil.openBox();
        DappFundFlowEntity boxFundFlow = new DappFundFlowEntity(member.getId(), new BigDecimal(box.getIndex()), 6, 2, BigDecimal.ZERO);
        dappFundFlowDao.insert(boxFundFlow);
        new Thread(() -> {
            try {
                int count = box.getIndex();
                while (count > 0) {
                    ChainService.getInstance(ChainEnum.BSC_NFT_SDC.name()).safeMintNFT(member.getAddress());
                    Thread.sleep(5000);
                    count--;
                }
            } catch (Exception e) {
                e.printStackTrace();
                log.error("发放卡牌错误", e);
            }
        }).start();
        return box.getIndex();
    }
    @Override
    public IPage<DappIdoAssetsEntity> selectIdoAchieveInPage(DappIdoAssetsEntity dappIdoAssetsEntity, QueryRequest request) {
        Page<DappIdoAssetsEntity> page = new Page<>(request.getPageNum(), request.getPageSize());
        return this.dappIdoAssetsDao.selectAchieveListInPage(dappIdoAssetsEntity, page);
    }
}