Exemple #1
0
    def get_offer(self, offer_id: int) -> Optional[OfferInfo]:
        """Get the offer details.

        Args:
            offer_id: The id of the offer to get the details of.

        Returns:
            An instance of `OfferInfo` if the offer is still active, or `None` if the offer has been
            already completely taken.
        """

        # if an offer is None, it won't become not-None again for the same OTC instance
        if offer_id in self._none_offers:
            return None

        array = self._contract.call().offers(offer_id)
        if array[5] is not True:
            self._none_offers.add(offer_id)
            return None
        else:
            return OfferInfo(offer_id=offer_id,
                             sell_how_much=Wad(array[0]),
                             sell_which_token=Address(array[1]),
                             buy_how_much=Wad(array[2]),
                             buy_which_token=Address(array[3]),
                             owner=Address(array[4]),
                             timestamp=array[6])
    def test_transfer(self):
        # when
        self.token.transfer(self.second_address, Wad(500))

        # then
        self.token.balance_of(self.our_address) == Wad(999500)
        self.token.balance_of(self.second_address) == Wad(500)
 def __init__(self, args):
     self.token_get = Address(args['tokenGet'])
     self.amount_get = Wad(args['amountGet'])
     self.token_give = Address(args['tokenGive'])
     self.amount_give = Wad(args['amountGive'])
     self.expires = args['expires']
     self.nonce = args['nonce']
     self.user = Address(args['user'])
    def test_cups(self, sai: SaiDeployment):
        # when
        sai.tub.open()

        # then
        assert sai.tub.cups(1).art == Wad(0)
        assert sai.tub.cups(1).ink == Wad(0)
        assert sai.tub.cups(1).lad == sai.our_address
Exemple #5
0
 def __init__(self, args):
     self.id = bytes_to_int(args['id'])
     self.maker = Address(args['maker'])
     self.have_token = Address(args['haveToken'])
     self.have_amount = Wad(args['haveAmount'])
     self.want_token = Address(args['wantToken'])
     self.want_amount = Wad(args['wantAmount'])
     self.timestamp = args['timestamp']
    def test_burn(self):
        # given
        self.dstoken.mint(Wad(100000))

        # when
        self.dstoken.burn(Wad(40000))

        # then
        assert self.dstoken.balance_of(self.our_address) == Wad(60000)
    def test_withdraw(self):
        # given
        self.dsethtoken.deposit(Wad(100000))

        # when
        self.dsethtoken.withdraw(Wad(40000))

        # then
        assert self.dsethtoken.balance_of(self.our_address) == Wad(60000)
Exemple #8
0
    def withdraw_everything(self):
        eth_balance = self.etherdelta.balance_of(self.our_address)
        if eth_balance > Wad(0):
            self.etherdelta.withdraw(eth_balance)

        sai_balance = self.etherdelta.balance_of_token(self.sai.address,
                                                       self.our_address)
        if sai_balance > Wad(0):
            self.etherdelta.withdraw_token(self.sai.address, sai_balance)
Exemple #9
0
 def _net_value(self, transfer: Transfer, our_address: Address):
     if transfer.from_address == our_address and transfer.to_address == our_address:
         return Wad(0)
     elif transfer.from_address == our_address:
         return Wad(0) - transfer.value
     elif transfer.to_address == our_address:
         return transfer.value
     else:
         return Wad(0)
 def prepare_balances(self):
     recipient = Address('0x002ca7F9b416B2304cDd20c26882d1EF5c53F611')
     if self.sai.balance_of(self.our_address) > Wad(0):
         self.sai.transfer(recipient, self.sai.balance_of(self.our_address))
     if self.skr.balance_of(self.our_address) > Wad(0):
         self.skr.transfer(recipient, self.skr.balance_of(self.our_address))
     if self.gem.balance_of(self.our_address) > Wad.from_number(0.5):
         self.gem.transfer(
             recipient,
             self.gem.balance_of(self.our_address) - Wad.from_number(0.5))
Exemple #11
0
 def deposit_for_buy_orders(self):
     order_total = self.total_amount(self.our_buy_orders())
     currently_deposited = self.etherdelta.balance_of(self.our_address)
     if order_total > currently_deposited:
         depositable_eth = Wad.max(
             self.eth_balance(self.our_address) - self.eth_reserve, Wad(0))
         additional_deposit = Wad.min(order_total - currently_deposited,
                                      depositable_eth)
         if additional_deposit > Wad(0):
             self.etherdelta.deposit(additional_deposit)
Exemple #12
0
 def test_multiply_by_ray(self):
     assert Wad.from_number(2) * Ray.from_number(3) == Wad.from_number(6)
     assert Wad.from_number(2) * Ray(3) == Wad(0)
     assert Wad(2) * Ray(499999999999999999999999999) == Wad(0)
     assert Wad(2) * Ray(500000000000000000000000000) == Wad(1)
     assert Wad(2) * Ray(999999999999999999999999999) == Wad(1)
     assert Wad(2) * Ray(1000000000000000000000000000) == Wad(2)
 def required_top_up(self, cup):
     pro = cup.ink * self.tub.tag()
     tab = self.tub.tab(cup.cup_id)
     if tab > Wad(0):
         current_ratio = Ray(pro / tab)
         if current_ratio < self.minimum_ratio:
             return tab * (Wad(self.target_ratio - current_ratio) /
                           self.tub.tag())
         else:
             return None
     else:
         return None
 def from_json(data: dict):
     assert (isinstance(data, dict))
     return OffChainOrder(token_get=Address(data['tokenGet']),
                          amount_get=Wad(int(data['amountGet'])),
                          token_give=Address(data['tokenGive']),
                          amount_give=Wad(int(data['amountGive'])),
                          expires=int(data['expires']),
                          nonce=int(data['nonce']),
                          v=int(data['v']),
                          r=hexstring_to_bytes(data['r']),
                          s=hexstring_to_bytes(data['s']),
                          user=Address(data['user']))
Exemple #15
0
 def test_divide(self):
     assert Wad.from_number(4) / Wad.from_number(2) == Wad.from_number(2)
     assert Wad(4) / Wad.from_number(2) == Wad(2)
     assert Wad(3) / Wad.from_number(2) == Wad(1)
     assert Wad(39) / Wad.from_number(20) == Wad(1)
     assert Wad(40) / Wad.from_number(20) == Wad(2)
     assert Wad.from_number(0.2) / Wad.from_number(0.1) == Wad.from_number(2)
 def __init__(self, auction_manager, auctionlet_id, auctionlet_info,
              is_expired):
     self._auction_manager = auction_manager
     self._auction = None
     self.auctionlet_id = auctionlet_id
     self.auction_id = auctionlet_info[0]
     self.last_bidder = Address(auctionlet_info[1])
     self.last_bid_time = datetime.datetime.fromtimestamp(
         auctionlet_info[2])
     self.buy_amount = Wad(auctionlet_info[3])
     self.sell_amount = Wad(auctionlet_info[4])
     self.unclaimed = auctionlet_info[5]
     self.base = auctionlet_info[6]
     self.expired = is_expired
Exemple #17
0
    def test_should_bid_on_newly_created_auctions(self):
        # given
        # (newly created auction)

        # when
        strategy = BasicForwardAuctionStrategy(self.dai_token, self.mkr_token,
                                               0.05, 0.5, Wad(1))
        result = strategy.perform(self.auctionlet, self.context)

        # then
        self.assertEqual(
            "Placed a new bid at 100.500000000000000000 MKR, bid was successful. Our maximum bid on this auctionlet is 200.000000000000000000 MKR",
            result.description)
        self.assertFalse(result.forget)
        self.auctionlet.bid.assert_called_once_with(Wad(100.5 * self.wei()))
 def __init__(self, auction_manager, auction_id, auction_info):
     self._auction_manager = auction_manager
     self.auction_id = auction_id
     self.creator = Address(auction_info[0])
     self.selling = ERC20Token(web3=auction_manager.web3,
                               address=Address(auction_info[1]))
     self.buying = ERC20Token(web3=auction_manager.web3,
                              address=Address(auction_info[2]))
     self.start_bid = Wad(auction_info[3])
     self.min_increase = auction_info[4]
     self.min_decrease = auction_info[5]
     self.sell_amount = Wad(auction_info[6])
     self.ttl = auction_info[7]
     self.reversed = auction_info[8]
     self.unsold = auction_info[9]
Exemple #19
0
    def test_should_overbid_competitors(self):
        # given
        self.auctionlet.buy_amount = Wad(50 * self.wei())
        self.auctionlet.last_bidder = self.competitor_address

        # when
        strategy = BasicForwardAuctionStrategy(self.dai_token, self.mkr_token,
                                               0.05, 0.5, Wad(1))
        result = strategy.perform(self.auctionlet, self.context)

        # then
        self.assertEqual(
            "Placed a new bid at 125.000000000000000000 MKR, bid was successful. Our maximum bid on this auctionlet is 200.000000000000000000 MKR",
            result.description)
        self.assertFalse(result.forget)
        self.auctionlet.bid.assert_called_once_with(Wad(125 * self.wei()))
 def setup_method(self):
     self.web3 = Web3(EthereumTesterProvider())
     self.web3.eth.defaultAccount = self.web3.eth.accounts[0]
     self.our_address = Address(self.web3.eth.defaultAccount)
     self.second_address = Address(self.web3.eth.accounts[1])
     self.token = DSToken.deploy(self.web3, 'ABC')
     self.token.mint(Wad(1000000))
Exemple #21
0
    def test_should_not_bid_over_max_price(self):
        # given
        self.auctionlet.buy_amount = Wad(200 * self.wei())
        self.auctionlet.last_bidder = self.competitor_address

        # when
        strategy = BasicForwardAuctionStrategy(self.dai_token, self.mkr_token,
                                               0.05, 0.5, Wad(1))
        result = strategy.perform(self.auctionlet, self.context)

        # then
        self.assertEqual(
            "Our maximum possible bid (200.000000000000000000 MKR) reached",
            result.description)
        self.assertFalse(result.forget)
        self.auctionlet.bid.assert_not_called()
Exemple #22
0
 def create_new_sell_order(self):
     """If our SAI engagement is below the minimum amount, create a new offer up to the maximum amount"""
     total_amount = self.total_amount(self.our_sell_orders())
     if total_amount < self.min_sai_amount:
         our_balance = self.sai.balance_of(
             self.our_address) + self.etherdelta.balance_of_token(
                 self.sai.address, self.our_address)
         have_amount = Wad.min(self.max_sai_amount,
                               our_balance) - total_amount
         if have_amount > Wad(0):
             want_amount = self.fix_amount(
                 have_amount * self.apply_sell_margin(
                     self.target_rate(), self.avg_margin))
             if self.offchain:
                 self.etherdelta.place_order_offchain(
                     token_get=EtherDelta.ETH_TOKEN,
                     amount_get=want_amount,
                     token_give=self.sai.address,
                     amount_give=have_amount,
                     expires=self.web3.eth.blockNumber + self.order_age)
             else:
                 self.etherdelta.place_order_onchain(
                     token_get=EtherDelta.ETH_TOKEN,
                     amount_get=want_amount,
                     token_give=self.sai.address,
                     amount_give=have_amount,
                     expires=self.web3.eth.blockNumber + self.order_age)
Exemple #23
0
 def total_supply(self) -> Wad:
     """Returns the total supply of the token.
     
     Returns:
         The total supply of the token.
     """
     return Wad(self._contract.call().totalSupply())
    def close_position(self):
        cup_id = 8

        # (1) Exchange our ETH to SAI as we want to repay our SAI debt
        our_entire_gem = self.gem.balance_of(self.our_address)
        conversion = self.gem_to_sai_conversion()
        sequence = Sequence([conversion])
        if our_entire_gem > Wad.from_number(0.0001):
            sequence.set_amounts(our_entire_gem)
            receipt = sequence.steps[0].execute()
            if receipt:
                logging.info(receipt.transfers)

        self.print_balances()

        our_debt = self.tub.tab(cup_id)
        print(our_debt)
        our_sai_balanace = self.sai.balance_of(self.our_address)
        if our_sai_balanace < our_debt:
            logging.info("NOT ENOUGH SAI TO REPAY OUR DEBT!")
            exit(-1)

        # (2) Repay the debt and get back our SKR
        # some surplus of SAI will be left, this is the profit we made
        # self.tub.wipe(cup_id, Wad.from_number(1))
        logging.info(self.tub.shut(cup_id))
        self.print_balances()

        # (3) Exchange SKR back to ETH
        if self.skr.balance_of(self.our_address) > Wad(0):
            logging.info(self.tub.exit(self.skr.balance_of(self.our_address)))
            self.print_balances()
Exemple #25
0
    def test_should_still_bid_if_minimal_bid_is_equal_to_max_price(self):
        # given
        self.auctionlet.buy_amount = Wad(50 * self.wei())
        self.auctionlet.last_bidder = self.competitor_address

        # when
        strategy = BasicForwardAuctionStrategy(self.dai_token,
                                               self.mkr_token, 0.05, 0.5,
                                               Wad(200 * self.wei()))
        result = strategy.perform(self.auctionlet, self.context)

        # then
        self.assertEqual(
            "Placed a new bid at 200.000000000000000000 MKR, bid was successful",
            result.description)
        self.assertFalse(result.forget)
        self.auctionlet.bid.assert_called_once_with(Wad(200 * self.wei()))
Exemple #26
0
    def test_should_fail_if_unable_to_bid(self):
        # given
        self.auctionlet.buy_amount = Wad(50 * self.wei())
        self.auctionlet.last_bidder = self.competitor_address
        self.auctionlet.bid = MagicMock(return_value=False)

        # when
        strategy = BasicForwardAuctionStrategy(self.dai_token, self.mkr_token,
                                               0.05, 0.5, Wad(1))
        result = strategy.perform(self.auctionlet, self.context)

        # then
        self.assertEqual(
            "Tried to place a new bid at 125.000000000000000000 MKR, but the bid failed",
            result.description)
        self.assertFalse(result.forget)
        self.auctionlet.bid.assert_called_once_with(Wad(125 * self.wei()))
    def test_cork_and_hat(self, sai: SaiDeployment):
        # given
        assert sai.tub.hat() == Wad(0)

        # when
        sai.tub.cork(Wad.from_number(150000))

        # then
        assert sai.tub.hat() == Wad.from_number(150000)
    def test_join_and_exit(self, sai: SaiDeployment):
        # given
        assert sai.skr.balance_of(sai.our_address) == Wad(0)
        assert sai.skr.total_supply() == Wad(0)

        # when
        sai.tub.join(Wad.from_number(5))

        # then
        assert sai.skr.balance_of(sai.our_address) == Wad.from_number(5)
        assert sai.skr.total_supply() == Wad.from_number(5)

        # when
        sai.tub.exit(Wad.from_number(4))

        # then
        assert sai.skr.balance_of(sai.our_address) == Wad.from_number(1)
        assert sai.skr.total_supply() == Wad.from_number(1)
Exemple #29
0
    def test_should_still_bid_if_available_balance_equal_to_minimal_bid(self):
        # given
        self.auctionlet.buy_amount = Wad(50 * self.wei())
        self.auctionlet.last_bidder = self.competitor_address
        self.auctionlet.can_split = MagicMock(return_value=False)
        self.set_mkr_balance(Wad(60 * self.wei()))

        # when
        strategy = BasicForwardAuctionStrategy(self.dai_token, self.mkr_token,
                                               0.05, 0.5, Wad(60 * self.wei()))
        result = strategy.perform(self.auctionlet, self.context)

        # then
        self.assertEqual(
            "Placed a new bid at 60.000000000000000000 MKR, bid was successful. Our maximum bid on this auctionlet is 200.000000000000000000 MKR",
            result.description)
        self.assertFalse(result.forget)
        self.auctionlet.bid.assert_called_once_with(Wad(60 * self.wei()))
Exemple #30
0
    def test_should_fail_if_unable_to_raise_allowance(self):
        # given
        self.auctionlet.buy_amount = Wad(50 * self.wei())
        self.auctionlet.last_bidder = self.competitor_address
        self.set_mkr_allowance(Wad(52 * self.wei()))
        self.mkr_token.approve = MagicMock(return_value=False)

        # when
        strategy = BasicForwardAuctionStrategy(self.dai_token, self.mkr_token,
                                               0.05, 0.5, Wad(1))
        result = strategy.perform(self.auctionlet, self.context)

        # then
        self.assertEqual(
            "Tried to raise MKR allowance, but the attempt failed",
            result.description)
        self.assertFalse(result.forget)
        self.auctionlet.bid.assert_not_called()