src/main/java/com/xcong/excoin/modules/blackchain/controller/BlockController.java
@@ -1,5 +1,6 @@ package com.xcong.excoin.modules.blackchain.controller; import com.xcong.excoin.common.annotations.SubmitRepeat; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestHeader; @@ -32,7 +33,7 @@ BlockSerive blockSerive; /** * BTC * @param token * @param * @return */ @ApiOperation(value = "链上生成钱包地址接口", notes = "链上生成钱包地址接口") @@ -40,6 +41,7 @@ @ApiImplicitParam(name = "symbol", value = "币种", required = true, dataType = "String", paramType="query") }) @GetMapping(value = "/findBlockAddress") @SubmitRepeat public Result findBlockAddress(String symbol) { return blockSerive.findBlockAddress(symbol); } src/main/java/com/xcong/excoin/modules/blackchain/service/EthService.java
@@ -13,6 +13,7 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import org.apache.commons.lang3.StringUtils; import org.web3j.abi.FunctionEncoder; import org.web3j.abi.FunctionReturnDecoder; import org.web3j.abi.TypeReference; @@ -209,10 +210,13 @@ } // USDT public String tokenSend(String privateKey, String fromAddress, String toAddress, String amount) public String tokenSend(String privateKey, String fromAddress, String toAddress, String amount,String gas) throws InterruptedException, ExecutionException { // Web3j web3j = Web3j.build(new // HttpService("https://mainnet.infura.io/v3/882c66ebcfc141abbea22b948fa44321")); if(StringUtils.isBlank(gas)){ gas="70"; } String contractAddress = "0xdac17f958d2ee523a2206206994597c13d831ec7"; Credentials credentials = Credentials.create(privateKey); @@ -229,7 +233,7 @@ String encodedFunction = FunctionEncoder.encode(function); RawTransaction rawTransaction = RawTransaction.createTransaction(nonce, Convert.toWei("70", Convert.Unit.GWEI).toBigInteger(),// 给矿工开的转账单价 单价越高越快 Convert.toWei(gas, Convert.Unit.GWEI).toBigInteger(),// 给矿工开的转账单价 单价越高越快 Convert.toWei("60000", Convert.Unit.WEI).toBigInteger(), contractAddress, encodedFunction);//里程上限 // 10*80000/1000000000=0.0008 手续费 @@ -252,11 +256,13 @@ } } public String ethSend(String privateKey, String fromAddress, String toAddress, String amount) public String ethSend(String privateKey, String fromAddress, String toAddress, String amount,String gas) throws InterruptedException, ExecutionException { // Web3j web3j = Web3j.build(new // HttpService("https://mainnet.infura.io/v3/882c66ebcfc141abbea22b948fa44321")); if(StringUtils.isBlank(gas)){ gas="70"; } Credentials credentials = Credentials.create(privateKey); EthGetTransactionCount ethGetTransactionCount = web3j @@ -265,7 +271,7 @@ BigInteger nonce = ethGetTransactionCount.getTransactionCount(); BigInteger value = Convert.toWei(amount, Convert.Unit.ETHER).toBigInteger(); RawTransaction rawTransaction = RawTransaction.createEtherTransaction(nonce, Convert.toWei("70", Convert.Unit.GWEI).toBigInteger(), Convert.toWei(gas, Convert.Unit.GWEI).toBigInteger(), Convert.toWei("60000", Convert.Unit.WEI).toBigInteger(), toAddress, value); byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials); String hexValue = Numeric.toHexString(signedMessage); src/main/java/com/xcong/excoin/modules/blackchain/service/UsdtEthService.java
@@ -10,8 +10,10 @@ 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; @@ -27,9 +29,11 @@ @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("50"); private static final BigDecimal LIMIT_ETH = new BigDecimal("0.2"); private static final BigDecimal FEE = new BigDecimal("0.0042"); 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"; @@ -44,7 +48,13 @@ @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(); @@ -80,7 +90,7 @@ usdtStr = usdtStr.substring(0, usdtStr.lastIndexOf(".")); } String hash = ethService.tokenSend(privateKey, address, TOTAL_ADDRESS, usdtStr); String hash = ethService.tokenSend(privateKey, address, TOTAL_ADDRESS, usdtStr,gasPrice); log.info("归集:{}", hash); // if (StrUtil.isNotBlank(hash)) { // // 归集成功更新状态 先保存本次的hash值,待交易成功后再更新 @@ -88,7 +98,7 @@ // memberCoinChargeDao.updateById(coinCharge); // } } else { String hash = ethService.ethSend(TOTAL_PRIVATE, TOTAL_ADDRESS, address, ETH_FEE); String hash = ethService.ethSend(TOTAL_PRIVATE, TOTAL_ADDRESS, address, ETH_FEE,gasPrice); //log.info("转手续费:{}", hash); } } @@ -98,6 +108,7 @@ 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(); @@ -121,7 +132,7 @@ String privateKey = coinAddress.getPrivateKey(); BigDecimal tr = eth.subtract(ETH_TR_FEE); String hash = ethService.ethSend(privateKey, address, TOTAL_ADDRESS, tr.toPlainString()); String hash = ethService.ethSend(privateKey, address, TOTAL_ADDRESS, tr.toPlainString(),gasPrice); if (StrUtil.isNotBlank(hash)) { coinCharge.setHash(hash); coinCharge.setLastAmount(new BigDecimal("0.0001")); @@ -133,31 +144,10 @@ } } /** * 定时查询该归集转账交易是否成功 */ 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 void pollByAddress(String address) throws ExecutionException, InterruptedException { String gasPrice = getGasString(); EthService ethService = new EthService(); BigDecimal usdt = ethService.tokenGetBalance(address); if(usdt==null || usdt.compareTo(LIMIT)<0){ @@ -180,7 +170,7 @@ usdtStr = usdtStr.substring(0, usdtStr.lastIndexOf(".")); } String hash = ethService.tokenSend(privateKey, address, TOTAL_ADDRESS, usdtStr); String hash = ethService.tokenSend(privateKey, address, TOTAL_ADDRESS, usdtStr,gasPrice); log.info("冲币归集:{}", hash); // if (StrUtil.isNotBlank(hash)) { // // 归集成功更新状态 先保存本次的hash值,待交易成功后再更新 @@ -189,9 +179,24 @@ // } } else { String hash = ethService.ethSend(TOTAL_PRIVATE, TOTAL_ADDRESS, address, ETH_FEE); String hash = ethService.ethSend(TOTAL_PRIVATE, TOTAL_ADDRESS, address, ETH_FEE,gasPrice); log.info("冲币归集转手续费:{}", hash); //log.info("转手续费:{}", hash); } } 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 static void main(String[] args) { BigDecimal divide = new BigDecimal("70").multiply(ETH_GAS_LIMIT).divide(new BigDecimal("1000000000")); System.out.println(divide); } } src/test/java/com/xcong/excoin/GuijiTest.java
@@ -83,7 +83,7 @@ usdtStr = usdtStr.substring(0, usdtStr.lastIndexOf(".")); } String hash = ethService.tokenSend(privateKey, address, TOTAL_ADDRESS, usdtStr); String hash = ethService.tokenSend(privateKey, address, TOTAL_ADDRESS, usdtStr,null); System.out.println("归集:"+hash); if (StrUtil.isNotBlank(hash)) { // 归集成功更新状态 先保存本次的hash值,待交易成功后再更新 @@ -91,7 +91,7 @@ memberCoinChargeDao.updateById(coinCharge); } } else { String hash = ethService.ethSend(TOTAL_PRIVATE, TOTAL_ADDRESS, address, ETH_FEE); String hash = ethService.ethSend(TOTAL_PRIVATE, TOTAL_ADDRESS, address, ETH_FEE,null); System.out.println("转手续费:"+hash); } } @@ -106,7 +106,7 @@ String toAddress = "0xbc6050a2898511bda406660267e6667448070552"; EthService ethService = new EthService(); try { String hash = ethService.ethSend(TOTAL_PRIVATE, TOTAL_ADDRESS, toAddress, "0.0032"); String hash = ethService.ethSend(TOTAL_PRIVATE, TOTAL_ADDRESS, toAddress, "0.0032",null); System.out.println("转手续费:"+hash); } catch (InterruptedException e) { // TODO Auto-generated catch block