| | |
| | | package cc.mrbird.febs.mall.chain.job; |
| | | |
| | | import cc.mrbird.febs.common.enumerates.FlowTypeEnum; |
| | | import cc.mrbird.febs.common.enumerates.MallMoneyFlowTypeEnum; |
| | | import cc.mrbird.febs.common.utils.AppContants; |
| | | import cc.mrbird.febs.common.utils.RedisUtils; |
| | | import cc.mrbird.febs.mall.chain.constants.ChainConstants; |
| | | import cc.mrbird.febs.mall.chain.enums.ChainEnum; |
| | | import cc.mrbird.febs.mall.chain.enums.CoinTypeEnum; |
| | | import cc.mrbird.febs.mall.chain.enums.EthService; |
| | | import cc.mrbird.febs.mall.chain.service.ChainService; |
| | | import cc.mrbird.febs.mall.chain.service.ContractChainService; |
| | | import cc.mrbird.febs.mall.chain.service.TrxUsdtUpdateService; |
| | | import cc.mrbird.febs.mall.entity.MallMemberWallet; |
| | | import cc.mrbird.febs.mall.entity.MallMoneyFlow; |
| | | import cc.mrbird.febs.mall.entity.MemberCoinAddressEntity; |
| | | import cc.mrbird.febs.mall.entity.MemberCoinChargeEntity; |
| | | import cc.mrbird.febs.mall.mapper.MallMemberWalletMapper; |
| | | import cc.mrbird.febs.mall.mapper.MemberCoinAddressDao; |
| | | import cc.mrbird.febs.mall.mapper.MemberCoinChargeDao; |
| | | import cc.mrbird.febs.mall.service.IMallMoneyFlowService; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.hutool.http.HttpUtil; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| | | import org.springframework.scheduling.annotation.Scheduled; |
| | | import org.springframework.stereotype.Component; |
| | | import org.web3j.utils.Convert; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.math.BigDecimal; |
| | | import java.math.BigInteger; |
| | | import java.util.Set; |
| | | import java.util.concurrent.ExecutionException; |
| | | |
| | | /** |
| | | * 归集定时任务 |
| | |
| | | |
| | | @Slf4j |
| | | @Component |
| | | @ConditionalOnProperty(prefix = "system", name = "block-job", havingValue = "true") |
| | | @ConditionalOnProperty(prefix = "system", name = "chain-listener", havingValue = "true") |
| | | public class NotionalPoolingJob { |
| | | |
| | | @Resource |
| | | private RedisUtils redisUtils; |
| | | |
| | | @Resource |
| | | private TrxUsdtUpdateService trxUsdtUpdateService; |
| | | |
| | | // /** |
| | | // * usdt 归集 |
| | | // */ |
| | | // @Scheduled(cron = "0 5/30 * * * ? ") |
| | | // public void poolUsdtEth() { |
| | | // try { |
| | | // log.info("USDT归集开始"); |
| | | // usdtEthService.pool(); |
| | | // log.info("USDT归集结束"); |
| | | // } catch (ExecutionException | InterruptedException e) { |
| | | // log.error("#usdt归集错误#", e); |
| | | // } |
| | | // } |
| | | // |
| | | // /** |
| | | // * 使用扫块 不需要这个 |
| | | // */ |
| | | // //@Scheduled(cron = "0 2/8 * * * ? ") |
| | | // @Deprecated |
| | | // public void usdtEthPoolCheck() { |
| | | // log.info("USDTETH归集结果扫描开始"); |
| | | // usdtEthService.usdtEthPoolCheck(); |
| | | // } |
| | | // |
| | | // @Scheduled(cron = "0 2/30 * * * ? ") |
| | | // public void poolEth() { |
| | | // try { |
| | | // usdtEthService.ethPool(); |
| | | // } catch (ExecutionException | InterruptedException e) { |
| | | // log.info("#ETH归集错误#", e); |
| | | // } |
| | | // } |
| | | private MemberCoinAddressDao memberCoinAddressDao; |
| | | @Resource |
| | | private MallMemberWalletMapper mallMemberWalletMapper; |
| | | @Resource |
| | | private IMallMoneyFlowService mallMoneyFlowService; |
| | | @Resource |
| | | private MemberCoinChargeDao memberCoinChargeDao; |
| | | |
| | | /** |
| | | * 归集TRC20 |
| | | * 归集ERC20 |
| | | */ |
| | | @Scheduled(cron = "0 0/30 * * * ? ") |
| | | @Scheduled(cron = "0 0/10 * * * ? ") |
| | | public void poolUsdtTrc20() { |
| | | // TODO 执行BSC归集逻辑 |
| | | log.info("归集TRC20执行"); |
| | | // Set<Object> poolAddress = redisUtils.sGet(ChainConstants.REDIS_KEY_POOL_ADDRESS); |
| | | // if (poolAddress == null || poolAddress.isEmpty()) { |
| | | log.info("归集ERC20执行"); |
| | | Set<Object> poolAddress = redisUtils.sGet(ChainConstants.REDIS_KEY_SYSTEM_ADDRESS); |
| | | if (poolAddress == null || poolAddress.isEmpty()) { |
| | | return; |
| | | } |
| | | |
| | | for (Object address : poolAddress) { |
| | | String next = (String) address; |
| | | log.info("归集地址:{}", next); |
| | | //查询USDT余额 |
| | | MemberCoinAddressEntity memberCoinAddressEntity = memberCoinAddressDao.selectCoinAddressByAddressAndSymbolTag(next, "USDT", "ERC20"); |
| | | if(ObjectUtil.isEmpty(memberCoinAddressEntity)){ |
| | | continue; |
| | | } |
| | | BigDecimal balanceOf = ChainService.getInstance(ChainEnum.BSC_USDT.name()).balanceOf(next); |
| | | if (balanceOf == null || balanceOf.compareTo(new BigDecimal("0.05")) < 1) { |
| | | continue; |
| | | } |
| | | // //查询手续费 |
| | | // BigDecimal balanceOfBaseToken = ChainService.getInstance(ChainEnum.BSC_USDT.name()).balanceOfBaseToken(next); |
| | | // |
| | | // BigDecimal gas = new BigDecimal(ChainService.getInstance(ChainEnum.BSC_USDT.name()).getGas()).multiply(new BigDecimal(0.0001)); |
| | | // if(balanceOfBaseToken.compareTo(gas) < 0){ |
| | | // //转手续费 |
| | | // ChainService.getInstance(ChainEnum.BSC_USDT.name()).transferBaseToken(next, gas); |
| | | // } |
| | | |
| | | String transfer = ChainService.getInstance(ChainEnum.BSC_USDT.name()).transfer(memberCoinAddressEntity.getPrivateKey(), |
| | | memberCoinAddressEntity.getAddress(), |
| | | AppContants.ERC20_POOL_ADDRESS, |
| | | balanceOf.toString()); |
| | | if(StrUtil.isNotEmpty(transfer)){ |
| | | Long memberId = memberCoinAddressEntity.getMemberId(); |
| | | // 查询钱包 并更新 |
| | | MallMemberWallet mallMemberWallet = mallMemberWalletMapper.selectWalletByMemberId(memberId); |
| | | if (mallMemberWallet == null) { |
| | | return; |
| | | } |
| | | mallMemberWalletMapper.updateBlockBalanceById(mallMemberWallet.getId(), balanceOf.setScale(4, BigDecimal.ROUND_DOWN)); |
| | | |
| | | String orderNo = insertCoinCharge(next, memberId, balanceOf.setScale(4, BigDecimal.ROUND_DOWN), CoinTypeEnum.USDT.name(), "ERC20", BigDecimal.ZERO); |
| | | Long chargeFlowId = mallMoneyFlowService.addMoneyFlow( |
| | | memberId, |
| | | orderNo, |
| | | balanceOf.setScale(4, BigDecimal.ROUND_DOWN), |
| | | MallMoneyFlowTypeEnum.CHARGE.getCode(), |
| | | MallMoneyFlow.STATUS_SUCCESS, |
| | | MallMoneyFlow.IS_RETURN_Y, |
| | | memberId, |
| | | FlowTypeEnum.BALANCE.getValue(), |
| | | MallMoneyFlowTypeEnum.CHARGE.getName() |
| | | ); |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | public String insertCoinCharge(String address, Long memberId, BigDecimal newBalance, String symbol, String tag, BigDecimal lastAmount) { |
| | | MemberCoinChargeEntity memberCoinChargeEntity = new MemberCoinChargeEntity(); |
| | | memberCoinChargeEntity.setAddress(address); |
| | | memberCoinChargeEntity.setMemberId(memberId); |
| | | memberCoinChargeEntity.setAmount(newBalance); |
| | | memberCoinChargeEntity.setSymbol(symbol); |
| | | memberCoinChargeEntity.setTag(tag); |
| | | memberCoinChargeEntity.setStatus(1); |
| | | memberCoinChargeEntity.setLastAmount(lastAmount); |
| | | String orderNo = generateNo(); |
| | | memberCoinChargeEntity.setOrderCode(orderNo); |
| | | memberCoinChargeDao.insert(memberCoinChargeEntity); |
| | | return orderNo; |
| | | } |
| | | private String generateNo() { |
| | | // 生成订单号 |
| | | Long timestamp = System.currentTimeMillis(); |
| | | // 随机数 |
| | | int random = (int) (Math.random() * 10); |
| | | return String.valueOf(timestamp).substring(2) + random; |
| | | } |
| | | |
| | | /** |
| | | * 归集ERC20 |
| | | */ |
| | | @Scheduled(cron = "0 0/7 * * * ? ") |
| | | public void poolBnbTrc20() { |
| | | // TODO 执行BSC归集逻辑 |
| | | log.info("归集ERC20执行"); |
| | | Set<Object> poolAddress = redisUtils.sGet(ChainConstants.REDIS_KEY_SYSTEM_ADDRESS); |
| | | if (poolAddress == null || poolAddress.isEmpty()) { |
| | | return; |
| | | } |
| | | |
| | | for (Object address : poolAddress) { |
| | | String next = (String) address; |
| | | log.info("归集地址:{}", next); |
| | | //查询USDT余额 |
| | | MemberCoinAddressEntity memberCoinAddressEntity = memberCoinAddressDao.selectCoinAddressByAddressAndSymbolTag(next, "USDT", "ERC20"); |
| | | if(ObjectUtil.isEmpty(memberCoinAddressEntity)){ |
| | | continue; |
| | | } |
| | | BigDecimal balanceOf = ChainService.getInstance(ChainEnum.BSC_USDT.name()).balanceOf(next); |
| | | if (balanceOf == null || balanceOf.compareTo(new BigDecimal("0.05")) < 1) { |
| | | continue; |
| | | } |
| | | //查询手续费 |
| | | BigDecimal balanceOfBaseToken = ChainService.getInstance(ChainEnum.BSC_USDT.name()).balanceOfBaseToken(next); |
| | | |
| | | BigDecimal gas = new BigDecimal(ChainService.getInstance(ChainEnum.BSC_USDT.name()).getGas()).multiply(new BigDecimal(0.0001)); |
| | | if(balanceOfBaseToken.compareTo(gas) < 0){ |
| | | //转手续费 |
| | | ChainService.getInstance(ChainEnum.BSC_USDT.name()).transferBaseToken(next, gas); |
| | | } |
| | | |
| | | } |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | String addressTo = "0xA60AaC0da34C76F3f60207ee09e9F75043319ab4"; |
| | | String address = "0xc1be17a02127e5cc1e6b2298436e8b467531f798"; |
| | | |
| | | |
| | | ChainService.getInstance(ChainEnum.BSC_USDT.name()).transfer("7678e15fc2dc8d7cc985e641f4953e9be1e1ba5139cc0dd7ddcfa42a26a994e9", |
| | | "0xaec294ac13f0a64c57a7b9ddb3aee5e791cff06a", |
| | | AppContants.ERC20_POOL_ADDRESS, |
| | | "100"); |
| | | // BigInteger allowance = ChainService.getInstance(ChainEnum.BSC_USDT.name()).allowance(address); |
| | | // System.out.println(allowance); |
| | | // |
| | | // BigDecimal balanceOf = ChainService.getInstance(ChainEnum.BSC_USDT.name()).balanceOf(addressTo); |
| | | // System.out.println(balanceOf); |
| | | // |
| | | // BigInteger balanceOfUnDecimal = ChainService.getInstance(ChainEnum.BSC_USDT.name()).balanceOfUnDecimal(address); |
| | | // System.out.println(balanceOfUnDecimal); |
| | | // |
| | | // BigInteger blockNumber = ChainService.getInstance(ChainEnum.BSC_USDT.name()).blockNumber(); |
| | | // System.out.println(blockNumber); |
| | | |
| | | // BigDecimal balanceOfBaseToken = ChainService.getInstance(ChainEnum.BSC_USDT.name()).balanceOfBaseToken(addressTo); |
| | | // System.out.println(balanceOfBaseToken); |
| | | // |
| | | // String s = ChainService.getInstance(ChainEnum.BSC_USDT.name()).transferBaseToken(address, new BigDecimal(0.0005)); |
| | | // System.out.println(s); |
| | | // BigDecimal balanceOf = ChainService.getInstance(ChainEnum.BSC_USDT.name()).balanceOf(address); |
| | | // if (balanceOf == null || balanceOf.compareTo(new BigDecimal("0.05")) < 1) { |
| | | // return; |
| | | // } |
| | | // System.out.println(balanceOf); |
| | | // //查询手续费 |
| | | // BigDecimal balanceOfBaseToken = ChainService.getInstance(ChainEnum.BSC_USDT.name()).balanceOfBaseToken(address); |
| | | // |
| | | // for (Object address : poolAddress) { |
| | | // String next = (String) address; |
| | | // log.info("归集地址:{}", next); |
| | | // trxUsdtUpdateService.poolByAddress(next); |
| | | // System.out.println(balanceOfBaseToken); |
| | | // String gas = "5"; |
| | | // if(balanceOfBaseToken.compareTo(new BigDecimal(gas).multiply(new BigDecimal(0.0001))) < 0){ |
| | | // //转手续费 |
| | | // String s1 = ChainService.getInstance(ChainEnum.BSC_USDT.name()).transferBaseToken(address, new BigDecimal(gas)); |
| | | // } |
| | | // String transfer = ChainService.getInstance(ChainEnum.BSC_USDT.name()).transfer(address,new BigDecimal(1)); |
| | | |
| | | // ContractChainService contract = new EthService("https://bsc-dataseed1.ninicoin.io", |
| | | // address, |
| | | // "7761233246f68ab3d35eba04c2fbc794714652d2acc15ee5b27ecd1c4005d279", |
| | | // "0x55d398326f99059fF775485246999027B3197955"); |
| | | // String transfer = ChainService.getInstance(ChainEnum.BSC_USDT.name()).transfer( |
| | | // "7761233246f68ab3d35eba04c2fbc794714652d2acc15ee5b27ecd1c4005d279", |
| | | // address, |
| | | // AppContants.ERC20_POOL_ADDRESS, |
| | | // balanceOf.toString()); |
| | | // System.out.println(transfer); |
| | | |
| | | // ChainService.getInstance(ChainEnum.BSC_USDT.name()).poolErc(address); |
| | | |
| | | } |
| | | } |