def test_should_ignore_orders_owned_by_others(self, deployment: Deployment):
        # given
        keeper = OasisMarketMakerCancel(args=args(f"--eth-from {deployment.web3.eth.defaultAccount} "
                                             f"--oasis-address {deployment.otc.address}"),
                                        web3=deployment.web3)

        # and
        DSToken(web3=deployment.web3, address=deployment.gem.address).mint(Wad.from_number(1000)).transact()
        DSToken(web3=deployment.web3, address=deployment.sai.address).mint(Wad.from_number(1000)).transact()

        # and
        deployment.gem.transfer(Address(deployment.web3.eth.accounts[1]), Wad.from_number(500)).transact()
        deployment.sai.transfer(Address(deployment.web3.eth.accounts[1]), Wad.from_number(500)).transact()

        # and
        deployment.otc.approve([deployment.gem, deployment.sai], directly())
        deployment.otc.make(deployment.gem.address, Wad.from_number(10), deployment.sai.address, Wad.from_number(5)).transact()

        # and
        deployment.web3.eth.defaultAccount = deployment.web3.eth.accounts[1]
        deployment.otc.approve([deployment.gem, deployment.sai], directly())
        deployment.otc.make(deployment.sai.address, Wad.from_number(5), deployment.gem.address, Wad.from_number(12)).transact()
        deployment.web3.eth.defaultAccount = deployment.web3.eth.accounts[0]

        # and
        assert len(deployment.otc.get_orders()) == 2

        # when
        keeper.main()

        # then
        assert len(deployment.otc.get_orders()) == 1
        assert deployment.otc.get_orders()[0].maker == Address(deployment.web3.eth.accounts[1])
Example #2
0
    def test_should_execute_arbitrage_in_one_transaction_if_tx_manager_configured(
            self, deployment: Deployment):
        # given
        tx_manager = TxManager.deploy(deployment.web3)

        # and
        keeper = ArbitrageKeeper(args=args(
            f"--eth-from {deployment.our_address.address}"
            f" --tub-address {deployment.tub.address}"
            f" --tap-address {deployment.tap.address}"
            f" --oasis-address {deployment.otc.address}"
            f" --base-token {deployment.sai.address}"
            f" --min-profit 13.0 --max-engagement 100.0"
            f" --tx-manager {tx_manager.address}"),
                                 web3=deployment.web3)

        # and
        DSValue(web3=deployment.web3,
                address=deployment.tub.pip()).poke_with_int(
                    Wad.from_number(500).value).transact()
        deployment.tub.mold_gap(Wad.from_number(1.05)).transact()
        deployment.tub.join(Wad.from_number(1000)).transact()
        deployment.tap.mold_gap(Wad.from_number(1.05)).transact()

        # and
        deployment.sai.mint(Wad.from_number(1000)).transact()

        # and
        deployment.otc.approve(
            [deployment.gem, deployment.sai, deployment.skr], directly())
        deployment.otc.add_token_pair_whitelist(
            deployment.sai.address, deployment.skr.address).transact()
        deployment.otc.add_token_pair_whitelist(
            deployment.skr.address, deployment.gem.address).transact()
        deployment.otc.add_token_pair_whitelist(
            deployment.gem.address, deployment.sai.address).transact()
        deployment.otc.make(deployment.skr.address,
                            Wad.from_number(105), deployment.sai.address,
                            Wad.from_number(100)).transact()
        deployment.otc.make(deployment.gem.address,
                            Wad.from_number(110), deployment.skr.address,
                            Wad.from_number(105)).transact()
        deployment.otc.make(deployment.sai.address,
                            Wad.from_number(115), deployment.gem.address,
                            Wad.from_number(110)).transact()
        assert len(deployment.otc.get_orders()) == 3

        # when
        keeper.approve()
        block_number_before = deployment.web3.eth.blockNumber
        keeper.process_block()
        block_number_after = deployment.web3.eth.blockNumber

        # then
        assert len(deployment.otc.get_orders()) == 0

        # and
        # [keeper used only one transaction, as TxManager is configured]
        assert (block_number_after - block_number_before) == 1
Example #3
0
    def test_take_partial(self):
        # given
        self.otc.approve([self.token1], directly())
        self.otc.make(pay_token=self.token1.address,
                      pay_amount=Wad.from_number(1),
                      buy_token=self.token2.address,
                      buy_amount=Wad.from_number(2)).transact()

        # when
        self.otc.approve([self.token2], directly())
        self.otc.take(1, Wad.from_number(0.25)).transact()

        # then
        assert self.otc.get_order(1).pay_amount == Wad.from_number(0.75)
        assert self.otc.get_order(1).buy_amount == Wad.from_number(1.5)
        assert self.otc.get_orders() == [self.otc.get_order(1)]
        assert self.otc.get_last_order_id() == 1
Example #4
0
    def test_remaining_sell_and_buy_amounts(self):
        # given
        self.otc.approve([self.token1], directly())
        self.otc.make(pay_token=self.token1.address, pay_amount=Wad.from_number(1),
                      buy_token=self.token2.address, buy_amount=Wad.from_number(2)).transact()

        # and
        assert self.otc.get_order(1).remaining_sell_amount == Wad.from_number(1)
        assert self.otc.get_order(1).remaining_buy_amount == Wad.from_number(2)

        # when
        self.otc.approve([self.token2], directly())
        self.otc.take(1, Wad.from_number(0.25)).transact()

        # then
        assert self.otc.get_order(1).remaining_sell_amount == Wad.from_number(0.75)
        assert self.otc.get_order(1).remaining_buy_amount == Wad.from_number(1.5)
    def approve(self):
        token_buy = ERC20Token(web3=self.web3,
                               address=Address(self.pair.buy_token_address))
        token_sell = ERC20Token(web3=self.web3,
                                address=Address(self.pair.sell_token_address))

        self.zrx_exchange.approve([token_sell, token_buy],
                                  directly(gas_price=self.gas_price))
Example #6
0
    def test_order_hashable(self):
        # given
        self.otc.approve([self.token1], directly())
        self.otc.make(pay_token=self.token1.address, pay_amount=Wad.from_number(1),
                      buy_token=self.token2.address, buy_amount=Wad.from_number(2)).transact()

        # expect
        assert is_hashable(self.otc.get_order(1))
Example #7
0
    def approve(self, token: Token):
        assert (isinstance(token, Token))

        erc20_token = ERC20Token(self.web3, token.address)

        approval_function = directly()
        return approval_function(erc20_token, self.router_address,
                                 'UniswapV2Router02')
Example #8
0
    def approve(self, token_address: Address):
        assert (isinstance(token_address, Address))

        erc20_token = ERC20Token(self.web3, token_address)

        approval_function = directly()
        return approval_function(erc20_token, self.contract_address,
                                 self.contract_name)
Example #9
0
    def approve(self, token: Token):
        assert (isinstance(token, Token))

        erc20_token = ERC20Token(self.web3, token.address)

        approval_function = directly()
        return approval_function(erc20_token,
                                 self.nft_position_manager_address,
                                 'NonfungiblePositionManager')
Example #10
0
    def test_execute_zero_calls(self):
        # given
        self.tx.approve([self.token1], directly())

        # when
        res = self.tx.execute([self.token1.address], []).transact()

        # then
        assert res.successful
Example #11
0
    def test_approval(self):
        # given
        assert self.token.allowance_of(self.our_address, self.idex.address) == Wad(0)

        # when
        self.idex.approve([self.token], directly())

        # then
        assert self.token.allowance_of(self.our_address, self.idex.address) > Wad(0)
Example #12
0
    def approve(self, usr: Address):
        """
        Allows the user to move this collateral into and out of their CDP.

        Args
            usr: User making transactions with this collateral
        """
        self.adapter.approve(hope_directly(from_address=usr), self.flipper.vat())
        self.adapter.approve_token(directly(from_address=usr))
Example #13
0
    def approve(self):
        token_addresses = filter(
            lambda address: address != IDEX.ETH_TOKEN,
            [self.token_sell(), self.token_buy()])
        tokens = list(
            map(lambda address: ERC20Token(web3=self.web3, address=address),
                token_addresses))

        self.idex.approve(tokens, directly(gas_price=self.gas_price))
Example #14
0
    def test_scenario(self, web3, mcd, flapper, our_address, other_address,
                      deployment_address):
        create_surplus(mcd, flapper, deployment_address)

        joy_before = mcd.vat.dai(mcd.vow.address)
        # total surplus > total debt + surplus auction lot size + surplus buffer
        assert joy_before > mcd.vat.sin(
            mcd.vow.address) + mcd.vow.bump() + mcd.vow.hump()
        assert (mcd.vat.sin(mcd.vow.address) -
                mcd.vow.sin()) - mcd.vow.ash() == Rad(0)
        assert mcd.vow.flap().transact()
        kick = flapper.kicks()
        assert kick == 1
        assert len(flapper.active_auctions()) == 1
        check_active_auctions(flapper)
        current_bid = flapper.bids(1)
        assert current_bid.lot > Rad(0)
        log = self.last_log(flapper)
        assert isinstance(log, Flapper.KickLog)
        assert log.id == kick
        assert log.lot == current_bid.lot
        assert log.bid == current_bid.bid

        # Allow the auction to expire, and then resurrect it
        time_travel_by(web3, flapper.tau() + 1)
        assert flapper.tick(kick).transact()

        # Bid on the resurrected auction
        mint_mkr(mcd.mkr, our_address, Wad.from_number(10))
        flapper.approve(mcd.mkr.address, directly(from_address=our_address))
        bid = Wad.from_number(0.001)
        assert mcd.mkr.balance_of(our_address) > bid
        TestFlapper.tend(flapper, kick, our_address, current_bid.lot, bid)
        current_bid = flapper.bids(kick)
        assert current_bid.bid == bid
        assert current_bid.guy == our_address

        # Exercise _deal_ after bid has expired
        time_travel_by(web3, flapper.ttl() + 1)
        now = datetime.now().timestamp()
        assert 0 < current_bid.tic < now or current_bid.end < now
        assert flapper.deal(kick).transact(from_address=our_address)
        joy_after = mcd.vat.dai(mcd.vow.address)
        print(f'joy_before={str(joy_before)}, joy_after={str(joy_after)}')
        assert joy_before - joy_after == mcd.vow.bump()
        log = self.last_log(flapper)
        assert isinstance(log, Flapper.DealLog)
        assert log.usr == our_address
        assert log.id == kick

        # Grab our dai
        mcd.approve_dai(our_address)
        assert mcd.dai_adapter.exit(our_address, Wad(
            current_bid.lot)).transact(from_address=our_address)
        assert mcd.dai.balance_of(our_address) >= Wad(current_bid.lot)
        assert (mcd.vat.sin(mcd.vow.address) -
                mcd.vow.sin()) - mcd.vow.ash() == Rad(0)
    def test_should_ignore_orders_owned_by_others(self,
                                                  deployment: Deployment):
        # given
        keeper = OasisMarketMakerCancel(args=args(
            f"--eth-from {deployment.web3.eth.defaultAccount} "
            f"--oasis-address {deployment.otc.address}"),
                                        web3=deployment.web3)

        # and
        DSToken(web3=deployment.web3, address=deployment.gem.address).mint(
            Wad.from_number(1000)).transact()
        DSToken(web3=deployment.web3, address=deployment.sai.address).mint(
            Wad.from_number(1000)).transact()

        # and
        deployment.gem.transfer(Address(deployment.web3.eth.accounts[1]),
                                Wad.from_number(500)).transact()
        deployment.sai.transfer(Address(deployment.web3.eth.accounts[1]),
                                Wad.from_number(500)).transact()

        # and
        deployment.otc.approve([deployment.gem, deployment.sai], directly())
        deployment.otc.make(deployment.gem.address,
                            Wad.from_number(10), deployment.sai.address,
                            Wad.from_number(5)).transact()

        # and
        deployment.web3.eth.defaultAccount = deployment.web3.eth.accounts[1]
        deployment.otc.approve([deployment.gem, deployment.sai], directly())
        deployment.otc.make(deployment.sai.address,
                            Wad.from_number(5), deployment.gem.address,
                            Wad.from_number(12)).transact()
        deployment.web3.eth.defaultAccount = deployment.web3.eth.accounts[0]

        # and
        assert len(deployment.otc.get_orders()) == 2

        # when
        keeper.main()

        # then
        assert len(deployment.otc.get_orders()) == 1
        assert deployment.otc.get_orders()[0].maker == Address(
            deployment.web3.eth.accounts[1])
Example #16
0
    def test_offchain_order_happy_path(self):
        # given
        self.etherdelta.approve([self.token1, self.token2], directly())
        self.etherdelta.deposit_token(self.token1.address,
                                      Wad.from_number(10)).transact()
        self.etherdelta.deposit_token(self.token2.address,
                                      Wad.from_number(10)).transact()

        # when
        order = self.etherdelta.create_order(pay_token=self.token1.address,
                                             pay_amount=Wad.from_number(2),
                                             buy_token=self.token2.address,
                                             buy_amount=Wad.from_number(4),
                                             expires=100000000)

        # then
        assert order.maker == self.our_address
        assert order.pay_token == self.token1.address
        assert order.pay_amount == Wad.from_number(2)
        assert order.buy_token == self.token2.address
        assert order.buy_amount == Wad.from_number(4)
        assert order.expires == 100000000

        # and
        assert self.etherdelta.amount_available(order) == Wad.from_number(4)
        assert self.etherdelta.amount_filled(order) == Wad.from_number(0)
        assert order.remaining_sell_amount == Wad.from_number(2)
        assert order.remaining_buy_amount == Wad.from_number(4)

        # and
        assert self.etherdelta.can_trade(order, Wad.from_number(1.5))
        assert not self.etherdelta.can_trade(order, Wad.from_number(5.5))

        # when
        self.etherdelta.trade(order, Wad.from_number(1.5)).transact()

        # then
        assert self.etherdelta.amount_available(order) == Wad.from_number(2.5)
        assert self.etherdelta.amount_filled(order) == Wad.from_number(1.5)
        assert order.remaining_sell_amount == Wad.from_number(1.25)
        assert order.remaining_buy_amount == Wad.from_number(2.5)

        # when
        self.etherdelta.withdraw_token(self.token1.address,
                                       Wad.from_number(9.3)).transact()

        # then
        assert self.etherdelta.amount_available(order) == Wad.from_number(1.4)
        assert self.etherdelta.amount_filled(order) == Wad.from_number(1.5)

        # when
        self.etherdelta.cancel_order(order).transact()

        # then
        assert self.etherdelta.amount_available(order) == Wad.from_number(0)
        assert self.etherdelta.amount_filled(order) == Wad.from_number(4)
    def approve(self):
        """Approve EtherDelta to access our tokens, so we can deposit them with the exchange"""
        token_addresses = filter(
            lambda address: address != EtherDelta.ETH_TOKEN,
            [self.token_sell(), self.token_buy()])
        tokens = list(
            map(lambda address: ERC20Token(web3=self.web3, address=address),
                token_addresses))

        self.etherdelta.approve(tokens, directly(gas_price=self.gas_price))
Example #18
0
    def approve(self, usr: Address, **kwargs):
        """
        Allows the user to move this collateral into and out of their CDP.

        Args
            usr: User making transactions with this collateral
        """
        gas_price = kwargs['gas_price'] if 'gas_price' in kwargs else DefaultGasPrice()
        self.adapter.approve(hope_directly(from_address=usr, gas_price=gas_price), self.flipper.vat())
        self.adapter.approve_token(directly(from_address=usr, gas_price=gas_price))
    def test_should_obey_min_profit(self, deployment: Deployment):
        # given
        keeper = ArbitrageKeeper(args=args(
            f"--eth-from {deployment.our_address.address}"
            f" --tub-address {deployment.tub.address}"
            f" --tap-address {deployment.tap.address}"
            f" --oasis-address {deployment.otc.address}"
            f" --base-token {deployment.sai.address}"
            f" --min-profit 16.0 --max-engagement 1000.0"),
                                 web3=deployment.web3)

        # and
        DSValue(web3=deployment.web3,
                address=deployment.tub.pip()).poke_with_int(
                    Wad.from_number(500).value).transact()
        deployment.tub.mold_gap(Wad.from_number(1.05)).transact()
        deployment.tub.join(Wad.from_number(1000)).transact()
        deployment.tap.mold_gap(Wad.from_number(1.05)).transact()

        # and
        deployment.sai.mint(Wad.from_number(1000)).transact()

        # and
        deployment.otc.approve(
            [deployment.gem, deployment.sai, deployment.skr], directly())
        deployment.otc.add_token_pair_whitelist(
            deployment.sai.address, deployment.skr.address).transact()
        deployment.otc.add_token_pair_whitelist(
            deployment.skr.address, deployment.gem.address).transact()
        deployment.otc.add_token_pair_whitelist(
            deployment.gem.address, deployment.sai.address).transact()
        deployment.otc.make(deployment.skr.address,
                            Wad.from_number(105), deployment.sai.address,
                            Wad.from_number(100)).transact()
        deployment.otc.make(deployment.gem.address,
                            Wad.from_number(110), deployment.skr.address,
                            Wad.from_number(105)).transact()
        deployment.otc.make(deployment.sai.address,
                            Wad.from_number(115), deployment.gem.address,
                            Wad.from_number(110)).transact()
        assert len(deployment.otc.get_orders()) == 3

        # when
        keeper.approve()
        keeper.process_block()

        # then
        assert len(deployment.otc.get_orders()) == 3
        assert deployment.otc.get_orders()[0].buy_amount == Wad.from_number(
            100)
        assert deployment.otc.get_orders()[1].buy_amount == Wad.from_number(
            105)
        assert deployment.otc.get_orders()[2].buy_amount == Wad.from_number(
            110)
Example #20
0
    def test_approval(self):
        # given
        assert self.token1.allowance_of(self.our_address, self.token_transfer_proxy_address) == Wad(0)
        assert self.zrx_token.allowance_of(self.our_address, self.token_transfer_proxy_address) == Wad(0)

        # when
        self.exchange.approve([self.token1], directly())

        # then
        assert self.token1.allowance_of(self.our_address, self.token_transfer_proxy_address) > Wad(0)
        assert self.zrx_token.allowance_of(self.our_address, self.token_transfer_proxy_address) > Wad(0)
Example #21
0
    def approve(self):
        """Approve Mooniswap to access our balances, so we can place orders."""
        tokens = [self.first_token, self.second_token]
        approved_tokens = []

        for token in tokens:
            if isinstance(token, ERC20Token):
                approved_tokens.append(token)

        self.mooniswap.approve(approved_tokens,
                               directly(gas_price=self.gas_price))
Example #22
0
    def test_approve(self):
        # given
        assert self.token1.allowance_of(self.our_address, self.tx.address) == Wad(0)
        assert self.token2.allowance_of(self.our_address, self.tx.address) == Wad(0)

        # when
        self.tx.approve([self.token1, self.token2], directly())

        # then
        assert self.token1.allowance_of(self.our_address, self.tx.address) == Wad(2**256-1)
        assert self.token2.allowance_of(self.our_address, self.tx.address) == Wad(2**256-1)
Example #23
0
    def test_make_returns_new_order_ids(self):
        # given
        self.otc.approve([self.token1], directly())

        # expect
        for number in range(1, 10):
            receipt = self.otc.make(pay_token=self.token1.address, pay_amount=Wad.from_number(1),
                                    buy_token=self.token2.address, buy_amount=Wad.from_number(2)).transact()

            assert receipt.result == number
            assert self.otc.get_last_order_id() == number
Example #24
0
    def test_get_orders_by_pair(self):
        # given
        self.otc.approve([self.token1, self.token2, self.token3], directly())

        # when
        self.otc.make(pay_token=self.token1.address, pay_amount=Wad.from_number(1),
                      buy_token=self.token2.address, buy_amount=Wad.from_number(2)).transact()

        self.otc.make(pay_token=self.token1.address, pay_amount=Wad.from_number(1),
                      buy_token=self.token2.address, buy_amount=Wad.from_number(4)).transact()

        self.otc.make(pay_token=self.token1.address, pay_amount=Wad.from_number(1),
                      buy_token=self.token2.address, buy_amount=Wad.from_number(3)).transact()

        self.otc.make(pay_token=self.token1.address, pay_amount=Wad.from_number(1),
                      buy_token=self.token3.address, buy_amount=Wad.from_number(2)).transact()

        self.otc.make(pay_token=self.token1.address, pay_amount=Wad.from_number(1),
                      buy_token=self.token3.address, buy_amount=Wad.from_number(4)).transact()

        self.otc.make(pay_token=self.token1.address, pay_amount=Wad.from_number(1),
                      buy_token=self.token3.address, buy_amount=Wad.from_number(3)).transact()

        self.otc.make(pay_token=self.token2.address, pay_amount=Wad.from_number(1),
                      buy_token=self.token3.address, buy_amount=Wad.from_number(2)).transact()

        self.otc.make(pay_token=self.token2.address, pay_amount=Wad.from_number(1),
                      buy_token=self.token3.address, buy_amount=Wad.from_number(4)).transact()

        self.otc.make(pay_token=self.token2.address, pay_amount=Wad.from_number(1),
                      buy_token=self.token3.address, buy_amount=Wad.from_number(3)).transact()

        self.otc.make(pay_token=self.token2.address, pay_amount=Wad.from_number(1),
                      buy_token=self.token1.address, buy_amount=Wad.from_number(5)).transact()

        self.otc.make(pay_token=self.token2.address, pay_amount=Wad.from_number(1),
                      buy_token=self.token1.address, buy_amount=Wad.from_number(6)).transact()

        # then
        def order_ids(orders: List[Order]) -> List[int]:
            return list(map(lambda order: order.order_id, orders))

        assert len(self.otc.get_orders()) == 11
        assert order_ids(self.otc.get_orders(self.token1.address, self.token2.address)) == [1, 2, 3]
        assert order_ids(self.otc.get_orders(self.token1.address, self.token3.address)) == [4, 5, 6]
        assert order_ids(self.otc.get_orders(self.token2.address, self.token3.address)) == [7, 8, 9]
        assert order_ids(self.otc.get_orders(self.token2.address, self.token1.address)) == [10, 11]

        # when
        self.otc.kill(8).transact()

        # then
        assert order_ids(self.otc.get_orders(self.token2.address, self.token3.address)) == [7, 9]
Example #25
0
    def test_execute_one_call(self):
        # given
        self.tx.approve([self.token1], directly())

        # when
        res = self.tx.execute([self.token1.address],
                              [self.token1.transfer(self.other_address, Wad.from_number(500)).invocation()]).transact()

        # then
        assert res.successful
        assert self.token1.balance_of(self.our_address) == Wad.from_number(999500)
        assert self.token1.balance_of(self.other_address) == Wad.from_number(500)
    def test_should_identify_arbitrage_against_oasis_and_exit(
            self, deployment: Deployment):
        # given
        keeper = ArbitrageKeeper(args=args(
            f"--eth-from {deployment.our_address.address}"
            f" --tub-address {deployment.tub.address}"
            f" --tap-address {deployment.tap.address}"
            f" --oasis-address {deployment.otc.address}"
            f" --base-token {deployment.gem.address}"
            f" --min-profit 5.0 --max-engagement 100.0"),
                                 web3=deployment.web3)

        # and
        # [a price is set, so the arbitrage keeper knows prices of `boom` and `bust`]
        DSValue(web3=deployment.web3,
                address=deployment.tub.pip()).poke_with_int(
                    Wad.from_number(500).value).transact()

        # and
        # [we have 100 WETH]
        deployment.gem.mint(Wad.from_number(100)).transact()

        # and
        # [somebody else placed an order on OASIS offering 110 SKR for 100 WETH]
        second_address = Address(deployment.web3.eth.accounts[1])

        deployment.gem.mint(Wad.from_number(110)).transact()
        deployment.tub.join(Wad.from_number(110)).transact()
        deployment.skr.transfer(second_address,
                                Wad.from_number(110)).transact()
        deployment.otc.add_token_pair_whitelist(
            deployment.skr.address, deployment.gem.address).transact()
        deployment.otc.approve([deployment.gem, deployment.skr],
                               directly(from_address=second_address))
        deployment.otc.make(
            deployment.skr.address, Wad.from_number(110),
            deployment.gem.address,
            Wad.from_number(100)).transact(from_address=second_address)
        assert deployment.skr.total_supply() == Wad.from_number(110)
        assert len(deployment.otc.get_orders()) == 1

        # when
        keeper.approve()
        keeper.process_block()

        # then
        # [the order on Oasis has been taken by the keeper]
        assert len(deployment.otc.get_orders()) == 0

        # and
        # [the total supply of SKR has decreased, so we know the keeper did call exit('110.0')]
        assert deployment.skr.total_supply() == Wad.from_number(0)
Example #27
0
    def test_kill(self):
        # given
        self.otc.approve([self.token1], directly())
        self.otc.make(pay_token=self.token1.address, pay_amount=Wad.from_number(1),
                      buy_token=self.token2.address, buy_amount=Wad.from_number(2)).transact()

        # when
        self.otc.kill(1).transact(gas=4000000)

        # then
        assert self.otc.get_order(1) is None
        assert self.otc.get_orders() == []
        assert self.otc.get_last_order_id() == 1
Example #28
0
    def test_take_complete(self):

        if isinstance(self.otc, MatchingMarket):
            pay_val = self.token1_tokenclass
            buy_val = self.token2_tokenclass
        else:
            pay_val = self.token1.address
            buy_val = self.token2.address

        # given
        self.otc.approve([self.token1], directly())
        self.otc.make(pay_val, Wad.from_number(1), buy_val,
                      Wad.from_number(2)).transact()

        # when
        self.otc.approve([self.token2], directly())
        self.otc.take(1, Wad.from_number(1)).transact()

        # then
        assert self.otc.get_order(1) is None
        assert self.otc.get_orders() == []
        assert self.otc.get_last_order_id() == 1
Example #29
0
    def test_on_take_wih_filter(self):
        # given
        on_take_filter1_mock = Mock()
        on_take_filter2_mock = Mock()
        self.otc.on_take(on_take_filter1_mock, {'maker': self.our_address.address})
        self.otc.on_take(on_take_filter2_mock, {'maker': '0x0101010101020202020201010101010303030303'})

        # when
        self.otc.approve([self.token1], directly())
        self.otc.make(pay_token=self.token1.address, pay_amount=Wad.from_number(1),
                      buy_token=self.token2.address, buy_amount=Wad.from_number(2)).transact()

        # and
        self.otc.approve([self.token2], directly())
        self.otc.take(1, Wad.from_number(0.5)).transact()

        # then
        assert len(wait_until_mock_called(on_take_filter1_mock)) == 1

        # and
        time.sleep(2)
        assert not on_take_filter2_mock.called
Example #30
0
    def test_past_take(self):
        # when
        self.otc.approve([self.token1], directly())
        self.otc.make(pay_token=self.token1.address, pay_amount=Wad.from_number(1),
                      buy_token=self.token2.address, buy_amount=Wad.from_number(2)).transact()

        # and
        self.otc.approve([self.token2], directly())
        self.otc.take(1, Wad.from_number(0.5)).transact()

        # then
        past_take = self.otc.past_take(PAST_BLOCKS)
        assert len(past_take) == 1
        assert past_take[0].order_id == 1
        assert past_take[0].maker == self.our_address
        assert past_take[0].taker == self.our_address
        assert past_take[0].pay_token == self.token1.address
        assert past_take[0].buy_token == self.token2.address
        assert past_take[0].take_amount == Wad.from_number(0.5)
        assert past_take[0].give_amount == Wad.from_number(1)
        assert past_take[0].timestamp != 0
        assert past_take[0].raw['blockNumber'] > 0
Example #31
0
    def test_take_partial(self):

        if isinstance(self.otc, MatchingMarket):
            pay_val = self.token1_tokenclass
            buy_val = self.token2_tokenclass
        else:
            pay_val = self.token1.address
            buy_val = self.token2.address

        # given
        self.otc.approve([self.token1], directly())
        self.otc.make(pay_val, Wad.from_number(1), buy_val,
                      Wad.from_number(2)).transact()

        # when
        self.otc.approve([self.token2], directly())
        self.otc.take(1, Wad.from_number(0.25)).transact()

        # then
        assert self.otc.get_order(1).pay_amount == Wad.from_number(0.75)
        assert self.otc.get_order(1).buy_amount == Wad.from_number(1.5)
        assert self.otc.get_orders() == [self.otc.get_order(1)]
        assert self.otc.get_last_order_id() == 1
    def test_should_use_gas_price_specified(self, deployment: Deployment):
        # given
        some_gas_price = 15000000000
        keeper = OasisMarketMakerCancel(args=args(f"--eth-from {deployment.web3.eth.defaultAccount} "
                                             f"--oasis-address {deployment.otc.address} "
                                             f"--gas-price {some_gas_price}"),
                                        web3=deployment.web3)

        # and
        DSToken(web3=deployment.web3, address=deployment.gem.address).mint(Wad.from_number(1000)).transact()
        DSToken(web3=deployment.web3, address=deployment.sai.address).mint(Wad.from_number(1000)).transact()

        # and
        deployment.otc.approve([deployment.gem, deployment.sai], directly())
        deployment.otc.make(deployment.sai.address, Wad.from_number(5), deployment.gem.address, Wad.from_number(12)).transact()
        assert len(deployment.otc.get_orders()) == 1

        # when
        keeper.main()

        # then
        assert len(deployment.otc.get_orders()) == 0
        assert deployment.web3.eth.getBlock('latest', True)['transactions'][0]['gasPrice'] == some_gas_price
 def approve(self):
     """Approve OasisDEX to access our balances, so we can place orders."""
     self.otc.approve([self.token_sell, self.token_buy], directly(gas_price=self.gas_price))
    def approve(self):
        token_addresses = filter(lambda address: address != IDEX.ETH_TOKEN, [self.token_sell(), self.token_buy()])
        tokens = list(map(lambda address: ERC20Token(web3=self.web3, address=address), token_addresses))

        self.idex.approve(tokens, directly(gas_price=self.gas_price))
 def approve(self):
     self.zrx_exchange.approve([self.token_sell, self.token_buy], directly(gas_price=self.gas_price))