From f3d2ba2591cec5b395e6987009c371d15ac96018 Mon Sep 17 00:00:00 2001 From: pokkst Date: Sat, 24 Sep 2022 02:57:46 -0500 Subject: [PATCH] Add estimate fee method, but based on Monero's code it doesn't seem to take priority into account --- app/src/main/cpp/monerujo.cpp | 41 +++++++++++++++++++ .../dialog/SendBottomSheetDialog.java | 2 + .../java/net/mynero/wallet/model/Wallet.java | 9 ++++ 3 files changed, 52 insertions(+) diff --git a/app/src/main/cpp/monerujo.cpp b/app/src/main/cpp/monerujo.cpp index 21489de..1fafbd9 100644 --- a/app/src/main/cpp/monerujo.cpp +++ b/app/src/main/cpp/monerujo.cpp @@ -41,6 +41,7 @@ static jclass class_Transfer; static jclass class_Ledger; static jclass class_WalletStatus; static jclass class_CoinsInfo; +static jclass class_Pair; std::mutex _listenerMutex; @@ -55,6 +56,8 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved) { class_ArrayList = static_cast(jenv->NewGlobalRef( jenv->FindClass("java/util/ArrayList"))); + class_Pair = static_cast(jenv->NewGlobalRef( + jenv->FindClass("android/util/Pair"))); class_TransactionInfo = static_cast(jenv->NewGlobalRef( jenv->FindClass("net/mynero/wallet/model/TransactionInfo"))); class_Transfer = static_cast(jenv->NewGlobalRef( @@ -249,6 +252,34 @@ std::set java2cpp_set(JNIEnv *env, jobject arrayList) { return result; } +std::pair extract_pair(JNIEnv *env, jobject p) { + jfieldID first = env->GetFieldID(class_Pair, "first", "Ljava/lang/Object;"); + jfieldID second = env->GetFieldID(class_Pair, "second", "Ljava/lang/Object;"); + jstring string = static_cast(env->GetObjectField(p, first)); + std::string converted_string = env->GetStringUTFChars(string, nullptr); + uint64_t value = reinterpret_cast(env->GetObjectField(p, second)); + auto pair = std::make_pair(converted_string, value); + return pair; +} + +std::vector> java2cpp_pairvector(JNIEnv *env, jobject arrayList) { + + jmethodID java_util_ArrayList_size = env->GetMethodID(class_ArrayList, "size", "()I"); + jmethodID java_util_ArrayList_get = env->GetMethodID(class_ArrayList, "get", + "(I)Ljava/lang/Object;"); + + jint len = env->CallIntMethod(arrayList, java_util_ArrayList_size); + std::vector> result; + + for (jint i = 0; i < len; i++) { + jobject element = static_cast(env->CallObjectMethod(arrayList, + java_util_ArrayList_get, i)); + auto pair = extract_pair(env, element); + result.emplace_back(pair); + } + return result; +} + jobject cpp2java(JNIEnv *env, const std::vector &vector) { jmethodID java_util_ArrayList_ = env->GetMethodID(class_ArrayList, "", "(I)V"); @@ -998,6 +1029,16 @@ Java_net_mynero_wallet_model_Wallet_createTransactionJ(JNIEnv *env, jobject inst return reinterpret_cast(tx); } +JNIEXPORT jlong JNICALL +Java_net_mynero_wallet_model_Wallet_estimateTransactionFee(JNIEnv *env, jobject instance, + jobject destinations, jint priority) { + std::vector> dest_vector = java2cpp_pairvector(env, destinations); + Monero::PendingTransaction::Priority _priority = static_cast(priority); + Monero::Wallet *wallet = getHandle(env, instance); + uint64_t fee = wallet->estimateTransactionFee(dest_vector, _priority); + return fee; +} + JNIEXPORT jlong JNICALL Java_net_mynero_wallet_model_Wallet_createTransactionSingleJ(JNIEnv *env, jobject instance, jstring key_image, jstring dst_addr, diff --git a/app/src/main/java/net/mynero/wallet/fragment/dialog/SendBottomSheetDialog.java b/app/src/main/java/net/mynero/wallet/fragment/dialog/SendBottomSheetDialog.java index 6d8870b..a66d3d6 100644 --- a/app/src/main/java/net/mynero/wallet/fragment/dialog/SendBottomSheetDialog.java +++ b/app/src/main/java/net/mynero/wallet/fragment/dialog/SendBottomSheetDialog.java @@ -4,6 +4,7 @@ import android.app.Activity; import android.content.Context; import android.os.AsyncTask; import android.os.Bundle; +import android.util.Pair; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -30,6 +31,7 @@ import net.mynero.wallet.R; import net.mynero.wallet.model.CoinsInfo; import net.mynero.wallet.model.PendingTransaction; import net.mynero.wallet.model.Wallet; +import net.mynero.wallet.model.WalletManager; import net.mynero.wallet.service.BalanceService; import net.mynero.wallet.service.TxService; import net.mynero.wallet.service.UTXOService; diff --git a/app/src/main/java/net/mynero/wallet/model/Wallet.java b/app/src/main/java/net/mynero/wallet/model/Wallet.java index 5dff2df..18a14ea 100644 --- a/app/src/main/java/net/mynero/wallet/model/Wallet.java +++ b/app/src/main/java/net/mynero/wallet/model/Wallet.java @@ -16,6 +16,8 @@ package net.mynero.wallet.model; +import android.util.Pair; + import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -273,6 +275,13 @@ public class Wallet { } } + public long estimateTransactionFee(List> destinations, PendingTransaction.Priority priority) { + int _priority = priority.getValue(); + return estimateTransactionFee(destinations, _priority); + } + + private native long estimateTransactionFee(List> destinations, int priority); + public PendingTransaction createTransaction(TxData txData) { return createTransaction( txData.getDestinationAddress(),