| | |
| | | package cc.mrbird.febs.dapp.chain; |
| | | |
| | | import cc.mrbird.febs.dapp.contract.andao.Abi; |
| | | import cn.hutool.core.util.HexUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.hutool.http.HttpUtil; |
| | | import com.alibaba.fastjson.JSONObject; |
| | |
| | | } |
| | | |
| | | public String tokenTransferFrom(String privateKey, String fromAddress, String toAddress, String amount) throws ExecutionException, InterruptedException { |
| | | String gas = getGas(); |
| | | // String gas = getGas(); |
| | | String gas = "5"; |
| | | |
| | | BigDecimal amountPow = new BigDecimal(amount).multiply(BigDecimal.TEN.pow(decimals())); |
| | | amount = amountPow.toPlainString(); |
| | |
| | | } |
| | | |
| | | public String tokenTransfer(String privateKey, String fromAddress, String toAddress, String amount) throws ExecutionException, InterruptedException { |
| | | String gas = getGas(); |
| | | // String gas = "5"; |
| | | // String gas = getGas(); |
| | | String gas = "5"; |
| | | |
| | | BigDecimal amountPow = new BigDecimal(amount).multiply(BigDecimal.TEN.pow(decimals())); |
| | | amount = amountPow.toPlainString(); |
| | |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | @Override |
| | | public String transferUSDT(String address, BigDecimal amount) { |
| | | try { |
| | | return tokenUSDTTransfer(privateKey, ownerAddress, address, amount.toPlainString()); |
| | | } catch (ExecutionException | InterruptedException e) { |
| | | e.printStackTrace(); |
| | | return ""; |
| | | } |
| | | } |
| | | |
| | | public String tokenUSDTTransfer(String privateKey, String fromAddress, String toAddress, String amount) throws ExecutionException, InterruptedException { |
| | | // String gas = getGas(); |
| | | String gas = "5"; |
| | | BigInteger amountOut = new BigInteger(Convert.toWei(amount, Convert.Unit.ETHER).setScale(0).toString()); |
| | | // BigDecimal amountPow = new BigDecimal(amount).multiply(BigDecimal.TEN.pow(decimals())); |
| | | // amount = amountPow.toPlainString(); |
| | | // if (amount.contains(".")) { |
| | | // amount = amount.substring(0, amount.lastIndexOf(".")); |
| | | // } |
| | | |
| | | Credentials credentials = Credentials.create(privateKey); |
| | | |
| | | EthGetTransactionCount ethGetTransactionCount = web3j |
| | | .ethGetTransactionCount(fromAddress, DefaultBlockParameterName.LATEST).sendAsync().get(); |
| | | |
| | | BigInteger nonce = ethGetTransactionCount.getTransactionCount(); |
| | | |
| | | Function function = new Function("withdrawANDAO", |
| | | Arrays.asList(new Address(toAddress), new Uint256(amountOut)), |
| | | Arrays.asList(new TypeReference<Type>() { |
| | | })); |
| | | |
| | | String encodedFunction = FunctionEncoder.encode(function); |
| | | |
| | | RawTransaction rawTransaction = RawTransaction.createTransaction(nonce, |
| | | Convert.toWei(gas, Convert.Unit.GWEI).toBigInteger(),// 给矿工开的转账单价 单价越高越快 |
| | | Convert.toWei("100000", Convert.Unit.WEI).toBigInteger(), contractAddress, encodedFunction);//里程上限 |
| | | |
| | | byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials); |
| | | String hexValue = Numeric.toHexString(signedMessage); |
| | | |
| | | CompletableFuture<EthSendTransaction> ethSendTransactionCompletableFuture = web3j.ethSendRawTransaction(hexValue).sendAsync(); |
| | | EthSendTransaction ethSendTransaction = ethSendTransactionCompletableFuture.get(); |
| | | |
| | | |
| | | if (ethSendTransaction.hasError()) { |
| | | return ""; |
| | | } else { |
| | | return ethSendTransaction.getTransactionHash(); |
| | | } |
| | | } |
| | | } |