Account Functionality

Here you'll find the API calls for querying the following information:

  • all client accounts
  • coinbase account
  • account balance
  • account transaction count

The final subsection contains code illustrating all of the above interactions.

Retrieve All Client Accounts

The examples below show how to query the APIs for the list of addresses owned by the client. The functionality is compatible with eth_accounts. In each code snippet, the addresses are retrieved from the API and printed to the standard output.

// get accounts from API
List<Address> accounts = api.getWallet().getAccounts().getObject();

// print accounts to standard output
System.out.format("the keystore contains %d accounts, as follow:%n", accounts.size());
for (Address account : accounts) {
    System.out.format("\t%s%n", account.toString());
}
// get accounts from API
let accounts = web3.eth.accounts;

// print accounts to standard output
console.log("the keystore contains " + accounts.length + " accounts, as follow:");
console.log(accounts);
// get accounts from API
let accounts = web3.personal.listAccounts;

// print accounts to standard output
console.log("the keystore contains " + accounts.length + " accounts, as follow:");
console.log(accounts);

Sample output:

the keystore contains 4 accounts, as follow:
	a0bd0ef93902d9e123521a67bef7391e9487e963b2346ef3b3ff78208835545e
	a04164b007f75fb77e79ebfd90ace768e5d8ab26855035df4f2b9e03c1ca40f4
	a06f02e986965ddd3398c4de87e3708072ad58d96e9c53e87c31c8c970b211e5
	a0eb00973749a0f7b7f8e66e2a13bc2725e3a02804d44a0b88c1666512031591
the keystore contains 4 accounts, as follow:
[ '0xa0bd0ef93902d9e123521a67bef7391e9487e963b2346ef3b3ff78208835545e',
  '0xa04164b007f75fb77e79ebfd90ace768e5d8ab26855035df4f2b9e03c1ca40f4',
  '0xa06f02e986965ddd3398c4de87e3708072ad58d96e9c53e87c31c8c970b211e5',
  '0xa0eb00973749a0f7b7f8e66e2a13bc2725e3a02804d44a0b88c1666512031591' ]

Retrieve Coinbase Account

The examples below show how to query the APIs for the address configured to receive the mining rewards. The functionality is compatible with eth_coinbase. In each code snippet, the address is retrieved from the API and printed to the standard output.

// get miner account
Address account = api.getWallet().getMinerAccount().getObject();

// print retrieved value
System.out.format("coinbase account = %s%n", account.toString());
// get miner account
let miner = web3.eth.coinbase;

// print retrieved value
console.log("coinbase account = " + miner);

Sample output:

coinbase account = a0bd0ef93902d9e123521a67bef7391e9487e963b2346ef3b3ff78208835545e
coinbase account = 0xa0bd0ef93902d9e123521a67bef7391e9487e963b2346ef3b3ff78208835545e

Retrieve Account Balance

The examples below show how to query the APIs for the balance of a given address.
The functionality is compatible with eth_getBalance. In each code snippet, the balance is retrieved from the API and printed to the standard output.

// interpret string as address
Address account = Address.wrap("a06f02e986965ddd3398c4de87e3708072ad58d96e9c53e87c31c8c970b211e5");

// get account balance
BigInteger balance = api.getChain().getBalance(account).getObject();

// print balance
System.out.format("%s has balance = %d nAmp (over %d AION)%n",
                  account.toString(),
                  balance,
                  balance.divide(BigInteger.TEN.pow(18)));
// set address
let account = 'a06f02e986965ddd3398c4de87e3708072ad58d96e9c53e87c31c8c970b211e5';

// get account balance
let balance = web3.eth.getBalance(account);

// print balance
console.log(account + " has balance = " + balance + " nAmp (" + balance.shiftedBy(-18).toString() + " AION)");

Sample output:

a06f02e986965ddd3398c4de87e3708072ad58d96e9c53e87c31c8c970b211e5 has balance = 1003070000000000000 nAmp (over 1 AION)
a06f02e986965ddd3398c4de87e3708072ad58d96e9c53e87c31c8c970b211e5 has balance = 1003070000000000000 nAmp (1.00307 AION)

Retrieve Account Transaction Count

The examples below show how to query the APIs for the number of transactions sent by a given address. The functionality is compatible with eth_getTransactionCount. In each code snippet, the transaction count is retrieved from the API and printed to the standard output.

// interpret string as address
// note that hex prefix '0x' is optional
Address account = Address.wrap("0xa06f02e986965ddd3398c4de87e3708072ad58d96e9c53e87c31c8c970b211e5");

// get number of transactions sent by account
BigInteger txCount = api.getChain().getNonce(account).getObject();

// print performed transactions
// set address
// note that hex prefix '0x' is optional
account = '0xa06f02e986965ddd3398c4de87e3708072ad58d96e9c53e87c31c8c970b211e5';

// get number of transactions sent by account
let txCount = web3.eth.getTransactionCount(account);

// print performed transactions
console.log(account + " performed " + txCount + " transactions");

Sample output:

a06f02e986965ddd3398c4de87e3708072ad58d96e9c53e87c31c8c970b211e5 performed 33 transactions
a06f02e986965ddd3398c4de87e3708072ad58d96e9c53e87c31c8c970b211e5 performed 33 transactions

Complete Examples

Each code example below retrieves and prints to the standard output the following:

  1. all keystore addresses,
  2. the coinbase (miner) address,
  3. the balance for a given account,
  4. the number of transactions performed by a given account, and
  5. the balance and number of transactions performed by each account in the keystore.
package org.aion.tutorials;

import org.aion.api.IAionAPI;
import org.aion.api.type.ApiMsg;
import org.aion.base.type.Address;

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

public class AccountExample {

    public static void main(String[] args) {

        // connect to Java API
        IAionAPI api = IAionAPI.init();
        ApiMsg apiMsg = api.connect(IAionAPI.LOCALHOST_URL);

        // failed connection
        if (apiMsg.isError()) {
            System.out.format("Could not connect due to <%s>%n", apiMsg.getErrString());
            System.exit(-1);
        }

        // 1. eth_accounts

        // get accounts from API
        List<Address> accounts = api.getWallet().getAccounts().getObject();

        // print accounts to standard output
        System.out.format("the keystore contains %d accounts, as follow:%n", accounts.size());
        for (Address account : accounts) {
            System.out.format("\t%s%n", account.toString());
        }

        // 2. eth_coinbase

        // get miner account
        Address account = api.getWallet().getMinerAccount().getObject();

        // print retrieved value
        System.out.format("%ncoinbase account = %s%n", account.toString());

        // 3. eth_getBalance

        // interpret string as address
        account = Address.wrap("a06f02e986965ddd3398c4de87e3708072ad58d96e9c53e87c31c8c970b211e5");

        // get account balance
        BigInteger balance = api.getChain().getBalance(account).getObject();

        // print balance
        System.out.format("%n%s has balance = %d nAmp (over %d AION)%n",
                          account.toString(),
                          balance,
                          balance.divide(BigInteger.TEN.pow(18)));

        // 4. eth_getTransactionCount

        // interpret string as address
        // note that hex prefix '0x' is optional
        account = Address.wrap("0xa06f02e986965ddd3398c4de87e3708072ad58d96e9c53e87c31c8c970b211e5");

        // get number of transactions sent by account
        BigInteger txCount = api.getChain().getNonce(account).getObject();

        // print performed transactions
        System.out.format("%n%s performed %d transactions%n", account.toString(), txCount);

        // 5. eth_getBalance & eth_getTransactionCount for each keystore account

        // print balance & tx count to standard output
        System.out.format("%nthe keystore contains %d accounts, as follow:%n", accounts.size());
        for (Address acc : accounts) {
            System.out.format("\t%s balance = %22d nAmp, tx count = %2d%n",
                              acc.toString(),
                              api.getChain().getBalance(acc).getObject(),
                              api.getChain().getNonce(acc).getObject());
        }

        // disconnect from api
        api.destroyApi();

        System.exit(0);
    }
}
const Web3 = require('/path/to/aion/web3');
const web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

// 1.a) eth_accounts

// get accounts from API
let accounts = web3.eth.accounts;

// print accounts to standard output
console.log("the keystore contains " + accounts.length + " accounts, as follow:");
console.log(accounts);

// 1.b) eth_accounts

// get accounts from API
accounts = web3.personal.listAccounts;

// print accounts to standard output
console.log("\nthe keystore contains " + accounts.length + " accounts, as follow:");
console.log(accounts);

// 2. eth_coinbase

// get miner account
let miner = web3.eth.coinbase;

// print retrieved value
console.log("\ncoinbase account = " + miner);

// 3. eth_getBalance

// set address
let account = 'a06f02e986965ddd3398c4de87e3708072ad58d96e9c53e87c31c8c970b211e5';

// get account balance
let balance = web3.eth.getBalance(account);

// print balance
console.log("\n" + account + " has balance = " + balance + " nAmp (" + balance.shiftedBy(-18).toString() + " AION)");

// 4. eth_getTransactionCount

// set address
// note that hex prefix '0x' is optional
account = '0xa06f02e986965ddd3398c4de87e3708072ad58d96e9c53e87c31c8c970b211e5';

// get number of transactions sent by account
let txCount = web3.eth.getTransactionCount(account);

// print performed transactions
console.log("\n" + account + " performed " + txCount + " transactions");

// 5. eth_getBalance & eth_getTransactionCount for each keystore account

// print balance & tx count to standard output
console.log("\nthe keystore contains " + accounts.length + " accounts, as follow:");
accounts.forEach(print);

function print(acc, index) {
    console.log("\t" + acc + " balance = " + web3.eth.getBalance(acc) + " nAmp, tx count = " + web3.eth.getTransactionCount(acc));
}

Sample output:

the keystore contains 4 accounts, as follow:
	a0bd0ef93902d9e123521a67bef7391e9487e963b2346ef3b3ff78208835545e
	a04164b007f75fb77e79ebfd90ace768e5d8ab26855035df4f2b9e03c1ca40f4
	a06f02e986965ddd3398c4de87e3708072ad58d96e9c53e87c31c8c970b211e5
	a0eb00973749a0f7b7f8e66e2a13bc2725e3a02804d44a0b88c1666512031591

coinbase account = a0bd0ef93902d9e123521a67bef7391e9487e963b2346ef3b3ff78208835545e

a06f02e986965ddd3398c4de87e3708072ad58d96e9c53e87c31c8c970b211e5 has balance = 1003070000000000000 nAmp (over 1 AION)

a06f02e986965ddd3398c4de87e3708072ad58d96e9c53e87c31c8c970b211e5 performed 33 transactions

the keystore contains 4 accounts, as follow:
	a0bd0ef93902d9e123521a67bef7391e9487e963b2346ef3b3ff78208835545e balance = 1251613319407197678222 nAmp, tx count = 35
	a04164b007f75fb77e79ebfd90ace768e5d8ab26855035df4f2b9e03c1ca40f4 balance =    5000000000000000000 nAmp, tx count =  0
	a06f02e986965ddd3398c4de87e3708072ad58d96e9c53e87c31c8c970b211e5 balance =    1003070000000000000 nAmp, tx count = 33
	a0eb00973749a0f7b7f8e66e2a13bc2725e3a02804d44a0b88c1666512031591 balance =                      0 nAmp, tx count =  0
the keystore contains 4 accounts, as follow:
[ '0xa0bd0ef93902d9e123521a67bef7391e9487e963b2346ef3b3ff78208835545e',
  '0xa04164b007f75fb77e79ebfd90ace768e5d8ab26855035df4f2b9e03c1ca40f4',
  '0xa06f02e986965ddd3398c4de87e3708072ad58d96e9c53e87c31c8c970b211e5',
  '0xa0eb00973749a0f7b7f8e66e2a13bc2725e3a02804d44a0b88c1666512031591' ]

the keystore contains 4 accounts, as follow:
[ '0xa0bd0ef93902d9e123521a67bef7391e9487e963b2346ef3b3ff78208835545e',
  '0xa04164b007f75fb77e79ebfd90ace768e5d8ab26855035df4f2b9e03c1ca40f4',
  '0xa06f02e986965ddd3398c4de87e3708072ad58d96e9c53e87c31c8c970b211e5',
  '0xa0eb00973749a0f7b7f8e66e2a13bc2725e3a02804d44a0b88c1666512031591' ]

coinbase account = 0xa0bd0ef93902d9e123521a67bef7391e9487e963b2346ef3b3ff78208835545e

a06f02e986965ddd3398c4de87e3708072ad58d96e9c53e87c31c8c970b211e5 has balance = 1003070000000000000 nAmp (1.00307 AION)

0xa06f02e986965ddd3398c4de87e3708072ad58d96e9c53e87c31c8c970b211e5 performed 33 transactions

the keystore contains 4 accounts, as follow:
	0xa0bd0ef93902d9e123521a67bef7391e9487e963b2346ef3b3ff78208835545e balance = 1.253111308690440988407e+21 nAmp, tx count = 35
	0xa04164b007f75fb77e79ebfd90ace768e5d8ab26855035df4f2b9e03c1ca40f4 balance = 5000000000000000000 nAmp, tx count = 0
	0xa06f02e986965ddd3398c4de87e3708072ad58d96e9c53e87c31c8c970b211e5 balance = 1003070000000000000 nAmp, tx count = 33
	0xa0eb00973749a0f7b7f8e66e2a13bc2725e3a02804d44a0b88c1666512031591 balance = 0 nAmp, tx count = 0
Block Functionality