This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Commands : CommandData { | |
class Issue : TypeOnlyCommandData(), Commands | |
class Transfer : TypeOnlyCommandData(), Commands | |
} | |
/* | |
Add to IOUState | |
fun withNewLender(newLender: Party) = copy(lender = newLender) | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 | |
} | |
// .. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*@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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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"); |