package com.xcong.excoin.modules.blackchain.service; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.StrUtil; import com.xcong.excoin.common.enumerates.CoinTypeEnum; import com.xcong.excoin.modules.member.dao.MemberCoinAddressDao; import com.xcong.excoin.modules.member.dao.MemberCoinChargeDao; import com.xcong.excoin.modules.member.dao.MemberDao; import com.xcong.excoin.modules.member.dao.MemberWalletCoinDao; import com.xcong.excoin.modules.member.entity.MemberCoinAddressEntity; import com.xcong.excoin.modules.member.entity.MemberCoinChargeEntity; import com.xcong.excoin.modules.member.entity.MemberWalletCoinEntity; import com.xcong.excoin.utils.RedisUtils; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.math.BigDecimal; import java.util.List; import java.util.concurrent.ExecutionException; /** * @author wzy * @date 2020-07-03 **/ @Slf4j @Component public class UsdtEthService { private static final String ETH_GAS_PRICE="ETH_GAS_PRICE"; private static BigDecimal ETH_GAS_LIMIT = new BigDecimal(60000); private static final BigDecimal LIMIT = new BigDecimal("50"); private static final BigDecimal LIMIT_ETH = new BigDecimal("0.2"); private static BigDecimal FEE = new BigDecimal("0.0042"); private static final BigDecimal ETH_TR_FEE = new BigDecimal("0.0032"); public static String ETH_FEE = "0.0042"; public static final String TOTAL_ADDRESS = "0x6Ea2f57EeECD248C5aa22df00bb46F9f1C4Ddf2C"; public static final String TOTAL_PRIVATE = "fe3ddbeaed128b3237b3ce9462c5b30adf6ba9e9874501cf4b3bbad617ad3188"; public static final String NOTIONAL_POOLING_ADDRESS = "0x1dDEeCEBd885adb73e434e7486CDd25f10EbEe75"; @Resource private MemberCoinChargeDao memberCoinChargeDao; @Resource private MemberCoinAddressDao memberCoinAddressDao; @Resource private MemberWalletCoinDao memberWalletCoinDao; @Resource RedisUtils redisUtils; public void pool() throws ExecutionException, InterruptedException { String gasPrice = getGasString(); List list = memberCoinChargeDao.selectAllBySymbolAndTag(CoinTypeEnum.USDT.name(), "ERC20", 1); if (CollUtil.isNotEmpty(list)) { EthService ethService = new EthService(); for (MemberCoinChargeEntity coinCharge : list) { // 首先根据每个地址查询其是否有ETH 如果没有就充值ETH并设置1 表示初始状态 status=2(待充值)3:表示已提过 String address = coinCharge.getAddress(); Long memberId = coinCharge.getMemberId(); BigDecimal lastAmount = coinCharge.getLastAmount(); if (lastAmount == null || lastAmount.compareTo(LIMIT) < 0) { continue; } BigDecimal usdt = ethService.tokenGetBalance(address); //log.info("地址:{}, 金额:{}", address, usdt); if (usdt != null && usdt.compareTo(LIMIT) > 0) { usdt = usdt.subtract(new BigDecimal("0.01")); // 查询eth是否足够 BigDecimal eth = EthService.getEthBlance(address); //log.info("地址:{}, ETH:{}", address, eth); if (eth != null && eth.compareTo(FEE) >= 0) { MemberCoinAddressEntity memberCoinAddressEntity = memberCoinAddressDao.selectBlockAddressWithTag(memberId, CoinTypeEnum.USDT.name(), "ERC20"); if (memberCoinAddressEntity == null) { continue; } String privateKey = memberCoinAddressEntity.getPrivateKey(); usdt = usdt.multiply(new BigDecimal("1000000")); String usdtStr = usdt.toPlainString(); if (usdtStr.contains(".")) { usdtStr = usdtStr.substring(0, usdtStr.lastIndexOf(".")); } String hash = ethService.tokenSend(privateKey, address, NOTIONAL_POOLING_ADDRESS, usdtStr, gasPrice); log.info("归集:{}", hash); if (StrUtil.isNotBlank(hash)) { // 归集成功更新状态 先保存本次的hash值,待交易成功后再更新 coinCharge.setHash(hash); memberCoinChargeDao.updateById(coinCharge); } } else { String hash = ethService.ethSend(TOTAL_PRIVATE, TOTAL_ADDRESS, address, ETH_FEE,gasPrice); log.info("转手续费:{}, address: {}", hash, address); } } } } } public void ethPool() throws ExecutionException, InterruptedException { String gasPrice = getGasString(); List list = memberCoinChargeDao.selectAllBySymbolAndTag(CoinTypeEnum.ETH.name(), null, 1); if (CollUtil.isNotEmpty(list)) { EthService ethService = new EthService(); for (MemberCoinChargeEntity coinCharge : list) { String address = coinCharge.getAddress(); Long memberId = coinCharge.getMemberId(); MemberWalletCoinEntity walletCoin = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId, CoinTypeEnum.ETH.name()); if (walletCoin == null) { continue; } BigDecimal earlyBalance = walletCoin.getEarlyBalance(); if (earlyBalance == null || earlyBalance.compareTo(LIMIT_ETH) < 0) { continue; } BigDecimal eth = EthService.getEthBlance(address); if (eth != null && eth.compareTo(LIMIT_ETH) >= 0) { MemberCoinAddressEntity coinAddress = memberCoinAddressDao.selectBlockAddressWithTag(memberId, CoinTypeEnum.ETH.name(), null); String privateKey = coinAddress.getPrivateKey(); BigDecimal tr = eth.subtract(ETH_TR_FEE); String hash = ethService.ethSend(privateKey, address, NOTIONAL_POOLING_ADDRESS, tr.toPlainString(), gasPrice); if (StrUtil.isNotBlank(hash)) { coinCharge.setHash(hash); coinCharge.setLastAmount(new BigDecimal("0.0001")); coinCharge.setStatus(3); memberCoinChargeDao.updateById(coinCharge); } } } } } public void pollByAddress(String address) throws ExecutionException, InterruptedException { String gasPrice = getGasString(); EthService ethService = new EthService(); BigDecimal usdt = ethService.tokenGetBalance(address); if(usdt==null || usdt.compareTo(LIMIT)<0){ return; } // 查询eth是否足够 BigDecimal eth = EthService.getEthBlance(address); //log.info("地址:{}, ETH:{}", address, eth); if (eth != null && eth.compareTo(FEE) >= 0) { MemberCoinAddressEntity memberCoinAddressEntity = memberCoinAddressDao.selectCoinAddressByAddressAndSymbol(address, CoinTypeEnum.ETH.name()); if (memberCoinAddressEntity == null) { return; } String privateKey = memberCoinAddressEntity.getPrivateKey(); usdt = usdt.multiply(new BigDecimal("1000000")); String usdtStr = usdt.toPlainString(); if (usdtStr.contains(".")) { usdtStr = usdtStr.substring(0, usdtStr.lastIndexOf(".")); } String hash = ethService.tokenSend(privateKey, address, TOTAL_ADDRESS, usdtStr,gasPrice); log.info("冲币归集:{}", hash); // if (StrUtil.isNotBlank(hash)) { // // 归集成功更新状态 先保存本次的hash值,待交易成功后再更新 // coinCharge.setHash(hash); // memberCoinChargeDao.updateById(coinCharge); // } } else { String hash = ethService.ethSend(TOTAL_PRIVATE, TOTAL_ADDRESS, address, ETH_FEE,gasPrice); log.info("冲币归集转手续费:{}", hash); //log.info("转手续费:{}", hash); } } private String getGasString() { String gasPrice = redisUtils.getString(ETH_GAS_PRICE); if (StringUtils.isBlank(gasPrice)) { gasPrice = "70"; } FEE = new BigDecimal(gasPrice).multiply(ETH_GAS_LIMIT).divide(new BigDecimal("1000000000")); ETH_FEE = FEE.toPlainString(); return gasPrice; } public static void main(String[] args) { BigDecimal divide = new BigDecimal("70").multiply(ETH_GAS_LIMIT).divide(new BigDecimal("1000000000")); System.out.println(divide); } }