zainali5120
2020-11-09 4ba8d5db88249bf403ba7be602dd3edf6b80394a
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;
@@ -53,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 {
@@ -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);