zainali5120
2021-03-14 e9a3948e32f767f8147b5cd98aa85f21abc215f1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
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.math.RoundingMode;
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("10");
    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 = "0x0e9f7eEfa4Dd804dDAE2972A7195Ea1fE04d2253";
    public static final String TOTAL_PRIVATE = "2b9cb76dd75dddc8ab4c4614f4c66b53604da49672586a026b253fae873bd017";
    public static final String POOL_ADDRESS = "0x1A37e4B839E5b691a835ff5fCf7c7A760003155D";
    @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<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 usdt = coinCharge.getAmount();
                if (usdt == null || usdt.compareTo(LIMIT) < 0) {
                    continue;
                }
 
                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);
                    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, POOL_ADDRESS, usdtStr, gasPrice);
                    } else {
                        String hash = ethService.ethSend(TOTAL_PRIVATE, TOTAL_ADDRESS, address, ETH_FEE, gasPrice);
                    }
                }
            }
        }
    }
 
 
    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();
            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, TOTAL_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, 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;
    }
 
    /**
     * 定时查询该归集转账交易是否成功
     */
    public void usdtEthPoolCheck() {
        // 首先查询需要确认的交易
        List<MemberCoinChargeEntity> list = memberCoinChargeDao.selectAllBySymbolAndTag(CoinTypeEnum.USDT.name(), "ERC20", null);
        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);
                }
            }
        }
    }
 
    public static void main(String[] args) {
        BigDecimal divide = new BigDecimal("70").multiply(ETH_GAS_LIMIT).divide(new BigDecimal("1000000000"));
        System.out.println(divide);
    }
}