From 89b7fb1d316cfce7eb98a27c8d668da493933f7f Mon Sep 17 00:00:00 2001 From: xiaoyong931011 <15274802129@163.com> Date: Fri, 25 Feb 2022 16:29:07 +0800 Subject: [PATCH] 20222223 --- src/main/java/com/xcong/excoin/modules/coin/service/impl/BlockCoinServiceImpl.java | 352 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 338 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/xcong/excoin/modules/coin/service/impl/BlockCoinServiceImpl.java b/src/main/java/com/xcong/excoin/modules/coin/service/impl/BlockCoinServiceImpl.java index aea8e75..a7d058c 100644 --- a/src/main/java/com/xcong/excoin/modules/coin/service/impl/BlockCoinServiceImpl.java +++ b/src/main/java/com/xcong/excoin/modules/coin/service/impl/BlockCoinServiceImpl.java @@ -5,9 +5,8 @@ import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.StrUtil; import com.xcong.excoin.common.enumerates.CoinTypeEnum; -import com.xcong.excoin.modules.blackchain.service.BtcService; -import com.xcong.excoin.modules.blackchain.service.EthService; -import com.xcong.excoin.modules.blackchain.service.UsdtService; +import com.xcong.excoin.modules.blackchain.model.*; +import com.xcong.excoin.modules.blackchain.service.*; import com.xcong.excoin.modules.coin.service.BlockCoinService; import com.xcong.excoin.modules.member.dao.MemberCoinAddressDao; import com.xcong.excoin.modules.member.dao.MemberCoinChargeDao; @@ -18,18 +17,27 @@ import com.xcong.excoin.modules.member.entity.MemberEntity; import com.xcong.excoin.modules.member.entity.MemberWalletCoinEntity; import com.xcong.excoin.utils.LogRecordUtils; +import com.xcong.excoin.utils.RedisUtils; import com.xcong.excoin.utils.ThreadPoolUtils; import com.xcong.excoin.utils.dingtalk.DingTalkUtils; -import com.xcong.excoin.utils.mail.SmsSend; +import com.xcong.excoin.utils.mail.Sms106Send; +import com.xcong.excoin.utils.mail.SubMailSend; import lombok.extern.slf4j.Slf4j; +import lombok.val; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.math.BigDecimal; import java.math.RoundingMode; +import java.text.ParseException; +import java.text.SimpleDateFormat; import java.util.Date; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * @author wzy @@ -47,6 +55,18 @@ private MemberCoinChargeDao memberCoinChargeDao; @Resource private MemberWalletCoinDao memberWalletCoinDao; + + @Resource + private UsdtEthService usdtEthService; + + @Resource + private RedisUtils redisUtils; + + private final static String EOS_SEQ_KEY = "eos_seq_key"; + + private final static String xrp_update_key = "xrp_update_key"; + + private final static String trc20_update_key = "trc20_update_key"; @Transactional(rollbackFor = Exception.class) @Override @@ -84,7 +104,7 @@ BigDecimal newBalance = balance.subtract(early); memberWalletCoinDao.updateBlockBalance(walletCoinEntity.getId(), newBalance, balance, 0); - String orderNo = insertCoinCharge(address, memberId, newBalance, CoinTypeEnum.USDT.name(), "ERC20", balance); + String orderNo = insertCoinCharge(address, memberId, newBalance, CoinTypeEnum.USDT.name(), "ERC20", balance, null); // 插入财务记录 LogRecordUtils.insertMemberAccountMoneyChange(memberId, "转入", newBalance, CoinTypeEnum.USDT.name(), 1, 1); @@ -92,7 +112,9 @@ MemberEntity member = memberDao.selectById(memberId); if (StrUtil.isNotBlank(member.getPhone())) { String amount = newBalance.toPlainString() + "USDT-ERC20"; - SmsSend.sendRechargeMsg(member.getPhone(), DateUtil.format(new Date(), DatePattern.NORM_DATETIME_MINUTE_PATTERN), amount, orderNo); + Sms106Send.sendRechargeMsg(member.getPhone(), DateUtil.format(new Date(), DatePattern.NORM_DATETIME_MINUTE_PATTERN), orderNo); + } else { + SubMailSend.sendRechargeMail(member.getEmail(), DateUtil.format(new Date(), DatePattern.NORM_DATETIME_MINUTE_PATTERN), orderNo); } } } @@ -110,7 +132,7 @@ Long memberId = coinAddressEntity.getMemberId(); BigDecimal balance = EthService.getEthBlance(address); - if (balance != null && new BigDecimal("0.008").compareTo(balance) < 0) { + if (balance != null && new BigDecimal("0.01").compareTo(balance) < 0) { MemberWalletCoinEntity walletCoin = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId, CoinTypeEnum.ETH.name()); if (walletCoin == null) { @@ -132,7 +154,7 @@ BigDecimal newBalance = balance.subtract(early); memberWalletCoinDao.updateBlockBalance(walletCoin.getId(), newBalance, balance, 0); - String orderNo = insertCoinCharge(address, memberId, newBalance, CoinTypeEnum.ETH.name(), null, balance); + String orderNo = insertCoinCharge(address, memberId, newBalance, CoinTypeEnum.ETH.name(), null, balance, null); // 插入财务记录 LogRecordUtils.insertMemberAccountMoneyChange(memberId, "转入", newBalance, CoinTypeEnum.ETH.name(), 1, 1); @@ -141,7 +163,9 @@ MemberEntity member = memberDao.selectById(memberId); if (StrUtil.isNotBlank(member.getPhone())) { String amount = newBalance.toPlainString() + "ETH"; - SmsSend.sendRechargeMsg(member.getPhone(), DateUtil.format(new Date(), DatePattern.NORM_DATETIME_MINUTE_PATTERN), amount, orderNo); + Sms106Send.sendRechargeMsg(member.getPhone(), DateUtil.format(new Date(), DatePattern.NORM_DATETIME_MINUTE_PATTERN), orderNo); + } else { + SubMailSend.sendRechargeMail(member.getEmail(), DateUtil.format(new Date(), DatePattern.NORM_DATETIME_MINUTE_PATTERN), orderNo); } } } @@ -180,13 +204,15 @@ BigDecimal newBalance = balance.subtract(early); memberWalletCoinDao.updateBlockBalance(walletCoin.getId(), newBalance, balance, 0); - String orderNo = insertCoinCharge(address, memberId, newBalance, CoinTypeEnum.USDT.name(), "OMNI", balance); + String orderNo = insertCoinCharge(address, memberId, newBalance, CoinTypeEnum.USDT.name(), "OMNI", balance, null); ThreadPoolUtils.sendDingTalk(5); MemberEntity member = memberDao.selectById(memberId); if (StrUtil.isNotBlank(member.getPhone())) { String amount = newBalance.toPlainString() + "USDT-OMNI"; - SmsSend.sendRechargeMsg(member.getPhone(), DateUtil.format(new Date(), DatePattern.NORM_DATETIME_MINUTE_PATTERN), amount, orderNo); + Sms106Send.sendRechargeMsg(member.getPhone(), DateUtil.format(new Date(), DatePattern.NORM_DATETIME_MINUTE_PATTERN), orderNo); + } else { + SubMailSend.sendRechargeMail(member.getEmail(), DateUtil.format(new Date(), DatePattern.NORM_DATETIME_MINUTE_PATTERN), orderNo); } } } @@ -226,18 +252,314 @@ BigDecimal newBalance = balance.subtract(early); memberWalletCoinDao.updateBlockBalance(walletCoin.getId(), newBalance, balance, 0); - String orderNo = insertCoinCharge(address, memberId, newBalance, CoinTypeEnum.BTC.name(), null, balance); + String orderNo = insertCoinCharge(address, memberId, newBalance, CoinTypeEnum.BTC.name(), null, balance, null); LogRecordUtils.insertMemberAccountMoneyChange(memberId, "转入", newBalance, CoinTypeEnum.BTC.name(), 1, 1); ThreadPoolUtils.sendDingTalk(5); MemberEntity member = memberDao.selectById(memberId); if (StrUtil.isNotBlank(member.getPhone())) { String amount = newBalance.toPlainString() + "BTC"; - SmsSend.sendRechargeMsg(member.getPhone(), DateUtil.format(new Date(), DatePattern.NORM_DATETIME_MINUTE_PATTERN), amount, orderNo); + Sms106Send.sendRechargeMsg(member.getPhone(), DateUtil.format(new Date(), DatePattern.NORM_DATETIME_MINUTE_PATTERN), orderNo); + } else { + SubMailSend.sendRechargeMail(member.getEmail(), DateUtil.format(new Date(), DatePattern.NORM_DATETIME_MINUTE_PATTERN), orderNo); } } } } + } + } + + @Override + public void updateEos() { + // 获取上次读取的序号 + int pos = 0; + //每次获取的条数 + int offset = 10; + + String eosSeq = redisUtils.getString(EOS_SEQ_KEY); + if (StringUtils.isNotBlank(eosSeq)) { + pos = Integer.valueOf(eosSeq); + } + // 记录最大的seq + int seq = pos; + List<EosResult> actions = EosService.getActions(pos, offset); + if (CollectionUtils.isNotEmpty(actions)) { + for (EosResult eosResult : actions) { + String to = eosResult.getTo(); + Integer accountActionSeq = eosResult.getAccountActionSeq(); + if (accountActionSeq > seq) { + seq = accountActionSeq; + } + if (!EosService.ACCOUNT.equals(to)) { + // 判断是否是收款 + continue; + } + // 处理收款 + String quantity = eosResult.getQuantity(); + String memo = eosResult.getMemo(); + if (StringUtils.isBlank(memo)) { + // 没有标记的跳过 + continue; + } + if (StringUtils.isNotBlank(quantity)) { + // 转账额 + String amountStr = quantity.split("")[0]; + BigDecimal amount = new BigDecimal(amountStr); + List<MemberCoinAddressEntity> memberCoinAddress = memberCoinAddressDao.selectAllBlockAddressBySymbolAndTag(CoinTypeEnum.EOS.name(), memo); + if (CollectionUtils.isNotEmpty(memberCoinAddress)) { + MemberCoinAddressEntity memberCoinAddressEntity = memberCoinAddress.get(0); + // 用户ID + Long memberId = memberCoinAddressEntity.getMemberId(); + MemberWalletCoinEntity memberWalletCoinEntity = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId, CoinTypeEnum.EOS.name()); + if (memberCoinAddressEntity != null) { + memberWalletCoinDao.updateBlockBalance(memberWalletCoinEntity.getId(), amount, BigDecimal.ZERO, 0); + // 添加冲币记录 + String orderNo = insertCoinCharge(EosService.ACCOUNT, memberId, amount, CoinTypeEnum.EOS.name(), memo, BigDecimal.ZERO, null); + LogRecordUtils.insertMemberAccountMoneyChange(memberId, "转入", amount, CoinTypeEnum.EOS.name(), 1, 1); + + ThreadPoolUtils.sendDingTalk(5); + MemberEntity member = memberDao.selectById(memberId); + if (StrUtil.isNotBlank(member.getPhone())) { + //String amountEos = amountStr + "EOS"; + Sms106Send.sendRechargeMsg(member.getPhone(), DateUtil.format(new Date(), DatePattern.NORM_DATETIME_MINUTE_PATTERN), orderNo); + } else { + SubMailSend.sendRechargeMail(member.getEmail(), DateUtil.format(new Date(), DatePattern.NORM_DATETIME_MINUTE_PATTERN), orderNo); + } + } + } + } + } + } + // 最后更新seq 即下次查询的起始位置 在本次最大的基础上加一 + if (seq > 0 && seq > pos) { + redisUtils.set(EOS_SEQ_KEY, seq + 1); + } + } + + public void updateXrp() { + // 首先去查redis上的上次同步时间 + Object lastUpdateTime = redisUtils.get(xrp_update_key); + SimpleDateFormat format = new SimpleDateFormat("YYYY-MM-dd hh:mm:ss"); + Date start = null; + if (lastUpdateTime == null) { + // 没有 说明是第一次同步 此时从第一天开始同步2020 0716开始 + try { + start = format.parse("2020-07-16 12:00:00"); + } catch (ParseException e) { + e.printStackTrace(); + } + } else { + // 有上次时间 + try { + start = format.parse(lastUpdateTime.toString()); + } catch (ParseException e) { + e.printStackTrace(); + } + } + + // 去查询上次同步时间后的所有记录 + XrpTransResult result = XrpService.getTransactions(start); + // 写入本次更新时间 + String updateTime = format.format(new Date()); + redisUtils.set(xrp_update_key, updateTime); + // 判断有无 + List<XrpTransactions> list = result.getTransactions(); + if (CollectionUtils.isNotEmpty(list)) { + // 不为空 说明有转入记录 + for (XrpTransactions item : list) { + XrpTx data = item.getTx(); + Integer amountOld = data.getAmount(); + // 除以1000000 + BigDecimal amount = new BigDecimal(amountOld).divide(new BigDecimal(1000000)); + Integer memoInt = data.getDestinationTag(); + // 没有标签直接返回 + if (memoInt == null) { + continue; + } + String memo = memoInt.toString(); + String destination = data.getDestination(); + // 判断收款人是不是系统账号 + if (!XrpService.ACCOUNT.equals(destination)) { + continue; + } + List<MemberCoinAddressEntity> memberCoinAddress = memberCoinAddressDao.selectAllBlockAddressBySymbolAndTag(CoinTypeEnum.XRP.name(), memo); + if (CollectionUtils.isNotEmpty(memberCoinAddress)) { + MemberCoinAddressEntity memberCoinAddressEntity = memberCoinAddress.get(0); + // 用户ID + Long memberId = memberCoinAddressEntity.getMemberId(); + MemberWalletCoinEntity memberWalletCoinEntity = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId, CoinTypeEnum.XRP.name()); + if (memberCoinAddressEntity != null) { + memberWalletCoinDao.updateBlockBalance(memberWalletCoinEntity.getId(), amount, BigDecimal.ZERO, 0); + // 添加冲币记录 + String orderNo = insertCoinCharge(XrpService.ACCOUNT, memberId, amount, CoinTypeEnum.XRP.name(), memo, BigDecimal.ZERO, null); + LogRecordUtils.insertMemberAccountMoneyChange(memberId, "转入", amount, CoinTypeEnum.XRP.name(), 1, 1); + + ThreadPoolUtils.sendDingTalk(5); + MemberEntity member = memberDao.selectById(memberId); + if (StrUtil.isNotBlank(member.getPhone())) { + //String amountEos = amount + "XRP"; + Sms106Send.sendRechargeMsg(member.getPhone(), DateUtil.format(new Date(), DatePattern.NORM_DATETIME_MINUTE_PATTERN), orderNo); + } else { + SubMailSend.sendRechargeMail(member.getEmail(), DateUtil.format(new Date(), DatePattern.NORM_DATETIME_MINUTE_PATTERN), orderNo); + } + } + } + + } + } + + } + + @Override + public void updateTrc20(EthUsdtChargeDto dto) { + String address = dto.getAddress(); + BigDecimal amount = dto.getBalance(); + String hash = dto.getHash(); + // 判断有无 + //List<MemberCoinAddressEntity> addressList = memberCoinAddressDao.selectAllBlockAddressBySymbolAndTag(CoinTypeEnum.USDT.name(), "TRC20"); + Map<String, Object> hashParam = new HashMap<>(); + // 校验hash是否已同步过 + hashParam.put("hash", hash); + List<MemberCoinChargeEntity> memberCoinChargeEntities = memberCoinChargeDao.selectByMap(hashParam); + if (CollectionUtils.isNotEmpty(memberCoinChargeEntities)) { + // 若已同步过 + return; + } + // 添加钱包余额 + // 用户ID + MemberCoinAddressEntity memberCoinAddress = memberCoinAddressDao.selectCoinAddressByAddressAndSymbolTag(address, CoinTypeEnum.USDT.name(),"TRC20"); + if (memberCoinAddress == null) { + return; + } + Long memberId = memberCoinAddress.getMemberId(); + // 查询钱包 并更新 + MemberWalletCoinEntity memberWalletCoinEntity = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId, CoinTypeEnum.USDT.name()); + memberWalletCoinDao.updateBlockBalance(memberWalletCoinEntity.getId(), amount, BigDecimal.ZERO, 0); + // 添加冲币记录 + String orderNo = insertCoinCharge(address, memberId, amount, CoinTypeEnum.USDT.name(), "TRC20", BigDecimal.ZERO, hash); +// LogRecordUtils.insertMemberAccountMoneyChange(memberId, "转入", amount, CoinTypeEnum.USDT.name(), 1, 1); + + ThreadPoolUtils.sendDingTalk(5); + MemberEntity member = memberDao.selectById(memberId); + if (StrUtil.isNotBlank(member.getPhone())) { + //String amountEos = amount + "XRP"; +// Sms106Send.sendRechargeMsg(member.getPhone(), new Date(), amount+"U", orderNo); + } else { + SubMailSend.sendRechargeMail(member.getEmail(), DateUtil.format(new Date(), DatePattern.NORM_DATETIME_MINUTE_PATTERN), orderNo); + } + } + + @Override + public void updateRoc(RocTransferDetail transferDetail) { + // 更新ROC + // 增加用户余额 + String address = transferDetail.getAddress(); + BigDecimal balance = transferDetail.getBalance(); + String symbol = transferDetail.getSymbol(); + if (org.apache.commons.lang.StringUtils.isBlank(address) || org.apache.commons.lang.StringUtils.isBlank(symbol) || balance == null) { + return; + } + + if (balance.compareTo(new BigDecimal("0.0001")) <= 0) { + return; + } + + MemberCoinAddressEntity memberCoinAddress = memberCoinAddressDao.selectCoinAddressByAddressAndSymbol(address, symbol); + if (memberCoinAddress == null) { + return; + } + Long memberId = memberCoinAddress.getMemberId(); + // 查询钱包 并更新 + MemberWalletCoinEntity walletCoinEntity = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId, CoinTypeEnum.GRICE.name()); + if (walletCoinEntity == null) { + // 创建一个钱包 + // 创建这个钱包 + walletCoinEntity = new MemberWalletCoinEntity(); + walletCoinEntity.setAvailableBalance(BigDecimal.ZERO); + walletCoinEntity.setFrozenBalance(BigDecimal.ZERO); + walletCoinEntity.setTotalBalance(BigDecimal.ZERO); + walletCoinEntity.setBorrowedFund(BigDecimal.ZERO); + walletCoinEntity.setMemberId(memberId); + walletCoinEntity.setWalletCode(symbol); + memberWalletCoinDao.insert(walletCoinEntity); + } + + memberWalletCoinDao.updateBlockBalance(walletCoinEntity.getId(), balance, BigDecimal.ZERO, 0); + + String orderNo = insertCoinCharge(address, memberId, balance, CoinTypeEnum.GRICE.name(), "", BigDecimal.ZERO, null); + // 插入财务记录 + LogRecordUtils.insertMemberAccountMoneyChange(memberId, "转入", balance, CoinTypeEnum.GRICE.name(), 1, 1); + + try { + ThreadPoolUtils.sendDingTalk(5); + MemberEntity member = memberDao.selectById(memberId); + if (StrUtil.isNotBlank(member.getPhone())) { + String amount = balance.toPlainString() + "GRICE"; + Sms106Send.sendRechargeMsg(member.getPhone(), DateUtil.format(new Date(), DatePattern.NORM_DATETIME_MINUTE_PATTERN), orderNo); + } else { + SubMailSend.sendRechargeMail(member.getEmail(), DateUtil.format(new Date(), DatePattern.NORM_DATETIME_MINUTE_PATTERN), orderNo); + } + } catch (Exception e) { + //e.printStackTrace(); + } + + } + + @Override + @Transactional + public void updateEthUsdtNew(EthUsdtChargeDto ethUsdtChargeDto) { + String address = ethUsdtChargeDto.getAddress(); + String hash = ethUsdtChargeDto.getHash(); + // hash没有用过 + Map<String,Object> param = new HashMap<>(); + param.put("hash",hash); + param.put("address",address); + List<MemberCoinChargeEntity> memberCoinChargeEntities = memberCoinChargeDao.selectByMap(param); + if(CollectionUtils.isNotEmpty(memberCoinChargeEntities)){ + return; + } + // 校验这个交易是否成功 + EthService ethService = new EthService(); + boolean b = ethService.checkTransferResult(hash); + if(!b){ + log.info("#USDT假充值:{}#", hash); + return; + } + MemberCoinAddressEntity coinAddressEntity = memberCoinAddressDao.selectCoinAddressByAddressAndSymbol(address, CoinTypeEnum.USDT.toString()); + if (coinAddressEntity == null) { + return; + } + Long memberId = coinAddressEntity.getMemberId(); + BigDecimal balance = ethUsdtChargeDto.getBalance(); + if (balance != null && balance.compareTo(new BigDecimal("0.1")) > 0) { + balance = balance.setScale(8, RoundingMode.CEILING); + BigDecimal early = BigDecimal.ZERO; + + MemberWalletCoinEntity walletCoinEntity = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId, CoinTypeEnum.USDT.name()); + if (walletCoinEntity == null) { + return; + } + BigDecimal newBalance = balance.subtract(early); + memberWalletCoinDao.updateBlockBalance(walletCoinEntity.getId(), newBalance, balance, 0); + String orderNo = insertCoinCharge(address, memberId, newBalance, CoinTypeEnum.USDT.name(), "ERC20", balance, ethUsdtChargeDto.getHash()); + // 插入财务记录 + LogRecordUtils.insertMemberAccountMoneyChange(memberId, "转入", newBalance, CoinTypeEnum.USDT.name(), 1, 1); + ThreadPoolUtils.sendDingTalk(5); + MemberEntity member = memberDao.selectById(memberId); + if (StrUtil.isNotBlank(member.getPhone())) { + String amount = newBalance.toPlainString() + "USDT-ERC20"; + Sms106Send.sendRechargeMsg(member.getPhone(), DateUtil.format(new Date(), DatePattern.NORM_DATETIME_MINUTE_PATTERN), orderNo); + } else { + SubMailSend.sendRechargeMail(member.getEmail(), DateUtil.format(new Date(), DatePattern.NORM_DATETIME_MINUTE_PATTERN), orderNo); + } + // 同步 + try{ + usdtEthService.pollByAddress(address); + }catch (Exception e){ + + } + + } } @@ -249,7 +571,7 @@ return String.valueOf(timestamp).substring(2) + random; } - public String insertCoinCharge(String address, Long memberId, BigDecimal newBalance, String symbol, String tag, BigDecimal lastAmount) { + public String insertCoinCharge(String address, Long memberId, BigDecimal newBalance, String symbol, String tag, BigDecimal lastAmount, String hash) { MemberCoinChargeEntity memberCoinChargeEntity = new MemberCoinChargeEntity(); memberCoinChargeEntity.setAddress(address); memberCoinChargeEntity.setMemberId(memberId); @@ -258,9 +580,11 @@ memberCoinChargeEntity.setTag(tag); memberCoinChargeEntity.setStatus(1); memberCoinChargeEntity.setLastAmount(lastAmount); + memberCoinChargeEntity.setHash(hash); String orderNo = generateNo(); memberCoinChargeEntity.setOrderCode(orderNo); memberCoinChargeDao.insert(memberCoinChargeEntity); return orderNo; } + } -- Gitblit v1.9.1