add coin pool and tranfer
9 files modified
3 files added
| | |
| | | <scope>system</scope> |
| | | <systemPath>${basedir}/lib/utils-0.4.0.jar</systemPath> |
| | | </dependency> |
| | | |
| | | |
| | | <dependency> |
| | | <groupId>org.tron.trident</groupId> |
| | | <artifactId>abi</artifactId> |
| | | <version>0.3.0</version> |
| | | <scope>system</scope> |
| | | <systemPath>${basedir}/lib/abi-0.3.0.jar</systemPath> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>org.tron.trident</groupId> |
| | | <artifactId>utils</artifactId> |
| | | <version>0.3.0</version> |
| | | <scope>system</scope> |
| | | <systemPath>${basedir}/lib/utils-0.3.0.jar</systemPath> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>org.tron.trident</groupId> |
| | | <artifactId>core</artifactId> |
| | | <version>0.3.0</version> |
| | | <scope>system</scope> |
| | | <systemPath>${basedir}/lib/core-0.3.0.jar</systemPath> |
| | | </dependency> |
| | | </dependencies> |
| | | |
| | | <build> |
| | |
| | | * @author wzy |
| | | */ |
| | | public enum CoinTypeEnum { |
| | | USDT, BTC, ETH, LTC, EOS, XRP, BCH, ETC,BEA |
| | | USDT, BTC, ETH, LTC, EOS, XRP, BCH, ETC,BEA, XCT |
| | | } |
| | |
| | | import org.springframework.context.annotation.Configuration; |
| | | import org.springframework.web.socket.server.standard.ServerEndpointExporter; |
| | | |
| | | @Configuration |
| | | //@Configuration |
| | | public class WebSocketConfig { |
| | | /** |
| | | * 注入一个ServerEndpointExporter,该Bean会自动注册使用@ServerEndpoint注解申明的websocket endpoint |
| | |
| | | public interface Symbol{ |
| | | String USDT_ERC20 = "USDT_ERC20"; |
| | | String USDT_TRC20 = "USDT_TRC20"; |
| | | String COIN_TRC20 = "XCT_TRC20"; |
| | | } |
| | | |
| | | public EthUsdtChargeDto() { |
| | |
| | | package com.xcong.excoin.modules.blackchain.service.Impl;
|
| | |
|
| | | import java.util.HashMap;
|
| | | import java.util.List;
|
| | | import java.util.Map;
|
| | |
|
| | | import javax.annotation.Resource;
|
| | |
|
| | | import com.xcong.excoin.common.enumerates.CoinTypeEnum;
|
| | | import com.xcong.excoin.modules.blackchain.service.*;
|
| | | import com.xcong.excoin.rabbit.producer.UsdtUpdateProducer;
|
| | | import lombok.extern.slf4j.Slf4j;
|
| | |
| | | // 发送新增的地址到监听集合
|
| | | usdtUpdateProducer.sendAddressMsg(address+","+"ERC20");
|
| | | } else if ("TRC20".equals(lable)) {
|
| | | MemberCoinAddressEntity coinAddress = memberMapper.selectBlockAddress(Long.parseLong(mId), CoinTypeEnum.XCT.name());
|
| | | if (coinAddress != null) {
|
| | | address = coinAddress.getAddress();
|
| | | key = coinAddress.getPrivateKey();
|
| | | } else {
|
| | | Map<String, String> usdtMap = Trc20Service.createAddress();
|
| | | address = usdtMap.get("address");
|
| | | key = usdtMap.get("privateKey");
|
| | | map.put("address", address);
|
| | | // 发送新增的地址到监听集合
|
| | | usdtUpdateProducer.sendAddressMsg(address+","+"TRC20");
|
| | | }
|
| | | }
|
| | | break;
|
| | | case "ROC":
|
| | | address = RocService.createWallet();
|
| | | map.put("address", address);
|
| | | break;
|
| | | case "XCT" :
|
| | | MemberCoinAddressEntity trc20Usdt = memberMapper.selectBlockAddressWithTag(Long.parseLong(mId), CoinTypeEnum.USDT.name(), "TRC20");
|
| | | if (trc20Usdt != null) {
|
| | | address = trc20Usdt.getAddress();
|
| | | key = trc20Usdt.getPrivateKey();
|
| | | } else {
|
| | | Map<String, String> usdtMap = Trc20Service.createAddress();
|
| | | address = usdtMap.get("address");
|
| | | key = usdtMap.get("privateKey");
|
| | |
| | | // 发送新增的地址到监听集合
|
| | | usdtUpdateProducer.sendAddressMsg(address+","+"TRC20");
|
| | | }
|
| | | break;
|
| | | case "ROC":
|
| | | address = RocService.createWallet();
|
| | | map.put("address", address);
|
| | | break;
|
| | | default:
|
| | | break;
|
| | |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.web.client.RestTemplate; |
| | | import org.tron.common.utils.ByteArray; |
| | | import org.tron.trident.core.ApiWrapper; |
| | | import org.tron.trident.core.contract.Contract; |
| | | import org.tron.trident.core.contract.Trc20Contract; |
| | | import org.tron.walletserver.WalletApi; |
| | | |
| | | import javax.annotation.Resource; |
| | |
| | | public class TrxUsdtUpdateService { |
| | | |
| | | public static List<String> addressList = new ArrayList<>(); |
| | | private static String http = "https://api.trongrid.io"; |
| | | private static String http = "https://api.shasta.trongrid.io"; |
| | | |
| | | private static String TRC20_CONTRACT_ADDRESS = "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t"; |
| | | |
| | | /** |
| | | * 代币合约地址 |
| | | */ |
| | | public static String COIN_CONTRACT_ADDRESS = "TRTE68V3TJBLduBoJPLMgBsvwkq8LJBin6"; |
| | | /** |
| | | * 手续费 |
| | | */ |
| | |
| | | * @param addressList |
| | | * @param num |
| | | */ |
| | | private void httpTransactionInfo(List<String> addressList, Long num) { |
| | | public void httpTransactionInfo(List<String> addressList, Long num) { |
| | | // 查询详情,包含了所有交易信息 |
| | | String transactionInfoByBlockNum = getblockbynum(BigInteger.valueOf(num)); |
| | | if (StringUtils.isBlank(transactionInfoByBlockNum)) { |
| | |
| | | } |
| | | |
| | | BigDecimal amount = BigDecimal.ZERO; |
| | | //相匹配的合约地址 |
| | | if (!TRC20_CONTRACT_ADDRESS.equals(contract_address)) { |
| | | // 相匹配的合约地址 USDT合约地址和代币合约地址 |
| | | if (!TRC20_CONTRACT_ADDRESS.equals(contract_address) && !COIN_CONTRACT_ADDRESS.equals(contract_address)) { |
| | | return; |
| | | } |
| | | |
| | |
| | | // 金额 |
| | | // 发送消息队列 |
| | | EthUsdtChargeDto dto = new EthUsdtChargeDto(address, txId, amount); |
| | | dto.setSymbol(EthUsdtChargeDto.Symbol.USDT_TRC20); |
| | | if (TRC20_CONTRACT_ADDRESS.equals(contract_address)) { |
| | | dto.setSymbol(EthUsdtChargeDto.Symbol.USDT_TRC20); |
| | | } else { |
| | | dto.setSymbol(EthUsdtChargeDto.Symbol.COIN_TRC20); |
| | | } |
| | | usdtUpdateProducer.sendMsg(JSONObject.toJSONString(dto)); |
| | | log.info("===to_address:" + to_address + "===amount:" + amount); |
| | | } |
| | |
| | | return false; |
| | | } |
| | | if (trxBalance.compareTo(TRX_FEE) >= 0) { |
| | | // 转 |
| | | BigDecimal trc20Balance = Trc20Service.getTrc20Balance(address); |
| | | if (trc20Balance == null) { |
| | | |
| | | boolean usdtResult = poolUsdt(address); |
| | | boolean coinResult = poolCoin(address); |
| | | if (usdtResult && coinResult) { |
| | | return false; |
| | | } |
| | | MemberCoinAddressEntity coinAddressEntity = memberCoinAddressDao.selectCoinAddressByAddressAndSymbolTag(address, "USDT", "TRC20"); |
| | | if (coinAddressEntity == null) { |
| | | return false; |
| | | } |
| | | Trc20Service.sendTrc20(coinAddressEntity.getPrivateKey(), Trc20Service.POOL_ADDRESS, trc20Balance); |
| | | |
| | | // 需要将存在redis的待归集地址删除 |
| | | Object trc20_pool = redisUtils.get("TRC20_POOL"); |
| | | if (trc20_pool != null) { |
| | |
| | | } |
| | | } |
| | | |
| | | // https://api.trongrid.io/wallet/getnowblock |
| | | public boolean poolUsdt(String address) { |
| | | // 转 |
| | | BigDecimal trc20Balance = Trc20Service.getTrc20Balance(address); |
| | | if (trc20Balance == null) { |
| | | return false; |
| | | } |
| | | MemberCoinAddressEntity coinAddressEntity = memberCoinAddressDao.selectCoinAddressByAddressAndSymbolTag(address, "USDT", "TRC20"); |
| | | if (coinAddressEntity == null) { |
| | | return false; |
| | | } |
| | | Trc20Service.sendTrc20(coinAddressEntity.getPrivateKey(), Trc20Service.POOL_ADDRESS, trc20Balance); |
| | | return true; |
| | | } |
| | | |
| | | public boolean poolCoin(String address) { |
| | | MemberCoinAddressEntity coinAddress = memberCoinAddressDao.selectCoinAddressByAddressAndSymbol(address, CoinTypeEnum.XCT.name()); |
| | | if (coinAddress == null) { |
| | | return false; |
| | | } |
| | | |
| | | Trc20Contract token = contractToken(coinAddress.getAddress(), coinAddress.getPrivateKey()); |
| | | BigInteger balance = token.balanceOf(address); |
| | | if (balance == null || balance.compareTo(BigInteger.ZERO) == 0) { |
| | | return false; |
| | | } |
| | | |
| | | token.transfer(Trc20Service.POOL_ADDRESS, balance.longValue(), 0, "memo", 100000000L); |
| | | return true; |
| | | } |
| | | |
| | | /** |
| | | * 获取最新区块 |
| | | * https://api.trongrid.io/wallet/getnowblock |
| | | * |
| | | * @return |
| | | */ |
| | |
| | | String wholeBlockCount = JSON.parseObject(forObject).getString("whole_block_count"); |
| | | return Long.valueOf(wholeBlockCount); |
| | | } |
| | | |
| | | /** |
| | | * 获取对应代币合约token |
| | | * |
| | | * @param address 地址 |
| | | * @param privateKey 密钥 |
| | | * @return |
| | | */ |
| | | public Trc20Contract contractToken(String address, String privateKey) { |
| | | ApiWrapper wrapper = ApiWrapper.ofShasta(privateKey); |
| | | Contract trc20Contract = wrapper.getContract(COIN_CONTRACT_ADDRESS); |
| | | return new Trc20Contract(trc20Contract, address, wrapper); |
| | | } |
| | | } |
| | |
| | | String address = dto.getAddress(); |
| | | BigDecimal amount = dto.getBalance(); |
| | | String hash = dto.getHash(); |
| | | String symbol; |
| | | |
| | | if (StrUtil.isBlank(dto.getSymbol())) { |
| | | log.error("更新trc20失败:{}, {}, {}, {}", hash, dto.getSymbol(), amount, address); |
| | | return; |
| | | } |
| | | |
| | | List<String> symbolArr = StrUtil.split(dto.getSymbol(), ','); |
| | | if (CollUtil.isEmpty(symbolArr) || symbolArr.size() != 2) { |
| | | log.error("更新trc20失败:{}, {}, {}, {}", hash, dto.getSymbol(), amount, address); |
| | | return; |
| | | } |
| | | symbol = symbolArr.get(0); |
| | | |
| | | // 判断有无 |
| | | //List<MemberCoinAddressEntity> addressList = memberCoinAddressDao.selectAllBlockAddressBySymbolAndTag(CoinTypeEnum.USDT.name(), "TRC20"); |
| | | Map<String, Object> hashParam = new HashMap<>(); |
| | | // 校验hash是否已同步过 |
| | | hashParam.put("hash", hash); |
| | | hashParam.put("symbol", symbol); |
| | | List<MemberCoinChargeEntity> memberCoinChargeEntities = memberCoinChargeDao.selectByMap(hashParam); |
| | | if (CollectionUtils.isNotEmpty(memberCoinChargeEntities)) { |
| | | // 若已同步过 |
| | |
| | | } |
| | | // 添加钱包余额 |
| | | // 用户ID |
| | | MemberCoinAddressEntity memberCoinAddress = memberCoinAddressDao.selectCoinAddressByAddressAndSymbolTag(address, CoinTypeEnum.USDT.name(),"TRC20"); |
| | | MemberCoinAddressEntity memberCoinAddress = memberCoinAddressDao.selectCoinAddressByAddressAndSymbolTag(address, symbol,"TRC20"); |
| | | if (memberCoinAddress == null) { |
| | | return; |
| | | } |
| | | Long memberId = memberCoinAddress.getMemberId(); |
| | | // 查询钱包 并更新 |
| | | MemberWalletCoinEntity memberWalletCoinEntity = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId, CoinTypeEnum.USDT.name()); |
| | | MemberWalletCoinEntity memberWalletCoinEntity = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId, symbol); |
| | | memberWalletCoinDao.updateBlockBalance(memberWalletCoinEntity.getId(), amount, BigDecimal.ZERO, 0); |
| | | // 添加冲币记录 |
| | | String orderNo = insertCoinCharge(address, memberId, amount, CoinTypeEnum.USDT.name(), "TRC20", BigDecimal.ZERO, hash); |
| | | String orderNo = insertCoinCharge(address, memberId, amount, symbol, "TRC20", BigDecimal.ZERO, hash); |
| | | // LogRecordUtils.insertMemberAccountMoneyChange(memberId, "转入", amount, CoinTypeEnum.USDT.name(), 1, 1); |
| | | |
| | | ThreadPoolUtils.sendDingTalk(5); |
| | |
| | | if(EthUsdtChargeDto.Symbol.USDT_ERC20.equals(ethUsdtChargeDto.getSymbol())){ |
| | | blockCoinService.updateEthUsdtNew(ethUsdtChargeDto); |
| | | } |
| | | if(EthUsdtChargeDto.Symbol.USDT_TRC20.equals(ethUsdtChargeDto.getSymbol())){ |
| | | if(EthUsdtChargeDto.Symbol.USDT_TRC20.equals(ethUsdtChargeDto.getSymbol()) || EthUsdtChargeDto.Symbol.COIN_TRC20.equals(ethUsdtChargeDto.getSymbol())){ |
| | | blockCoinService.updateTrc20(ethUsdtChargeDto); |
| | | // 同步完直接归集 |
| | | trxUsdtUpdateService.poolByAddress(ethUsdtChargeDto.getAddress()); |
| | |
| | |
|
| | | import javax.annotation.Resource;
|
| | |
|
| | | import com.xcong.excoin.modules.blackchain.service.TrxUsdtUpdateService;
|
| | | import org.junit.jupiter.api.Test;
|
| | | import org.springframework.beans.factory.annotation.Autowired;
|
| | | import org.springframework.boot.test.context.SpringBootTest;
|
| | |
|
| | | import com.xcong.excoin.common.enumerates.CoinTypeEnum;
|
| | |
| | | }
|
| | | }
|
| | |
|
| | | @Autowired
|
| | | private TrxUsdtUpdateService trxUsdtUpdateService;
|
| | |
|
| | | @Test
|
| | | public void coinTest() {
|
| | | List<String> address = new ArrayList<>();
|
| | | address.add("TKcyw4igFYhNpTzyZv25j6uDwvTYzncnjp");
|
| | | trxUsdtUpdateService.httpTransactionInfo(address, 20137024L);
|
| | | }
|
| | |
|
| | |
|
| | |
|
| | | }
|