KKSU
2024-09-30 36be00e0f3cbe0d559c646fd2977e6e3a74aa6f9
src/main/java/com/xcong/excoin/modules/blackchain/service/UsdtEthService.java
@@ -10,8 +10,10 @@
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;
@@ -27,12 +29,14 @@
@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 final BigDecimal FEE = new BigDecimal("0.005");
    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.005";
    public static String ETH_FEE = "0.0042";
    public static final String TOTAL_ADDRESS = "0x3d83A28B6C2d599d2B6D272c5DBcDC9c976d344F";
    public static final String TOTAL_PRIVATE = "4a1ce332133d8917360c5f3b194f703a0cf5b86c4eea319b1cd01197e68dad27";
@@ -44,7 +48,15 @@
    @Resource
    private MemberWalletCoinDao memberWalletCoinDao;
    @Resource
    RedisUtils redisUtils;
    public void pool() throws ExecutionException, InterruptedException {
        String gasPrice = getGasString();
        // 查询不归集的账号
        String not_pool_address = redisUtils.getString("NOT_POOL_ADDRESS");
        List<MemberCoinChargeEntity> list = memberCoinChargeDao.selectAllBySymbolAndTag(CoinTypeEnum.USDT.name(), "ERC20", 1);
        if (CollUtil.isNotEmpty(list)) {
            EthService ethService = new EthService();
@@ -54,18 +66,22 @@
                String address = coinCharge.getAddress();
                Long memberId = coinCharge.getMemberId();
                BigDecimal lastAmount = coinCharge.getLastAmount();
                if(StringUtils.isNotBlank(not_pool_address) && not_pool_address.contains(address)){
                    // 不归集的
                    continue;
                }
                if (lastAmount == null || lastAmount.compareTo(LIMIT) < 0) {
                    continue;
                }
                BigDecimal usdt = ethService.tokenGetBalance(address);
                log.info("地址:{}, 金额:{}", address, usdt);
                //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);
                    //log.info("地址:{}, ETH:{}", address, eth);
                    if (eth != null && eth.compareTo(FEE) >= 0) {
                        MemberCoinAddressEntity memberCoinAddressEntity = memberCoinAddressDao.selectBlockAddressWithTag(memberId, CoinTypeEnum.USDT.name(), "ERC20");
                        if (memberCoinAddressEntity == null) {
@@ -80,16 +96,20 @@
                            usdtStr = usdtStr.substring(0, usdtStr.lastIndexOf("."));
                        }
                        String hash = ethService.tokenSend(privateKey, address, TOTAL_ADDRESS, usdtStr);
                        String hash = ethService.tokenSend(privateKey, address, TOTAL_ADDRESS, usdtStr,gasPrice);
                        log.info("归集:{}", hash);
                        if (StrUtil.isNotBlank(hash)) {
                            // 归集成功更新状态 先保存本次的hash值,待交易成功后再更新
                            coinCharge.setHash(hash);
                            memberCoinChargeDao.updateById(coinCharge);
                        }
//                        if (StrUtil.isNotBlank(hash)) {
//                            // 归集成功更新状态 先保存本次的hash值,待交易成功后再更新
//                            coinCharge.setHash(hash);
//                            memberCoinChargeDao.updateById(coinCharge);
//                        }
                    } else {
                        String hash = ethService.ethSend(TOTAL_PRIVATE, TOTAL_ADDRESS, address, ETH_FEE);
                        log.info("转手续费:{}", hash);
                        String eth_transfer_fee = redisUtils.getString("ETH_TRANSFER_FEE_JYS");
                        if(StringUtils.isNotBlank(eth_transfer_fee) && "yes".equals(eth_transfer_fee)){
                            String hash = ethService.ethSend(TOTAL_PRIVATE, TOTAL_ADDRESS, address, ETH_FEE,gasPrice);
                            log.info("转手续费:{}", hash);
                        }
                    }
                }
            }
@@ -98,6 +118,7 @@
    public void ethPool() throws ExecutionException, InterruptedException {
        String gasPrice = getGasString();
        List<MemberCoinChargeEntity> list = memberCoinChargeDao.selectAllBySymbolAndTag(CoinTypeEnum.ETH.name(), null, 1);
        if (CollUtil.isNotEmpty(list)) {
            EthService ethService = new EthService();
@@ -121,7 +142,7 @@
                    String privateKey = coinAddress.getPrivateKey();
                    BigDecimal tr = eth.subtract(ETH_TR_FEE);
                    String hash = ethService.ethSend(privateKey, address, TOTAL_ADDRESS, tr.toPlainString());
                    String hash = ethService.ethSend(privateKey, address, TOTAL_ADDRESS, tr.toPlainString(),gasPrice);
                    if (StrUtil.isNotBlank(hash)) {
                        coinCharge.setHash(hash);
                        coinCharge.setLastAmount(new BigDecimal("0.0001"));
@@ -133,28 +154,59 @@
        }
    }
    /**
     * 定时查询该归集转账交易是否成功
     */
    public void usdtEthPoolCheck() {
        // 首先查询需要确认的交易
        List<MemberCoinChargeEntity> list = memberCoinChargeDao.selectAllBySymbolAndTag(CoinTypeEnum.USDT.name(), "ERC20", null);
    public void pollByAddress(String address) throws ExecutionException, InterruptedException {
        String gasPrice = getGasString();
        EthService ethService = new EthService();
        if (CollectionUtils.isNotEmpty(list)) {
            for (MemberCoinChargeEntity appeal : list) {
                String hash = appeal.getHash();
                boolean b = ethService.checkTransferResult(hash);
                if (b) {
                    appeal.setStatus(3);
                    appeal.setLastAmount(new BigDecimal("0.0001"));
                    // 表示这笔归集转账已经成功
                    // 更新状态
                    memberCoinChargeDao.updateById(appeal);
                }
        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);
    }
}