Skip to content

Commit

Permalink
Fix transaction change output index out of range
Browse files Browse the repository at this point in the history
  • Loading branch information
timemarkovqtum committed Sep 16, 2024
1 parent 0350365 commit 90612c8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
6 changes: 2 additions & 4 deletions src/wallet/rpc/contract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,12 +318,11 @@ RPCHelpMan createcontract()

// Create and send the transaction
std::vector<CRecipient> vecSend;
int nChangePosRet = -1;
CRecipient recipient = {CNoDestination(scriptPubKey), 0, false};
vecSend.push_back(recipient);

bool sign = !fPsbt;
auto res = CreateTransaction(*pwallet, vecSend, nChangePosRet, coinControl, sign, nGasFee, true, signSenderAddress);
auto res = CreateTransaction(*pwallet, vecSend, std::nullopt, coinControl, sign, nGasFee, true, signSenderAddress);
if (!res) {
throw JSONRPCError(RPC_WALLET_ERROR, util::ErrorString(res).original);
}
Expand Down Expand Up @@ -581,12 +580,11 @@ UniValue SendToContract(CWallet& wallet, const UniValue& params, ChainstateManag

// Create and send the transaction
std::vector<CRecipient> vecSend;
int nChangePosRet = -1;
CRecipient recipient = {CNoDestination(scriptPubKey), nAmount, false};
vecSend.push_back(recipient);

bool sign = !fPsbt;
auto res = CreateTransaction(wallet, vecSend, nChangePosRet, coinControl, sign, nGasFee, true, signSenderAddress);
auto res = CreateTransaction(wallet, vecSend, std::nullopt, coinControl, sign, nGasFee, true, signSenderAddress);
if (!res) {
throw JSONRPCError(RPC_WALLET_ERROR, util::ErrorString(res).original);
}
Expand Down
10 changes: 4 additions & 6 deletions src/wallet/rpc/spend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1857,7 +1857,6 @@ CTransactionRef SplitUTXOs(std::shared_ptr<CWallet> const pwallet, const CTxDest

// Split into utxos with nValue
std::vector<CRecipient> vecSend;
constexpr int RANDOM_CHANGE_POSITION = -1;
int numOfRecipients = static_cast<int>(nTotal / nValue);

// Compute the number of recipients
Expand Down Expand Up @@ -1892,7 +1891,7 @@ CTransactionRef SplitUTXOs(std::shared_ptr<CWallet> const pwallet, const CTxDest
CTransactionRef tx;
if((nTxAmount + pwallet->m_default_max_tx_fee) <= nTotal)
{
auto res = CreateTransaction(*pwallet, vecSend, RANDOM_CHANGE_POSITION, coin_control, sign, 0, false);
auto res = CreateTransaction(*pwallet, vecSend, std::nullopt, coin_control, sign, 0, false);
if (!res) {
throw JSONRPCError(RPC_WALLET_ERROR, util::ErrorString(res).original);
}
Expand All @@ -1907,7 +1906,7 @@ CTransactionRef SplitUTXOs(std::shared_ptr<CWallet> const pwallet, const CTxDest
vecSend[vecSend.size() - 1] = lastRecipient;
CAmount nFeeRequired = 0;
{
auto res = CreateTransaction(*pwallet, vecSend, RANDOM_CHANGE_POSITION, coin_control, sign, 0, false);
auto res = CreateTransaction(*pwallet, vecSend, std::nullopt, coin_control, sign, 0, false);
if (!res) {
throw JSONRPCError(RPC_WALLET_ERROR, util::ErrorString(res).original);
}
Expand Down Expand Up @@ -1943,7 +1942,7 @@ CTransactionRef SplitUTXOs(std::shared_ptr<CWallet> const pwallet, const CTxDest
SplitRemainder(vecSend, nValueLast2, maxValue);
}

auto res = CreateTransaction(*pwallet, vecSend, RANDOM_CHANGE_POSITION, coin_control, sign, 0, false);
auto res = CreateTransaction(*pwallet, vecSend, std::nullopt, coin_control, sign, 0, false);
if (!res) {
throw JSONRPCError(RPC_WALLET_ERROR, util::ErrorString(res).original);
}
Expand Down Expand Up @@ -2237,8 +2236,7 @@ RPCHelpMan sendmanywithdupes()
std::shuffle(vecSend.begin(), vecSend.end(), FastRandomContext());

// Send
constexpr int RANDOM_CHANGE_POSITION = -1;
auto res = CreateTransaction(*pwallet, vecSend, RANDOM_CHANGE_POSITION, coin_control);
auto res = CreateTransaction(*pwallet, vecSend, std::nullopt, coin_control);
if (!res) {
throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, util::ErrorString(res).original);
}
Expand Down

0 comments on commit 90612c8

Please sign in to comment.