| | |
| | | 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;
|
| | |
| | | /**
|
| | | * 服务器地址
|
| | | */
|
| | | //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 {
|
| | |
| | | }
|
| | |
|
| | | // 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);
|
| | |
|
| | |
| | | String encodedFunction = FunctionEncoder.encode(function);
|
| | |
|
| | | RawTransaction rawTransaction = RawTransaction.createTransaction(nonce,
|
| | | Convert.toWei("40", Convert.Unit.GWEI).toBigInteger(),// 给矿工开的转账单价 单价越高越快
|
| | | Convert.toWei("80000", Convert.Unit.WEI).toBigInteger(), contractAddress, encodedFunction);//里程上限
|
| | | Convert.toWei(gas, Convert.Unit.GWEI).toBigInteger(),// 给矿工开的转账单价 单价越高越快
|
| | | Convert.toWei("60000", Convert.Unit.WEI).toBigInteger(), contractAddress, encodedFunction);//里程上限
|
| | | // 10*80000/1000000000=0.0008 手续费
|
| | |
|
| | | byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
|
| | |
| | |
|
| | | // 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());
|
| | |
| | | }
|
| | | }
|
| | |
|
| | | 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
|
| | |
| | | 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);
|