def test_direct_approval_should_raise_exception_if_approval_fails(): # given global web3, our_address, second_address, token token.approve = MagicMock(return_value=FailingTransact()) # expect with pytest.raises(Exception): directly()(token, second_address, "some-name")
def test_direct_approval(): # given global web3, our_address, second_address, token # when directly()(token, second_address, "some-name") # then assert token.allowance_of(our_address, second_address) == Wad(2**256 - 1)
def test_direct_approval_should_not_approve_if_already_approved(): # given global web3, our_address, second_address, token token.approve(second_address, Wad(2**248 + 17)).transact() # when directly()(token, second_address, "some-name") # then assert token.allowance_of(our_address, second_address) == Wad(2**248 + 17)
def test_direct_approval_should_obey_gas_price(): # given global web3, our_address, second_address, token # when directly(gas_price=FixedGasPrice(25000000000))(token, second_address, "some-name") # then assert web3.eth.getBlock( 'latest', full_transactions=True).transactions[0].gasPrice == 25000000000
def test_direct_approval_should_obey_from_address(): # given global web3, our_address, second_address, third_address, token # and # [there is already approval from the `defaultAccount`] # [so that we make sure we check for the existing approval properly] directly()(token, second_address, "some-name") # when directly(from_address=third_address)(token, second_address, "some-name") # then assert token.allowance_of(third_address, second_address) == Wad(2**256 - 1)
def create_surplus_auction(geb: GfDeployment, deployment_address: Address, our_address: Address, collateral: Collateral): assert isinstance(geb, GfDeployment) assert isinstance(deployment_address, Address) assert isinstance(our_address, Address) surplus_auction_house = geb.surplus_auction_house create_surplus(geb, surplus_auction_house, deployment_address, collateral) coin_balance = geb.safe_engine.coin_balance(geb.accounting_engine.address) assert coin_balance > geb.safe_engine.debt_balance( geb.accounting_engine.address ) + geb.accounting_engine.surplus_auction_amount_to_sell( ) + geb.accounting_engine.surplus_buffer() assert (geb.safe_engine.debt_balance(geb.accounting_engine.address) - geb.accounting_engine.total_queued_debt() ) - geb.accounting_engine.total_on_auction_debt() == Rad(0) assert geb.accounting_engine.auction_surplus().transact() mint_prot(geb.prot, our_address, Wad.from_number(10)) surplus_auction_house.approve(geb.prot.address, directly(from_address=our_address)) bid = Wad.from_number(0.001) assert geb.prot.balance_of(our_address) > bid assert surplus_auction_house.increase_bid_size( surplus_auction_house.auctions_started(), geb.accounting_engine.surplus_auction_amount_to_sell(), bid).transact(from_address=our_address)
def test_past_cancel(self): # given self.exchange.approve([self.token1, self.token2], directly()) # when order = self.exchange.create_order(pay_token=self.token1.address, pay_amount=Wad.from_number(10), buy_token=self.token2.address, buy_amount=Wad.from_number(4), expiration=1763920792) # and self.exchange.cancel_order(self.exchange.sign_order(order)).transact() # then past_cancel = self.exchange.past_cancel(PAST_BLOCKS) assert len(past_cancel) == 1 assert past_cancel[0].maker == self.our_address assert past_cancel[0].fee_recipient == Address( "0x0000000000000000000000000000000000000000") assert past_cancel[0].pay_token == self.token1.address assert past_cancel[0].cancelled_pay_amount == Wad.from_number(10) assert past_cancel[0].buy_token == self.token2.address assert past_cancel[0].cancelled_buy_amount == Wad.from_number(4) assert past_cancel[0].tokens.startswith('0x') assert past_cancel[0].order_hash == self.exchange.get_order_hash( self.exchange.sign_order(order)) assert past_cancel[0].raw['blockNumber'] > 0
def test_past_fill(self): # given self.exchange.approve([self.token1, self.token2], directly()) # when order = self.exchange.create_order( pay_asset=ERC20Asset(self.token1.address), pay_amount=Wad.from_number(10), buy_asset=ERC20Asset(self.token2.address), buy_amount=Wad.from_number(4), expiration=1763920792) # and self.exchange.fill_order(self.exchange.sign_order(order), Wad.from_number(3)).transact() # then past_fill = self.exchange.past_fill(PAST_BLOCKS) assert len(past_fill) == 1 assert past_fill[0].sender == self.our_address assert past_fill[0].maker == self.our_address assert past_fill[0].taker == self.our_address assert past_fill[0].fee_recipient == Address( "0x0000000000000000000000000000000000000000") assert past_fill[0].pay_asset == ERC20Asset(self.token1.address) assert past_fill[0].buy_asset == ERC20Asset(self.token2.address) assert past_fill[0].filled_pay_amount == Wad.from_number(7.5) assert past_fill[0].filled_buy_amount == Wad.from_number(3) assert past_fill[0].paid_maker_fee == Wad.from_number(0) assert past_fill[0].paid_taker_fee == Wad.from_number(0) assert past_fill[0].order_hash == self.exchange.get_order_hash( self.exchange.sign_order(order)) assert past_fill[0].raw['blockNumber'] > 0
def setup_method(self): self.web3 = get_web3() self.our_address = get_our_address(self.web3) self.keeper_address = get_keeper_address(self.web3) self.other_address = get_other_address(self.web3) self.auction_income_recipient_address = get_auction_income_recipient_address(self.web3) self.geb = get_geb(self.web3) self.surplus_auction_house = self.geb.surplus_auction_house self.surplus_auction_house.approve(self.geb.prot.address, directly(from_address=self.other_address)) #self.min_auction = self.geb.surplus_auction_house.auctions_started() + 1 self.keeper = AuctionKeeper(args=args(f"--eth-from {self.keeper_address} " f"--type surplus " f"--from-block 1 " f"--bid-delay 0 " #f"--min-auction {self.min_auction} " f"--model ./bogus-model.sh"), web3=self.web3) self.keeper.approve() mint_prot(self.geb.prot, self.keeper_address, Wad.from_number(50000)) mint_prot(self.geb.prot, self.other_address, Wad.from_number(50000)) assert isinstance(self.keeper.gas_price, DynamicGasPrice) # Since no args were assigned, gas strategy should return a GeometricGasPrice starting at the node gas price self.default_gas_price = get_node_gas_price(self.web3)
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')
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
def test_approval(self): # given assert self.token1.allowance_of(self.our_address, self.asset_proxy) == Wad(0) assert self.zrx_token.allowance_of(self.our_address, self.asset_proxy) == Wad(0) # when self.exchange.approve([self.token1], directly()) # then assert self.token1.allowance_of(self.our_address, self.asset_proxy) > Wad(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)
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_fill_order(self): # given self.exchange.approve([self.token1, self.token2], directly()) # when order = self.exchange.create_order(pay_token=self.token1.address, pay_amount=Wad.from_number(10), buy_token=self.token2.address, buy_amount=Wad.from_number(4), expiration=1763920792) # and signed_order = self.exchange.sign_order(order) # then assert self.exchange.get_unavailable_buy_amount(signed_order) == Wad(0) # when self.exchange.fill_order(signed_order, Wad.from_number(3.5)).transact() # then assert self.exchange.get_unavailable_buy_amount( signed_order) == Wad.from_number(3.5)
def test_remaining_buy_amount_and_remaining_sell_amount(self): # given self.exchange.approve([self.token1, self.token2], directly()) # when order = self.exchange.create_order(pay_token=self.token1.address, pay_amount=Wad.from_number(10), buy_token=self.token2.address, buy_amount=Wad.from_number(4), expiration=1763920792) # and signed_order = self.exchange.sign_order(order) # then assert signed_order.remaining_sell_amount == Wad.from_number(10) assert signed_order.remaining_buy_amount == Wad.from_number(4) # when self.exchange.fill_order(signed_order, Wad.from_number(3.5)).transact() # then assert signed_order.remaining_sell_amount == Wad.from_number(1.25) assert signed_order.remaining_buy_amount == Wad.from_number(0.5)
def test_execute_multiple_calls_with_multiple_tokens(self): # given self.tx.approve([self.token1, self.token2], directly()) # when res = self.tx.execute([self.token1.address, self.token2.address], [ self.token1.transfer(self.other_address, Wad.from_number(500)).invocation(), self.token1.transfer(self.other_address, Wad.from_number(200)).invocation(), self.token2.transfer(self.other_address, Wad.from_number(150)).invocation() ]).transact() # then assert res.successful assert self.token1.balance_of( self.our_address) == Wad.from_number(999300) assert self.token1.balance_of( self.other_address) == Wad.from_number(700) assert self.token2.balance_of( self.our_address) == Wad.from_number(999850) assert self.token2.balance_of( self.other_address) == Wad.from_number(150)
def create_surplus_auction(geb: GfDeployment, deployment_address: Address, our_address: Address): assert isinstance(geb, GfDeployment) assert isinstance(deployment_address, Address) assert isinstance(our_address, Address) surplus_auction_house = geb.surplus_auction_house print( f"Before Surplus: {geb.safe_engine.coin_balance(geb.accounting_engine.address)}" ) create_surplus(geb, surplus_auction_house, deployment_address) print( f"After Surplus: {geb.safe_engine.coin_balance(geb.accounting_engine.address)}" ) # start surplus auction surplus = geb.safe_engine.coin_balance(geb.accounting_engine.address) assert surplus > geb.safe_engine.debt_balance(geb.accounting_engine.address) + \ geb.accounting_engine.surplus_auction_amount_to_sell() + \ geb.accounting_engine.surplus_buffer() assert (geb.safe_engine.debt_balance(geb.accounting_engine.address) - \ geb.accounting_engine.total_queued_debt()) - geb.accounting_engine.total_on_auction_debt() == Rad(0) assert geb.accounting_engine.auction_surplus().transact() auction_id = surplus_auction_house.auctions_started() assert auction_id == 1 assert len(surplus_auction_house.active_auctions()) == 1 mint_prot(geb.prot, our_address, Wad.from_number(10)) surplus_auction_house.approve(geb.prot.address, directly(from_address=our_address)) bid_amount = Wad.from_number(0.001) assert geb.prot.balance_of(our_address) > bid_amount assert surplus_auction_house.increase_bid_size( surplus_auction_house.auctions_started(), geb.accounting_engine.surplus_auction_amount_to_sell(), bid_amount).transact(from_address=our_address)
def approve(self, gas_price: GasPrice): self.surplus_auction_house.approve(self.prot, directly(gas_price=gas_price))