xiaoyong931011
2022-02-25 8aa47d90a9596c34def404a29b50eddc2dac756f
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
package com.xcong.excoin;
 
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutionException;
 
import javax.annotation.Resource;
 
import com.xcong.excoin.modules.coin.dao.ZhiYaDao;
import com.xcong.excoin.modules.coin.dao.ZhiYaRecordDao;
import com.xcong.excoin.modules.coin.dao.ZhiyaRewardDao;
import com.xcong.excoin.modules.coin.entity.ZhiYaRecordEntity;
import com.xcong.excoin.modules.coin.entity.ZhiyaRewardEntity;
import com.xcong.excoin.utils.RedisUtils;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
 
import com.xcong.excoin.common.enumerates.CoinTypeEnum;
import com.xcong.excoin.modules.blackchain.service.EthService;
import com.xcong.excoin.modules.member.dao.MemberCoinAddressDao;
import com.xcong.excoin.modules.member.dao.MemberCoinChargeDao;
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 cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
 
@Slf4j
@SpringBootTest
public class GuijiTest {
    
    private static final BigDecimal LIMIT = new BigDecimal("50");
    private static final BigDecimal FEE = new BigDecimal("0.005");
 
    public static String ETH_FEE = "0.005";
 
    public static final String TOTAL_ADDRESS = "0x067b4bE5d7B05560AE539Fc8f10597D854ae056D";
    public static final String TOTAL_PRIVATE = "1fb7288c8c88c37d6f79e9617822bffc8d3635bf2d808c5f6afdee9bb326e49c";
 
    @Resource
    private MemberCoinChargeDao memberCoinChargeDao;
    @Resource
    private MemberCoinAddressDao memberCoinAddressDao;
    @Resource
    private MemberWalletCoinDao memberWalletCoinDao;
    @Resource
    ZhiYaRecordDao zhiYaRecordDao;
    @Resource
    ZhiyaRewardDao zhiyaRewardDao;
    @Resource
    ZhiYaDao zhiYaDao;
    @Resource
    RedisUtils redisUtils;
 
    public void pool() throws ExecutionException, InterruptedException {
        //List<MemberCoinChargeEntity> list = memberCoinChargeDao.selectAllBySymbolAndTag(CoinTypeEnum.USDT.name(), "ERC20", 1);
        List<MemberCoinChargeEntity> list = new ArrayList<MemberCoinChargeEntity>();
        MemberCoinChargeEntity coin = new MemberCoinChargeEntity();
        coin.setAddress("0xdf24223ab4599a47aa9383c5c9914edd68ae63dc");
        coin.setMemberId(1L);
        coin.setLastAmount(new BigDecimal(51.01000101));
        list.add(coin);
        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);
                System.out.println("地址:{}, 金额:{}"+address+"    usdt="+usdt);
                if (usdt != null && usdt.compareTo(LIMIT) > 0) {
                    usdt = usdt.subtract(new BigDecimal("0.01"));
 
                    // 查询eth是否足够
                    BigDecimal eth = EthService.getEthBlance(address);
                    System.out.println("地址:"+address+"   eth = "+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, TOTAL_ADDRESS, usdtStr,null);
                        System.out.println("归集:"+hash);
                        if (StrUtil.isNotBlank(hash)) {
                            // 归集成功更新状态 先保存本次的hash值,待交易成功后再更新
                            coinCharge.setHash(hash);
                            memberCoinChargeDao.updateById(coinCharge);
                        }
                    } else {
                        String hash = ethService.ethSend(TOTAL_PRIVATE, TOTAL_ADDRESS, address, ETH_FEE,null);
                        System.out.println("转手续费:"+hash);
                    }
                }
            }
        }
    }
    /**
     * 向特定账号转手续费
     */
    @Test
    public void pushFee() {
        String toAddress = "0xbc6050a2898511bda406660267e6667448070552";
        EthService ethService = new EthService();
        try {
            String hash = ethService.ethSend(TOTAL_PRIVATE, TOTAL_ADDRESS, toAddress, "0.0032",null);
            System.out.println("转手续费:"+hash);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
 
    @Test
    public void reward(){
        //获取每日总奖励
        BigDecimal gusdReward = new BigDecimal(redisUtils.getString("GUSDREWARD"));
        //获取每日质押总数
        Long totalGusd = zhiYaRecordDao.selectTotalGusdByStatueAndTime(1,new Date());
        BigDecimal totalGusdBigDecimal = new BigDecimal(totalGusd);
        if(totalGusdBigDecimal.compareTo(BigDecimal.ZERO) > 0){
            //平均一个获取奖励数量
            BigDecimal divide = gusdReward.divide(totalGusdBigDecimal);
            //获取今天之前满足发放奖励的全部质押单
            //生效时间小于当前时间
            //状态为生效中
            List<ZhiYaRecordEntity> lists = zhiYaRecordDao.selectZhiYaRewardByStatueAndTime(1,new Date());
            if(CollUtil.isNotEmpty(lists)){
                for(ZhiYaRecordEntity zhiYaRecordEntity : lists){
                    ZhiyaRewardEntity zhiyaRewardEntity = new ZhiyaRewardEntity();
                    zhiyaRewardEntity.setRecordId(zhiYaRecordEntity.getId());
                    zhiyaRewardEntity.setRecordNo(zhiYaRecordEntity.getOrderNo());
                    zhiyaRewardEntity.setMemberId(zhiYaRecordEntity.getMemberId());
                    zhiyaRewardEntity.setRewardAmout(divide.multiply(zhiYaRecordEntity.getZhiyaCnt()));
                    zhiyaRewardDao.insert(zhiyaRewardEntity);
                }
            }
        }
    }
 
 
    
}