From beba8a0b4037cd3f34716948179f433200e788d4 Mon Sep 17 00:00:00 2001 From: "Nelson R. Perez" Date: Thu, 5 Apr 2018 11:17:29 -0500 Subject: [PATCH] Introduced the GetRequiredFees API call --- .../graphenej/api/calls/GetRequiredFees.java | 41 +++++++++ .../labs/sample/SecondActivity.java | 91 +++++++++++++++++++ .../src/main/res/layout/activity_second.xml | 45 ++++++++- 3 files changed, 176 insertions(+), 1 deletion(-) create mode 100644 graphenej/src/main/java/cy/agorise/graphenej/api/calls/GetRequiredFees.java diff --git a/graphenej/src/main/java/cy/agorise/graphenej/api/calls/GetRequiredFees.java b/graphenej/src/main/java/cy/agorise/graphenej/api/calls/GetRequiredFees.java new file mode 100644 index 0000000..d9fd785 --- /dev/null +++ b/graphenej/src/main/java/cy/agorise/graphenej/api/calls/GetRequiredFees.java @@ -0,0 +1,41 @@ +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.BaseOperation; +import cy.agorise.graphenej.BlockData; +import cy.agorise.graphenej.RPC; +import cy.agorise.graphenej.Transaction; +import cy.agorise.graphenej.models.ApiCall; + +/** + * Wrapper around the "get_required_fees" API call + */ + +public class GetRequiredFees implements ApiCallable { + + private Transaction mTransaction; + private Asset mFeeAsset; + + public GetRequiredFees(Transaction transaction, Asset feeAsset){ + this.mTransaction = transaction; + this.mFeeAsset = feeAsset; + } + + public GetRequiredFees(List operations, Asset feeAsset){ + this.mTransaction = new Transaction(new BlockData(0, 0, 0), operations); + this.mFeeAsset = feeAsset; + } + + @Override + public ApiCall toApiCall(int apiId, long sequenceId) { + // Building a new API call to request fees information + ArrayList accountParams = new ArrayList<>(); + accountParams.add((Serializable) mTransaction.getOperations()); + accountParams.add(this.mFeeAsset.getObjectId()); + return new ApiCall(apiId, RPC.CALL_GET_REQUIRED_FEES, accountParams, RPC.VERSION, sequenceId); + } +} diff --git a/sample/src/main/java/com/luminiasoft/labs/sample/SecondActivity.java b/sample/src/main/java/com/luminiasoft/labs/sample/SecondActivity.java index 56c529c..6da0e2f 100644 --- a/sample/src/main/java/com/luminiasoft/labs/sample/SecondActivity.java +++ b/sample/src/main/java/com/luminiasoft/labs/sample/SecondActivity.java @@ -8,21 +8,66 @@ import android.os.Bundle; import android.os.IBinder; import android.support.v7.app.AppCompatActivity; import android.util.Log; +import android.view.View; +import android.widget.TextView; +import com.google.common.primitives.UnsignedLong; +import com.google.gson.Gson; + +import java.util.ArrayList; +import java.util.List; + +import butterknife.BindView; +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.UserAccount; import cy.agorise.graphenej.api.ApiAccess; import cy.agorise.graphenej.api.android.NetworkService; +import cy.agorise.graphenej.api.android.RxBus; +import cy.agorise.graphenej.api.calls.GetRequiredFees; +import cy.agorise.graphenej.models.JsonRpcResponse; +import cy.agorise.graphenej.operations.LimitOrderCreateOperation; +import cy.agorise.graphenej.operations.TransferOperation; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.functions.Consumer; public class SecondActivity extends AppCompatActivity { private final String TAG = this.getClass().getName(); + @BindView(R.id.text_field) + TextView mTextField; + // In case we want to interact directly with the service private NetworkService mService; + private Gson gson = new Gson(); + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); + + ButterKnife.bind(this); + + RxBus.getBusInstance() + .asFlowable() + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(new Consumer() { + + @Override + public void accept(Object message) throws Exception { + if(message instanceof String){ + Log.d(TAG,"Got text message: "+(message)); + mTextField.setText(mTextField.getText() + ((String) message) + "\n"); + }else if(message instanceof JsonRpcResponse){ + mTextField.setText(mTextField.getText() + gson.toJson(message, JsonRpcResponse.class) + "\n"); + } + } + }); } @Override @@ -58,4 +103,50 @@ public class SecondActivity extends AppCompatActivity { Log.d(TAG,"onServiceDisconnected"); } }; + + @OnClick(R.id.transfer_fee_usd) + public void onTransferFeeUsdClicked(View v){ + List operations = getTransferOperation(); + mService.sendMessage(new GetRequiredFees(operations, new Asset("1.3.121"))); + } + + @OnClick(R.id.transfer_fee_bts) + public void onTransferFeeBtsClicked(View v){ + List operations = getTransferOperation(); + mService.sendMessage(new GetRequiredFees(operations, new Asset("1.3.0"))); + } + + @OnClick(R.id.exchange_fee_usd) + public void onExchangeFeeUsdClicked(View v){ + List operations = getExchangeOperation(); + mService.sendMessage(new GetRequiredFees(operations, new Asset("1.3.121"))); + } + + @OnClick(R.id.exchange_fee_bts) + public void onExchangeFeeBtsClicked(View v){ + List operations = getExchangeOperation(); + mService.sendMessage(new GetRequiredFees(operations, new Asset("1.3.0"))); + } + + private List getTransferOperation(){ + TransferOperation transferOperation = new TransferOperation( + new UserAccount("1.2.138632"), + new UserAccount("1.2.129848"), + new AssetAmount(UnsignedLong.ONE, new Asset("1.3.0"))); + ArrayList operations = new ArrayList(); + operations.add(transferOperation); + return operations; + } + + public List getExchangeOperation() { + LimitOrderCreateOperation operation = new LimitOrderCreateOperation( + new UserAccount("1.2.138632"), + new AssetAmount(UnsignedLong.valueOf(10000), new Asset("1.3.0")), + new AssetAmount(UnsignedLong.valueOf(10), new Asset("1.3.121")), + 1000000, + true); + ArrayList operations = new ArrayList(); + operations.add(operation); + return operations; + } } diff --git a/sample/src/main/res/layout/activity_second.xml b/sample/src/main/res/layout/activity_second.xml index b05ee9a..51793b0 100644 --- a/sample/src/main/res/layout/activity_second.xml +++ b/sample/src/main/res/layout/activity_second.xml @@ -5,5 +5,48 @@ android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.luminiasoft.labs.sample.SecondActivity"> - + + +