How the WebHook uses the blockchain

We will regularly go back to the WebHook after discussing each component around it.

We already know how our blockchain and smart contracts work. They record mobile number registration, and maintain states of transfer transactions. In this section, we discuss how the WebHook function interacts with the blockchain. The following is the snippet from the WebHook function. The lookup method inside the function obtains the smart contract RegistrationRepository and then invokes findByTelNo() on the blockchain. The result is then available inside a transaction receipt. We check what kind of event is stored inside that receipt. If it is an RegistrationFound event, then this method returns a result object containing information regarding the bank name and account number.

There is room for improving this check.

How should the reader optimize the smart contract to fire only one event and meaningfully check whether the telephone number is already registered or not?

That is basically about the lookup part:

@Data
@AllArgsConstructor
static class RegistrationResult {
private String bankName;
private String accountId;
}

public RegistrationResult lookup(String telNo) throws Exception {
val repo = ContractRegistry.registrationRepository();
val receipt = repo.findByTelNo(telNo).send();
val foundEvents = repo.getRegistrationFoundEvents(receipt);
if (foundEvents.isEmpty() == false) {
val reg = foundEvents.get(0);
return new RegistrationResult(reg.bank, reg.accNo);
} else {
val notFoundEvents = repo.getRegistrationNotFoundEvents(receipt);
if(notFoundEvents.isEmpty() == false) {
val reg = notFoundEvents.get(0);
return null;
}
}

throw new Exception("Lookup does not find any event in receipt.");
}

The transfer state management part is implemented inside a set of methods whose names start with transfer.

Here's the method to tell that we start new transactions with ID txId. It uses the ContractRegistry to obtain the smart contract, TransferStateRepository. Then we create a new transaction state and set its state to be STARTED. If everything is OK, we should get a transaction receipt from the call with an event, TransferStartedEvent, embedded in the receipt:

private boolean transferStart(String txId) {
try {
val repo = ContractRegistry.transferStateRepository();
val receipt = repo.start(txId).send();
val events = repo.getTransferStartedEvents(receipt);
if (events.isEmpty()) {
return false;
}
return true;
} catch (Exception e) {
return false;
}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset