zainali5120
2020-11-09 4ba8d5db88249bf403ba7be602dd3edf6b80394a
src/main/java/com/xcong/excoin/modules/blackchain/service/EthService.java
@@ -10,8 +10,10 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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;
@@ -52,8 +54,8 @@
   /**
    * 服务器地址
    */
   //private static final String ETH_UTL = "https://mainnet.infura.io/v3/882c66ebcfc141abbea22b948fa44321";
   private static final String ETH_UTL = "http://120.55.86.146:8545";
   //private static final String ETH_UTL = "http://120.55.86.146:8545";
   private static final String ETH_UTL = "http://114.55.250.231:8545";
   public EthService() {
      try {
@@ -208,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);
@@ -228,7 +233,7 @@
      String encodedFunction = FunctionEncoder.encode(function);
      RawTransaction rawTransaction = RawTransaction.createTransaction(nonce,
            Convert.toWei("60", 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 手续费
@@ -237,8 +242,9 @@
      // log.debug("transfer hexValue:" + hexValue);
      EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).sendAsync().get();
      CompletableFuture<EthSendTransaction> ethSendTransactionCompletableFuture = web3j.ethSendRawTransaction(hexValue).sendAsync();
      EthSendTransaction ethSendTransaction = ethSendTransactionCompletableFuture.get();
      //return "hash";
      if (ethSendTransaction.hasError()) {
         // log.info("transfer error:", ethSendTransaction.getError().getMessage());
@@ -250,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
@@ -263,7 +271,7 @@
      BigInteger nonce = ethGetTransactionCount.getTransactionCount();
      BigInteger value = Convert.toWei(amount, Convert.Unit.ETHER).toBigInteger();
      RawTransaction rawTransaction = RawTransaction.createEtherTransaction(nonce,
            Convert.toWei("50", 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);