Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
6229e75
Add support for send_transaction and make it the default.
opi-smccoole Oct 9, 2020
6132df0
Update README to include send transaction.
opi-smccoole Oct 9, 2020
ec3c8ec
Merge pull request #5 from EOSIO/feature/send-transaction
opi-smccoole Oct 9, 2020
99246bb
Add support for get_kv_table_rows and get_block_info.
opi-smccoole Oct 22, 2020
280d371
Update README file.
opi-smccoole Oct 22, 2020
66e859d
Merge pull request #6 from EOSIO/feature/add-eosio2.1-rpc-calls
opi-smccoole Oct 22, 2020
089a913
Update eosio-java dependency to 0.1.6-eosio2.1
opi-smccoole Oct 29, 2020
0737850
Remove override from pushTransaction.
opi-smccoole Oct 30, 2020
2b94b34
Add EXAMPLES.md file for advanced snippets.
opi-smccoole Nov 4, 2020
3df1b0c
Merge pull request #7 from EOSIO/feature/add-examples
opi-smccoole Nov 4, 2020
159db10
Set version numbers to 1.0.0 for merge to develop and release.
opi-smccoole Nov 5, 2020
04711e7
Fix cast issue in EXAMPLES snippet.
opi-smccoole Nov 5, 2020
d1dc818
Create LiveServerUnitTest to run against an actual blockchain instanc…
opi-smccoole Nov 6, 2020
867a1cc
Merge pull request #9 from EOSIO/feature/live-server-unit-test
opi-smccoole Nov 6, 2020
5e260c3
Add new unit test file. Not sure why commit -a did not...
opi-smccoole Nov 6, 2020
c497fe5
Update get_kv_table_rows tests with json results.
opi-smccoole Nov 9, 2020
a70e23e
Address feedback from PR review.
opi-smccoole Nov 19, 2020
c45e775
Update from PR feedback on unit test assert.
opi-smccoole Nov 19, 2020
51125f5
Exclude junit from json-simple. Comment testImplementation dependenc…
opi-smccoole Nov 19, 2020
7ffbf70
Put back testImplementation dependencies for LiveServerUnitTest. Eve…
opi-smccoole Nov 19, 2020
1dda011
Update unit tests based on feedback to add specific catches in tests …
opi-smccoole Nov 19, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,4 @@ lint/tmp/

gradle.properties

.DS_Store
101 changes: 101 additions & 0 deletions EXAMPLES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# EOSIO SDK for Java: RPC Provider Examples

EOSIO SDK for Java: RPC Provider contains an extensive set of functionality beyond the basics required for transactions. The code snippets below show how to use some of this extended functionality. It is important to note that these are simply example snippets and may not work the way you expect if you just copy and paste them into a method.

Note: For clarity, some of these examples may use the soft key signature provider which is NOT recommended for production use!

## Extended RPC Provider Examples

### Get Account Information
This snippet retrieves information for an account on the blockchain. There are several layers of response to unpack if all information is desired. Some portions of the response are not fully unmarshalled, either due to size or because the responses can vary in structure. These are returned as general `Map` objects. The [NODEOS Reference](https://developers.eos.io/eosio-nodeos/reference) is helpful for decoding the parts of responses that are not fully unmarshalled.

```java
try {
EosioJavaRpcProviderImpl rpcProvider = new EosioJavaRpcProviderImpl(
"https://my.test.blockchain");
String testAccount = "test_account";

RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), "{\"name\":\""+testAccount+"\"}");
String response = rpcProvider.getAccount(requestBody);

JSONObject jsonObject = (JSONObject)parser.parse(response);

String account_name = (String) jsonObject.get("account_name");
Double ramQuota = (Double) jsonObject.get("ram_quota");
JSONArray permissions = (JSONArray) jsonObject.get("permissions");
JSONObject permission = (JSONObject) permissions.get(0);
String permissionName = (String) permission.get("perm_name");
// Keep going for more information...
} catch (Exception ex) {
println("Exception when calling getAccount(): " + ex.getLocalizedMessage()
+ "\n" + getStackTraceString(ex));
}
```

### Getting Transaction Information From the History Plugin

This snippet returns information on a transaction from the History API plugin. Only a few fields are shown in the decoding example below. The [NODEOS Reference](https://developers.eos.io/eosio-nodeos/reference) is helpful for decoding the parts of responses that are not fully unmarshalled.

```java
try {
EosioJavaRpcProviderImpl rpcProvider = new EosioJavaRpcProviderImpl(
"https://my.test.blockchain");

String getTransactionRequest = "{\n" +
"\t\"id\": \"transaction id\",\n" +
"\t\t\"block_num_hint\": 49420058\n" +
"}";

RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), getTransactionRequest);
String response = rpcProvider.getTransaction(requestBody);

JSONObject jsonObject = (JSONObject)parser.parse(response);
String transactionId = (String) jsonObject.get("id");
Long blockNum = (Long) jsonObject.get("block_num");
} catch (Exception ex) {
println("Exception when calling getTransaction(): " + ex.getLocalizedMessage()
+ "\n" + getStackTraceString(ex));
}
```

### Retrieving Values From KV Tables

This snippet retrieves values from a KV table defined by a contract on the server. The example below is requesting the values from the contract "todo" in the table named "todo". It is querying the index named "uuid" for the value "bf581bee-9f2c-447b-94ad-78e4984b6f51". The encoding type of the indexValue being supplied is "string". Other valid values include: "bytes", "dec", "hex" and "name", depending on the index type.

```java
// Creating RPC Provider
IRPCProvider rpcProvider;
try {
rpcProvider = new EosioJavaRpcProviderImpl("https://my.test.blockchain", ENABLE_NETWORK_LOG);
} catch (EosioJavaRpcProviderInitializerError eosioJavaRpcProviderInitializerError) {
eosioJavaRpcProviderInitializerError.printStackTrace();
println(Boolean.toString(false), eosioJavaRpcProviderInitializerError.getMessage());
return null;
}

String getKvTablesRequestJson = "{\n" +
" \"json\" : false\n" +
" \"code\" : \"todo\"\n" +
" \"table\" : \"todo\"\n" +
" \"encode_type\" : \"string\"\n" +
" \"index_name\" : \"uuid\"\n" +
" \"index_value\" : \"bf581bee-9f2c-447b-94ad-78e4984b6f51\"\n" +
" \"reverse\" : false\n" +
"}";

// If the RPC Provider was declared as the IRPCProvider interface type you have to cast it.
EosioJavaRpcProviderImpl fullRpcProvider = (EosioJavaRpcProviderImpl)rpcProvider;

RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), getKvTablesRequestJson);
try {
println("Requesting KV table rows...");
String response = fullRpcProvider.getKvTableRows(requestBody);
JSONParser parser = new JSONParser();
JSONObject jsonObject = (JSONObject)parser.parse(response);
JSONArray jsonArray = (JSONArray)jsonObject.get("rows");
String serializedResult = (String)jsonArray.get(0);
println("Your row result (serialized) is: " + serializedResult);
} catch (Exception getKvTableRowError) {
this.publishProgress(Boolean.toString(false), "Error getting table rows: " + getKvTableRowError.getLocalizedMessage());
}
```
26 changes: 20 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ Java RPC Provider is intended to be used in conjunction with [EOSIO SDK for Java
To use Java RPC Provider with EOSIO SDK for Java in your app, add the following modules to your `build.gradle`:

```groovy
implementation 'one.block:eosiojava:0.1.2'
implementation 'one.block:eosio-java-rpc-provider:0.1.1'
implementation 'one.block:eosiojava:1.0.0'
implementation 'one.block:eosio-java-rpc-provider:1.0.0'
```

The `build.gradle` files for the project currently include configurations for publishing the project to Artifactory. These should be removed if you are not planning to use Artifactory or you will encounter build errors. To do so, make the changes marked by comments throughout the files.
Expand Down Expand Up @@ -63,10 +63,10 @@ Please note that only the following five RPC endpoints have proper response mars

```java
Call<GetInfoResponse> getInfo();
Call<GetBlockResponse> getBlock(@Body GetBlockRequest getBlockRequest);
Call<GetBlockInfoResponse> getBlockInfo(@Body GetBlockInfoRequest getBlockInfoRequest);
Call<GetRawAbiResponse> getRawAbi(@Body GetRawAbiRequest getRawAbiRequest);
Call<GetRequiredKeysResponse> getRequiredKeys(@Body GetRequiredKeysRequest getRequiredKeysRequest);
Call<PushTransactionResponse> pushTransaction(@Body PushTransactionRequest pushTransactionRequest);
Call<SendTransactionResponse> sendTransaction(@Body SendTransactionRequest sendTransactionRequest);
```

The remaining endpoints accept a `RequestBody` as the request object and return a raw JSON string as the response. We aim to continue improving response marshalling for all endpoints, and we invite you to [help us improve](https://github.com/EOSIO/eosio-java-android-rpc-provider/issues/22) responses too. Check [EosioJavaRpcProviderImpl](https://github.com/EOSIO/eosio-java-android-rpc-provider/blob/master/eosiojavarpcprovider/src/main/java/one/block/eosiojavarpcprovider/implementations/EosioJavaRpcProviderImpl.java) for more details.
Expand All @@ -88,9 +88,23 @@ String balance = jsonArray.getString(0);

## Releases

10/09/20
10/01/20
Version 0.1.1
This version consumes the eosio-java library version 0.1.2 and provides functionality equal to the older eosio-java-android-rpc-provider 0.1.1 release.

Version 0.1.1 The version consumes the eosio-java library version 0.1.2 and provides functionality equal to the older eosio-java-android-rpc-provider 0.1.1 release.
10/09/20
Version 0.1.3
This version consumes the eosio-java library version 0.1.3 and adds support for the send_transaction endpoint, and return values.

10/22/20
Version 0.1.4
This version consumes the eosio-java library version 0.1.5 and adds support for the get_block_info, and get_kv_table_rows endpoints.
GetBlockInfo has replaced GetBlock in the IRPCProvider interface since it is the preferred method for getting the information required to calculate TAPOS. GetBlock is still available.
PushTransaction has been removed from IRPCProvider in favor of SendTransaction. PushTransaction is still available.

11/05/2020
Version 1.0.0
Support for EOSIO 3.0 functionality, including action return values and KV tables. Project jar implemenation that works on both Android and server side Java.

## Want to help?

Expand Down
8 changes: 6 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ allprojects {

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'one.block:eosiojava:0.1.2'
implementation 'one.block:eosiojava:1.0.0'

implementation 'org.jetbrains:annotations:17.0.0'
implementation (group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1') {
exclude group: 'org.hamcrest', module: 'hamcrest-core'
exclude group: 'junit', module: 'junit'
}
api 'com.squareup.retrofit2:retrofit:2.5.0'
api 'com.squareup.retrofit2:converter-gson:2.5.0'
Expand All @@ -49,6 +50,9 @@ dependencies {
testImplementation 'com.squareup.retrofit2:converter-gson:2.5.0'
testImplementation 'com.squareup.okhttp3:logging-interceptor:3.12.1'
testImplementation 'com.squareup.okhttp3:mockwebserver:3.12.1'
// These are for LiveServerUnitTest.
testImplementation 'one.block:eosiojavasoftkeysignatureprovider:1.0.0'
testImplementation 'one.block:eosio-java-abieos-serialization-provider:1.0.0'
}

test {
Expand All @@ -59,7 +63,7 @@ test {

def libraryGroupId = 'one.block'
def libraryArtifactId = 'eosio-java-rpc-provider'
def libraryVersion = '0.1.1'
def libraryVersion = '1.0.0'

task sourcesJar(type: Jar, dependsOn: classes){
classifier = 'sources'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ public class EosioJavaRpcErrorConstants {
*/
public static final String RPC_PROVIDER_ERROR_PUSHING_TRANSACTION = "Error pushing transaction.";

/**
* Error message gets thrown if {@link EosioJavaRpcProviderImpl#sendTransaction(SendTransactionRequest)} returns error.
*/
public static final String RPC_PROVIDER_ERROR_SENDING_TRANSACTION = "Error sending transaction.";

/**
* Error message gets thrown if {@link EosioJavaRpcProviderImpl#pushTransactions(RequestBody)} returns error.
*/
Expand Down Expand Up @@ -98,6 +103,11 @@ public class EosioJavaRpcErrorConstants {
*/
public static final String RPC_PROVIDER_ERROR_GET_TABLE_ROWS = "Error get table rows.";

/**
* Error message gets thrown if {@link EosioJavaRpcProviderImpl#getKvTableRows(RequestBody)} returns error.
*/
public static final String RPC_PROVIDER_ERROR_GET_KV_TABLE_ROWS = "Error get kv table rows.";

/**
* Error message gets thrown if {@link EosioJavaRpcProviderImpl#getCode(RequestBody)} returns error.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,38 @@

import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.util.Locale;

import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import okhttp3.logging.HttpLoggingInterceptor;
import okhttp3.logging.HttpLoggingInterceptor.Level;
import one.block.eosiojava.error.rpcProvider.GetBlockInfoRpcError;
import one.block.eosiojava.error.rpcProvider.GetBlockRpcError;
import one.block.eosiojava.error.rpcProvider.GetInfoRpcError;
import one.block.eosiojava.error.rpcProvider.GetRawAbiRpcError;
import one.block.eosiojava.error.rpcProvider.GetRequiredKeysRpcError;
import one.block.eosiojava.error.rpcProvider.PushTransactionRpcError;
import one.block.eosiojava.error.rpcProvider.RpcProviderError;
import one.block.eosiojava.interfaces.IRPCProvider;
import one.block.eosiojava.error.rpcProvider.SendTransactionRpcError;
import one.block.eosiojava.models.rpcProvider.request.GetBlockInfoRequest;
import one.block.eosiojava.models.rpcProvider.request.GetBlockRequest;
import one.block.eosiojava.models.rpcProvider.request.GetRawAbiRequest;
import one.block.eosiojava.models.rpcProvider.request.GetRequiredKeysRequest;
import one.block.eosiojava.models.rpcProvider.request.PushTransactionRequest;
import one.block.eosiojava.models.rpcProvider.request.SendTransactionRequest;
import one.block.eosiojava.models.rpcProvider.response.GetBlockInfoResponse;
import one.block.eosiojava.models.rpcProvider.response.GetBlockResponse;
import one.block.eosiojava.models.rpcProvider.response.GetInfoResponse;
import one.block.eosiojava.models.rpcProvider.response.GetRawAbiResponse;
import one.block.eosiojava.models.rpcProvider.response.GetRequiredKeysResponse;
import one.block.eosiojava.models.rpcProvider.response.PushTransactionResponse;
import one.block.eosiojava.models.rpcProvider.response.RPCResponseError;
//import one.block.eosiojavarpcprovider.BuildConfig;
import one.block.eosiojava.models.rpcProvider.response.SendTransactionResponse;
import org.jetbrains.annotations.NotNull;

import java.util.Locale;

import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import okhttp3.logging.HttpLoggingInterceptor;
import okhttp3.logging.HttpLoggingInterceptor.Level;
import one.block.eosiojava.interfaces.IRPCProvider;
import one.block.eosiojavarpcprovider.error.EosioJavaRpcErrorConstants;
import one.block.eosiojavarpcprovider.error.EosioJavaRpcProviderCallError;
import one.block.eosiojavarpcprovider.error.EosioJavaRpcProviderInitializerError;
Expand Down Expand Up @@ -152,7 +157,6 @@ private <O> O processCall(Call<O> call) throws Exception {
* @return GetBlockRsponse on successful return.
* @throws GetBlockRpcError Thrown if any errors occur calling or processing the request.
*/
@Override
public @NotNull GetBlockResponse getBlock(GetBlockRequest getBlockRequest)
throws GetBlockRpcError {
try {
Expand All @@ -164,6 +168,24 @@ private <O> O processCall(Call<O> call) throws Exception {
}
}

/**
* Issue a getBlockInfo() call to the blockchain and process the response.
* @param getBlockInfoRequest Info about a specific block.
* @return GetBlockInfoResponse on successful return.
* @throws GetBlockInfoRpcError Thrown if any errors occur calling or processing the request.
*/
@Override
public @NotNull GetBlockInfoResponse getBlockInfo(GetBlockInfoRequest getBlockInfoRequest)
throws GetBlockInfoRpcError {
try {
Call<GetBlockInfoResponse> syncCall = this.rpcProviderApi.getBlockInfo(getBlockInfoRequest);
return processCall(syncCall);
} catch (Exception ex) {
throw new GetBlockInfoRpcError(EosioJavaRpcErrorConstants.RPC_PROVIDER_ERROR_GETTING_BLOCK_INFO,
ex);
}
}

/**
* Issue a getRawAbi() request to the blockchain and process the response.
* @param getRawAbiRequest Info about a specific smart contract.
Expand Down Expand Up @@ -206,7 +228,6 @@ private <O> O processCall(Call<O> call) throws Exception {
* @return PushTransactionResponse on successful return.
* @throws PushTransactionRpcError Thrown if any errors occur calling or processing the request.
*/
@Override
public @NotNull PushTransactionResponse pushTransaction(
PushTransactionRequest pushTransactionRequest) throws PushTransactionRpcError {
try {
Expand All @@ -218,6 +239,23 @@ private <O> O processCall(Call<O> call) throws Exception {
}
}

/**
* Send a given transaction to the blockchain and process the response.
* @param sendTransactionRequest the transaction to send with signatures.
* @return SendTransactionResponse on successful return.
* @throws SendTransactionRpcError Thrown if any errors occur calling or processing the request.
*/
public @NotNull SendTransactionResponse sendTransaction(
SendTransactionRequest sendTransactionRequest) throws SendTransactionRpcError {
try {
Call<SendTransactionResponse> syncCall = this.rpcProviderApi.sendTransaction(sendTransactionRequest);
return processCall(syncCall);
} catch (Exception ex) {
throw new SendTransactionRpcError(EosioJavaRpcErrorConstants.RPC_PROVIDER_ERROR_SENDING_TRANSACTION,
ex);
}
}

/**
* Issue a get_account call to the blockchain and process the response.
* @param requestBody request body of get_account API
Expand Down Expand Up @@ -388,6 +426,23 @@ private <O> O processCall(Call<O> call) throws Exception {
}
}

/**
* Issue a getKvTableRows() call to the blockchain and process the response.
* @param requestBody request body of get_kv_table_rows API
* @return String content of ResponseBody on successful return.
* @throws RpcProviderError Thrown if any errors occur calling or processing the request.
*/
public @NotNull String getKvTableRows(RequestBody requestBody) throws RpcProviderError {
try {
Call<ResponseBody> syncCall = this.rpcProviderApi.getKvTableRows(requestBody);
try(ResponseBody responseBody = processCall(syncCall)) {
return responseBody.string();
}
} catch (Exception ex) {
throw new RpcProviderError(EosioJavaRpcErrorConstants.RPC_PROVIDER_ERROR_GET_KV_TABLE_ROWS, ex);
}
}

/**
* Issue a getCode() call to the blockchain and process the response.
* @param requestBody request body of get_code API
Expand Down
Loading