Added support for the broadcast_transaction API call in the single connection mode

This commit is contained in:
Nelson R. Perez 2018-12-06 19:17:32 -05:00
parent 9ceb3f048c
commit 0cba6a9f8d
4 changed files with 77 additions and 2 deletions

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

@ -29,7 +29,8 @@ android {
dependencies { dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar']) 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:appcompat-v7:27.1.1'
implementation 'com.android.support:recyclerview-v7:27.1.1' implementation 'com.android.support:recyclerview-v7:27.1.1'
implementation 'com.android.support:design:27.1.1' implementation 'com.android.support:design:27.1.1'

View file

@ -78,6 +78,7 @@ public class CallsActivity extends AppCompatActivity {
RPC.CALL_GET_DYNAMIC_GLOBAL_PROPERTIES, RPC.CALL_GET_DYNAMIC_GLOBAL_PROPERTIES,
RPC.CALL_GET_KEY_REFERENCES, RPC.CALL_GET_KEY_REFERENCES,
RPC.CALL_GET_ACCOUNT_BALANCES, RPC.CALL_GET_ACCOUNT_BALANCES,
RPC.CALL_BROADCAST_TRANSACTION,
REMOVE_CURRENT_NODE REMOVE_CURRENT_NODE
}; };

View file

@ -17,6 +17,8 @@ import com.google.common.primitives.UnsignedLong;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import org.bitcoinj.core.ECKey;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
@ -28,13 +30,17 @@ import butterknife.OnClick;
import cy.agorise.graphenej.Asset; import cy.agorise.graphenej.Asset;
import cy.agorise.graphenej.AssetAmount; import cy.agorise.graphenej.AssetAmount;
import cy.agorise.graphenej.BaseOperation; import cy.agorise.graphenej.BaseOperation;
import cy.agorise.graphenej.BlockData;
import cy.agorise.graphenej.BrainKey;
import cy.agorise.graphenej.Memo; import cy.agorise.graphenej.Memo;
import cy.agorise.graphenej.OperationType; import cy.agorise.graphenej.OperationType;
import cy.agorise.graphenej.RPC; import cy.agorise.graphenej.RPC;
import cy.agorise.graphenej.Transaction;
import cy.agorise.graphenej.UserAccount; import cy.agorise.graphenej.UserAccount;
import cy.agorise.graphenej.api.ConnectionStatusUpdate; import cy.agorise.graphenej.api.ConnectionStatusUpdate;
import cy.agorise.graphenej.api.android.DeserializationMap; import cy.agorise.graphenej.api.android.DeserializationMap;
import cy.agorise.graphenej.api.android.RxBus; 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.GetAccountBalances;
import cy.agorise.graphenej.api.calls.GetAccountByName; import cy.agorise.graphenej.api.calls.GetAccountByName;
import cy.agorise.graphenej.api.calls.GetAccountHistoryByOperations; import cy.agorise.graphenej.api.calls.GetAccountHistoryByOperations;
@ -50,6 +56,7 @@ import cy.agorise.graphenej.api.calls.ListAssets;
import cy.agorise.graphenej.errors.MalformedAddressException; import cy.agorise.graphenej.errors.MalformedAddressException;
import cy.agorise.graphenej.models.JsonRpcResponse; import cy.agorise.graphenej.models.JsonRpcResponse;
import cy.agorise.graphenej.operations.TransferOperation; import cy.agorise.graphenej.operations.TransferOperation;
import cy.agorise.graphenej.operations.TransferOperationBuilder;
import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable; import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer; import io.reactivex.functions.Consumer;
@ -155,6 +162,9 @@ public class PerformCallActivity extends ConnectedActivity {
break; break;
case RPC.CALL_GET_ACCOUNT_BALANCES: case RPC.CALL_GET_ACCOUNT_BALANCES:
setupGetAccountBalances(); setupGetAccountBalances();
break;
case RPC.CALL_BROADCAST_TRANSACTION:
setupBroadcastTransaction();
default: default:
Log.d(TAG,"Default called"); Log.d(TAG,"Default called");
} }
@ -291,6 +301,12 @@ public class PerformCallActivity extends ConnectedActivity {
param2.setHint(R.string.get_account_balances_arg2); 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){ private void requiredInput(int inputCount){
if(inputCount == 0){ if(inputCount == 0){
mParam1View.setVisibility(View.GONE); mParam1View.setVisibility(View.GONE);
@ -365,6 +381,9 @@ public class PerformCallActivity extends ConnectedActivity {
break; break;
case RPC.CALL_GET_ACCOUNT_BALANCES: case RPC.CALL_GET_ACCOUNT_BALANCES:
getAccountBalances(); getAccountBalances();
break;
case RPC.CALL_BROADCAST_TRANSACTION:
broadcastTransaction();
default: default:
Log.d(TAG,"Default called"); Log.d(TAG,"Default called");
} }
@ -486,6 +505,28 @@ public class PerformCallActivity extends ConnectedActivity {
responseMap.put(id, mRPC); 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 * Internal method that will decide what to do with each JSON-RPC response
* *
@ -521,7 +562,12 @@ public class PerformCallActivity extends ConnectedActivity {
break; break;
default: default:
Log.w(TAG,"Case not handled"); 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 // Remember to remove the used id entry from the map, as it would
// otherwise just increase the app's memory usage // otherwise just increase the app's memory usage