def _order_tuple(order): return (order.maker.address, order.taker.address, order.fee_recipient.address, order.sender.address, order.pay_amount.value, order.buy_amount.value, order.maker_fee.value, order.taker_fee.value, order.expiration, order.salt, hexstring_to_bytes(order.pay_asset.serialize()), hexstring_to_bytes(order.buy_asset.serialize()))
def write(self, code: str): assert (isinstance(code, str)) 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, 'write', [b32_code])
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 read(self, code: str) -> Optional[Address]: assert (isinstance(code, str)) if code.startswith('0x'): b32_code = hexstring_to_bytes(code) else: b32_code = hexstring_to_bytes('0x' + code) address = Address(self._contract.functions.read(b32_code).call()) if address == Address('0x0000000000000000000000000000000000000000'): return None else: return address
def fill_order(self, order: Order, fill_buy_amount: Wad) -> Transact: """Fills an order. Args: order: The order to be filled. fill_buy_amount: The amount (in terms of `buy_token` of the original order) to be filled. Returns: A :py:class:`pyflex.Transact` instance, which can be used to trigger the transaction. """ assert (isinstance(order, Order)) assert (isinstance(fill_buy_amount, Wad)) method_signature = self.web3.keccak( text=f"fillOrder({self.ORDER_INFO_TYPE},uint256,bytes)")[0:4] method_parameters = encode_single( f"({self.ORDER_INFO_TYPE},uint256,bytes)", [ self._order_tuple(order), fill_buy_amount.value, hexstring_to_bytes(order.signature) ]) request = bytes_to_hexstring(method_signature + method_parameters) return Transact(self, self.web3, self.abi, self.address, self._contract, None, [request])
def asset_transfer_proxy(self, proxy_id: str) -> Address: """Get the address of the `ERC20Proxy` contract associated with this `Exchange` contract. Returns: The address of the `ERC20Proxy` token. """ assert (isinstance(proxy_id, str)) return Address( self._contract.functions.getAssetProxy( hexstring_to_bytes(proxy_id)).call())
def test_permit_specific_addresses_and_sig(self): # when self.ds_guard.permit(src=Address('0x1111111111222222222211111111112222222222'), dst=Address('0x3333333333444444444433333333334444444444'), sig=hexstring_to_bytes('0xab121fd7')).transact() # then assert self.can_call(src='0x1111111111222222222211111111112222222222', dst='0x3333333333444444444433333333334444444444', sig='0xab121fd7') # and assert not self.can_call(src='0x3333333333444444444433333333334444444444', dst='0x1111111111222222222211111111112222222222', sig='0xab121fd7') # different addresses assert not self.can_call(src='0x1111111111222222222211111111112222222222', dst='0x3333333333444444444433333333334444444444', sig='0xab121fd8') # different sig
def sign_order(self, order: Order) -> Order: """Signs an order so it can be submitted to the relayer. Order will be signed by the `web3.eth.defaultAccount` account. Args: order: Order you want to sign. Returns: Signed order. Copy of the order passed as a parameter with the `signature` field filled with signature. """ assert (isinstance(order, Order)) signature = eth_sign(hexstring_to_bytes(self.get_order_hash(order)), self.web3) v, r, s = to_vrs(signature) signed_order = copy.copy(order) signed_order.signature = bytes_to_hexstring(bytes([v])) + \ bytes_to_hexstring(r)[2:] + \ bytes_to_hexstring(s)[2:] + \ "03" # EthSign return signed_order
def can_call(self, src: str, dst: str, sig: str) -> bool: return self.ds_guard._contract.functions.canCall(src, dst, hexstring_to_bytes(sig)).call()
def test_hexstring_to_bytes(): assert hexstring_to_bytes('0x00') == bytes([0x00]) assert hexstring_to_bytes('0x010203') == bytes([0x01, 0x02, 0x03]) assert hexstring_to_bytes('0xffff') == bytes([0xff, 0xff])