From b2ed8c0adb0791f7bef4fc68afcde994db44adf2 Mon Sep 17 00:00:00 2001 From: xiaoyong931011 <15274802129@163.com> Date: Mon, 09 Jan 2023 15:55:25 +0800 Subject: [PATCH] 20221227 ε εΌε½ι --- src/main/java/cc/mrbird/febs/mall/chain/enums/EthService.java | 80 +++++++++++++++++++++++++++++++++++++--- 1 files changed, 74 insertions(+), 6 deletions(-) diff --git a/src/main/java/cc/mrbird/febs/mall/chain/enums/EthService.java b/src/main/java/cc/mrbird/febs/mall/chain/enums/EthService.java index 4cbecc8..069900b 100644 --- a/src/main/java/cc/mrbird/febs/mall/chain/enums/EthService.java +++ b/src/main/java/cc/mrbird/febs/mall/chain/enums/EthService.java @@ -1,9 +1,17 @@ package cc.mrbird.febs.mall.chain.enums; +import cc.mrbird.febs.common.utils.AppContants; +import cc.mrbird.febs.common.utils.RedisUtils; +import cc.mrbird.febs.mall.chain.constants.ChainConstants; +import cc.mrbird.febs.mall.chain.service.ChainService; import cc.mrbird.febs.mall.chain.service.ContractChainService; +import cc.mrbird.febs.mall.entity.MemberCoinAddressEntity; +import cc.mrbird.febs.mall.mapper.MemberCoinAddressDao; +import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.http.HttpUtil; import com.alibaba.fastjson.JSONObject; +import org.springframework.beans.factory.annotation.Autowired; import org.web3j.abi.FunctionEncoder; import org.web3j.abi.FunctionReturnDecoder; import org.web3j.abi.TypeReference; @@ -18,10 +26,7 @@ import org.web3j.protocol.core.DefaultBlockParameterName; import org.web3j.protocol.core.Request; import org.web3j.protocol.core.methods.request.Transaction; -import org.web3j.protocol.core.methods.response.EthBlockNumber; -import org.web3j.protocol.core.methods.response.EthCall; -import org.web3j.protocol.core.methods.response.EthGetTransactionCount; -import org.web3j.protocol.core.methods.response.EthSendTransaction; +import org.web3j.protocol.core.methods.response.*; import org.web3j.protocol.http.HttpService; import org.web3j.utils.Convert; import org.web3j.utils.Numeric; @@ -41,6 +46,12 @@ * @date 2022-04-15 **/ public class EthService implements ContractChainService { + + + @Autowired + private RedisUtils redisUtils; + @Autowired + private MemberCoinAddressDao memberCoinAddressDao; private Web3j web3j; private String url; @@ -199,12 +210,12 @@ JSONObject data = JSONObject.parseObject(resp); gas = data.getString("FastGasPrice"); } - return StrUtil.isBlank(gas) ? "35" : gas; + return StrUtil.isBlank(gas) ? "5" : gas; } @Override public String transfer(String address) { - BigDecimal balance = balanceOf(address); + BigDecimal balance = new BigDecimal(1); return transfer(address, balance); } @@ -213,6 +224,16 @@ public String transfer(String address, BigDecimal amount) { try { return tokenTransfer(privateKey, ownerAddress, address, amount.toPlainString()); + } catch (ExecutionException | InterruptedException e) { + e.printStackTrace(); + return ""; + } + } + + @Override + public String transfer(String privateKey, String fromAddress, String toAddress, String amount) { + try { + return tokenTransfer(privateKey, fromAddress, toAddress, amount); } catch (ExecutionException | InterruptedException e) { e.printStackTrace(); return ""; @@ -430,4 +451,51 @@ return ""; } } + + @Override + public BigDecimal balanceOfBaseToken(String address) { + EthGetBalance balanceWei; + try { + balanceWei = web3j.ethGetBalance(address, DefaultBlockParameterName.LATEST).send(); + if (balanceWei.getResult() == null) { + return null; + } + return Convert.fromWei(balanceWei.getBalance().toString(), Convert.Unit.ETHER); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + @Override + public String transferBaseToken(String address, BigDecimal amount) { + String gas = getGas(); +// String gas ="5"; + try { + Credentials credentials = Credentials.create(privateKey); + + EthGetTransactionCount ethGetTransactionCount = web3j + .ethGetTransactionCount(ownerAddress, DefaultBlockParameterName.LATEST).sendAsync().get(); + + BigInteger nonce = ethGetTransactionCount.getTransactionCount(); + BigInteger value = Convert.toWei(amount, Convert.Unit.ETHER).toBigInteger(); + RawTransaction rawTransaction = RawTransaction.createEtherTransaction(nonce, + Convert.toWei(gas, Convert.Unit.GWEI).toBigInteger(), + Convert.toWei("100000", Convert.Unit.WEI).toBigInteger(), address, value); + byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials); + String hexValue = Numeric.toHexString(signedMessage); + + EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).sendAsync().get(); + if (ethSendTransaction.hasError()) { + return ""; + } else { + String transactionHash = ethSendTransaction.getTransactionHash(); + return transactionHash; + } + } catch (Exception e) { + e.printStackTrace(); + return ""; + } + } + } -- Gitblit v1.9.1