Skip to content

Commit

Permalink
Expert mode for transaction sending (add data, own gas limit)
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelsc committed Jun 17, 2017
1 parent 9f88104 commit 67a1ca9
Show file tree
Hide file tree
Showing 15 changed files with 433 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
applicationId "com.rehanced.lunary"
minSdkVersion 16
targetSdkVersion 25
versionCode 15
versionName "1.25"
versionCode 16
versionName "1.26"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
multiDexEnabled true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TextView;
Expand All @@ -43,6 +44,7 @@
import rehanced.com.simpleetherwallet.services.TransactionService;
import rehanced.com.simpleetherwallet.utils.AddressNameConverter;
import rehanced.com.simpleetherwallet.utils.Blockies;
import rehanced.com.simpleetherwallet.utils.CollapseAnimator;
import rehanced.com.simpleetherwallet.utils.ExchangeCalculator;
import rehanced.com.simpleetherwallet.utils.ResponseParser;
import rehanced.com.simpleetherwallet.utils.WalletStorage;
Expand All @@ -69,6 +71,8 @@ public class FragmentSend extends Fragment {
private BigDecimal curTxCost = new BigDecimal("0.000252");
private BigDecimal curAmount = BigDecimal.ZERO;
private ExchangeCalculator exchange = ExchangeCalculator.getInstance();
private LinearLayout expertMode;
private EditText data, userGasLimit;

public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
Expand Down Expand Up @@ -99,6 +103,20 @@ public View onCreateView(LayoutInflater inflater, final ViewGroup container,
gasText = (TextView) rootView.findViewById(R.id.gasText);
toicon = (ImageView) rootView.findViewById(R.id.toicon);
fromicon = (ImageView) rootView.findViewById(R.id.fromicon);
expertMode = (LinearLayout) rootView.findViewById(R.id.expertmode);
data = (EditText) rootView.findViewById(R.id.data);
userGasLimit = (EditText) rootView.findViewById(R.id.gaslimit);

((LinearLayout) rootView.findViewById(R.id.expertmodetrigger)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(expertMode.getVisibility()==View.GONE) {
CollapseAnimator.expand(expertMode);
} else {
CollapseAnimator.collapse(expertMode);
}
}
});

if (getArguments().containsKey("TO_ADDRESS")){
setToAddress(getArguments().getString("TO_ADDRESS"), ac);
Expand All @@ -125,7 +143,7 @@ public void onStopTrackingTouch(SeekBar seekBar) {}
});

spinner = (Spinner) rootView.findViewById(R.id.spinner);
final ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(ac, android.R.layout.simple_spinner_item, WalletStorage.getInstance(ac).getFullOnly()){
final ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(ac, R.layout.address_spinner, WalletStorage.getInstance(ac).getFullOnly()){
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
Expand Down Expand Up @@ -216,7 +234,7 @@ public void onNothingSelected(AdapterView<?> adapterView) {}
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(amount.getText().length() <= 0 || new BigDecimal(amount.getText().toString()).compareTo(new BigDecimal("0")) <= 0) {
if((amount.getText().length() <= 0 || new BigDecimal(amount.getText().toString()).compareTo(new BigDecimal("0")) <= 0) && data.getText().length() <= 0) {
ac.snackError(getString(R.string.err_send_noamount));
return;
}
Expand All @@ -227,8 +245,8 @@ public void onClick(View view) {
if(spinner == null || spinner.getSelectedItem() == null) return;
try {
if(BuildConfig.DEBUG)
Log.d("etherbalance", (getCurTotalCost().compareTo(curAvailable) < 0)+" | "+getCurTotalCost()+" | "+curAvailable);
if (getCurTotalCost().compareTo(curAvailable) < 0 || BuildConfig.DEBUG){
Log.d("etherbalance", (getCurTotalCost().compareTo(curAvailable) < 0)+" | "+getCurTotalCost()+" | "+curAvailable+ " | "+data.getText()+" | "+curAmount);
if (getCurTotalCost().compareTo(curAvailable) < 0 || BuildConfig.DEBUG || data.getText().length() > 0){
askForPasswordAndDecode(spinner.getSelectedItem().toString());
} else {
ac.snackError(getString(R.string.err_send_not_enough_ether));
Expand Down Expand Up @@ -338,6 +356,12 @@ public void onFailure(Call call, IOException e) {}
public void onResponse(Call call, Response response) throws IOException {
try {
gaslimit = ResponseParser.parseGasPrice(response.body().string());
ac.runOnUiThread(new Runnable() {
@Override
public void run() {
userGasLimit.setText(gaslimit+"");
}
});
} catch (Exception e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -403,8 +427,9 @@ private void sendEther(String password, String fromAddress) {
txService.putExtra("TO_ADDRESS", toAddress.getText().toString());
txService.putExtra("AMOUNT", curAmount.toPlainString()); // In ether, gets converted by the service itself
txService.putExtra("GAS_PRICE", new BigDecimal((gas.getProgress()+1)+"").multiply(new BigDecimal("1000000000")).toPlainString());// "21000000000");
txService.putExtra("GAS_LIMIT", gaslimit.toString());
txService.putExtra("GAS_LIMIT", userGasLimit.getText().length() <= 0 ? gaslimit.toString() : userGasLimit.getText().toString());
txService.putExtra("PASSWORD", password);
txService.putExtra("DATA", data.getText().toString());
ac.startService(txService);

// For statistics
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ protected void onHandleIntent(Intent intent) {
final String amount = intent.getStringExtra("AMOUNT");
final String gas_price = intent.getStringExtra("GAS_PRICE");
final String gas_limit = intent.getStringExtra("GAS_LIMIT");
final String data = intent.getStringExtra("DATA");
String password = intent.getStringExtra("PASSWORD");

final Credentials keys = WalletStorage.getInstance(getApplicationContext()).getFullWallet(getApplicationContext(), password, fromAddress);
Expand All @@ -61,20 +62,22 @@ public void onResponse(Call call, final Response response) throws IOException {
JSONObject o = new JSONObject(response.body().string());
BigInteger nonce = new BigInteger(o.getString("result").substring(2), 16);

RawTransaction tx = RawTransaction.createEtherTransaction(
RawTransaction tx = RawTransaction.createTransaction(
nonce,
new BigInteger(gas_price),
new BigInteger(gas_limit),
toAddress,
new BigDecimal(amount).multiply(ExchangeCalculator.ONE_ETHER).toBigInteger()
new BigDecimal(amount).multiply(ExchangeCalculator.ONE_ETHER).toBigInteger(),
data
);

Log.d("txx",
"Nonce: "+tx.getNonce()+"\n"+
"gasPrice: "+tx.getGasPrice()+"\n"+
"gasLimit: "+tx.getGasLimit()+"\n"+
"To: "+tx.getTo()+"\n"+
"Amount: "+tx.getValue()
"Amount: "+tx.getValue()+"\n"+
"Data: "+tx.getData()
);

byte [] signed = TransactionEncoder.signMessage(tx, (byte) 1, keys);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package rehanced.com.simpleetherwallet.utils;

import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Transformation;

/**
* By Tom Esterez
* https://stackoverflow.com/a/13381228/2463984
*/

public class CollapseAnimator {

public static void expand(final View v) {
v.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
final int targetHeight = v.getMeasuredHeight();

// Older versions of android (pre API 21) cancel animations for views with a height of 0.
v.getLayoutParams().height = 1;
v.setVisibility(View.VISIBLE);
Animation a = new Animation()
{
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
v.getLayoutParams().height = interpolatedTime == 1
? ViewGroup.LayoutParams.WRAP_CONTENT
: (int)(targetHeight * interpolatedTime);
v.requestLayout();
}

@Override
public boolean willChangeBounds() {
return true;
}
};

// 1dp/ms
a.setDuration((int)(targetHeight / v.getContext().getResources().getDisplayMetrics().density));
v.startAnimation(a);
}

public static void collapse(final View v) {
final int initialHeight = v.getMeasuredHeight();

Animation a = new Animation()
{
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
if(interpolatedTime == 1){
v.setVisibility(View.GONE);
}else{
v.getLayoutParams().height = initialHeight - (int)(initialHeight * interpolatedTime);
v.requestLayout();
}
}

@Override
public boolean willChangeBounds() {
return true;
}
};

// 1dp/ms
a.setDuration((int)(initialHeight / v.getContext().getResources().getDisplayMetrics().density));
v.startAnimation(a);
}
}
Binary file added app/src/main/res/drawable-hdpi/ic_action_expand.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-mdpi/ic_action_expand.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-xhdpi/ic_action_expand.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 67a1ca9

Please sign in to comment.