Transfer flow example solution
- Reading time: 2 mins
- Discuss on Slack
An example transfer IOU flow
You looked at the Issue flow solution example and then worked on your own Transfer flow.
TransferFlows
The transfer flow will allow the lender to transfer the IOU to a new lender. This flow could be developed on the same concepts mentioned in the previous chapter. So, here we won’t discuss what has already been covered but will see what is different. For quick reference find the code in Java.
What the transfer initiator flow does in a nutshell is:
- Collect the required information.
- Generate the transaction.
- Verify it.
- Sign the transaction.
- Collect signatures from other lender and borrower.
- Request a signature from the notary.
- Send it over to all holders.
And all the transfer responder flow does is:
- Sign the transaction.
- Accept the fully signed transaction.
As this is not an issuance transaction, the flow must take an input state. Thus, to collect the required information, this flow will query the vault. All it takes as an input in constructor is stateLinearId and the new lender.
public static class InitiatorFlow extends FlowLogic<SignedTransaction> {
private final UniqueIdentifier stateLinearId;
private final Party newLender;
public InitiatorFlow(UniqueIdentifier stateLinearId, Party newLender) {
this.stateLinearId = stateLinearId;
this.newLender = newLender;
}
The stateLinearId
will then be used to query the vault. Notice the use of StateAndRef
.
@Suspendable
@Override
public SignedTransaction call() throws FlowException {
// 1. Retrieve the IOU State from the vault using LinearStateQueryCriteria
List<UUID> listOfLinearIds = new ArrayList<>();
listOfLinearIds.add(stateLinearId.getId());
QueryCriteria queryCriteria = new QueryCriteria.LinearStateQueryCriteria(null, listOfLinearIds);
// 2. Get a reference to the inputState data that we are going to settle.
Vault.Page results = getServiceHub().getVaultService().queryBy(IOUState.class, queryCriteria);
StateAndRef inputStateAndRefToTransfer = (StateAndRef) results.getStates().get(0);
IOUState inputStateToTransfer = (IOUState) inputStateAndRefToTransfer.getState().getData();
Once the StateAndRef
has been fetched one could get the required and build the transaction.
The further steps will be similar to the issue flow.
Tests
Once again, the tests are pretty run of the mill. They check that:
- The transaction created is as expected, which includes:
- Signatures.
- Inputs.
- Outputs.
- The transaction has been recorded in vaults.
- States have been recorded, or not, in vaults.
Settle Flow
Hope you were able to get your settle flow running. The settle flow will allow for the settling of the IOU. It won’t need any new concept that hasn’t been discussed. You could find the entire code in the repository. Use the steps in the README of the repository to test your CorDapp.