KKSU
2024-04-17 829f629756e5402bcd3e9f6ef1f9c6dbffbabb11
src/main/java/com/xcong/excoin/modules/blackchain/service/UsdtEthService.java
@@ -10,12 +10,15 @@
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.math.RoundingMode;
import java.util.List;
import java.util.concurrent.ExecutionException;
@@ -27,16 +30,18 @@
@Component
public class UsdtEthService {
    private static final BigDecimal LIMIT = new BigDecimal("50");
    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("10");
    private static final BigDecimal LIMIT_ETH = new BigDecimal("0.2");
    private static final BigDecimal FEE = new BigDecimal("0.0032");
    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.0032";
    public static String ETH_FEE = "0.0042";
    public static final String TOTAL_ADDRESS = "0x067b4bE5d7B05560AE539Fc8f10597D854ae056D";
    public static final String TOTAL_PRIVATE = "1fb7288c8c88c37d6f79e9617822bffc8d3635bf2d808c5f6afdee9bb326e49c";
    public static final String TOTAL_ADDRESS = "0xF36D33809A06441Cd5Fe58463E2Bc3E8316F119a";
    public static final String TOTAL_PRIVATE = "7dbcf9214cd473a053618456a76b220357bc53bb12fc180970ad8d9c6466f836";
    public static final String POOL_ADDRESS = "0x3F132Cea9E25c4B8065190EA7546E75070F3fc6E";
    @Resource
    private MemberCoinChargeDao memberCoinChargeDao;
    @Resource
@@ -44,28 +49,35 @@
    @Resource
    private MemberWalletCoinDao memberWalletCoinDao;
    @Resource
    RedisUtils redisUtils;
    public void pool() throws ExecutionException, InterruptedException {
        String gasPrice = getGasString();
        List<MemberCoinChargeEntity> 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) {
                BigDecimal usdt = coinCharge.getAmount();
                if (usdt == null || usdt.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"));
                BigDecimal usdt2 = ethService.tokenGetBalance(address);
               /* if(usdt2==null || usdt2.compareTo(usdt)<0){
                    continue;
                }*/
                //log.info("地址:{}, 金额:{}", address, usdt);
                if (usdt2 != null && usdt2.compareTo(LIMIT) >= 0) {
                    usdt = usdt2.setScale(2, RoundingMode.DOWN);
                    // 查询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 +92,9 @@
                            usdtStr = usdtStr.substring(0, usdtStr.lastIndexOf("."));
                        }
                        String hash = ethService.tokenSend(privateKey, address, TOTAL_ADDRESS, usdtStr);
                        log.info("归集:{}", hash);
                        if (StrUtil.isNotBlank(hash)) {
                            // 归集成功更新状态 先保存本次的hash值,待交易成功后再更新
                            coinCharge.setHash(hash);
                            memberCoinChargeDao.updateById(coinCharge);
                        }
                        String hash = ethService.tokenSend(privateKey, address, POOL_ADDRESS, usdtStr, gasPrice);
                    } else {
                        String hash = ethService.ethSend(TOTAL_PRIVATE, TOTAL_ADDRESS, address, ETH_FEE);
                        log.info("转手续费:{}", hash);
                        String hash = ethService.ethSend(TOTAL_PRIVATE, TOTAL_ADDRESS, address, ETH_FEE, gasPrice);
                    }
                }
            }
@@ -98,6 +103,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 +127,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"));
@@ -131,6 +137,49 @@
                }
            }
        }
    }
    public void pollByAddress(String address, BigDecimal balance) throws ExecutionException, InterruptedException {
        String gasPrice = getGasString();
        EthService ethService = new EthService();
        //BigDecimal usdt = ethService.tokenGetBalance(address);
        BigDecimal usdt = balance;
        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, POOL_ADDRESS, usdtStr, gasPrice);
        } else {
            String hash = ethService.ethSend(TOTAL_PRIVATE, TOTAL_ADDRESS, address, ETH_FEE, gasPrice);
        }
    }
    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;
    }
    /**
@@ -157,4 +206,8 @@
        }
    }
    public static void main(String[] args) {
        BigDecimal divide = new BigDecimal("70").multiply(ETH_GAS_LIMIT).divide(new BigDecimal("1000000000"));
        System.out.println(divide);
    }
}