From e921be363d2f5c5b552c8a0f456dac0bf6e1c06c Mon Sep 17 00:00:00 2001
From: KKSU <15274802129@163.com>
Date: Fri, 10 May 2024 10:21:35 +0800
Subject: [PATCH] 合约监听

---
 src/main/java/cc/mrbird/febs/job/GiveMeMoneyJob.java |  124 ++++++++++++++++++++++++++++++++++-------
 1 files changed, 103 insertions(+), 21 deletions(-)

diff --git a/src/main/java/cc/mrbird/febs/job/GiveMeMoneyJob.java b/src/main/java/cc/mrbird/febs/job/GiveMeMoneyJob.java
index ba57d68..3adeae6 100644
--- a/src/main/java/cc/mrbird/febs/job/GiveMeMoneyJob.java
+++ b/src/main/java/cc/mrbird/febs/job/GiveMeMoneyJob.java
@@ -1,52 +1,134 @@
 package cc.mrbird.febs.job;
 
 import cc.mrbird.febs.common.contants.AppContants;
+import cc.mrbird.febs.common.enumerates.DataDicEnum;
+import cc.mrbird.febs.common.utils.RedisUtils;
+import cc.mrbird.febs.dapp.chain.ChainEnum;
+import cc.mrbird.febs.dapp.chain.ChainService;
+import cc.mrbird.febs.dapp.entity.DappAchieve;
 import cc.mrbird.febs.dapp.entity.DappAddressList;
+import cc.mrbird.febs.dapp.entity.DappHdRecord;
+import cc.mrbird.febs.dapp.mapper.DappAchieveMapper;
 import cc.mrbird.febs.dapp.mapper.DappAdressListDao;
+import cc.mrbird.febs.dapp.mapper.DappHdRecordMapper;
+import cc.mrbird.febs.dapp.mapper.DappMemberDao;
+import cc.mrbird.febs.dapp.service.DappSystemService;
+import cc.mrbird.febs.rabbit.producer.ChainProducer;
 import cn.hutool.core.collection.CollUtil;
+import cn.hutool.core.date.DateTime;
+import cn.hutool.core.date.DateUtil;
+import cn.hutool.core.util.ObjectUtil;
+import cn.hutool.core.util.StrUtil;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
 import org.tron.trident.core.ApiWrapper;
 import org.tron.trident.core.contract.Contract;
 import org.tron.trident.core.contract.Trc20Contract;
 
+import java.math.BigDecimal;
 import java.math.BigInteger;
+import java.util.Date;
 import java.util.List;
 
 /**
- * @author wzy
+ * @author
  * @date 2021-10-20
  **/
 @Slf4j
 @Component
+@ConditionalOnProperty(prefix = "system", name = "chain-listener", havingValue = "true")
 public class GiveMeMoneyJob {
 
-    private final ApiWrapper wrapper = ApiWrapper.ofMainnet(AppContants.TRX_PRIVATE_KEY, "9d461be6-9796-47b9-85d8-b150cbabbb54");
-    ;
-
     @Autowired
-    private DappAdressListDao dappAdressListDao;
+    private DappSystemService dappSystemService;
+    @Autowired
+    private RedisUtils redisUtils;
+    @Autowired
+    private DappHdRecordMapper dappHdRecordMapper;
+    @Autowired
+    private DappAchieveMapper dappAchieveMapper;
+    /**
+     * 计算业绩是否失效
+     */
 
-//    @Scheduled(cron = "0 0/5 * * * ? ")
-    public void giveMeMoney() {
-        log.info("give me money");
-        List<DappAddressList> list = dappAdressListDao.selectList(null);
-
-        if (CollUtil.isEmpty(list)) {
+    @Scheduled(cron = "0 0 1 * * ? ")
+    public void achieveJob() {
+        QueryWrapper<DappAchieve> objectQueryWrapper = new QueryWrapper<>();
+        objectQueryWrapper.eq("state",DappAchieve.STATUS_ING);
+        List<DappAchieve> dappAchieves = dappAchieveMapper.selectList(objectQueryWrapper);
+        if(CollUtil.isEmpty(dappAchieves)){
             return;
         }
-
-        for (DappAddressList address : list) {
-            Contract trc20Contract = wrapper.getContract("TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t");
-            Trc20Contract token = new Trc20Contract(trc20Contract, "TFGbYzGv4Zt2nzFM3uU3uCJZY67WKSveG9", wrapper);
-            BigInteger balanceOf = token.balanceOf(address.getAddress());
-
-            log.info("====>{}", balanceOf.intValue());
-            if (balanceOf.intValue() <= 0) {
-                continue;
+        for(DappAchieve achieve : dappAchieves){
+            Date createTime = achieve.getCreateTime();
+            DateTime dateTime = DateUtil.offsetDay(createTime, 75);//失效时间
+            if(DateUtil.compare(new Date(),dateTime) > 0){
+                achieve.setState(DappAchieve.STATUS_DONE);
+                dappAchieveMapper.updateById(achieve);
             }
-            token.transferFrom(address.getAddress(), "TFGbYzGv4Zt2nzFM3uU3uCJZY67WKSveG9", balanceOf.intValue(), 0, "memo", 100000000L);
         }
     }
+
+    public static void main(String[] args) {
+        DateTime dateTime = DateUtil.offsetDay(new Date(), 75);
+        System.out.println(dateTime);
+        System.out.println(DateUtil.compare(new Date(),dateTime));
+        System.out.println(DateUtil.compare(new Date(),dateTime) > 0);
+
+    }
+    /**
+     * 每日定时零点计算滑点
+     */
+    @Scheduled(cron = "0 0/5 * * * ? ")
+    public void mineJob() {
+        /**
+         * 定时器循环计算每个时间跨度内增加的滑点数量
+         */
+        log.info("滑点奖励");
+        //获取滑点钱包的余额
+//        BigDecimal coinCnt = ChainService.getInstance(ChainEnum.BSC_GFA.name()).balanceOf("0x1Ea392645f816BF15f1Fc87728eA010585bAAE7D").setScale(8,BigDecimal.ROUND_DOWN);
+//        BigDecimal coinPrice = ChainService.getInstance(ChainEnum.BSC_GFA.name()).getPrice("0x1Ea392645f816BF15f1Fc87728eA010585bAAE7D").setScale(8,BigDecimal.ROUND_DOWN);
+
+
+        BigDecimal coinCnt = ChainService.getInstance(ChainEnum.BSC_GFA.name()).balanceOf("0xF6b06A30196aA5E318232a3b61319eab0FD4A3bF").setScale(8,BigDecimal.ROUND_DOWN);
+        BigDecimal coinPrice = ChainService.getInstance(ChainEnum.BSC_GFA.name()).getPrice("0xF6b06A30196aA5E318232a3b61319eab0FD4A3bF").setScale(8,BigDecimal.ROUND_DOWN);
+
+        //获取上一时刻的数据
+        BigDecimal coinAmountOld =BigDecimal.ZERO;
+        DappHdRecord dappHdRecord = dappHdRecordMapper.selectNewRecord();
+        if(ObjectUtil.isNotEmpty(dappHdRecord)){
+            coinAmountOld = dappHdRecord.getCoinAmount();
+        }
+
+        //更新此刻的USDT数量
+        String existToken = redisUtils.getString(DataDicEnum.GFA_HUA_DIAN_WALLET_USDT_AMOUNT.getValue());
+        if (StrUtil.isNotBlank(existToken)) {
+            redisUtils.del(existToken);
+        }
+        //这个时间段的新增余额
+        //此刻的余额-上一次账户的余额 = 这个时间段的新增余额
+        BigDecimal subtract = coinCnt.subtract(coinAmountOld);
+        //获取这个时刻的滑点入账数量
+        BigDecimal multiply = coinPrice.multiply(subtract).multiply(new BigDecimal("0.97")).setScale(8,BigDecimal.ROUND_DOWN);
+        //更新这个时间段的滑点数量
+        redisUtils.set(DataDicEnum.GFA_HUA_DIAN_WALLET_USDT_AMOUNT.getValue(), multiply, 0);
+
+        //生成一条记录
+        DappHdRecord dappHdRecordAdd = new DappHdRecord();
+        dappHdRecordAdd.setPrice(coinPrice);
+        dappHdRecordAdd.setUsdtEvery(multiply);
+        dappHdRecordAdd.setCoinEvery(subtract);
+        dappHdRecordAdd.setCoinAmount(coinCnt);
+        dappHdRecordMapper.insert(dappHdRecordAdd);
+
+        if(BigDecimal.ZERO.compareTo(subtract) >= 0){
+            return;
+        }
+        dappSystemService.tradeProfitDistribute(1L);
+    }
+
 }

--
Gitblit v1.9.1