Merge branch 'develop' of github.com:Agorise/graphenej into develop

This commit is contained in:
Nelson R. Perez 2018-12-19 20:41:55 -05:00
commit 101b4a5aba
9 changed files with 199 additions and 6 deletions

View file

@ -24,6 +24,7 @@ public class RPC {
public static final String CALL_GET_ACCOUNT_HISTORY_BY_OPERATIONS = "get_account_history_by_operations";
public static final String CALL_LOOKUP_ACCOUNTS = "lookup_accounts";
public static final String CALL_LIST_ASSETS = "list_assets";
public static final String CALL_GET_ASSETS = "get_assets";
public static final String CALL_GET_OBJECTS = "get_objects";
public static final String CALL_GET_ACCOUNT_BALANCES = "get_account_balances";
public static final String CALL_LOOKUP_ASSET_SYMBOLS = "lookup_asset_symbols";

View file

@ -23,6 +23,7 @@ import cy.agorise.graphenej.api.calls.GetAccountBalances;
import cy.agorise.graphenej.api.calls.GetAccountByName;
import cy.agorise.graphenej.api.calls.GetAccountHistoryByOperations;
import cy.agorise.graphenej.api.calls.GetAccounts;
import cy.agorise.graphenej.api.calls.GetAssets;
import cy.agorise.graphenej.api.calls.GetBlock;
import cy.agorise.graphenej.api.calls.GetBlockHeader;
import cy.agorise.graphenej.api.calls.GetDynamicGlobalProperties;
@ -193,6 +194,13 @@ public class DeserializationMap {
.registerTypeAdapter(AssetAmount.class, new AssetAmount.AssetAmountDeserializer())
.create();
mGsonMap.put(GetAccountBalances.class, getAccountBalancesGson);
// GetAssets
mClassMap.put(GetAssets.class, List.class);
Gson getAssetsGson = new GsonBuilder()
.registerTypeAdapter(Asset.class, new Asset.AssetDeserializer())
.create();
mGsonMap.put(GetAssets.class, getAssetsGson);
}
public Class getReceivedClass(Class _class){

View file

@ -34,6 +34,7 @@ import cy.agorise.graphenej.api.ConnectionStatusUpdate;
import cy.agorise.graphenej.api.calls.ApiCallable;
import cy.agorise.graphenej.api.calls.GetAccountBalances;
import cy.agorise.graphenej.api.calls.GetAccounts;
import cy.agorise.graphenej.api.calls.GetAssets;
import cy.agorise.graphenej.api.calls.GetFullAccounts;
import cy.agorise.graphenej.api.calls.GetKeyReferences;
import cy.agorise.graphenej.api.calls.GetLimitOrders;
@ -582,6 +583,9 @@ public class NetworkService extends Service {
} else if(requestClass == GetAccountBalances.class){
Type GetAccountBalancesResponse = new TypeToken<JsonRpcResponse<List<AssetAmount>>>(){}.getType();
parsedResponse = gson.fromJson(text, GetAccountBalancesResponse);
} else if(requestClass == GetAssets.class){
Type GetAssetsResponse = new TypeToken<JsonRpcResponse<List<Asset>>>(){}.getType();
parsedResponse = gson.fromJson(text, GetAssetsResponse);
}else {
Log.w(TAG,"Unknown request class");
}
@ -712,7 +716,8 @@ public class NetworkService extends Service {
// Remove node from nodeLatencyVerifier, so that it publishes its removal
nodeLatencyVerifier.removeNode(mSelectedNode);
} else {
// Avoid crash #133
} else if (mSelectedNode != null){
// Adding a very high latency value to this node in order to prevent
// us from getting it again
mSelectedNode.addLatencyValue(Long.MAX_VALUE);

View file

@ -0,0 +1,27 @@
package cy.agorise.graphenej.api.calls;
import java.io.Serializable;
import java.util.ArrayList;
import cy.agorise.graphenej.RPC;
import cy.agorise.graphenej.Transaction;
import cy.agorise.graphenej.api.ApiAccess;
import cy.agorise.graphenej.models.ApiCall;
public class BroadcastTransaction implements ApiCallable {
public static final int REQUIRED_API = ApiAccess.API_NETWORK_BROADCAST;
private Transaction mTransaction;
public BroadcastTransaction(Transaction transaction){
if(!transaction.hasPrivateKey()) throw new IllegalStateException("The Transaction instance has to be provided with a private key in order to be broadcasted");
mTransaction = transaction;
}
@Override
public ApiCall toApiCall(int apiId, long sequenceId) {
ArrayList<Serializable> transactions = new ArrayList<>();
transactions.add(mTransaction);
return new ApiCall(apiId, RPC.CALL_BROADCAST_TRANSACTION, transactions, RPC.VERSION, sequenceId);
}
}

View file

@ -0,0 +1,54 @@
package cy.agorise.graphenej.api.calls;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import cy.agorise.graphenej.Asset;
import cy.agorise.graphenej.RPC;
import cy.agorise.graphenej.api.ApiAccess;
import cy.agorise.graphenej.models.ApiCall;
public class GetAssets implements ApiCallable {
public static final int REQUIRED_API = ApiAccess.API_NONE;
private List<Asset> assetList = new ArrayList<>();
/**
* Constructor that will receive a List of Asset instances.
*
* @param assets List of Asset instances.
*/
public GetAssets(List<Asset> assets){
assetList.addAll(assets);
}
/**
* Constructor that will accept a string containing the asset id.
*
* @param id String containing the asset id of the desired asset.
*/
public GetAssets(String id){
assetList.add(new Asset(id));
}
/**
* Constructor that accepts an {@link Asset} object instance.
*
* @param asset Asset class instance.
*/
public GetAssets(Asset asset){
assetList.add(asset);
}
@Override
public ApiCall toApiCall(int apiId, long sequenceId) {
ArrayList<Serializable> params = new ArrayList<>();
ArrayList<Serializable> assetIds = new ArrayList<>();
for(Asset asset : assetList){
assetIds.add(asset.getObjectId());
}
params.add(assetIds);
return new ApiCall(apiId, RPC.CALL_GET_ASSETS, params, RPC.VERSION, sequenceId);
}
}

View file

@ -29,7 +29,8 @@ android {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
api project(':graphenej')
implementation project(':graphenej')
implementation 'org.bitcoinj:bitcoinj-core:0.14.3'
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:recyclerview-v7:27.1.1'
implementation 'com.android.support:design:27.1.1'

View file

@ -70,6 +70,7 @@ public class CallsActivity extends AppCompatActivity {
RPC.CALL_GET_REQUIRED_FEES,
RPC.CALL_LOOKUP_ASSET_SYMBOLS,
RPC.CALL_LIST_ASSETS,
RPC.CALL_GET_ASSETS,
RPC.CALL_GET_ACCOUNT_BY_NAME,
RPC.CALL_GET_LIMIT_ORDERS,
RPC.CALL_GET_ACCOUNT_HISTORY_BY_OPERATIONS,
@ -78,6 +79,7 @@ public class CallsActivity extends AppCompatActivity {
RPC.CALL_GET_DYNAMIC_GLOBAL_PROPERTIES,
RPC.CALL_GET_KEY_REFERENCES,
RPC.CALL_GET_ACCOUNT_BALANCES,
RPC.CALL_BROADCAST_TRANSACTION,
REMOVE_CURRENT_NODE
};

View file

@ -13,9 +13,12 @@ import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.common.primitives.UnsignedLong;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.bitcoinj.core.ECKey;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
@ -26,26 +29,35 @@ import butterknife.ButterKnife;
import butterknife.OnClick;
import cy.agorise.graphenej.Asset;
import cy.agorise.graphenej.AssetAmount;
import cy.agorise.graphenej.BaseOperation;
import cy.agorise.graphenej.BlockData;
import cy.agorise.graphenej.BrainKey;
import cy.agorise.graphenej.Memo;
import cy.agorise.graphenej.OperationType;
import cy.agorise.graphenej.RPC;
import cy.agorise.graphenej.Transaction;
import cy.agorise.graphenej.UserAccount;
import cy.agorise.graphenej.api.ConnectionStatusUpdate;
import cy.agorise.graphenej.api.android.DeserializationMap;
import cy.agorise.graphenej.api.android.RxBus;
import cy.agorise.graphenej.api.calls.BroadcastTransaction;
import cy.agorise.graphenej.api.calls.GetAccountBalances;
import cy.agorise.graphenej.api.calls.GetAccountByName;
import cy.agorise.graphenej.api.calls.GetAccountHistoryByOperations;
import cy.agorise.graphenej.api.calls.GetAccounts;
import cy.agorise.graphenej.api.calls.GetAssets;
import cy.agorise.graphenej.api.calls.GetBlock;
import cy.agorise.graphenej.api.calls.GetDynamicGlobalProperties;
import cy.agorise.graphenej.api.calls.GetFullAccounts;
import cy.agorise.graphenej.api.calls.GetKeyReferences;
import cy.agorise.graphenej.api.calls.GetLimitOrders;
import cy.agorise.graphenej.api.calls.GetObjects;
import cy.agorise.graphenej.api.calls.GetRequiredFees;
import cy.agorise.graphenej.api.calls.ListAssets;
import cy.agorise.graphenej.errors.MalformedAddressException;
import cy.agorise.graphenej.models.JsonRpcResponse;
import cy.agorise.graphenej.operations.TransferOperation;
import cy.agorise.graphenej.operations.TransferOperationBuilder;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
@ -124,6 +136,7 @@ public class PerformCallActivity extends ConnectedActivity {
setupGetRelativeAccountHistory();
break;
case RPC.CALL_GET_REQUIRED_FEES:
setupGetRequiredFees();
break;
case RPC.CALL_LOOKUP_ASSET_SYMBOLS:
setupLookupAssetSymbols();
@ -131,6 +144,9 @@ public class PerformCallActivity extends ConnectedActivity {
case RPC.CALL_LIST_ASSETS:
setupListAssets();
break;
case RPC.CALL_GET_ASSETS:
setupGetAssets();
break;
case RPC.CALL_GET_ACCOUNT_BY_NAME:
setupAccountByName();
break;
@ -150,6 +166,9 @@ public class PerformCallActivity extends ConnectedActivity {
break;
case RPC.CALL_GET_ACCOUNT_BALANCES:
setupGetAccountBalances();
break;
case RPC.CALL_BROADCAST_TRANSACTION:
setupBroadcastTransaction();
default:
Log.d(TAG,"Default called");
}
@ -209,6 +228,11 @@ public class PerformCallActivity extends ConnectedActivity {
mParam4View.setHint(resources.getString(R.string.get_relative_account_history_arg4));
}
private void setupGetRequiredFees(){
requiredInput(1);
mParam1View.setHint(getString(R.string.get_required_fees_asset));
}
private void setupLookupAssetSymbols(){
requiredInput(4);
Resources resources = getResources();
@ -220,12 +244,16 @@ public class PerformCallActivity extends ConnectedActivity {
private void setupListAssets(){
requiredInput(2);
Resources resources = getResources();
mParam1View.setHint(resources.getString(R.string.list_assets_arg1));
mParam2View.setHint(resources.getString(R.string.list_assets_arg2));
mParam1View.setHint(getString(R.string.list_assets_arg1));
mParam2View.setHint(getString(R.string.list_assets_arg2));
param2.setInputType(InputType.TYPE_CLASS_NUMBER);
}
private void setupGetAssets(){
requiredInput(1);
mParam1View.setHint(getString(R.string.get_assets_arg));
}
private void setupAccountByName(){
requiredInput(1);
Resources resources = getResources();
@ -281,6 +309,12 @@ public class PerformCallActivity extends ConnectedActivity {
param2.setHint(R.string.get_account_balances_arg2);
}
private void setupBroadcastTransaction(){
requiredInput(2);
param1.setText("1.2.116354");
param2.setText("1");
}
private void requiredInput(int inputCount){
if(inputCount == 0){
mParam1View.setVisibility(View.GONE);
@ -328,12 +362,16 @@ public class PerformCallActivity extends ConnectedActivity {
case RPC.CALL_GET_RELATIVE_ACCOUNT_HISTORY:
break;
case RPC.CALL_GET_REQUIRED_FEES:
sendGetRequiredFees();
break;
case RPC.CALL_LOOKUP_ASSET_SYMBOLS:
break;
case RPC.CALL_LIST_ASSETS:
sendListAssets();
break;
case RPC.CALL_GET_ASSETS:
sendGetAssets();
break;
case RPC.CALL_GET_ACCOUNT_BY_NAME:
getAccountByName();
break;
@ -354,6 +392,9 @@ public class PerformCallActivity extends ConnectedActivity {
break;
case RPC.CALL_GET_ACCOUNT_BALANCES:
getAccountBalances();
break;
case RPC.CALL_BROADCAST_TRANSACTION:
broadcastTransaction();
default:
Log.d(TAG,"Default called");
}
@ -383,6 +424,16 @@ public class PerformCallActivity extends ConnectedActivity {
}
}
private void sendGetRequiredFees(){
String input = param1.getText().toString();
ArrayList<BaseOperation> operations = new ArrayList<>();
AssetAmount transfer = new AssetAmount(UnsignedLong.valueOf("1000"), new Asset("1.3.0"));
AssetAmount fee = new AssetAmount(UnsignedLong.valueOf("1000"), new Asset("1.3.0"));
operations.add(new TransferOperation(new UserAccount("1.2.12300"), new UserAccount("1.2.12301"), fee, transfer));
long id = mNetworkService.sendMessage(new GetRequiredFees(operations, new Asset(input)), GetRequiredFees.REQUIRED_API);
responseMap.put(id, mRPC);
}
private void sendListAssets(){
try{
String lowerBound = param1.getText().toString();
@ -396,6 +447,16 @@ public class PerformCallActivity extends ConnectedActivity {
}
}
private void sendGetAssets(){
String assetIds = param1.getText().toString();
ArrayList<Asset> assetList = new ArrayList<>();
for(String id :assetIds.split(",")){
assetList.add(new Asset(id));
}
long id = mNetworkService.sendMessage(new GetAssets(assetList), GetAssets.REQUIRED_API);
responseMap.put(id, mRPC);
}
private void getAccountByName(){
String accountName = param1.getText().toString();
long id = mNetworkService.sendMessage(new GetAccountByName(accountName), GetAccountByName.REQUIRED_API);
@ -465,6 +526,28 @@ public class PerformCallActivity extends ConnectedActivity {
responseMap.put(id, mRPC);
}
private void broadcastTransaction(){
String destinationId = param1.getText().toString();
String amount = param2.getText().toString();
UnsignedLong transferAmount = UnsignedLong.valueOf(amount).times(UnsignedLong.valueOf(100000));
TransferOperation operation = new TransferOperationBuilder()
.setSource(new UserAccount("1.2.1029856"))
.setDestination(new UserAccount(destinationId))
.setTransferAmount(new AssetAmount( transferAmount, new Asset("1.3.0")))
.setFee(new AssetAmount(UnsignedLong.valueOf("10420"), new Asset("1.3.0")))
.build();
ArrayList<BaseOperation> ops = new ArrayList<>();
ops.add(operation);
// >> Replace with your brainkey <<
BrainKey brainKey = new BrainKey(">> Place your brainkey here <<", 0);
ECKey privKey = brainKey.getPrivateKey();
// Use valid BlockData
BlockData blockData = new BlockData(44542, 3342959171L, 1544917202L);
Transaction tx = new Transaction(privKey, blockData, ops);
long id = mNetworkService.sendMessage(new BroadcastTransaction(tx), BroadcastTransaction.REQUIRED_API);
responseMap.put(id, mRPC);
}
/**
* Internal method that will decide what to do with each JSON-RPC response
*
@ -490,6 +573,7 @@ public class PerformCallActivity extends ConnectedActivity {
case RPC.CALL_GET_REQUIRED_FEES:
case RPC.CALL_LOOKUP_ASSET_SYMBOLS:
case RPC.CALL_LIST_ASSETS:
case RPC.CALL_GET_ASSETS:
case RPC.CALL_GET_ACCOUNT_BY_NAME:
case RPC.CALL_GET_LIMIT_ORDERS:
case RPC.CALL_GET_ACCOUNT_HISTORY_BY_OPERATIONS:
@ -500,7 +584,12 @@ public class PerformCallActivity extends ConnectedActivity {
break;
default:
Log.w(TAG,"Case not handled");
if(response.result != null)
mResponseView.setText(mResponseView.getText() + response.result.toString());
else if(response.error != null)
mResponseView.setText(mResponseView.getText() + String.format("Error code: %d, Msg: %s", response.error.code, response.error.message));
else
mResponseView.setText(mResponseView.getText() + "\nnull");
}
// Remember to remove the used id entry from the map, as it would
// otherwise just increase the app's memory usage

View file

@ -28,6 +28,9 @@
<string name="get_relative_account_history_arg3">Limit</string>
<string name="get_relative_account_history_arg4">Start timestamp (Earliest)</string>
<!-- GetRequiredFees input fields -->
<string name="get_required_fees_asset">Enter asset id</string>
<!-- LookupAssetSymbols input fields -->
<string name="lookup_asset_symbols_arg1">Asset 1 id</string>
<string name="lookup_asset_symbols_arg2">Asset 2 id</string>
@ -38,6 +41,9 @@
<string name="list_assets_arg1">Lower bound of symbol names to retrieve</string>
<string name="list_assets_arg2">Maximum number of assets to fetch (must not exceed 100)</string>
<!-- Get assets input fields -->
<string name="get_assets_arg">List of asset ids separated by commas</string>
<!-- Get account by name fields -->
<string name="get_accounts_by_name_arg1">Account name</string>