Skip to content

Commit

Permalink
decred: Fix send all.
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeGruffins committed Jan 19, 2025
1 parent b88e4cf commit 1b31198
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 22 deletions.
37 changes: 19 additions & 18 deletions cw_decred/lib/wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ abstract class DecredWalletBase
syncTimer = Timer.periodic(
Duration(seconds: syncIntervalSynced), (Timer t) => performBackgroundTasks());
}
updateTransactionHistory();
await updateTransactionHistory();
}

Future<void> updateTransactionHistory() async {
Expand Down Expand Up @@ -292,30 +292,29 @@ abstract class DecredWalletBase
throw "unable to send with watching only wallet";
});
}
var totalIn = 0;
final ignoreInputs = [];
this.unspentCoinsInfo.values.forEach((unspent) {
if (unspent.isFrozen || !unspent.isSending) {
final input = {"txid": unspent.hash, "vout": unspent.vout};
ignoreInputs.add(input);
return;
}
totalIn += unspent.value;
});
final creds = credentials as DecredTransactionCredentials;
var totalAmt = 0;
var sendAll = false;
final outputs = [];
for (final out in creds.outputs) {
var amt = 0;
if (out.sendAll) {
// get all spendable inputs amount
totalAmt = unspentCoinsInfo.values.fold<int>(0, (sum, unspent) {
if (unspent.isFrozen || !unspent.isSending) {
return sum;
}

return sum + unspent.value;
});
break;
}
if (out.cryptoAmount != null) {
if (creds.outputs.length != 1) {
throw "can only send all to one output";
}
sendAll = true;
totalAmt = totalIn;
} else if (out.cryptoAmount != null) {
final coins = double.parse(out.cryptoAmount!);
amt = (coins * 1e8).toInt();
}
Expand All @@ -328,26 +327,28 @@ abstract class DecredWalletBase
}

// The inputs are always used. Currently we don't have use for this
// argument.
// argument. sendall ingores output value and sends everything.
final signReq = {
// "inputs": inputs,
"ignoreInputs": ignoreInputs,
"outputs": outputs,
"feerate": creds.feeRate ?? defaultFeeRate,
"password": _password,
"sendall": sendAll,
};
final res = libdcrwallet.createSignedTransaction(walletInfo.name, jsonEncode(signReq));
final decoded = json.decode(res);
final signedHex = decoded["signedhex"];
final send = () async {
libdcrwallet.sendRawTransaction(walletInfo.name, signedHex);
await updateBalance();
};
final fee = decoded["fee"] ?? 0;
if (sendAll) {
totalAmt = (totalAmt - fee).toInt() ?? totalAmt;
}
return DecredPendingTransaction(
txid: decoded["txid"] ?? "",
amount: totalAmt,
fee: decoded["fee"] ?? 0,
rawHex: signedHex,
send: send);
txid: decoded["txid"] ?? "", amount: totalAmt, fee: fee, rawHex: signedHex, send: send);
}

int feeRate(TransactionPriority priority) {
Expand Down
4 changes: 2 additions & 2 deletions scripts/android/build_decred.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ cd "$(dirname "$0")"
CW_DECRED_DIR=$(realpath ../..)/cw_decred
LIBWALLET_PATH="${PWD}/decred/libwallet"
LIBWALLET_URL="https://github.com/decred/libwallet.git"
LIBWALLET_VERSION="10344df786c8d488d46ebf38f259efddd7094913"
LIBWALLET_VERSION="82ed8a80ae9fa3b15a2d5609bc748fc663be7e37" # v2.2.1

if [ -e $LIBWALLET_PATH ]; then
rm -fr $LIBWALLET_PATH/{*,.*} || true
Expand Down Expand Up @@ -62,4 +62,4 @@ done
HEADER_DIR=$CW_DECRED_DIR/lib/api
cp ${LIBWALLET_PATH}/build/${TRIPLET}-libdcrwallet.h $HEADER_DIR/libdcrwallet.h
cd $CW_DECRED_DIR
dart run ffigen
dart run ffigen
2 changes: 1 addition & 1 deletion scripts/ios/build_decred.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ set -e
. ./config.sh
LIBWALLET_PATH="${EXTERNAL_IOS_SOURCE_DIR}/libwallet"
LIBWALLET_URL="https://github.com/decred/libwallet.git"
LIBWALLET_VERSION="10344df786c8d488d46ebf38f259efddd7094913"
LIBWALLET_VERSION="82ed8a80ae9fa3b15a2d5609bc748fc663be7e37" # v2.2.1

if [ -e $LIBWALLET_PATH ]; then
rm -fr $LIBWALLET_PATH
Expand Down
2 changes: 1 addition & 1 deletion scripts/macos/build_decred.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

LIBWALLET_PATH="${EXTERNAL_MACOS_SOURCE_DIR}/libwallet"
LIBWALLET_URL="https://github.com/decred/libwallet.git"
LIBWALLET_VERSION="v2.1.0"
LIBWALLET_VERSION="82ed8a80ae9fa3b15a2d5609bc748fc663be7e37" # v2.2.1

echo "======================= DECRED LIBWALLET ========================="

Expand Down

0 comments on commit 1b31198

Please sign in to comment.