<commit_before>/*
 * This file is part of Bisq.
 *
 * Bisq is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 *
 * Bisq is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Bisq. If not, see <http://www.gnu.org/licenses/>.
 */

package bisq.core.btc;

import bisq.core.btc.wallet.BtcWalletService;
import bisq.core.provider.fee.FeeService;
import bisq.core.user.Preferences;

import bisq.common.util.Tuple2;

import org.bitcoinj.core.Coin;
import org.bitcoinj.core.InsufficientMoneyException;

import javax.inject.Inject;

import com.google.common.annotations.VisibleForTesting;

import java.util.List;

import lombok.extern.slf4j.Slf4j;

import static com.google.common.base.Preconditions.checkArgument;

/**
 * Util class for getting the estimated tx fee for maker or taker fee tx.
 */
@Slf4j
public class TxFeeEstimationService {

//  Size/vsize of typical trade txs
//  Real txs size/vsize may vary in 1 or 2 bytes from the estimated values.
//  Values calculated with https://gist.github.com/oscarguindzberg/3d1349cb65d9fd9af9de0feaa3fd27ac
//  legacy fee tx with 1 input, maker/taker fee paid in btc size/vsize = 258
//  legacy deposit tx without change size/vsize = 381
//  legacy deposit tx with change size/vsize = 414
//  legacy payout tx size/vsize = 337
//  legacy delayed payout tx size/vsize = 302
//  segwit fee tx with 1 input, maker/taker fee paid in btc vsize = 173
//  segwit deposit tx without change vsize = 232
//  segwit deposit tx with change vsize = 263
//  segwit payout tx vsize = 169
//  segwit delayed payout tx vsize = 139
    public static int TYPICAL_TX_WITH_1_INPUT_VSIZE = 175;
    private static int DEPOSIT_TX_VSIZE = 233;

    private static int BSQ_INPUT_INCREASE = 150;
    private static int MAX_ITERATIONS = 10;

    private final FeeService feeService;
    private final BtcWalletService btcWalletService;
    private final Preferences preferences;

    @Inject
    public TxFeeEstimationService(FeeService feeService,
                                  BtcWalletService btcWalletService,
                                  Preferences preferences) {

        this.feeService = feeService;
        this.btcWalletService = btcWalletService;
        this.preferences = preferences;
    }

    public Tuple2<Coin, Integer> getEstimatedFeeAndTxVsizeForTaker(Coin fundsNeededForTrade, Coin tradeFee) {
        return getEstimatedFeeAndTxVsize(true,
                fundsNeededForTrade,
                tradeFee,
                feeService,
                btcWalletService,
                preferences);<commit_msg>reduce BSQ fee estimation constant for segwit<commit_after>/*
 * This file is part of Bisq.
 *
 * Bisq is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 *
 * Bisq is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Bisq. If not, see <http://www.gnu.org/licenses/>.
 */

package bisq.core.btc;

import bisq.core.btc.wallet.BtcWalletService;
import bisq.core.provider.fee.FeeService;
import bisq.core.user.Preferences;

import bisq.common.util.Tuple2;

import org.bitcoinj.core.Coin;
import org.bitcoinj.core.InsufficientMoneyException;

import javax.inject.Inject;

import com.google.common.annotations.VisibleForTesting;

import java.util.List;

import lombok.extern.slf4j.Slf4j;

import static com.google.common.base.Preconditions.checkArgument;

/**
 * Util class for getting the estimated tx fee for maker or taker fee tx.
 */
@Slf4j
public class TxFeeEstimationService {

//  Size/vsize of typical trade txs
//  Real txs size/vsize may vary in 1 or 2 bytes from the estimated values.
//  Values calculated with https://gist.github.com/oscarguindzberg/3d1349cb65d9fd9af9de0feaa3fd27ac
//  legacy fee tx with 1 input, maker/taker fee paid in btc size/vsize = 258
//  legacy deposit tx without change size/vsize = 381
//  legacy deposit tx with change size/vsize = 414
//  legacy payout tx size/vsize = 337
//  legacy delayed payout tx size/vsize = 302
//  segwit fee tx with 1 input, maker/taker fee paid in btc vsize = 173
//  segwit deposit tx without change vsize = 232
//  segwit deposit tx with change vsize = 263
//  segwit payout tx vsize = 169
//  segwit delayed payout tx vsize = 139
    public static int TYPICAL_TX_WITH_1_INPUT_VSIZE = 175;
    private static int DEPOSIT_TX_VSIZE = 233;

    private static int BSQ_INPUT_INCREASE = 65;
    private static int MAX_ITERATIONS = 10;

    private final FeeService feeService;
    private final BtcWalletService btcWalletService;
    private final Preferences preferences;

    @Inject
    public TxFeeEstimationService(FeeService feeService,
                                  BtcWalletService btcWalletService,
                                  Preferences preferences) {

        this.feeService = feeService;
        this.btcWalletService = btcWalletService;
        this.preferences = preferences;
    }

    public Tuple2<Coin, Integer> getEstimatedFeeAndTxVsizeForTaker(Coin fundsNeededForTrade, Coin tradeFee) {
        return getEstimatedFeeAndTxVsize(true,
                fundsNeededForTrade,
                tradeFee,
                feeService,
                btcWalletService,
                preferences);
{'fraction_non_alphanumeric': 0.06623826000988631, 'fraction_numerical': 0.02158510463008733, 'mean_word_length': 3.137695978186776, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5042633', 'n_tokens_mistral': 1901, 'n_tokens_neox': 1799, 'n_words': 621}