Skip to content

Commit f6bcf76

Browse files
committed
Update sweep to build aggregated transactions
- This change updates the sweep to make it build transactions with multiple outputs and up to 500 pr. transaction.
1 parent 4bd78c9 commit f6bcf76

1 file changed

Lines changed: 48 additions & 28 deletions

File tree

src/Features/Blockcore.Features.Wallet/WalletManager.cs

Lines changed: 48 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2148,39 +2148,26 @@ public IEnumerable<string> Sweep(IEnumerable<string> privateKeys, string destAdd
21482148
currentOutputCount++;
21492149
total += txOut.Value;
21502150

2151-
// Not many wallets will have this many inputs, but we have to ensure that the resulting transactions are
2152-
// small enough to be broadcast without standardness problems.
2153-
// Since there is only 1 output the size of the inputs is the only consideration.
2154-
if (total == 0 || currentOutputCount > 500)
2151+
if (total == 0)
2152+
{
21552153
continue;
2154+
}
21562155

2157-
BitcoinAddress destination = BitcoinAddress.Create(destAddress, this.network);
2158-
2159-
builder.Send(destination, total);
2160-
2161-
// Cause the last destination to pay the fee, as we have no other funds to pay fees with.
2162-
builder.SubtractFees();
2163-
2164-
FeeRate feeRate = this.walletFeePolicy.GetFeeRate(FeeType.High.ToConfirmations());
2165-
builder.SendEstimatedFees(feeRate);
2166-
2167-
Transaction sweepTransaction = builder.BuildTransaction(true);
2168-
2169-
TransactionPolicyError[] errors = builder.Check(sweepTransaction);
2170-
2171-
// TODO: Perhaps return a model with an errors property to inform the user
2172-
if (errors.Length == 0)
2173-
sweepTransactions.Add(sweepTransaction.ToHex());
2174-
2175-
// Reset the builder and related state, as we are now creating a fresh transaction.
2176-
builder = new TransactionBuilder(this.network);
2156+
// If we reach a high total output count, we'll finalize the transaction and start building another one.
2157+
if (currentOutputCount > 500)
2158+
{
2159+
PrepareTransaction(destAddress, ref builder, total, sweepTransactions);
21772160

2178-
currentOutputCount = 0;
2179-
total = 0;
2161+
currentOutputCount = 0;
2162+
total = 0;
2163+
}
21802164
}
21812165

2182-
if (sweepTransactions.Count == 0)
2183-
return sweepTransactions;
2166+
// If there was a total of less than 500 inputs, or leftovers, we'll prepare the transaction.
2167+
if (currentOutputCount > 0)
2168+
{
2169+
PrepareTransaction(destAddress, ref builder, total, sweepTransactions);
2170+
}
21842171

21852172
if (broadcast)
21862173
{
@@ -2194,5 +2181,38 @@ public IEnumerable<string> Sweep(IEnumerable<string> privateKeys, string destAdd
21942181

21952182
return sweepTransactions;
21962183
}
2184+
2185+
public void PrepareTransaction(string destAddress, ref TransactionBuilder builder, Money total, List<string> sweepTransactions)
2186+
{
2187+
BitcoinAddress destination = BitcoinAddress.Create(destAddress, this.network);
2188+
2189+
builder.Send(destination, total);
2190+
2191+
// Cause the last destination to pay the fee, as we have no other funds to pay fees with.
2192+
builder.SubtractFees();
2193+
2194+
FeeRate feeRate = this.walletFeePolicy.GetFeeRate(FeeType.High.ToConfirmations());
2195+
builder.SendEstimatedFees(feeRate);
2196+
2197+
Transaction sweepTransaction = builder.BuildTransaction(true);
2198+
2199+
TransactionPolicyError[] errors = builder.Check(sweepTransaction);
2200+
2201+
if (errors.Length == 0)
2202+
{
2203+
sweepTransactions.Add(sweepTransaction.ToHex());
2204+
}
2205+
else
2206+
{
2207+
// If there are errors, simply append them to the list of return values.
2208+
foreach (var error in errors)
2209+
{
2210+
sweepTransactions.Add(error.ToString());
2211+
}
2212+
}
2213+
2214+
// Reset the builder and related state, as we are now creating a fresh transaction.
2215+
builder = new TransactionBuilder(this.network);
2216+
}
21972217
}
21982218
}

0 commit comments

Comments
 (0)