mirror of
https://github.com/corda/corda.git
synced 2025-02-01 16:58:27 +00:00
Update contract tutorial example to match JavaCommercialPaper
This commit is contained in:
parent
52be94866a
commit
e09710e421
@ -175,8 +175,8 @@ public class JavaCommercialPaper implements Contract {
|
|||||||
State output = single(outputs);
|
State output = single(outputs);
|
||||||
TimestampCommand timestampCommand = tx.getTimestampBy(issueCommand.notary);
|
TimestampCommand timestampCommand = tx.getTimestampBy(issueCommand.notary);
|
||||||
Instant time = null == timestampCommand
|
Instant time = null == timestampCommand
|
||||||
?
|
? null
|
||||||
null : timestampCommand.getBefore();
|
: timestampCommand.getBefore();
|
||||||
|
|
||||||
requireThat(require -> {
|
requireThat(require -> {
|
||||||
require.by("output values sum to more than the inputs", inputs.isEmpty());
|
require.by("output values sum to more than the inputs", inputs.isEmpty());
|
||||||
@ -206,8 +206,8 @@ public class JavaCommercialPaper implements Contract {
|
|||||||
} else if (cmd.getValue() instanceof JavaCommercialPaper.Commands.Redeem) {
|
} else if (cmd.getValue() instanceof JavaCommercialPaper.Commands.Redeem) {
|
||||||
TimestampCommand timestampCommand = tx.getTimestampBy(((Commands.Redeem) cmd.getValue()).notary);
|
TimestampCommand timestampCommand = tx.getTimestampBy(((Commands.Redeem) cmd.getValue()).notary);
|
||||||
Instant time = null == timestampCommand
|
Instant time = null == timestampCommand
|
||||||
?
|
? null
|
||||||
null : timestampCommand.getBefore();
|
: timestampCommand.getBefore();
|
||||||
Amount<Issued<Currency>> received = CashKt.sumCashBy(tx.getOutputs(), input.getOwner());
|
Amount<Issued<Currency>> received = CashKt.sumCashBy(tx.getOutputs(), input.getOwner());
|
||||||
|
|
||||||
requireThat(require -> {
|
requireThat(require -> {
|
||||||
|
@ -421,29 +421,34 @@ logic.
|
|||||||
// For now do not allow multiple pieces of CP to trade in a single transaction. Study this more!
|
// For now do not allow multiple pieces of CP to trade in a single transaction. Study this more!
|
||||||
State input = single(filterIsInstance(inputs, State.class));
|
State input = single(filterIsInstance(inputs, State.class));
|
||||||
|
|
||||||
if (!cmd.getSigners().contains(input.getOwner()))
|
requireThat(require -> {
|
||||||
throw new IllegalStateException("Failed requirement: the transaction is signed by the owner of the CP");
|
require.by("the transaction is signed by the owner of the CP", cmd.getSigners().contains(input.getOwner()));
|
||||||
|
return Unit.INSTANCE;
|
||||||
|
});
|
||||||
|
|
||||||
if (cmd.getValue() instanceof JavaCommercialPaper.Commands.Move) {
|
if (cmd.getValue() instanceof JavaCommercialPaper.Commands.Move) {
|
||||||
// Check the output CP state is the same as the input state, ignoring the owner field.
|
requireThat(require -> {
|
||||||
State output = single(outputs);
|
require.by("the state is propagated", outputs.size() == 1);
|
||||||
|
return Unit.INSTANCE;
|
||||||
if (!output.getFaceValue().equals(input.getFaceValue()) ||
|
});
|
||||||
!output.getIssuance().equals(input.getIssuance()) ||
|
// Don't need to check anything else, as if outputs.size == 1 then the output is equal to
|
||||||
!output.getMaturityDate().equals(input.getMaturityDate()))
|
// the input ignoring the owner field due to the grouping.
|
||||||
throw new IllegalStateException("Failed requirement: the output state is the same as the input state except for owner");
|
|
||||||
} else if (cmd.getValue() instanceof JavaCommercialPaper.Commands.Redeem) {
|
} else if (cmd.getValue() instanceof JavaCommercialPaper.Commands.Redeem) {
|
||||||
Amount received = CashKt.sumCashOrNull(inputs);
|
TimestampCommand timestampCommand = tx.getTimestampBy(((Commands.Redeem) cmd.getValue()).notary);
|
||||||
if (time == null)
|
Instant time = null == timestampCommand
|
||||||
throw new IllegalArgumentException("Redemption transactions must be timestamped");
|
? null
|
||||||
if (received == null)
|
: timestampCommand.getBefore();
|
||||||
throw new IllegalStateException("Failed requirement: no cash being redeemed");
|
Amount<Issued<Currency>> received = CashKt.sumCashBy(tx.getOutputs(), input.getOwner());
|
||||||
if (input.getMaturityDate().isAfter(time))
|
|
||||||
throw new IllegalStateException("Failed requirement: the paper must have matured");
|
requireThat(require -> {
|
||||||
if (!input.getFaceValue().equals(received))
|
require.by("must be timestamped", timestampCommand != null);
|
||||||
throw new IllegalStateException("Failed requirement: the received amount equals the face value");
|
require.by("received amount equals the face value: "
|
||||||
if (!outputs.isEmpty())
|
+ received + " vs " + input.getFaceValue(), received.equals(input.getFaceValue()));
|
||||||
throw new IllegalStateException("Failed requirement: the paper must be destroyed");
|
require.by("the paper must have matured", time != null && !time.isBefore(input.getMaturityDate()));
|
||||||
|
require.by("the received amount equals the face value", input.getFaceValue().equals(received));
|
||||||
|
require.by("the paper must be destroyed", outputs.isEmpty());
|
||||||
|
return Unit.INSTANCE;
|
||||||
|
});
|
||||||
} else if (cmd.getValue() instanceof JavaCommercialPaper.Commands.Issue) {
|
} else if (cmd.getValue() instanceof JavaCommercialPaper.Commands.Issue) {
|
||||||
// .. etc .. (see Kotlin for full definition)
|
// .. etc .. (see Kotlin for full definition)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user