class GasStrategies:
    def __init__(self):
        self.standard = GasNowStrategy("standard")
        self.fast = GasNowStrategy("fast")
        self.rapid = GasNowStrategy("rapid")
        self.bsc_static = StaticGasStrategy(bsc_static_price)

        print(self.fast.get_gas_price())

        self.exponentialScaling = ExponentialScalingStrategy(
            initial_gas_price=self.standard.get_gas_price(),
            max_gas_price=Wei(exponential_scaling_config["max_gas_price"]),
            time_duration=120,
        )

        self.exponentialScalingFast = ExponentialScalingStrategy(
            initial_gas_price=self.fast.get_gas_price(),
            max_gas_price=Wei(exponential_scaling_config["max_gas_price"]),
            time_duration=60,
        )

    def set_default(self, strategy):
        gas_price(strategy)

    def set_default_for_active_chain(self):
        chain = network_manager.get_active_network()
        if chain == "eth":
            self.set_default(self.exponentialScaling)
        elif chain == "bsc":
            self.set_default(self.bsc_static)
Exemple #2
0
class GasStrategies:
    def __init__(self):
        self.standard = GasNowStrategy("standard")
        self.fast = GasNowStrategy("fast")
        self.rapid = GasNowStrategy("rapid")
        self.bsc_static = StaticGasStrategy(bsc_static_price)
        self.analyzed = analyze_gas({"timeframe": "minutes", "periods": 15})

        print(
            "gas prices - fast:",
            self.fast.get_gas_price(),
            "recent average:",
            self.analyzed.mode,
        )

        self.exponentialScaling = ExponentialScalingStrategy(
            initial_gas_price=self.standard.get_gas_price(),
            max_gas_price=Wei(exponential_scaling_config["max_gas_price"]),
            time_duration=120,
        )

        self.exponentialScalingFast = ExponentialScalingStrategy(
            initial_gas_price=self.fast.get_gas_price(),
            max_gas_price=Wei(exponential_scaling_config["max_gas_price"]),
            time_duration=60,
        )

    def set_default(self, strategy):
        gas_price(strategy)

    def gas_cost(self, gas_estimate):
        """
        total gas cost of estimate in wei
        """
        return Web3.toWei(
            Web3.fromWei(self.fast.get_gas_price(), "gwei") * gas_estimate, "gwei"
        )

    def set_default_for_active_chain(self):
        chain = network_manager.get_active_network()
        if chain == "eth":
            self.set_default(self.exponentialScaling)
        elif chain == "bsc":
            self.set_default(self.bsc_static)

    def optimal_price(self):
        return min(self.fast.get_gas_price(), self.analyzed.mode)