def execute_opportunity_step_by_step(self, opportunity: Sequence):
        """Execute the opportunity step-by-step."""
        def incoming_transfer(our_address: Address):
            return lambda transfer: transfer.to_address == our_address

        def outgoing_transfer(our_address: Address):
            return lambda transfer: transfer.from_address == our_address

        all_transfers = []
        for step in opportunity.steps:
            receipt = step.transact().transact(gas_price=self.gas_price())
            if receipt:
                all_transfers += receipt.transfers
                outgoing = TransferFormatter().format(
                    filter(outgoing_transfer(self.our_address),
                           receipt.transfers), self.token_name)
                incoming = TransferFormatter().format(
                    filter(incoming_transfer(self.our_address),
                           receipt.transfers), self.token_name)
                self.logger.info(f"Exchanged {outgoing} to {incoming}")
            else:
                self.errors += 1
                return
        self.logger.info(
            f"The profit we made is {TransferFormatter().format_net(all_transfers, self.our_address, self.token_name)}"
        )
def test_support_iterators(token1, some_address):
    # given
    transfer = Transfer(token1, some_address, some_address,
                        Wad.from_number(11.5))

    # expect
    assert TransferFormatter().format(
        iter([transfer]), lambda a: str(a)
    ) == "11.500000000000000000 0x0101010101010101010101010101010101010101"
def test_should_format_single_transfer(token1, some_address):
    # given
    transfer = Transfer(token1, some_address, some_address,
                        Wad.from_number(105))

    # expect
    assert TransferFormatter().format(
        [transfer], lambda a: str(a)
    ) == "105.000000000000000000 0x0101010101010101010101010101010101010101"
def test_should_format_net_balances(token1, our_address, some_address):
    # given
    transfer1 = Transfer(token1, our_address, some_address,
                         Wad.from_number(15))
    transfer2 = Transfer(token1, some_address, our_address,
                         Wad.from_number(17))

    # expect
    assert TransferFormatter().format_net([transfer1, transfer2], our_address, lambda a: str(a)) \
        == "2.000000000000000000 0x0101010101010101010101010101010101010101"
def test_should_format_two_different_tokens(token1, token2, some_address):
    # given
    transfer1 = Transfer(token1, some_address, some_address,
                         Wad.from_number(105))
    transfer2 = Transfer(token2, some_address, some_address,
                         Wad.from_number(17))

    # expect
    assert TransferFormatter().format([transfer1, transfer2], lambda a: str(a)) \
        == "105.000000000000000000 0x0101010101010101010101010101010101010101 and 17.000000000000000000 0x0202020202020202020202020202020202020202"
def test_should_format_net_balances_excluding_transfers_between_us(
        token1, our_address, some_address):
    # given
    transfer1 = Transfer(token1, some_address, our_address, Wad.from_number(4))
    transfer2 = Transfer(token1, our_address, some_address,
                         Wad.from_number(1.5))
    transfer3 = Transfer(token1, our_address, our_address, Wad.from_number(50))

    # expect
    assert TransferFormatter().format_net([transfer1, transfer2, transfer3], our_address, lambda a: str(a)) \
        == "2.500000000000000000 0x0101010101010101010101010101010101010101"
def test_should_format_net_balances_if_multiple_transfers(
        token1, our_address, some_address):
    # given
    transfer1 = Transfer(token1, our_address, some_address,
                         Wad.from_number(15))
    transfer2 = Transfer(token1, some_address, our_address,
                         Wad.from_number(17))
    transfer3 = Transfer(token1, some_address, our_address,
                         Wad.from_number(3.5))

    # expect
    assert TransferFormatter().format_net([transfer1, transfer2, transfer3], our_address) \
        == "5.500000000000000000 0x0101010101010101010101010101010101010101"
def test_should_not_include_zeros_in_net_balances(token1, token2, our_address,
                                                  some_address):
    # given
    transfer1 = Transfer(token1, our_address, some_address,
                         Wad.from_number(15))
    transfer2 = Transfer(token1, some_address, our_address,
                         Wad.from_number(17))
    transfer3 = Transfer(token2, our_address, some_address,
                         Wad.from_number(22.5))
    transfer4 = Transfer(token2, some_address, our_address,
                         Wad.from_number(22.5))

    # expect
    assert TransferFormatter().format_net([transfer1, transfer2, transfer3, transfer4], our_address, lambda a: str(a)) \
        == "2.000000000000000000 0x0101010101010101010101010101010101010101"
def test_should_format_net_balances_for_more_than_one_token(
        token1, token2, our_address, some_address):
    # given
    transfer1 = Transfer(token1, our_address, some_address,
                         Wad.from_number(15))
    transfer2 = Transfer(token1, some_address, our_address,
                         Wad.from_number(17))
    transfer3 = Transfer(token2, our_address, some_address,
                         Wad.from_number(2.5))
    transfer4 = Transfer(token2, some_address, our_address,
                         Wad.from_number(100))

    # expect
    assert TransferFormatter().format_net([transfer1, transfer2, transfer3, transfer4], our_address, lambda a: str(a)) \
        == "2.000000000000000000 0x0101010101010101010101010101010101010101 and 97.500000000000000000 0x0202020202020202020202020202020202020202"
def test_should_return_empty_string_when_no_transfers():
    # expect
    assert TransferFormatter().format([], lambda a: str(a)) == ""