eth交易

   //获取链上最高块
    public long blockNumHight() throws IOException {
        return web3j.ethBlockNumber().send().getBlockNumber().longValue();
    }
    
   //获取gas
public BigInteger getGasPrice() throws Exception { return web3j.ethGasPrice().send().getGasPrice(); } //根据块号获取eth块信息 public EthBlock getBlock(long blockNum) throws IOException { return EthBlock.fromWeb3( //这里只是把块信息做了一个对象转换 web3j.ethGetBlockByNumber(new DefaultBlockParameterNumber(blockNum), true).send().getBlock() ); }
   //根据块哈希获取eth块信息
public EthBlock getBlock(String blockHash) throws Exception { if(StringUtils.isBlank(blockHash)) { return null; } return EthBlock.fromWeb3( web3j.ethGetBlockByHash(blockHash, true).send().getBlock() ); } //根据交易哈希获取eth交易信息 public Transaction getTransaction(String txHash) throws IOException { if(StringUtils.isBlank(txHash)) { return null; } return web3j.ethGetTransactionByHash(txHash).send().getResult(); } //根据地址获取nonce public BigInteger getNonce(String address) throws IOException{ return web3j.ethGetTransactionCount(address.toLowerCase(), DefaultBlockParameterName.PENDING).send().getTransactionCount(); } //获取地址的余额 public BigInteger getBalance(String address) throws IOException { String checkedAddr = EthUtils.checkAddress(address); if(StringUtils.isBlank(checkedAddr)) { return BigInteger.ZERO; } return web3j.ethGetBalance(address, DefaultBlockParameterName.PENDING).send().getBalance(); } //获取某一块上的地址的余额 public BigInteger getBalance(String address, long blockNum) throws IOException { String checkedAddr = EthUtils.checkAddress(address); if(StringUtils.isBlank(checkedAddr)) { return BigInteger.ZERO; } return web3j.ethGetBalance(address, new DefaultBlockParameterNumber(blockNum)).send().getBalance(); } //发eth交易 public SendTransaction transfer( String sourceAddr, String sourcePriKey, String targetAddr, BigInteger amount, BigInteger gasPrice, boolean amountIncludeFee) throws Exception { sourceAddr = EthUtils.checkAddress(sourceAddr); targetAddr = EthUtils.checkAddress(targetAddr); if(StringUtils.isAnyBlank(sourceAddr,targetAddr,sourcePriKey)) { return null; } if(gasPrice==null || gasPrice.compareTo(BigInteger.ZERO)<0 || amount==null || amount.compareTo(BigInteger.ZERO)<0 ) { return null; } BigInteger gas = EthUtils.calculateGas(null, gasPrice); BigInteger payAmount = amount; if(amountIncludeFee) { payAmount = amount.subtract(gas); if(payAmount.compareTo(BigInteger.ZERO)<0) { return null; } } //检查余额 BigInteger balance = this.getBalance(sourceAddr); if(balance.compareTo(payAmount.add(gas)) < 0) { return null; } BigInteger nonce = this.getNonce(sourceAddr); String txHexData = EthUtils.signTransaction(sourceAddr, sourcePriKey, nonce, targetAddr, gasPrice, payAmount, null); if(StringUtils.isBlank(txHexData)) { return null; } EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(txHexData).send(); return new SendTransaction(ethSendTransaction.getTransactionHash(), nonce, Convert.fromWei(gas.toString(), Unit.ETHER)); }

 

posted @ 2020-09-15 20:16  panda's  阅读(449)  评论(0编辑  收藏  举报