Add estimate fee method, but based on Monero's code it doesn't seem to take priority into account

This commit is contained in:
pokkst 2022-09-24 02:57:46 -05:00
parent 452cc12c8f
commit f3d2ba2591
No known key found for this signature in database
GPG Key ID: 90C2ED85E67A50FF
3 changed files with 52 additions and 0 deletions

View File

@ -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<jclass>(jenv->NewGlobalRef(
jenv->FindClass("java/util/ArrayList")));
class_Pair = static_cast<jclass>(jenv->NewGlobalRef(
jenv->FindClass("android/util/Pair")));
class_TransactionInfo = static_cast<jclass>(jenv->NewGlobalRef(
jenv->FindClass("net/mynero/wallet/model/TransactionInfo")));
class_Transfer = static_cast<jclass>(jenv->NewGlobalRef(
@ -249,6 +252,34 @@ std::set<std::string> java2cpp_set(JNIEnv *env, jobject arrayList) {
return result;
}
std::pair<std::string, uint64_t> 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<jstring>(env->GetObjectField(p, first));
std::string converted_string = env->GetStringUTFChars(string, nullptr);
uint64_t value = reinterpret_cast<uint64_t>(env->GetObjectField(p, second));
auto pair = std::make_pair(converted_string, value);
return pair;
}
std::vector<std::pair<std::string, uint64_t>> 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<std::pair<std::string, uint64_t>> result;
for (jint i = 0; i < len; i++) {
jobject element = static_cast<jobject>(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<std::string> &vector) {
jmethodID java_util_ArrayList_ = env->GetMethodID(class_ArrayList, "<init>", "(I)V");
@ -998,6 +1029,16 @@ Java_net_mynero_wallet_model_Wallet_createTransactionJ(JNIEnv *env, jobject inst
return reinterpret_cast<jlong>(tx);
}
JNIEXPORT jlong JNICALL
Java_net_mynero_wallet_model_Wallet_estimateTransactionFee(JNIEnv *env, jobject instance,
jobject destinations, jint priority) {
std::vector<std::pair<std::string, uint64_t>> dest_vector = java2cpp_pairvector(env, destinations);
Monero::PendingTransaction::Priority _priority = static_cast<Monero::PendingTransaction::Priority>(priority);
Monero::Wallet *wallet = getHandle<Monero::Wallet>(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,

View File

@ -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;

View File

@ -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<Pair<String, Long>> destinations, PendingTransaction.Priority priority) {
int _priority = priority.getValue();
return estimateTransactionFee(destinations, _priority);
}
private native long estimateTransactionFee(List<Pair<String, Long>> destinations, int priority);
public PendingTransaction createTransaction(TxData txData) {
return createTransaction(
txData.getDestinationAddress(),