diff --git a/graphenej/src/main/java/cy/agorise/graphenej/RPC.java b/graphenej/src/main/java/cy/agorise/graphenej/RPC.java index c5c68d4..7e2e306 100644 --- a/graphenej/src/main/java/cy/agorise/graphenej/RPC.java +++ b/graphenej/src/main/java/cy/agorise/graphenej/RPC.java @@ -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"; diff --git a/graphenej/src/main/java/cy/agorise/graphenej/api/android/DeserializationMap.java b/graphenej/src/main/java/cy/agorise/graphenej/api/android/DeserializationMap.java index 8d704ee..a7a6b21 100644 --- a/graphenej/src/main/java/cy/agorise/graphenej/api/android/DeserializationMap.java +++ b/graphenej/src/main/java/cy/agorise/graphenej/api/android/DeserializationMap.java @@ -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){ diff --git a/graphenej/src/main/java/cy/agorise/graphenej/api/android/NetworkService.java b/graphenej/src/main/java/cy/agorise/graphenej/api/android/NetworkService.java index 3eaf970..89cc4eb 100644 --- a/graphenej/src/main/java/cy/agorise/graphenej/api/android/NetworkService.java +++ b/graphenej/src/main/java/cy/agorise/graphenej/api/android/NetworkService.java @@ -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>>(){}.getType(); parsedResponse = gson.fromJson(text, GetAccountBalancesResponse); + } else if(requestClass == GetAssets.class){ + Type GetAssetsResponse = new TypeToken>>(){}.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); diff --git a/graphenej/src/main/java/cy/agorise/graphenej/api/calls/BroadcastTransaction.java b/graphenej/src/main/java/cy/agorise/graphenej/api/calls/BroadcastTransaction.java new file mode 100644 index 0000000..8dc1ba5 --- /dev/null +++ b/graphenej/src/main/java/cy/agorise/graphenej/api/calls/BroadcastTransaction.java @@ -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 transactions = new ArrayList<>(); + transactions.add(mTransaction); + return new ApiCall(apiId, RPC.CALL_BROADCAST_TRANSACTION, transactions, RPC.VERSION, sequenceId); + } +} diff --git a/graphenej/src/main/java/cy/agorise/graphenej/api/calls/GetAssets.java b/graphenej/src/main/java/cy/agorise/graphenej/api/calls/GetAssets.java new file mode 100644 index 0000000..91ca709 --- /dev/null +++ b/graphenej/src/main/java/cy/agorise/graphenej/api/calls/GetAssets.java @@ -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 assetList = new ArrayList<>(); + + /** + * Constructor that will receive a List of Asset instances. + * + * @param assets List of Asset instances. + */ + public GetAssets(List 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 params = new ArrayList<>(); + ArrayList 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); + } +} diff --git a/sample/build.gradle b/sample/build.gradle index de76e02..1ca3102 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -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' diff --git a/sample/src/main/java/cy/agorise/labs/sample/CallsActivity.java b/sample/src/main/java/cy/agorise/labs/sample/CallsActivity.java index 28f2e0d..c9899c8 100644 --- a/sample/src/main/java/cy/agorise/labs/sample/CallsActivity.java +++ b/sample/src/main/java/cy/agorise/labs/sample/CallsActivity.java @@ -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 }; diff --git a/sample/src/main/java/cy/agorise/labs/sample/PerformCallActivity.java b/sample/src/main/java/cy/agorise/labs/sample/PerformCallActivity.java index 8a47906..650d761 100644 --- a/sample/src/main/java/cy/agorise/labs/sample/PerformCallActivity.java +++ b/sample/src/main/java/cy/agorise/labs/sample/PerformCallActivity.java @@ -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 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 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 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"); - mResponseView.setText(mResponseView.getText() + response.result.toString()); + 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 diff --git a/sample/src/main/res/values/strings.xml b/sample/src/main/res/values/strings.xml index d660aaa..63865d5 100644 --- a/sample/src/main/res/values/strings.xml +++ b/sample/src/main/res/values/strings.xml @@ -28,6 +28,9 @@ Limit Start timestamp (Earliest) + + Enter asset id + Asset 1 id Asset 2 id @@ -38,6 +41,9 @@ Lower bound of symbol names to retrieve Maximum number of assets to fetch (must not exceed 100) + + List of asset ids separated by commas + Account name