Example #1
0
    def test_replacement(self):
        first_tx = collateral.adapter.join(our_address, Wad(4))
        logging.info(f"Submitting first TX with gas price deliberately too low")
        self._run_future(first_tx.transact_async(gas_price=FixedGasPrice(1000)))
        time.sleep(2)

        second_tx = collateral.adapter.join(our_address, Wad(6))
        logging.info(f"Replacing first TX with legitimate gas price")
        second_tx.transact(replace=first_tx, gas_price=FixedGasPrice(2*GWEI))

        assert first_tx.replaced
    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:
                pending_txes[0].cancel(gas_price=increasing_gas)
                # After the synchronous cancel, wait to see if subsequent transactions get mined
                time.sleep(15)
                pending_txes = get_pending_transactions(web3)
        else:
            logging.info(
                f"No pending transactions were found; submitting {stuck_txes_to_submit}"
            )
            for i in range(1, stuck_txes_to_submit + 1):
                self._run_future(
                    weth.deposit(Wad(i)).transact_async(
                        gas_price=FixedGasPrice(int(0.4 * i * GWEI))))
            time.sleep(2)

        self.shutdown()
Example #3
0
 def create_gas_price(arguments) -> GasPrice:
     if arguments.smart_gas_price:
         return SmartGasPrice()
     elif arguments.gas_price:
         return FixedGasPrice(arguments.gas_price)
     else:
         return DefaultGasPrice()
Example #4
0
 def create_gas_price(arguments) -> GasPrice:
     if arguments.smart_gas_price:
         return SmartGasPrice(arguments.ethgasstation_api_key)
     elif arguments.gas_price:
         return FixedGasPrice(arguments.gas_price)
     else:
         return DefaultGasPrice()
Example #5
0
    async def test_transaction_replace(self):
        # given
        original_send_transaction = self.web3.eth.sendTransaction
        original_get_transaction = self.web3.eth.getTransaction
        nonce = self.web3.eth.getTransactionCount(self.our_address.address)

        # when
        self.web3.eth.sendTransaction = MagicMock(
            return_value='0xaaaaaaaaaabbbbbbbbbbccccccccccdddddddddd')
        self.web3.eth.getTransaction = MagicMock(return_value={'nonce': nonce})
        # and
        transact_1 = self.token.transfer(self.second_address, Wad(500))
        future_receipt_1 = asyncio.ensure_future(
            transact_1.transact_async(
                gas_strategy=FixedGasPrice(1, None, None)))
        # and
        await asyncio.sleep(0.2)
        # then
        assert future_receipt_1.done() is False
        assert self.token.balance_of(self.second_address) == Wad(0)

        # when
        self.web3.eth.sendTransaction = original_send_transaction
        self.web3.eth.getTransaction = original_get_transaction
        # and
        transact_2 = self.token.transfer(self.third_address, Wad(700))
        # FIXME: Ganache produces a "the tx doesn't have the correct nonce" error.
        future_receipt_2 = asyncio.ensure_future(
            transact_2.transact_async(replace=transact_1,
                                      gas_price=FixedGasPrice(
                                          150000, None, None)))
        # and
        await asyncio.sleep(2)
        # then
        assert transact_1.status == TransactStatus.FINISHED
        assert future_receipt_1.done()
        assert future_receipt_1.result() is None
        # and
        assert transact_2.status == TransactStatus.FINISHED
        assert future_receipt_2.done()
        assert isinstance(future_receipt_2.result(), Receipt)
        assert future_receipt_2.result().successful is True
        # and
        assert self.token.balance_of(self.second_address) == Wad(0)
        assert self.token.balance_of(self.third_address) == Wad(700)
Example #6
0
    def test_custom_gas_price_async(self):
        # given
        gas_price = FixedGasPrice(25000000200)

        # when
        synchronize([self.token.transfer(self.second_address, Wad(500)).transact_async(gas_price=gas_price)])

        # then
        assert self.web3.eth.getBlock('latest', full_transactions=True).transactions[0].gasPrice == gas_price.gas_price
Example #7
0
    async def test_recover_pending_tx(self, other_address):
        # given
        low_gas = FixedGasPrice(1)
        await self.token.transfer(other_address,
                                  Wad(5)).transact_async(gas_price=low_gas)
        await asyncio.sleep(0.5)

        # when
        pending = get_pending_transactions(self.web3)

        # and
        assert len(pending) == 1
        recovered: RecoveredTransact = pending[0]
        high_gas = FixedGasPrice(int(1 * FixedGasPrice.GWEI))
        recovered.cancel(high_gas)

        # then
        assert get_pending_transactions(self.web3) == []
Example #8
0
    def test_custom_gas_price(self):
        # given
        gas_price = FixedGasPrice(25000000100, None, None)

        # when
        self.token.transfer(self.second_address,
                            Wad(500)).transact(gas_strategy=gas_price)

        # then
        assert self.web3.eth.getBlock(
            'latest', full_transactions=True
        ).transactions[0].gasPrice == gas_price.gas_price
Example #9
0
def test_direct_approval_should_obey_gas_price():
    # given
    global web3, our_address, second_address, token

    # when
    directly(gas_strategy=FixedGasPrice(25000000000))(token, second_address,
                                                      "some-name")

    # then
    assert web3.eth.getBlock(
        'latest',
        full_transactions=True).transactions[0].gasPrice == 25000000000
Example #10
0
def test_via_tx_manager_approval_should_obey_gas_price():
    # given
    global web3, our_address, second_address, token
    tx = TxManager.deploy(web3)

    # when
    via_tx_manager(tx, gas_strategy=FixedGasPrice(15000000000))(token,
                                                                second_address,
                                                                "some-name")

    # then
    assert web3.eth.getBlock(
        'latest',
        full_transactions=True).transactions[0].gasPrice == 15000000000
Example #11
0
 def create_gas_price(arguments) -> GasPrice:
     if arguments.smart_gas_price:
         return SmartGasPrice()
     elif arguments.gas_price_file:
         return GasPriceFile(arguments.gas_price_file)
     elif arguments.gas_price:
         if arguments.gas_price_increase is not None:
             return IncreasingGasPrice(initial_price=arguments.gas_price,
                                       increase_by=arguments.gas_price_increase,
                                       every_secs=arguments.gas_price_increase_every,
                                       max_price=arguments.gas_price_max)
         else:
             return FixedGasPrice(arguments.gas_price)
     else:
         return DefaultGasPrice()
Example #12
0
    def test_gas_price_should_stay_the_same(self):
        # given
        value = 9000000000
        fixed_gas_price = FixedGasPrice(value)

        # expect
        assert fixed_gas_price.get_gas_price(0) == value
        assert fixed_gas_price.get_gas_price(1) == value
        assert fixed_gas_price.get_gas_price(2) == value
        assert fixed_gas_price.get_gas_price(5) == value
        assert fixed_gas_price.get_gas_price(60) == value
        assert fixed_gas_price.get_gas_price(120) == value
        assert fixed_gas_price.get_gas_price(600) == value
        assert fixed_gas_price.get_gas_price(1000000) == value
Example #13
0
    def get_gas_price(self, time_elapsed: int) -> Optional[int]:
        assert(isinstance(time_elapsed, int))

        config = self.reloadable_config.get_config()
        gas_price = config.get('gasPrice', None)
        gas_price_increase = config.get('gasPriceIncrease', None)
        gas_price_increase_every = config.get('gasPriceIncreaseEvery', None)
        gas_price_max = config.get('gasPriceMax', None)

        if gas_price is not None:
            if gas_price_increase and gas_price_increase_every:
                strategy = IncreasingGasPrice(gas_price, gas_price_increase, gas_price_increase_every, gas_price_max)
            else:
                strategy = FixedGasPrice(gas_price)
        else:
            strategy = DefaultGasPrice()

        return strategy.get_gas_price(time_elapsed=time_elapsed)
Example #14
0
    def test_gas_price_should_be_updated_by_update_gas_price_method(self):
        # given
        value1 = 9000000000
        value2 = 16000000000

        # and
        fixed_gas_price = FixedGasPrice(value1)

        # and
        assert fixed_gas_price.get_gas_price(0) == value1
        assert fixed_gas_price.get_gas_price(1) == value1
        assert fixed_gas_price.get_gas_price(2) == value1
        assert fixed_gas_price.get_gas_price(5) == value1

        # when
        fixed_gas_price.update_gas_price(value2)

        # then
        assert fixed_gas_price.get_gas_price(60) == value2
        assert fixed_gas_price.get_gas_price(120) == value2
        assert fixed_gas_price.get_gas_price(600) == value2
Example #15
0
    def test_mint_token_pool(self, position_manager_helpers):
        """ Integration test to mint a pool with two ERC20 tokens """
        # create pool
        position_manager_helper = position_manager_helpers(
            self.web3, self.position_manager,
            self.NonfungiblePositionManager_abi, self.token_dai,
            self.token_usdc)
        pool = position_manager_helper.create_and_initialize_pool(
            self.get_starting_sqrt_ratio(1, 1), FEES.LOW.value)

        # generate MintParam
        mint_params = position_manager_helper.generate_mint_params(
            pool, Position(pool, -10, 10, 10), self.our_address,
            Fraction(1, 100))

        # mint new position
        gas_price = FixedGasPrice(gas_price=20000000000000000)
        mint_receipt = self.position_manager.mint(mint_params).transact(
            gas_price=gas_price)
        assert mint_receipt is not None and mint_receipt.successful
Example #16
0
    def __init__(self, args: list, **kwargs):
        parser = argparse.ArgumentParser("chief-keeper")
        self.add_arguments(parser)
        parser.set_defaults(cageFacilitated=False)
        self.arguments = parser.parse_args(args)

        provider = HTTPProvider(
            endpoint_uri=self.arguments.rpc_host,
            request_kwargs={'timeout': self.arguments.rpc_timeout})
        self.web3: Web3 = kwargs['web3'] if 'web3' in kwargs else Web3(
            provider)

        self.web3.eth.defaultAccount = self.arguments.eth_from
        register_keys(self.web3, self.arguments.eth_key)
        self.our_address = Address(self.arguments.eth_from)

        if self.arguments.dss_deployment_file:
            self.dss = DssDeployment.from_json(
                web3=self.web3,
                conf=open(self.arguments.dss_deployment_file, "r").read())
        else:
            self.dss = DssDeployment.from_node(web3=self.web3)

        self.deployment_block = self.arguments.chief_deployment_block

        self.max_errors = self.arguments.max_errors
        self.errors = 0

        self.confirmations = 0

        if self.arguments.fixed_gas_price is not None and self.arguments.fixed_gas_price > 0:
            self.gas_price_strategy = FixedGasPrice(
                gas_price=int(round(self.arguments.fixed_gas_price *
                                    self.GWEI)))
        else:
            self.gas_price_strategy = DefaultGasPrice()

        setup_logging(self.arguments)
 def gas_price(self):
     """ FixedGasPrice if gas_price argument present, otherwise node DefaultGasPrice """
     if self.arguments.gas_price > 0:
         return FixedGasPrice(self.arguments.gas_price)
     else:
         return DefaultGasPrice()
Example #18
0
        1]  # ex: 0x0000000000000000000000000000000aBcdef123
    if len(sys.argv) > 2:
        register_keys(
            web3, [sys.argv[2]]
        )  # ex: key_file=~keys/default-account.json,pass_file=~keys/default-account.pass
        transact = True
    our_address = Address(web3.eth.defaultAccount)
    stuck_txes_to_submit = int(sys.argv[3]) if len(sys.argv) > 3 else 0
else:
    our_address = None
    stuck_txes_to_submit = 0

GWEI = 1000000000
base_fee = int(web3.eth.get_block('pending')['baseFeePerGas'])
# Uses a type 0 TX
low_gas_type0 = FixedGasPrice(gas_price=base_fee, max_fee=None, tip=None)
# Forces a type 2 TX (erroring out if not supported by node)
tip = 1 * GWEI
low_gas_type2 = FixedGasPrice(gas_price=None,
                              max_fee=int(base_fee * 0.9) + tip,
                              tip=tip)
# Favors a type 2 TX if the node supports it, otherwise falls back to a type 0 TX
low_gas_nodechoice = FixedGasPrice(low_gas_type0.gas_price,
                                   low_gas_type2.max_fee, low_gas_type2.tip)
low_gas = low_gas_nodechoice
print(f"Base fee is {base_fee/GWEI}; using {low_gas} for low gas")


def get_pending_transactions(web3: Web3, address: Address = None) -> list:
    """Retrieves a list of pending transactions from the mempool.
 def gas_price(self):
     if self.arguments.gas_price > 0:
         return FixedGasPrice(self.arguments.gas_price)
     else:
         return DefaultGasPrice()