Skip to content

Instantly share code, notes, and snippets.

@anixon604
Last active July 30, 2020 16:19
Show Gist options
  • Save anixon604/5a324e4e63a334be206211fd4ad94d4e to your computer and use it in GitHub Desktop.
Save anixon604/5a324e4e63a334be206211fd4ad94d4e to your computer and use it in GitHub Desktop.
A Corda contract demonstrating business process for issuance and sale of medical drug.
public void verify(@NotNull LedgerTransaction tx) throws IllegalArgumentException {
CommandWithParties<DrugCommands> command = requireSingleCommand(tx.getCommands(), DrugCommands.class);
DrugCommands commandType = command.getValue();
if (commandType instanceof DrugCommands.IssueDrug) {
verifyIssuance(tx, command);
} else if (commandType instanceof DrugCommands.SellDrug) {
verifySale(tx, command);
} else {
throw new IllegalArgumentException("Command not recognized");
}
}
private void verifyIssuance(LedgerTransaction tx, CommandWithParties<DrugCommands> command) {
requireThat(require -> {
require.using("Drug issuance should consume no input states",
tx.getInputs().isEmpty());
require.using("Drug issuance should have one output state",
tx.getOutputs().size() == 1);
final Drug out = tx.outputsOfType(Drug.class).get(0);
require.using("Manufacturer must be initial owner/holder",
out.getOwner().equals(out.getManufacturer()));
// STAGE 1
require.using("Manufacturer must sign",
command.getSigners().contains(out.getManufacturer().getOwningKey()));
return null;
});
}
// For simplicity, batches will be sold in whole
private void verifySale(LedgerTransaction tx, CommandWithParties<DrugCommands> command) {
requireThat(require -> {
require.using("Drug sale should consume one input state",
tx.getInputs().size() == 1);
require.using("Drug sale should have one output state",
tx.getOutputs().size() == 1);
Drug in = tx.inputsOfType(Drug.class).get(0);
Drug out = tx.outputsOfType(Drug.class).get(0);
require.using("Output and input properties should match w/ new owner",
in.equals(out)); // .equals() override (-owner) in state class
// STAGE 2 a)
require.using("Drug should have passed inspection",
in.isInspected());
Drug out = tx.outputsOfType(Drug.class).get(0);
// STAGE 2 b)
require.using("Qty must conform to mins and maximums by law",
out.getQty() > 200 && out.getQty() < 2000);
// STAGE 2 c)
DrugCommands.SellDrug sellDrugCommand = (DrugCommands.SellDrug) command.getValue();
if (out.getQty() > 1500) {
require.using("Large Qty sales must notify Regulator",
out.getParticipants().contains(sellDrugCommand.getRegulator()));
}
// STAGE 3
require.using("Manufacturer must sign",
command.getSigners().contains(out.getManufacturer().getOwningKey()));
require.using("Purchaser must sign",
command.getSigners().contains(out.getOwner().getOwningKey()));
return null;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment