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 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()
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 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_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()))
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()
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()))
def test_should_not_bid_if_available_balance_below_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(65 * self.wei())) result = strategy.perform(self.auctionlet, self.context) # then self.assertEqual( "Not bidding as available balance (60.000000000000000000 MKR) is less than minimal allowed bid (65.000000000000000000 MKR)", result.description) self.assertFalse(result.forget) self.auctionlet.bid.assert_not_called()
def test_should_raise_allowance_if_too_low_before_bidding(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=True) # 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.mkr_token.approve.assert_called_once_with( self.auction_manager_address, Wad(2**256 - 1)) self.auctionlet.bid.assert_called_once_with(Wad(125 * self.wei()))
def test_should_not_make_a_partial_bid_if_available_balance_below_min_increase_and_cannot_split( self): # given self.auction.min_increase = 15 self.auction.can_split = MagicMock(return_value=False) 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(54 * self.wei())) # 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 available balance (54.000000000000000000 MKR is below minimal next bid (57.500000000000000000 MKR) and splitting is unavailable", result.description) self.assertFalse(result.forget) self.auctionlet.bid.assert_not_called()
def test_should_make_a_partial_bid_ensuring_relative_increase_in_value( self): # given self.auction.min_increase = 20 self.auction.can_split = MagicMock(return_value=False) self.auctionlet.buy_amount = Wad(50 * self.wei()) self.auctionlet.last_bidder = self.competitor_address self.auctionlet.can_split = MagicMock(return_value=True) self.set_mkr_balance(Wad(54 * self.wei())) # when strategy = BasicForwardAuctionStrategy(self.dai_token, self.mkr_token, 0.05, 0.05, Wad(1)) result = strategy.perform(self.auctionlet, self.context) # then self.assertEqual( "Placed a new partial bid at 54.000000000000000000 MKR (for 9.000000000000000000 DAI), bid was successful, new auctionlet got created", result.description) self.assertFalse(result.forget) self.auctionlet.bid.assert_called_once_with(Wad(54 * self.wei()), Wad(9 * self.wei()))
parser.add_argument( "--step", help="Incremental step towards the maximum price (value between 0 and 1)", required=True, type=float) args = parser.parse_args() web3 = Web3( HTTPProvider(endpoint_uri=f"http://{args.rpc_host}:{args.rpc_port}")) web3.eth.defaultAccount = args.eth_from #TODO allow to use ETH_FROM env variable config = Config(web3) auction_manager_address = Address( config.get_contract_address("auctionManager")) auction_manager = AuctionManager(web3=web3, address=auction_manager_address, is_splitting=True) trader_address = Address(args.eth_from) dai_address = ERC20Token.token_address_by_name("DAI") dai_token = ERC20Token(web3=web3, address=dai_address) mkr_address = ERC20Token.token_address_by_name("MKR") mkr_token = DSToken(web3=web3, address=mkr_address) strategy = BasicForwardAuctionStrategy( dai_token, mkr_token, args.mkr_dai_rate, args.step, Wad(args.minimal_mkr_bid * 1000000000000000000)) engine = AuctionEngine(auction_manager, trader_address, strategy, args.frequency) engine.start()