<commit_before>#!/usr/bin/env python3
#
# This file is part of "maker.py".
#
# Copyright (C) 2017 reverendus
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
from api.Address import Address
from api.Ray import Ray
from api.Wad import Wad
from api.otc import SimpleMarket
from api.otc.OfferInfo import OfferInfo
from api.sai import Tub
from keepers.arbitrage.Conversion import Conversion


class OasisConversion(Conversion):
    def __init__(self, tub: Tub, market: SimpleMarket, offer: OfferInfo):
        self.tub = tub
        self.market = market
        self.offer = offer

        super().__init__(from_currency=self._currency_symbol(offer.buy_which_token.address),
                         to_currency=self._currency_symbol(offer.sell_which_token.address),
                         rate=Ray(offer.sell_how_much)/Ray(offer.buy_how_much),
                         min_from_amount=Wad.from_number(0),  #TODO will probably change after dust order limitation gets introduced
                         max_from_amount=offer.buy_how_much,
                         method=f"oasis-take-{self.offer.offer_id}")

    def _currency_symbol(self, address):
        if address == self.tub.sai():
            return "SAI"
        elif address == self.tub.skr():
            return "SKR"
        elif address == self.tub.gem():
            return "ETH"
        else:
            return "___"

    def perform(self):
        quantity = self.to_amount
        if quantity > self.offer.sell_how_much:
            quantity = self.offer.sell_how_much
        print(f"  Executing take({self.offer.offer_id}, '{quantity}') in order exchange {self.from_amount} {self.from_currency} to {self.to_amount} {self.to_currency}")
        take_result = self.market.take(self.offer.offer_id, quantity)
        if take_result:
            our_address = Address(self.tub.web3.eth.defaultAccount)
            incoming_transfer_on_take = next(filter(lambda transfer: transfer.token_address == self.offer.sell_which_token.address and transfer.to_address == our_address, take_result.transfers))
            outgoing_transfer_on_take = next(filter(lambda transfer: transfer.token_address == self.offer.buy_which_token.address and transfer.from_address == our_address, take_result.transfers))
            print(f"  Take was successful, exchanged {outgoing_transfer_on_take.value} {self.from_currency} to {incoming_transfer_on_take.value} {self.to_currency}")
            return take_result
        else:
            print(f"  Take failed!")
            return None<commit_msg>Condition added so no residual offers on OasisDEX are left<commit_after>#!/usr/bin/env python3
#
# This file is part of "maker.py".
#
# Copyright (C) 2017 reverendus
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
from api.Address import Address
from api.Ray import Ray
from api.Wad import Wad
from api.otc import SimpleMarket
from api.otc.OfferInfo import OfferInfo
from api.sai import Tub
from keepers.arbitrage.Conversion import Conversion


class OasisConversion(Conversion):
    def __init__(self, tub: Tub, market: SimpleMarket, offer: OfferInfo):
        self.tub = tub
        self.market = market
        self.offer = offer

        super().__init__(from_currency=self._currency_symbol(offer.buy_which_token.address),
                         to_currency=self._currency_symbol(offer.sell_which_token.address),
                         rate=Ray(offer.sell_how_much)/Ray(offer.buy_how_much),
                         min_from_amount=Wad.from_number(0),  #TODO will probably change after dust order limitation gets introduced
                         max_from_amount=offer.buy_how_much,
                         method=f"oasis-take-{self.offer.offer_id}")

    def _currency_symbol(self, address):
        if address == self.tub.sai():
            return "SAI"
        elif address == self.tub.skr():
            return "SKR"
        elif address == self.tub.gem():
            return "ETH"
        else:
            return "___"

    def perform(self):
        quantity = self.to_amount

        # if by any chance rounding makes us want to buy more quantity than is available,
        # we just buy the whole lot
        if quantity > self.offer.sell_how_much:
            quantity = self.offer.sell_how_much

        # if by any chance rounding makes us want to buy only slightly less than the available lot,
        # we buy everything as this is probably what we wanted in the first place
        if self.offer.sell_how_much - quantity < Wad.from_number(0.0000000001):
            quantity = self.offer.sell_how_much

        print(f"  Executing take({self.offer.offer_id}, '{quantity}') in order exchange {self.from_amount} {self.from_currency} to {self.to_amount} {self.to_currency}")
        take_result = self.market.take(self.offer.offer_id, quantity)
        if take_result:
            our_address = Address(self.tub.web3.eth.defaultAccount)
            incoming_transfer_on_take = next(filter(lambda transfer: transfer.token_address == self.offer.sell_which_token.address and transfer.to_address == our_address, take_result.transfers))
            outgoing_transfer_on_take = next(filter(lambda transfer: transfer.token_address == self.offer.buy_which_token.address and transfer.from_address == our_address, take_result.transfers))
            print(f"  Take was successful, exchanged {outgoing_transfer_on_take.value} {self.from_currency} to {incoming_transfer_on_take.value} {self.to_currency}")
            return take_result
        else:
            print(f"  Take failed!")
            return None
{'fraction_non_alphanumeric': 0.08321038654470345, 'fraction_numerical': 0.003983475951608144, 'mean_word_length': 2.985302763080541, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6712753', 'n_tokens_mistral': 1988, 'n_tokens_neox': 1853, 'n_words': 582}