如果我的本地电子钱包安装在ubuntu上,则web3j ethGetBalance()始终为零

时间:2019-03-27 09:32:10

标签: java ethereum go-ethereum

我在AWS ubuntu上安装了以太坊钱包。 然后,我使用此命令(sudo geth --syncmode "fast" --cache=2048 --maxpeers=128 --metrics --rpc --rpcapi "db,eth,net,web3,personal,web3" --rpcaddr "0.0.0.0" --rpcport 8545 --rpcvhosts "*")运行以太坊钱包。然后启动以太坊钱包。 然后,我使用web3j Java创建了1个钱包

public Map<String, Object> createWallet(){
        String password = "your_password";
        ECKeyPair keyPair = Keys.createEcKeyPair();
        WalletFile wallet = Wallet.createStandard(password, keyPair);
        Map<String, Object> map = new HashMap<String, Object>();
        if (!MethodUtils.isObjectisNullOrEmpty(wallet)) {
            map.put("account", wallet.getAddress());
            LOGGER.debug("createWallet() : Account is {}.", wallet.getAddress());
        }
        if (!MethodUtils.isObjectisNullOrEmpty(keyPair)) {
            String privateKey = keyPair.getPrivateKey().toString(16);
            map.put("privateKey", keyPair.getPrivateKey().toString(16));
            LOGGER.debug("createWallet() : Private key is {}.", keyPair.getPrivateKey().toString(16));
            Credentials cs = Credentials.create(privateKey);
            privateKey = cs.getEcKeyPair().getPrivateKey().toString(16);
            String publicKey = cs.getEcKeyPair().getPublicKey().toString(16);
            String address = cs.getAddress();
            map.put("publicKey", publicKey);
            map.put("address", address);
        }
        return map;
}

支持我获得了地址=“ 0xE433A747ea68A558D49Fc2B558E36029bAC757d8” 然后我用一些乙醚填满余额到上述地址。 然后我用下面的web3j java代码检查余额

@Override
    public Map<String, Object> ethGetBalance(String address) throws InterruptedException, ExecutionException {

        Web3j web3 = Web3j.build(new HttpService("http://my_ip_address_of_aws_ubuntu:8545"))); // (Error : 0 Balance)
        // Below commented line gives correct balance
        //Web3j web3 = Web3j.build(new HttpService("https://mainnet.infura.io/v3/your_token"))); // (Correct Balance)
        EthGetBalance ethGetBalance = web3j.ethGetBalance(address, DefaultBlockParameterName.LATEST).sendAsync().get();

        Map<String, Object> map = new HashMap<String, Object>();
        if (!MethodUtils.isObjectisNullOrEmpty(ethGetBalance)) {

            map.put("id", ethGetBalance.getId());
            map.put("jsonRpc", ethGetBalance.getJsonrpc());
            map.put("result", ethGetBalance.getResult());
            map.put("rowResponse", ethGetBalance.getRawResponse());

            map.put("balance", ethGetBalance.getBalance());
        }

        return map;
    }

输出(错误:余额为0,但我的余额大于零)

{         “结果”:“ 0x0”,         “余额”:0,         “ id”:1         “ rowResponse”:null,         “ jsonRpc”:“ 2.0” }

输出说明

1. Web3j web3 = Web3j.build(new HttpService("http://my_ip_address_of_aws_ubuntu:8545")));
   Note : if i connect Web3j with my local ethereum wallet of my aws ubuntu then it gives balance 0 every time.
2. Web3j web3 = Web3j.build(new HttpService("https://mainnet.infura.io/v3/your_token"))); 
   Note : but if i connect Web3j with infura instread of local wallet then it gives correct balance

0 个答案:

没有答案