src/wallet/wallet.cpp
bool CWallet::FundTransaction(CMutableTransaction& tx, CAmount& nFeeRet, int& nChangePosInOut, std::string& strFailReason, bool lockUnspents, const std::set<int>& setSubtractFeeFromOutputs, CCoinControl coinControl)
{
    std::vector<CRecipient> vecSend;

    // Turn the txout set into a CRecipient vector.
    for (size_t idx = 0; idx < tx.vout.size(); idx++) {
        const CTxOut& txOut = tx.vout[idx];
        CRecipient recipient = {txOut.scriptPubKey, txOut.nValue, setSubtractFeeFromOutputs.count(idx) == 1};
        vecSend.push_back(recipient);
    }

    coinControl.fAllowOtherInputs = true;

    for (const CTxIn& txin : tx.vin) {
        coinControl.Select(txin.prevout);
    }

    // Acquire the locks to prevent races to the new locked unspents between the
    // CreateTransaction call and LockCoin calls (when lockUnspents is true).
    LOCK2(cs_main, cs_wallet);

    CReserveKey reservekey(this);
    CTransactionRef tx_new;
    CTransactionAttributes attr = CTransactionAttributes(CTransactionTypes::VALUE);
    if (!CreateTransaction(vecSend, tx_new, reservekey, nFeeRet, nChangePosInOut, strFailReason, coinControl, CTransactionTypes::VALUE, attr, NATIVE_ASSET, false)) {
        return false;
    }

    if (nChangePosInOut != -1) {
        tx.vout.insert(tx.vout.begin() + nChangePosInOut, tx_new->vout[nChangePosInOut]);
        // We don't have the normal Create/Commit cycle, and don't want to risk
        // reusing change, so just remove the key from the keypool here.
        reservekey.KeepKey();
    }

    // Copy output sizes from new transaction; they may have had the fee
    // subtracted from them.
    for (unsigned int idx = 0; idx < tx.vout.size(); idx++) {
        tx.vout[idx].nValue = tx_new->vout[idx].nValue;
    }

    // Add new txins while keeping original txin scriptSig/order.
    for (const CTxIn& txin : tx_new->vin) {
        if (!coinControl.IsSelected(txin.prevout)) {
            tx.vin.push_back(txin);

            if (lockUnspents) {
                LockCoin(txin.prevout);
            }
        }
    }

    return true;
}
// TODO: Only supports native coin at the moment.
Only supports native coin at the moment.
Only supports native coin at the moment .
bool CWallet::FundTransaction(CMutableTransaction& tx, CAmount& nFeeRet, int& nChangePosInOut, std::string& strFailReason, bool lockUnspents, const std::set<int>& setSubtractFeeFromOutputs, CCoinControl coinControl)
{
    std::vector<CRecipient> vecSend;
    for (size_t idx = 0; idx < tx.vout.size(); idx++) {
        const CTxOut& txOut = tx.vout[idx];
        CRecipient recipient = {txOut.scriptPubKey, txOut.nValue, setSubtractFeeFromOutputs.count(idx) == 1};
        vecSend.push_back(recipient);
    }
    coinControl.fAllowOtherInputs = true;
    for (const CTxIn& txin : tx.vin) {
        coinControl.Select(txin.prevout);
    }
    LOCK2(cs_main, cs_wallet);
    CReserveKey reservekey(this);
    CTransactionRef tx_new;
    CTransactionAttributes attr = CTransactionAttributes(CTransactionTypes::VALUE);
    if (!CreateTransaction(vecSend, tx_new, reservekey, nFeeRet, nChangePosInOut, strFailReason, coinControl, CTransactionTypes::VALUE, attr, NATIVE_ASSET, false)) {
        return false;
    }
    if (nChangePosInOut != -1) {
        tx.vout.insert(tx.vout.begin() + nChangePosInOut, tx_new->vout[nChangePosInOut]);
        reservekey.KeepKey();
    }
    for (unsigned int idx = 0; idx < tx.vout.size(); idx++) {
        tx.vout[idx].nValue = tx_new->vout[idx].nValue;
    }
    for (const CTxIn& txin : tx_new->vin) {
        if (!coinControl.IsSelected(txin.prevout)) {
            tx.vin.push_back(txin);
            if (lockUnspents) {
                LockCoin(txin.prevout);
            }
        }
    }
    return true;
}
bool CWallet :: FundTransaction ( CMutableTransaction & tx , CAmount & nFeeRet , int & nChangePosInOut , std :: string & strFailReason , bool lockUnspents , const std :: set < int > & setSubtractFeeFromOutputs , CCoinControl coinControl ) { std :: vector < CRecipient > vecSend ; for ( size_t idx = 0 ; idx < tx . vout . size ( ) ; idx ++ ) { const CTxOut & txOut = tx . vout [ idx ] ; CRecipient recipient = { txOut . scriptPubKey , txOut . nValue , setSubtractFeeFromOutputs . count ( idx ) == 1 } ; vecSend . push_back ( recipient ) ; } coinControl . fAllowOtherInputs = true ; for ( const CTxIn & txin : tx . vin ) { coinControl . Select ( txin . prevout ) ; } LOCK2 ( cs_main , cs_wallet ) ; CReserveKey reservekey ( this ) ; CTransactionRef tx_new ; CTransactionAttributes attr = CTransactionAttributes ( CTransactionTypes :: VALUE ) ; if ( ! CreateTransaction ( vecSend , tx_new , reservekey , nFeeRet , nChangePosInOut , strFailReason , coinControl , CTransactionTypes :: VALUE , attr , NATIVE_ASSET , false ) ) { return false ; } if ( nChangePosInOut != -1 ) { tx . vout . insert ( tx . vout . begin ( ) + nChangePosInOut , tx_new -> vout [ nChangePosInOut ] ) ; reservekey . KeepKey ( ) ; } for ( unsigned int idx = 0 ; idx < tx . vout . size ( ) ; idx ++ ) { tx . vout [ idx ] . nValue = tx_new -> vout [ idx ] . nValue ; } for ( const CTxIn & txin : tx_new -> vin ) { if ( ! coinControl . IsSelected ( txin . prevout ) ) { tx . vin . push_back ( txin ) ; if ( lockUnspents ) { LockCoin ( txin . prevout ) ; } } } return true ; }
TODO: Only supports native coin at the moment.
TODO : Only supports native coin at the moment .
// Turn the txout set into a CRecipient vector. // Acquire the locks to prevent races to the new locked unspents between the // CreateTransaction call and LockCoin calls (when lockUnspents is true). // We don't have the normal Create/Commit cycle, and don't want to risk // reusing change, so just remove the key from the keypool here. // Copy output sizes from new transaction; they may have had the fee // subtracted from them. // Add new txins while keeping original txin scriptSig/order.