Skip to content

Instantly share code, notes, and snippets.

@brenden-t-r
brenden-t-r / Flow Transfer Task 1.kt
Created January 31, 2019 20:26
Corda Training Solutions - Flows Transfer
@InitiatingFlow
@StartableByRPC
class IOUSettleFlow(val linearId: UniqueIdentifier, val amount: Amount<Currency>): FlowLogic<SignedTransaction>() {
@Suspendable
override fun call(): SignedTransaction {
// Stage 1. Retrieve IOU specified by linearId from the vault.
val queryCriteria = QueryCriteria.LinearStateQueryCriteria(linearId = listOf(linearId))
val iouStateAndRef = serviceHub.vaultService.queryBy<IOUState>(queryCriteria).states.single()
val inputIou = iouStateAndRef.state.data
@brenden-t-r
brenden-t-r / Flow Issue Task 1.kt
Last active April 6, 2019 15:11
Corda Training Solutions - Flows
@InitiatingFlow
@StartableByRPC
class IOUIssueFlow(val state: IOUState): FlowLogic<SignedTransaction>() {
@Suspendable
override fun call(): SignedTransaction {
// Step 1. Get a reference to the notary service on our network and our key pair.
// Note: ongoing work to support multiple notary identities is still in progress.
val notary = serviceHub.networkMapCache.notaryIdentities.first()
// Step 2. Create a new issue command.
@brenden-t-r
brenden-t-r / Contract Settle Task 1-2.kt
Created January 31, 2019 19:50
Corda Training Solutions - Contracts Settle
interface Commands : CommandData {
class Issue : TypeOnlyCommandData(), Commands
class Transfer : TypeOnlyCommandData(), Commands
class Settle : TypeOnlyCommandData(), Commands
}
is Commands.Settle -> {
// Check there is only one group of IOUs and that there is always an input IOU.
val ious = tx.groupStates<IOUState, UniqueIdentifier> { it.linearId }.single()
}
@brenden-t-r
brenden-t-r / Contract Transfer Task 1.kt
Last active February 5, 2019 00:55
Corda Training Solutions - Contract Transfer
interface Commands : CommandData {
class Issue : TypeOnlyCommandData(), Commands
class Transfer : TypeOnlyCommandData(), Commands
}
/*
Add to IOUState
fun withNewLender(newLender: Party) = copy(lender = newLender)
*/
@brenden-t-r
brenden-t-r / Contract Issue Task 1.kt
Last active January 31, 2019 19:33
Corda Training Solutions Kotlin - Contracts Issue
/**
* Add any commands required for this contract as classes within this interface.
* It is useful to encapsulate your commands inside an interface, so you can use the [requireSingleCommand]
* function to check for a number of commands which implement this interface.
*/
interface Commands : CommandData {
class Issue : TypeOnlyCommandData(), Commands
}
// ..
@brenden-t-r
brenden-t-r / States Tasks 1-4.kt
Last active January 31, 2019 19:22
Corda Training Solutions - States
/**
* The IOU State object, with the following properties:
* - [amount] The amount owed by the [borrower] to the [lender]
* - [lender] The lending party.
* - [borrower] The borrowing party.
* - [paid] Records how much of the [amount] has been paid.
*/
data class IOUState(
val amount: Amount<Currency>,
val lender: Party,
/*
* Borrowed from: https://docs.corda.net/tutorial-test-dsl.html
*/
ledgerServices = new MockServices(
// A list of packages to scan for cordapps
singletonList("net.corda.finance.contracts"),
// The identity represented by this set of mock services. Defaults to a test identity.
// You can also use the alternative parameter initialIdentityName which accepts a
// [CordaX500Name]
ledger(ledgerServices, l -> {
/*
Upload an attachment. Must provide and InputStream to a ZIP or JAR file.
Note that the "tx.attachments()" below just adds the hash to the transaction,
whereas this physically uploads a file.
*/
l.attachment( .. InputStream object .. )
// Verified transaction. It must create a valid transaction or call fails()
/*@Test
public void ShouldHaveOnlyOnePartySignature() {
ledger(ledgerServices, (ledger -> {
ledger.transaction(tx -> {
tx.output(TEMPLATE_CONTRACT_ID, new TemplateState(sandra.getParty(), BANK_ACCOUNT_NUMBER, 3000000));
TestIdentity otherParty = new TestIdentity(
new CordaX500Name("Edwardo Norton", "London", "GB"));
tx.command(ImmutableList.of(sandra.getPublicKey(), otherParty.getPublicKey()), new TemplateContract.Commands.Action());
tx.failsWith("There should be exactly 1 required signers.");
return null;
@Override
public void verify(LedgerTransaction tx) {
CommandWithParties<Commands> command = requireSingleCommand(tx.getCommands(), Commands.class);
if (command.getValue() instanceof Commands.Action) {
/*
Contract verification rules
*/
if (!tx.getInputs().isEmpty()) {
throw new IllegalArgumentException("There should be no input states");