Exemplo n.º 1
0
    def test_should_require_positive_coefficient(self):
        with pytest.raises(AssertionError):
            GeometricGasPrice(1000, 60, 0)

        with pytest.raises(AssertionError):
            GeometricGasPrice(1000, 60, 1)

        with pytest.raises(AssertionError):
            GeometricGasPrice(1000, 60, -1)
Exemplo n.º 2
0
    def test_behaves_with_realistic_values(self):
        # given
        GWEI = 1000000000
        geometric_gas_price = GeometricGasPrice(100 * GWEI, 10,
                                                1 + (0.125 * 2))

        for seconds in [0, 1, 10, 12, 30, 60]:
            print(
                f"gas price after {seconds} seconds is {geometric_gas_price.get_gas_price(seconds)/GWEI}"
            )

        assert round(geometric_gas_price.get_gas_price(0) / GWEI, 1) == 100.0
        assert round(geometric_gas_price.get_gas_price(1) / GWEI, 1) == 100.0
        assert round(geometric_gas_price.get_gas_price(10) / GWEI, 1) == 125.0
        assert round(geometric_gas_price.get_gas_price(12) / GWEI, 1) == 125.0
        assert round(geometric_gas_price.get_gas_price(30) / GWEI, 1) == 195.3
        assert round(geometric_gas_price.get_gas_price(60) / GWEI, 1) == 381.5
Exemplo n.º 3
0
 def test_simultaneous(self):
     gas = GeometricGasPrice(initial_price=int(0.5 * GWEI),
                             every_secs=30,
                             max_price=2000 * GWEI)
     self._run_future(
         collateral.adapter.join(our_address,
                                 Wad(1)).transact_async(gas_price=gas))
     self._run_future(
         collateral.adapter.join(our_address,
                                 Wad(5)).transact_async(gas_price=gas))
     asyncio.sleep(6)
Exemplo n.º 4
0
 def setup_class(self):
     """ I'm excluding initialization of a specific collateral perchance we use multiple collaterals
     to improve test speeds.  This prevents us from instantiating the keeper as a class member. """
     self.web3 = web3()
     self.mcd = mcd(self.web3)
     self.keeper_address = keeper_address(self.mcd.web3)
     self.web3.eth.defaultAccount = self.keeper_address.address
     self.collateral = self.mcd.collaterals['ETH-A']
     self.collateral.approve(self.keeper_address)
     assert self.collateral.gem.deposit(Wad.from_number(1)).transact()
     self.ilk = self.collateral.ilk
     self.gas = GeometricGasPrice(initial_price=int(1.1*GeometricGasPrice.GWEI), every_secs=2)
Exemplo n.º 5
0
    def test_gas_price_should_increase_with_time(self):
        # given
        geometric_gas_price = GeometricGasPrice(100, 10)

        # expect
        assert geometric_gas_price.get_gas_price(0) == 100
        assert geometric_gas_price.get_gas_price(1) == 100
        assert geometric_gas_price.get_gas_price(10) == 113
        assert geometric_gas_price.get_gas_price(15) == 113
        assert geometric_gas_price.get_gas_price(20) == 127
        assert geometric_gas_price.get_gas_price(30) == 143
        assert geometric_gas_price.get_gas_price(50) == 181
        assert geometric_gas_price.get_gas_price(100) == 325
Exemplo n.º 6
0
    def get_gas_price(self, time_elapsed: int) -> Optional[int]:
        # start with fast price from the configured gas API
        fast_price = self.gas_station.fast_price() if self.gas_station else None

        # if API produces no price, or remote feed not configured, start with a fixed price
        if fast_price is None:
            initial_price = self.fixed_gas if self.fixed_gas else 10 * self.GWEI
        # otherwise, use the API's fast price, adjusted by a coefficient, as our starting point
        else:
            initial_price = int(round(fast_price * self.initial_multiplier))

        return GeometricGasPrice(initial_price=initial_price,
                                 every_secs=61,
                                 coefficient=self.reactive_multiplier,
                                 max_price=self.gas_maximum).get_gas_price(time_elapsed)
Exemplo n.º 7
0
    def test_gas_price_should_obey_max_value(self):
        # given
        geometric_gas_price = GeometricGasPrice(1000, 60, 1.125, 2500)

        # expect
        assert geometric_gas_price.get_gas_price(0) == 1000
        assert geometric_gas_price.get_gas_price(1) == 1000
        assert geometric_gas_price.get_gas_price(59) == 1000
        assert geometric_gas_price.get_gas_price(60) == 1125
        assert geometric_gas_price.get_gas_price(119) == 1125
        assert geometric_gas_price.get_gas_price(120) == 1266
        assert geometric_gas_price.get_gas_price(1200) == 2500
        assert geometric_gas_price.get_gas_price(3000) == 2500
        assert geometric_gas_price.get_gas_price(1000000) == 2500
Exemplo n.º 8
0
if len(sys.argv) > 3:
    web3.eth.defaultAccount = sys.argv[2]
    register_keys(web3, [sys.argv[3]])
    our_address = Address(web3.eth.defaultAccount)
    run_transactions = True
elif len(sys.argv) > 2:
    our_address = Address(sys.argv[2])
    run_transactions = False
else:
    our_address = None
    run_transactions = False

gas_strategy = DefaultGasPrice() if len(sys.argv) <= 4 else \
    GeometricGasPrice(web3=web3,
                      initial_price=None,
                      initial_tip=int(float(sys.argv[4]) * GeometricGasPrice.GWEI),
                      every_secs=5,
                      max_price=50 * GeometricGasPrice.GWEI)

eth = EthToken(web3, Address.zero())


class TestApp:
    def main(self):
        with Lifecycle(web3) as lifecycle:
            lifecycle.on_block(self.on_block)

    def on_block(self):
        block = web3.eth.blockNumber
        logging.info(f"Found block; web3.eth.blockNumber={block}")
Exemplo n.º 9
0
logging.getLogger('urllib3').setLevel(logging.INFO)
logging.getLogger("web3").setLevel(logging.INFO)
logging.getLogger("asyncio").setLevel(logging.INFO)
logging.getLogger("requests").setLevel(logging.INFO)

pool_size = int(sys.argv[3]) if len(sys.argv) > 3 else 10
web3 = web3_via_http(endpoint_uri=os.environ['ETH_RPC_URL'], http_pool_size=pool_size)
web3.eth.defaultAccount = sys.argv[1]   # ex: 0x0000000000000000000000000000000aBcdef123
register_keys(web3, [sys.argv[2]])      # ex: key_file=~keys/default-account.json,pass_file=~keys/default-account.pass

mcd = DssDeployment.from_node(web3)
our_address = Address(web3.eth.defaultAccount)
weth = DssDeployment.from_node(web3).collaterals['ETH-A'].gem

GWEI = 1000000000
slow_gas = GeometricGasPrice(initial_price=int(0.8 * GWEI), every_secs=30, max_price=2000 * GWEI)
fast_gas = GeometricGasPrice(initial_price=int(1.1 * GWEI), every_secs=30, max_price=2000 * GWEI)


class TestApp:
    def main(self):
        self.test_replacement()
        self.test_simultaneous()
        self.shutdown()

    def test_replacement(self):
        first_tx = weth.deposit(Wad(4))
        logging.info(f"Submitting first TX with gas price deliberately too low")
        self._run_future(first_tx.transact_async(gas_price=slow_gas))
        time.sleep(0.5)
Exemplo n.º 10
0
pool_size = int(sys.argv[3]) if len(sys.argv) > 3 else 10
web3 = web3_via_http(endpoint_uri=os.environ['ETH_RPC_URL'],
                     http_pool_size=pool_size)
web3.eth.defaultAccount = sys.argv[
    1]  # ex: 0x0000000000000000000000000000000aBcdef123
register_keys(
    web3, [sys.argv[2]]
)  # ex: key_file=~keys/default-account.json,pass_file=~keys/default-account.pass

mcd = DssDeployment.from_node(web3)
our_address = Address(web3.eth.defaultAccount)
weth = DssDeployment.from_node(web3).collaterals['ETH-A'].gem

GWEI = 1000000000
slow_gas = GeometricGasPrice(initial_price=int(15 * GWEI),
                             every_secs=42,
                             max_price=200 * GWEI)
fast_gas = GeometricGasPrice(initial_price=int(30 * GWEI),
                             every_secs=42,
                             max_price=200 * GWEI)


class TestApp:
    def main(self):
        self.test_replacement()
        self.test_simultaneous()
        self.shutdown()

    def test_replacement(self):
        first_tx = weth.deposit(Wad(4))
        logging.info(
Exemplo n.º 11
0
web3 = web3_via_http(endpoint_uri=os.environ['ETH_RPC_URL'],
                     http_pool_size=pool_size)
web3.eth.defaultAccount = sys.argv[
    1]  # ex: 0x0000000000000000000000000000000aBcdef123
register_keys(
    web3, [sys.argv[2]]
)  # ex: key_file=~keys/default-account.json,pass_file=~keys/default-account.pass

mcd = DssDeployment.from_node(web3)
our_address = Address(web3.eth.defaultAccount)
weth = DssDeployment.from_node(web3).collaterals['ETH-A'].gem

GWEI = 1000000000
slow_gas = GeometricGasPrice(web3=web3,
                             initial_price=None,
                             initial_tip=int(0.1 * GWEI),
                             every_secs=42,
                             max_price=200 * GWEI)
fast_gas = GeometricGasPrice(web3=web3,
                             initial_price=None,
                             initial_tip=int(4.5 * GWEI),
                             every_secs=42,
                             max_price=200 * GWEI)


class TestApp:
    def main(self):
        self.test_replacement()
        self.test_simultaneous()
        self.shutdown()
Exemplo n.º 12
0
 def test_max_price_should_exceed_initial_price(self):
     with pytest.raises(AssertionError):
         GeometricGasPrice(6000, 30, 2.25, 5000)
Exemplo n.º 13
0
    def test_should_require_positive_max_price_if_provided(self):
        with pytest.raises(AssertionError):
            GeometricGasPrice(1000, 60, 1.125, 0)

        with pytest.raises(AssertionError):
            GeometricGasPrice(1000, 60, 1.125, -1)
Exemplo n.º 14
0
    def test_should_require_positive_every_secs_value(self):
        with pytest.raises(AssertionError):
            GeometricGasPrice(1000, 0)

        with pytest.raises(AssertionError):
            GeometricGasPrice(1000, -1)
Exemplo n.º 15
0
    def test_should_require_positive_initial_price(self):
        with pytest.raises(AssertionError):
            GeometricGasPrice(0, 60)

        with pytest.raises(AssertionError):
            GeometricGasPrice(-1, 60)
Exemplo n.º 16
0
logging.getLogger("asyncio").setLevel(logging.INFO)
logging.getLogger("requests").setLevel(logging.INFO)

pool_size = int(sys.argv[3]) if len(sys.argv) > 3 else 10
web3 = web3_via_http(endpoint_uri=os.environ['ETH_RPC_URL'])
web3.eth.defaultAccount = sys.argv[
    1]  # ex: 0x0000000000000000000000000000000aBcdef123
register_keys(
    web3, [sys.argv[2]]
)  # ex: key_file=~keys/default-account.json,pass_file=~keys/default-account.pass
our_address = Address(web3.eth.defaultAccount)
weth = DssDeployment.from_node(web3).collaterals['ETH-A'].gem

GWEI = 1000000000
increasing_gas = GeometricGasPrice(initial_price=int(1 * GWEI),
                                   every_secs=30,
                                   coefficient=1.5,
                                   max_price=100 * GWEI)


class TestApp:
    def main(self):
        self.startup()

        pending_txes = get_pending_transactions(web3)
        pprint(
            list(
                map(lambda t: f"{t.name()} with gas {t.current_gas}",
                    pending_txes)))

        if len(pending_txes) > 0:
            while len(pending_txes) > 0: