def cancel_orders(self, orders):
     """Cancel orders asynchronously."""
     synchronize([
         self.otc.kill(
             order.order_id).transact_async(gas_price=self.gas_price)
         for order in orders
     ])
Ejemplo n.º 2
0
    def test_should_raise_exception_on_unknown_kwarg(self):
        # expect
        with pytest.raises(Exception):
            self.token.transfer(self.second_address, Wad(123)).transact(unknown_kwarg="some_value")

        # expect
        with pytest.raises(Exception):
            synchronize([self.token.transfer(self.second_address, Wad(123)).transact_async(unknown_kwarg="some_value")])
Ejemplo n.º 3
0
 def test_gas_and_gas_buffer_not_allowed_at_the_same_time_async(self):
     # expect
     with pytest.raises(Exception):
         synchronize([
             self.token.transfer(self.second_address,
                                 Wad(500)).transact_async(
                                     gas=129995, gas_buffer=3000000)
         ])
Ejemplo n.º 4
0
    def test_transfer_out_of_gas_async(self):
        # when
        with pytest.raises(Exception):
            synchronize([self.token.transfer(self.second_address, Wad(500)).transact_async(gas=26000)])[0]

        # then
        assert self.token.balance_of(self.our_address) == Wad(1000000)
        assert self.token.balance_of(self.second_address) == Wad(0)
Ejemplo n.º 5
0
 def top_up_bands(self, our_orders: list, buy_bands: list, sell_bands: list,
                  target_price: Wad):
     """Asynchronously create new buy and sell orders in all send and buy bands if necessary."""
     synchronize([
         transact.transact_async(gas_price=self.gas_price)
         for transact in itertools.chain(
             self.top_up_buy_bands(our_orders, buy_bands, target_price),
             self.top_up_sell_bands(our_orders, sell_bands, target_price))
     ])
Ejemplo n.º 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
Ejemplo n.º 7
0
    def test_default_gas_async(self):
        # when
        receipt = synchronize([self.token.transfer(self.second_address, Wad(500)).transact_async()])[0]

        # then
        # [token transfer costs ~50k gas, we should add a 100k buffer by default which puts in the (100k, 200k) range]
        assert 100000 <= self.web3.eth.getTransaction(receipt.transaction_hash)['gas'] <= 200000
Ejemplo n.º 8
0
    def test_transfer_failed_async(self):
        # when
        receipt = synchronize([self.token.transfer(self.second_address, Wad(5000000)).transact_async()])[0]

        # then
        assert receipt is None
        assert self.token.balance_of(self.our_address) == Wad(1000000)
        assert self.token.balance_of(self.second_address) == Wad(0)
Ejemplo n.º 9
0
    def test_transfer_async(self):
        # when
        receipt = synchronize([self.token.transfer(self.second_address, Wad(750)).transact_async()])

        # then
        assert receipt is not None
        assert self.token.balance_of(self.our_address) == Wad(999250)
        assert self.token.balance_of(self.second_address) == Wad(750)
Ejemplo n.º 10
0
    def test_custom_gas_async(self):
        # when
        receipt = synchronize([
            self.token.transfer(self.second_address,
                                Wad(500)).transact_async(gas=129995)
        ])[0]

        # then
        assert self.web3.eth.getTransaction(
            receipt.transaction_hash)['gas'] == 129995
Ejemplo n.º 11
0
    def test_default_gas_async(self):
        # when
        receipt = synchronize([
            self.token.transfer(self.second_address,
                                Wad(500)).transact_async()
        ])[0]

        # then
        assert 100000 <= self.web3.eth.getTransaction(
            receipt.transaction_hash)['gas'] <= 1200000
    def create_orders(self, new_orders):
        """Create orders asynchronously."""
        def to_transaction(new_order: NewOrder):
            assert (isinstance(new_order, NewOrder))

            if new_order.is_sell:
                return self.otc.make(pay_token=self.token_sell().address,
                                     pay_amount=new_order.pay_amount,
                                     buy_token=self.token_buy().address,
                                     buy_amount=new_order.buy_amount)
            else:
                return self.otc.make(pay_token=self.token_buy().address,
                                     pay_amount=new_order.pay_amount,
                                     buy_token=self.token_sell().address,
                                     buy_amount=new_order.buy_amount)

        synchronize([
            transaction.transact_async(gas_price=self.gas_price)
            for transaction in map(to_transaction, new_orders)
        ])
Ejemplo n.º 13
0
    def transact(self, **kwargs) -> Optional[Receipt]:
        """Executes the Ethereum transaction synchronously.

        Executes the Ethereum transaction synchronously. The method will block until the
        transaction gets mined i.e. it will return when either the transaction execution
        succeeded or failed. In case of the former, a :py:class:`pymaker.Receipt`
        object will be returned.

        Out-of-gas exceptions are automatically recognized as transaction failures.

        Allowed keyword arguments are: `from_address`, `replace`, `gas`, `gas_buffer`, `gas_price`.
        `gas_price` needs to be an instance of a class inheriting from :py:class:`pymaker.gas.GasPrice`.
        `from_address` needs to be an instance of :py:class:`pymaker.Address`.

        The `gas` keyword argument is the gas limit for the transaction, whereas `gas_buffer`
        specifies how much gas should be added to the estimate. They can not be present
        at the same time. If none of them are present, a default buffer is added to the estimate.

        Returns:
            A :py:class:`pymaker.Receipt` object if the transaction invocation was successful.
            `None` otherwise.
        """
        return synchronize([self.transact_async(**kwargs)])[0]
 def cancel_orders(self, orders: list):
     synchronize([self.otc.kill(order.order_id).transact_async(gas_price=self.gas_price()) for order in orders])
Ejemplo n.º 15
0
 def cancel(self, gas_price: GasPrice):
     return synchronize([self.cancel_async(gas_price)])[0]
Ejemplo n.º 16
0
def test_synchronize_should_pass_exceptions():
    with pytest.raises(Exception):
        synchronize([async_return(1), async_exception(), async_return(3)])
Ejemplo n.º 17
0
def test_synchronize_should_return_results_of_all_async_calls():
    assert synchronize([async_return(1)]) == [1]
    assert synchronize([async_return(1), async_return(2)]) == [1, 2]
    assert synchronize([async_return(1), async_return(2), async_return(3)]) == [1, 2, 3]
Ejemplo n.º 18
0
def test_synchronize_should_return_empty_list_for_no_futures():
    assert synchronize([]) == []
Ejemplo n.º 19
0
 def cancel(self):
     return synchronize([self.cancel_async()])[0]
Ejemplo n.º 20
0
 def cancel_orders(self, orders: list):
     synchronize([
         self.otc.kill(
             order.order_id).transact_async(gas_price=self.gas_price())
         for order in orders
     ])