This project implements the API to interact with a NEAR Node. It wraps the Json-RPC requests and maps the results to Java objects.
- Java 8+
- Maven (via wrapper)
./mvnw package
<dependency>
<groupId>com.syntifi.near</groupId>
<artifactId>near-java-api</artifactId>
<version>0.1.0</version>
</dependency>
implementation 'com.syntifi.near:near-java-api:0.1.0'
This project used the following references:
- Official docs @ docs.near.org
nearClient = NearService.usingPeer("archival-rpc.testnet.near.org");
2. Access Keys
2.1 View access key
Returns information about a single access key for given account.
If permission of the key is FunctionCall, it will return more details such as the allowance, receiver_id, and method_names.
String accountId = "client.chainlink.testnet";
String publicKey = "ed25519:H9k5eiU4xXS3M4z8HzKJSLaZdqGdGwBG49o7orNC4eZW";
AccessKey accessKey = nearClient.viewAccessKey(Finality.FINAL, accountId, publicKey);
String accountId = "client.chainlink.testnet";
String publicKey = "ed25519:H9k5eiU4xXS3M4z8HzKJSLaZdqGdGwBG49o7orNC4eZW";
AccessKey accessKey = nearClient.viewAccessKey(78443365, accountId, publicKey);
String accountId = "client.chainlink.testnet";
String publicKey = "ed25519:H9k5eiU4xXS3M4z8HzKJSLaZdqGdGwBG49o7orNC4eZW";
AccessKey accessKey = nearClient.viewAccessKey("8bVg8wugs2QHqXr42oEsCYyH7jvR9pLaAP35dFqx2evU", accountId, publicKey);
Returns all access keys for a given account.
String accountId = "client.chainlink.testnet";
AccessKeyList accessKeyList = nearClient.viewAccessKeyList(Finality.FINAL, accountId);
String accountId = "client.chainlink.testnet";
AccessKeyList accessKeyList = nearClient.viewAccessKeyList(78772585, accountId);
String accountId = "client.chainlink.testnet";
AccessKeyList accessKeyList = nearClient.viewAccessKeyList("DwFpDPiQXBaX6Vw3aKazQ4nXjgzw1uk6XpUkfTSJrbXf", accountId);
Returns individual access key changes in a specific block. You can query multiple keys by passing an array of objects containing the account_id and public_key.
Key[] keys = new Key[1];
Key key0 = new Key("example-acct.testnet", "ed25519:25KEc7t7MQohAJ4EDThd2vkksKkwangnuJFzcoiXj9oM");
keys[0] = key0;
AccessKeyChanges accessKeyChanges = nearClient.viewSingleAccessKeyChanges(Finality.FINAL, keys);
Key[] keys = new Key[1];
Key key0 = new Key("example-acct.testnet", "ed25519:25KEc7t7MQohAJ4EDThd2vkksKkwangnuJFzcoiXj9oM");
keys[0] = key0;
AccessKeyChanges accessKeyChanges = nearClient.viewSingleAccessKeyChanges(78433961, keys);
Key[] keys = new Key[1];
Key key0 = new Key("example-acct.testnet", "ed25519:25KEc7t7MQohAJ4EDThd2vkksKkwangnuJFzcoiXj9oM");
keys[0] = key0;
AccessKeyChanges accessKeyChanges = nearClient.viewSingleAccessKeyChanges("Cr82U81VqHgCz9LzZjPivh9t16e8es6aFCv9qvDMMH88", keys);
Returns changes to all access keys of a specific block. Multiple accounts can be quereied by passing an array of account_ids.
String[] accountIds = new String[1];
accountIds[0] = "client.chainlink.testnet";
AccessKeyChanges accessKeyChanges = nearClient.viewAllAccessKeyChanges(Finality.FINAL, accountIds);
String[] accountIds = new String[1];
accountIds[0] = "client.chainlink.testnet";
AccessKeyChanges accessKeyChanges = nearClient.viewAllAccessKeyChanges(78433518, accountIds);
String[] accountIds = new String[1];
accountIds[0] = "client.chainlink.testnet";
AccessKeyChanges accessKeyChanges = nearClient.viewAllAccessKeyChanges("Ais9kPbHvk6XmEYptoEpBtyFW77V16TZNHHnYtmXWr1d",accountIds);
3.1 View account
Returns basic account information.
Account account = nearClient.viewAccount(Finality.FINAL, "nearkat.testnet");
Account account = nearClient.viewAccount(78439658, "nearkat.testnet");
Account account = nearClient.viewAccount("5hyGx7LiGaeRiAN4RrKcGomi1QXHqZwKXFQf6xTmvUgb", "nearkat.testnet");
Returns account changes from transactions in a given account.
String[] accountIds = new String[1];
accountIds[0] = "nearkat.testnet";
AccountChanges accountChanges = nearClient.viewAccountChanges(Finality.FINAL, accountIds);
String[] accountIds = new String[1];
accountIds[0] = "nearkat.testnet";
AccountChanges accountChanges = nearClient.viewAccountChanges(78440142, accountIds);
String[] accountIds = new String[1];
accountIds[0] = "nearkat.testnet";
AccountChanges accountChanges = nearClient.viewAccountChanges("7vWp2djKLoJ3RE1sr8RzSKQtyzKpe2wZ7NCcDuFNuL7j", accountIds);
Returns the contract code (Wasm binary) deployed to the account. Please note that the returned code will be encoded in base64.
ContractCode contractCode = nearClient.viewContractCode(Finality.FINAL, "guest-book.testnet");
ContractCode contractCode = nearClient.viewContractCode(78440518, "guest-book.testnet");
ContractCode contractCode = nearClient.viewContractCode("uLxyauKPhSk1tebYKi3k69pHSaT2ZLzWy4JwtGm52pu", "guest-book.testnet");
Returns the state (key value pairs) of a contract based on the key prefix (base64 encoded). Pass an empty string for prefix_base64 if you would like to return the entire state. Please note that the returned state will be base64 encoded as well.
ContractState contractState = nearClient.viewContractState(Finality.FINAL, "guest-book.testnet", "");
ContractState contractState = nearClient.viewContractState(78440679, "guest-book.testnet", "");
ContractState contractState = nearClient.viewContractState("342bkjvnzoZ7FGRE5BwDVkzSRUYXAScTz3GsDB9sEHXg", "guest-book.testnet", "");
Returns the state change details of a contract based on the key prefix (encoded to base64). Pass an empty string for this param if you would like to return all state changes.
String[] accountIds = new String[1];
accountIds[0] = "guest-book.testnet";
ContractStateChanges contractStateChanges = nearClient.viewContractStateChanges(Finality.FINAL, accountIds, "");
String[] accountIds = new String[1];
accountIds[0] = "guest-book.testnet";
ContractStateChanges contractStateChanges = nearClient.viewContractStateChanges(78441183, accountIds, "");
String[] accountIds = new String[1];
accountIds[0] = "guest-book.testnet";
ContractStateChanges contractStateChanges = nearClient.viewContractStateChanges("5KgQ8uu17bhUPnMUbkmciHxbpFvsbhwdkJu4ptRfR7Zn", accountIds, "");
Returns code changes made when deploying a contract. Change is returned is a base64 encoded WASM file.
String[] accountIds = new String[1];
accountIds[0] = "dev-1602714453032-7566969";
ContractCodeChanges contractCodeChanges = nearClient.viewContractCodeChanges(Finality.FINAL, accountIds);
String[] accountIds = new String[1];
accountIds[0] = "dev-1602714453032-7566969";
ContractCodeChanges contractCodeChanges = nearClient.viewContractCodeChanges(78441560, accountIds);
String[] accountIds = new String[1];
accountIds[0] = "dev-1602714453032-7566969";
ContractCodeChanges contractCodeChanges = nearClient.viewContractCodeChanges("HpsjZvjtuxarKRsXGVrgB6qtuCcHRgx3Xof1gfT2Jfj7", accountIds);
Allows you to call a contract method as a view function.
ContractFunctionCallResult contractFunctionCallResult = nearClient
.callContractFunction(
Finality.FINAL,
"guest-book.testnet",
"getMessages",
"e30=");
ContractFunctionCallResult contractFunctionCallResult = nearClient
.callContractFunction(79272492,
"guest-book.testnet",
"getMessages",
"e30=");
ContractFunctionCallResult contractFunctionCallResult = nearClient
.callContractFunction(
"J5QTB4Stz3iwtHvgr5KnVzNUgzm4J1bE5Et6JWrJPC8o",
"guest-book.testnet",
"getMessages",
"e30=");
4.1 Block details
Queries network and returns block for given height or hash. You can also use finality param to return latest block details.
Block block = nearClient.getBlock(Finality.FINAL);
Block block = nearClient.getBlock(78770817);
Block block = nearClient.getBlock("FXTWzPjqWztjHfneqJb9cBDB2QLTY1Rja4GHrswAv1b9");
4.2 Changes in Block
Returns changes in block for given block height or hash. You can also use finality param to return latest block details.
BlockChanges blockChanges = nearClient.getBlockChanges(Finality.FINAL);
BlockChanges blockChanges = nearClient.getBlockChanges(78770674);
BlockChanges blockChanges = nearClient.getBlockChanges("BmEZnrmov6h6rMPpWkMV2JtU1C5LP563V5Y5yXwUW2N5");
4.3 Chunk Details
Returns details of a specific chunk. You can run a block details query to get a valid chunk hash.
Chunk chunk = nearClient.getChunkDetails("9mdG2cRcV8Dsb1EoSjtya81NddjRB2stYCTVukZh7zzw");
Chunk chunk = nearClient.getChunkDetails(78567387, 0);
Chunk chunk = nearClient.getChunkDetails("F1HXTzeYgYq28rgsHuKUrRbo5QTBGKFYG7rbxXkRZWXN", 0);
5. Gas
5.1 Gas Price
Returns gas price for a specific block_height or block_hash.
- Using [null] will return the most recent block's gas price.
GasPrice gasPrice = nearClient.getGasPrice(null);
GasPrice gasPrice = nearClient.getGasPrice(78770817);
GasPrice gasPrice = nearClient.getGasPrice("FXTWzPjqWztjHfneqJb9cBDB2QLTY1Rja4GHrswAv1b9");
6. Protocol
6.1 Genesis Config
Returns current genesis configuration.
GenesisConfig genesisConfig = nearClient.getGenesisConfig();
6.2 Protocol Config
Returns most recent protocol configuration or a specific queried block. Useful for finding current storage and transaction costs.
ProtocolConfig protocolConfig = nearClient.getProtocolConfig(Finality.FINAL);
ProtocolConfig protocolConfig = nearClient.getProtocolConfig(78770817);
ProtocolConfig protocolConfig = nearClient.getProtocolConfig("FXTWzPjqWztjHfneqJb9cBDB2QLTY1Rja4GHrswAv1b9");
7. Network
7.1 Node Status
Returns general status of a given node (sync status, nearcore node version, protocol version, etc), and the current set of validators.
NodeStatus nodeStatus = nearClient.getNodeStatus();
7.2 Network Info
Returns the current state of node network connections (active peers, transmitted data, etc.)
NetworkInfo networkInfo = nearClient.getNetworkInfo();
Queries active validators on the network returning details and the state of validation on the blockchain.
ValidationStatus networkValidationStatus = nearClient.getNetworkValidationStatus(null);
Block lastBlock = nearClient.getBlock(Finality.OPTIMISTIC);
ValidationStatus networkValidationStatus = nearClient.getNetworkValidationStatus(lastBlock.getHeader().getHeight());
Block lastBlock = nearClient.getBlock(Finality.FINAL);
ValidationStatus networkValidationStatus = nearClient.getNetworkValidationStatus(lastBlock.getHeader().getHash());
8. Transactions
Sends a transaction and immediately returns transaction hash.
String signerId = "syntifi-bob.testnet";
String receiverId = "syntifi-alice.testnet";
BigInteger amount = new BigInteger("100", 10);
EncodedHash transactionAsync = TransactionService
.sendTransferActionAsync(nearClient, signerId, receiverId, bobNearPublicKey, bobNearPrivateKey, amount);
Sends a transaction and waits until transaction is fully complete. (Has a 10 second timeout)
String signerId = "syntifi-alice.testnet";
String receiverId = "syntifi-bob.testnet";
BigInteger amount = new BigInteger("100", 10);
TransactionAwait transactionAwait = TransactionService
.sendTransferActionAwait(nearClient, signerId, receiverId, aliceNearPublicKey, aliceNearPrivateKey, amount);
Queries status of a transaction by hash and returns the final transaction result.
String transactionHash = "DwWUi6WbVHKTCDjVu4gmuQfryqjwTjrZ6ntRcKcGN6Gd";
String accountId = "isonar.testnet";
TransactionStatus transactionStatus = nearClient.getTransactionStatus(transactionHash, accountId);
Queries status of a transaction by hash, returning the final transaction result and details of all receipts.
String transactionHash = "DwWUi6WbVHKTCDjVu4gmuQfryqjwTjrZ6ntRcKcGN6Gd";
String accountId = "isonar.testnet";
TransactionStatus transactionStatusWithReceipts = nearClient.getTransactionStatusWithReceipts(transactionHash, accountId);
8.5 Receipt by ID
Fetches a receipt by it's ID (as is, without a status or execution outcome)
String receiptId = "8b9Vt1xH8DZecMda1YqUcMWA41NvknUJJVd2XEQikPRs";
Receipt transactionReceipt = nearClient.getTransactionReceipt(receiptId);
Loads a Wallet object from a json file.
String fileName = "./my-wallet.json";
WalletService.writeWalletToFile(fileName, wallet)
Writes a Wallet object to a json file.
String fileName = "./my-wallet.json";
Wallet wallet = WalletService.loadWalletFromFile(fileName);