java接入web3j来和以太坊打交道

1:启动节点

geth --identity "xiaohong" --rpc --rpccorsdomain "*" --rpcaddr "192.168.1.105"  --datadir "./" --port "30303" --nodiscover --rpcapi "personal,db,eth,net,web3,miner" --networkid 1999 console 2>>geth.log

2:通过maven引入jar包


    org.web3j
    core
    3.3.1

3:创建web3j 、admin实例

//import org.web3j.protocol.Web3j;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.admin.Admin;
import org.web3j.protocol.admin.methods.response.PersonalListAccounts;
import org.web3j.protocol.http.HttpService;
import org.web3j.protocol.rx.Web3jRx;


public class Web3JClient {
    private static String ip = "http://192.168.1.105:8545";

    private Web3JClient(){}

    private volatile static Web3j web3j;
    private  volatile  static Admin admin;

    public static Web3j getClient(){
        if(web3j==null){
            synchronized (Web3JClient.class){
                if(web3j==null){

                    web3j = Web3j.build(new HttpService(ip));
                }
            }
        }
        return web3j;
    }

    public static Admin getAdmin(){
        if(admin==null){
            synchronized (Web3JClient.class){
                if(admin==null){


                    admin = Admin.build(new HttpService(ip));
                }
            }
        }
        return admin;
    }
}

 

4:使用web3j、admin相关方法

import org.web3j.protocol.Web3j;
import org.web3j.protocol.admin.Admin;
import org.web3j.protocol.admin.methods.response.NewAccountIdentifier;
import org.web3j.protocol.core.DefaultBlockParameter;
import org.web3j.protocol.core.DefaultBlockParameterNumber;
import org.web3j.protocol.core.methods.response.EthGetBalance;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Account {
    private static Web3j web3j = Web3JClient.getClient();
    private static Admin admin = Web3JClient.getAdmin();
    public List getAccountlist(){

        try{
            return  admin.personalListAccounts().send().getAccountIds();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public String createAccount(String password){
        try {
            NewAccountIdentifier newAccountIdentifier = admin.personalNewAccount(password).send();
            if(newAccountIdentifier!=null){
                String accountId = newAccountIdentifier.getAccountId();
//                admin.personalSetAccountName(accountId,accountName);
//                admin.
//
//                Map account = new HashMap();
//                account.put(accountId,accountInfo);
//                parity.personalSetAccountMeta(accountId,account);account

                return  accountId;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

//    public PersonalAccountsInfo.AccountsInfo getAccountInfo(String accountId){
//
//        try{
//            PersonalAccountsInfo personalAccountsInfo = parity.personalAccountsInfo().send();
//
//            return  personalAccountsInfo.getAccountsInfo().get(accountId);
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//        return null;
//    }

    public BigInteger getBalance(String accountId){
        try {
            DefaultBlockParameter defaultBlockParameter = new DefaultBlockParameterNumber(web3j.ethBlockNumber().send().getBlockNumber());
            EthGetBalance ethGetBalance =  web3j.ethGetBalance(accountId,defaultBlockParameter).send();
            if(ethGetBalance!=null){
                return ethGetBalance.getBalance();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}

 

5:在main里面测试

import org.web3j.protocol.Web3j;
import org.web3j.protocol.http.HttpService;

import java.io.IOException;
import java.math.BigInteger;
import java.util.List;

public class TestWeb3j {


    public static void main(String args[]) {
//        getBalance();
//    createAccount();
//        queryAccount();
        getBalance();
    }

    public static void getBalance(){
        Account account = new Account();
        String accoundId = account.getAccountlist().get(0);
        System.out.println(accoundId);
        BigInteger ba = account.getBalance(accoundId);

        System.out.print(ba);
    }

    public static  void queryAccount(){
        Account account = new Account();
        List accounts = account.getAccountlist();
        for(String accountId:accounts){
            System.out.println(accountId);
        }

    }

    public static void createAccount(){
        Account account = new Account();
        String accountId = account.createAccount("123456");
        System.out.println("注册账户成功:"+accountId);
//        PersonalAccountsInfo.AccountsInfo accountsInfo = account.getAccountInfo("0xad7bbca86e02e503076b06931e05938e51e49fb9");
//        System.out.println(accountsInfo.toString());
    }


}

 

 

 

6:注意web3j需要在java 8的环境才能运行

 

7:源码可以通过https://download.csdn.net/download/wahaha13168/10544553下载

web3j文档路径:https://docs.web3j.io/getting_started.html#working-with-smart-contracts-with-java-smart-contract-wrappers

你可能感兴趣的:(区块链)