mirror of
https://codeberg.org/anoncontributorxmr/mysu.git
synced 2024-11-22 07:22:26 +00:00
Cleanup code, fix bugs, etc
This commit is contained in:
parent
2ebc828d3e
commit
998836ebd1
8 changed files with 62 additions and 34 deletions
|
@ -1083,14 +1083,17 @@ Java_net_mynero_wallet_model_Wallet_getCoinsJ(JNIEnv *env, jobject instance) {
|
|||
|
||||
jobject newCoinsInfo(JNIEnv *env, Monero::CoinsInfo *info) {
|
||||
jmethodID c = env->GetMethodID(class_CoinsInfo, "<init>",
|
||||
"(JZLjava/lang/String;J)V");
|
||||
"(JZLjava/lang/String;JLjava/lang/String;)V");
|
||||
jstring _key_image = env->NewStringUTF(info->keyImage().c_str());
|
||||
jstring _hash = env->NewStringUTF(info->hash().c_str());
|
||||
jobject result = env->NewObject(class_CoinsInfo, c,
|
||||
static_cast<jlong> (info->globalOutputIndex()),
|
||||
info->spent(),
|
||||
_key_image,
|
||||
static_cast<jlong> (info->amount()));
|
||||
static_cast<jlong> (info->amount()),
|
||||
_hash);
|
||||
env->DeleteLocalRef(_key_image);
|
||||
env->DeleteLocalRef(_hash);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
|
@ -52,14 +52,6 @@ public class ReceiveBottomSheetDialog extends BottomSheetDialogFragment {
|
|||
addressTextView.setText(addr.getAddress());
|
||||
addressImageView.setImageBitmap(generate(addr.getAddress(), 256, 256));
|
||||
copyAddressImageButton.setOnClickListener(view1 -> Helper.clipBoardCopy(getContext(), "address", addr.getAddress()));
|
||||
|
||||
List<CoinsInfo> coins = WalletManager.getInstance().getWallet().getCoins().getAll();
|
||||
System.out.println("COINS::");
|
||||
for(CoinsInfo coinsInfo : coins) {
|
||||
if(!coinsInfo.isSpent()) {
|
||||
System.out.println(coinsInfo.getGlobalOutputIndex()+": "+coinsInfo.getKeyImage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Bitmap generate(String text, int width, int height) {
|
||||
|
|
|
@ -254,15 +254,25 @@ public class SendBottomSheetDialog extends BottomSheetDialogFragment {
|
|||
|
||||
private void createTx(String address, String amount, boolean sendAll, PendingTransaction.Priority feePriority) {
|
||||
AsyncTask.execute(() -> {
|
||||
PendingTransaction pendingTx = TxService.getInstance().createTx(address, amount, sendAll, feePriority, selectedUtxos);
|
||||
if (pendingTx != null && pendingTx.getStatus() == PendingTransaction.Status.Status_Ok) {
|
||||
_pendingTransaction.postValue(pendingTx);
|
||||
} else {
|
||||
try {
|
||||
PendingTransaction pendingTx = TxService.getInstance().createTx(address, amount, sendAll, feePriority, selectedUtxos);
|
||||
if (pendingTx != null && pendingTx.getStatus() == PendingTransaction.Status.Status_Ok) {
|
||||
_pendingTransaction.postValue(pendingTx);
|
||||
} else {
|
||||
Activity activity = getActivity();
|
||||
if (activity != null) {
|
||||
activity.runOnUiThread(() -> {
|
||||
createButton.setEnabled(true);
|
||||
Toast.makeText(getActivity(), getString(R.string.error_creating_tx), Toast.LENGTH_SHORT).show();
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Activity activity = getActivity();
|
||||
if (activity != null) {
|
||||
activity.runOnUiThread(() -> {
|
||||
createButton.setEnabled(true);
|
||||
Toast.makeText(getActivity(), getString(R.string.error_creating_tx), Toast.LENGTH_SHORT).show();
|
||||
Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_SHORT).show();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -78,10 +78,8 @@ public class UtxosFragment extends Fragment implements CoinsInfoAdapter.CoinsInf
|
|||
public void onUtxoSelected(CoinsInfo coinsInfo) {
|
||||
boolean selected = selectedUtxos.contains(coinsInfo.getKeyImage());
|
||||
if(selected) {
|
||||
System.out.println("Deselecting: " + coinsInfo.getKeyImage());
|
||||
selectedUtxos.remove(coinsInfo.getKeyImage());
|
||||
} else {
|
||||
System.out.println("Selecting: " + coinsInfo.getKeyImage());
|
||||
selectedUtxos.add(coinsInfo.getKeyImage());
|
||||
}
|
||||
|
||||
|
|
|
@ -33,12 +33,14 @@ public class CoinsInfo implements Parcelable {
|
|||
boolean spent;
|
||||
String keyImage;
|
||||
long amount;
|
||||
String hash;
|
||||
|
||||
public CoinsInfo(long globalOutputIndex, boolean spent, String keyImage, long amount) {
|
||||
public CoinsInfo(long globalOutputIndex, boolean spent, String keyImage, long amount, String hash) {
|
||||
this.globalOutputIndex = globalOutputIndex;
|
||||
this.spent = spent;
|
||||
this.keyImage = keyImage;
|
||||
this.amount = amount;
|
||||
this.hash = hash;
|
||||
}
|
||||
|
||||
protected CoinsInfo(Parcel in) {
|
||||
|
@ -69,6 +71,10 @@ public class CoinsInfo implements Parcelable {
|
|||
return keyImage;
|
||||
}
|
||||
|
||||
public String getHash() {
|
||||
return hash;
|
||||
}
|
||||
|
||||
public long getAmount() {
|
||||
return amount;
|
||||
}
|
||||
|
|
|
@ -17,11 +17,10 @@
|
|||
|
||||
package net.mynero.wallet.service;
|
||||
|
||||
import static net.mynero.wallet.model.Wallet.SWEEP_ALL;
|
||||
|
||||
import net.mynero.wallet.data.DefaultNodes;
|
||||
import net.mynero.wallet.data.Node;
|
||||
import net.mynero.wallet.data.TxData;
|
||||
import net.mynero.wallet.model.CoinsInfo;
|
||||
import net.mynero.wallet.model.PendingTransaction;
|
||||
import net.mynero.wallet.model.Wallet;
|
||||
import net.mynero.wallet.model.WalletListener;
|
||||
|
@ -122,17 +121,33 @@ public class MoneroHandlerThread extends Thread implements WalletListener {
|
|||
listener.onRefresh();
|
||||
}
|
||||
|
||||
public PendingTransaction createTx(String address, String amountStr, boolean sendAll, PendingTransaction.Priority feePriority, ArrayList<String> selectedUtxos) {
|
||||
long amount = sendAll ? SWEEP_ALL : Wallet.getAmountFromString(amountStr);
|
||||
public PendingTransaction createTx(String address, String amountStr, boolean sendAll, PendingTransaction.Priority feePriority, ArrayList<String> selectedUtxos) throws Exception {
|
||||
long amount = sendAll ? Wallet.SWEEP_ALL : Wallet.getAmountFromString(amountStr);
|
||||
ArrayList<String> preferredInputs;
|
||||
if(selectedUtxos.isEmpty()) {
|
||||
preferredInputs = UTXOService.getInstance().selectUtxos(amount, sendAll);
|
||||
} else {
|
||||
preferredInputs = selectedUtxos;
|
||||
checkSelectedAmounts(selectedUtxos, amount, sendAll);
|
||||
}
|
||||
return wallet.createTransaction(new TxData(address, amount, 0, feePriority, preferredInputs));
|
||||
}
|
||||
|
||||
private void checkSelectedAmounts(ArrayList<String> selectedUtxos, long amount, boolean sendAll) throws Exception {
|
||||
if(!sendAll) {
|
||||
long amountSelected = 0;
|
||||
for(CoinsInfo coinsInfo : UTXOService.getInstance().getUtxos()) {
|
||||
if(selectedUtxos.contains(coinsInfo.getKeyImage())) {
|
||||
amountSelected += coinsInfo.getAmount();
|
||||
}
|
||||
}
|
||||
|
||||
if(amountSelected <= amount) {
|
||||
throw new Exception("insufficient wallet balance");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean sendTx(PendingTransaction pendingTx) {
|
||||
return pendingTx.commit("", true);
|
||||
}
|
||||
|
|
|
@ -16,7 +16,7 @@ public class TxService extends ServiceBase {
|
|||
return instance;
|
||||
}
|
||||
|
||||
public PendingTransaction createTx(String address, String amount, boolean sendAll, PendingTransaction.Priority feePriority, ArrayList<String> selectedUtxos) {
|
||||
public PendingTransaction createTx(String address, String amount, boolean sendAll, PendingTransaction.Priority feePriority, ArrayList<String> selectedUtxos) throws Exception {
|
||||
return this.getThread().createTx(address, amount, sendAll, feePriority, selectedUtxos);
|
||||
}
|
||||
|
||||
|
|
|
@ -33,28 +33,32 @@ public class UTXOService extends ServiceBase {
|
|||
return WalletManager.getInstance().getWallet().getCoins().getAll();
|
||||
}
|
||||
|
||||
public ArrayList<String> selectUtxos(long amount, boolean sendAll) {
|
||||
public ArrayList<String> selectUtxos(long amount, boolean sendAll) throws Exception {
|
||||
ArrayList<String> selectedUtxos = new ArrayList<>();
|
||||
ArrayList<String> seenTxs = new ArrayList<>();
|
||||
List<CoinsInfo> utxos = getUtxos();
|
||||
if(sendAll) {
|
||||
for(CoinsInfo coinsInfo : utxos) {
|
||||
selectedUtxos.add(coinsInfo.getKeyImage());
|
||||
}
|
||||
} else {
|
||||
long amountSelected = 0;
|
||||
Collections.shuffle(utxos);
|
||||
for (CoinsInfo coinsInfo : utxos) {
|
||||
if (amount == Wallet.SWEEP_ALL) {
|
||||
long amountSelected = 0;
|
||||
Collections.shuffle(utxos);
|
||||
for (CoinsInfo coinsInfo : utxos) {
|
||||
if(!coinsInfo.isSpent()) {
|
||||
if (sendAll) {
|
||||
selectedUtxos.add(coinsInfo.getKeyImage());
|
||||
amountSelected = Wallet.SWEEP_ALL;
|
||||
} else {
|
||||
if (amountSelected <= amount) {
|
||||
if (amountSelected <= amount && !seenTxs.contains(coinsInfo.getHash())) {
|
||||
selectedUtxos.add(coinsInfo.getKeyImage());
|
||||
// we don't want to spend multiple utxos from the same transaction, so we prevent that from happening here.
|
||||
seenTxs.add(coinsInfo.getHash());
|
||||
amountSelected += coinsInfo.getAmount();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (amountSelected <= amount && !sendAll) {
|
||||
throw new Exception("insufficient wallet balance");
|
||||
}
|
||||
|
||||
return selectedUtxos;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue