def test_from_signature(self, web3): # given calldata1a = Calldata( '0xa9059cbb' # function 4byte signature '00000000000000000000000011223344556600000000000000000000000000ff' '000000000000000000000000000000000000000000000000000000000000007b') calldata1b = Calldata.from_signature( web3, 'transfer(address,uint256)', ['0x11223344556600000000000000000000000000ff', 123]) # expect assert calldata1a == calldata1b # given calldata2a = Calldata( '0x2b4e4e96' # function 4byte signature '00000000000000000000000011223344556600000000000000000000000000ff' '0000000000000000000000000000000000000000000000000000000000000040' '0000000000000000000000000000000000000000000000000000000000000002' '000000000000000000000000000000000000000000000000000000000000007b' '00000000000000000000000000000000000000000000000000000000000001c8') calldata2b = Calldata.from_signature( web3, 'transfer(address,uint256[])', ['0x11223344556600000000000000000000000000ff', [123, 456]]) # expect assert calldata2a == calldata2b
def test_equality(self): # given calldata1a = Calldata('0xa9059cbb') calldata1b = Calldata('0xa9059cbb') calldata2 = Calldata('0xa9059ccc') # expect assert calldata1a == calldata1b assert calldata1a != calldata2 assert calldata1b != calldata2
def execute_at(self, address: Address, calldata: Calldata) -> Transact: assert (isinstance(address, Address)) assert (isinstance(calldata, Calldata)) return Transact(self, self.web3, self.abi, self.address, self._contract, 'execute(address,bytes)', [address.address, calldata.as_bytes()])
def test_call_at(self, proxy: DSProxy): # given proxy_cache = DSProxyCache(proxy.web3, proxy.cache()) proxy_cache.write(DSProxyFactory.bin).transact() new_factory_addr = proxy_cache.read(DSProxyFactory.bin) receipt = proxy.execute_at( new_factory_addr, Calldata.from_signature(proxy.web3, "build(address)", [proxy.address.address])).transact() log_created: LogCreated = DSProxyFactory.log_created(receipt)[0] # when calldata = Calldata.from_signature(proxy.web3, "isProxy(address)", [log_created.proxy.address]) response = proxy.call_at(new_factory_addr, calldata) # then assert Web3.toInt(response) == 1
def test_call(self, proxy: DSProxy): # when calldata = Calldata.from_signature(proxy.web3, "isProxy(address)", [Address(40 * '0').address]) target, response = proxy.call(DSProxyFactory.bin, calldata) # then assert target != Address(40 * '0') assert Web3.toInt(response) == 0
def exit_all(self, proxy: DSProxy) -> Transact: assert (isinstance(proxy, DSProxy)) return proxy.execute_at( self.mcd.dss_proxy_actions.address, Calldata.from_signature(self.mcd.web3, "exitAll(address,address)", [ self.mcd.dai_adapter.address.address, self.mcd.pot.address.address ]))
def exit(self, amount: Wad, proxy: DSProxy) -> Transact: assert (isinstance(amount, Wad)) assert (isinstance(proxy, DSProxy)) return proxy.execute_at( self.mcd.dss_proxy_actions.address, Calldata.from_signature( self.mcd.web3, "exit(address,address,uint256)", [ self.mcd.dai_adapter.address.address, self.mcd.pot.address.address, amount.value ]))
def test_name_formatting_with_hexstrings(self): # given proxy_cache = DSProxyCache.deploy(self.web3) proxy = DSProxy.deploy(self.web3, proxy_cache.address) # when transact = proxy.execute("0x11223344", Calldata("0x55667788")) # then assert transact.name( ) == f"DSProxy('{proxy.address}').execute(bytes,bytes)('0x11223344', '0x55667788')"
def execute(self, code: str, calldata: Calldata) -> Transact: assert (isinstance(code, str)) assert (isinstance(calldata, Calldata)) if code.startswith('0x'): b32_code = hexstring_to_bytes(code) else: b32_code = hexstring_to_bytes('0x' + code) return Transact(self, self.web3, self.abi, self.address, self._contract, 'execute(bytes,bytes)', [b32_code, calldata.as_bytes()])
def test_execute_at(self, proxy: DSProxy): # given proxy_cache = DSProxyCache(proxy.web3, proxy.cache()) proxy_cache.write(DSProxyFactory.bin).transact() new_factory_addr = proxy_cache.read(DSProxyFactory.bin) assert new_factory_addr # when receipt = proxy.execute_at( new_factory_addr, Calldata.from_signature(proxy.web3, "build(address)", [proxy.address.address])).transact() assert receipt build_event = DSProxyFactory.log_created(receipt)[0] # then assert build_event.owner == proxy.address
def test_execute(self, proxy: DSProxy): assert proxy.execute( DSProxyFactory.bin, Calldata.from_signature(proxy.web3, "build()", [])).transact()
def test_creation(self): # expect assert Calldata('0xa9059cbb').value == '0xa9059cbb'
def test_creation_from_bytes(self): # expect assert Calldata(b'\xa9\x05\x9c\xbb').value == '0xa9059cbb'
def test_should_be_hashable(self): assert is_hashable(Calldata('0xa9059cbb'))
def test_repr(self): # expect assert repr(Calldata('0xa9059cbb')) == "Calldata('0xa9059cbb')"
def test_string_value(self): # expect assert str(Calldata('0xa9059cbb')) == '0xa9059cbb'
def test_as_bytes(self): # expect assert Calldata('0xa9059cbb').as_bytes() == b'\xa9\x05\x9c\xbb'
def test_should_fail_creation_from_invalid_calldata(self): # expect with pytest.raises(Exception): Calldata('a9059cbb') # without `0x`