def assert_field_error(json_dic, code, field, match, command_type=offchain.PaymentCommandObject): with pytest.raises(offchain.FieldError, match=match) as e: req = offchain.from_json(json.dumps(json_dic)) offchain.from_dict(req.command, field_path="command") assert e.value.code == code assert e.value.field == field
async def _handle_offchain_reference_i_d_command( self, request_sender_address: str, request: offchain.CommandRequestObject) -> Dict[str, Any]: ref_id_command_object = from_dict(request.command, ReferenceIDCommandObject) # Check if reference ID is duplicate try: self.app.validate_unique_reference_id( ref_id_command_object.reference_id) except ValueError: msg = f"Reference ID {ref_id_command_object.reference_id} already exists" raise command_error(ErrorCode.duplicate_reference_id, msg) # Check if receiver has a diem ID in this wallet try: account = self.app.store.find( Account, diem_id=ref_id_command_object.receiver) except NotFoundError: raise command_error( ErrorCode.invalid_receiver, f"Receiver with Diem ID {ref_id_command_object.receiver} not found", field="receiver", ) self.app.store.create( ReferenceID, account_id=account.id, reference_id=ref_id_command_object.reference_id, ) return to_dict( ReferenceIDCommandResultObject( receiver_address=self.app.diem_account.account_identifier(), ))
async def match_exchange_states() -> None: states = [] events = await receiver.events() for e in events: if e.type in ["created_payment_command", "updated_payment_command"]: payment_object = json.loads(e.data)["payment_object"] payment = offchain.from_dict(payment_object, offchain.PaymentObject) states.append(offchain.payment_state.MACHINE.match_state(payment).id) assert states == payment_command_states
def to_offchain_command(self) -> offchain.PaymentCommand: payment = offchain.from_dict(self.payment_object, offchain.PaymentObject) return offchain.PaymentCommand( my_actor_address=payment.sender.address if self.is_sender else payment.receiver.address, payment=payment, inbound=self.is_inbound, cid=self.cid, )
def payment_state_id(event: Event) -> str: payment = offchain.from_dict( json.loads(event.data)["payment_object"], offchain.PaymentObject) return offchain.payment_state.MACHINE.match_state(payment).id
def test_new_payment_request_and_object(factory): sender = LocalAccount.generate() receiver = LocalAccount.generate() payment = factory.new_payment_object(sender, receiver) request = offchain.new_payment_request(payment) reference_id = payment.reference_id assert reference_id assert_cid(request.cid) assert uuid.UUID(reference_id) assert "-" in reference_id payment = offchain.from_dict(request.command, offchain.PaymentCommandObject).payment address, subaddress = identifier.decode_account(payment.sender.address, identifier.TDM) assert subaddress is not None assert address == sender.account_address address, subaddress = identifier.decode_account(payment.receiver.address, identifier.TDM) assert subaddress is not None assert address == receiver.account_address expected = f"""{{ "cid": "{request.cid}", "command_type": "PaymentCommand", "command": {{ "_ObjectType": "PaymentCommand", "payment": {{ "reference_id": "{reference_id}", "sender": {{ "address": "{payment.sender.address}", "status": {{ "status": "needs_kyc_data" }}, "kyc_data": {{ "type": "individual", "payload_version": 1, "given_name": "Jack", "surname": "G", "address": {{ "city": "San Francisco" }} }} }}, "receiver": {{ "address": "{payment.receiver.address}", "status": {{ "status": "none" }} }}, "action": {{ "amount": 1000000000000, "currency": "XUS", "action": "charge", "timestamp": {payment.action.timestamp} }} }} }}, "_ObjectType": "CommandRequestObject" }}""" assert json.loads(offchain.to_json(request)) == json.loads(expected) assert request == offchain.from_json(expected, offchain.CommandRequestObject) assert request == offchain.from_json(expected)