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
package com.template; | |
import net.corda.core.contracts.ContractState; | |
import net.corda.core.identity.AbstractParty; | |
import java.util.Collections; | |
import java.util.List; | |
/** | |
* Define your state object here. |
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
public class TemplateState implements ContractState { | |
private final Party me; | |
private final Integer bankAccountNumber; | |
private final Integer bankAccountBalance; | |
public TemplateState(Party me, Integer bankAccountNumber, Integer bankAccountBalance) { | |
this.me = me; | |
this.bankAccountNumber = bankAccountNumber; | |
this.bankAccountBalance = bankAccountBalance; | |
} |
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
package com.template; | |
import net.corda.core.contracts.CommandData; | |
import net.corda.core.contracts.Contract; | |
import net.corda.core.transactions.LedgerTransaction; | |
/** | |
* Define your contract here. | |
*/ | |
public class TemplateContract implements Contract { | |
// This is used to identify our contract when building a transaction. |
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 will go here | |
*/ | |
} else { | |
throw new IllegalArgumentException("Unrecognized 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
public class ContractTests { | |
MockServices ledgerServices = new MockServices(); | |
static private TestIdentity sandra = new TestIdentity(new CordaX500Name("Sandra Bullocks", "London", "GB")); | |
static private Integer BANK_ACCOUNT_NUMBER = 9999999; | |
@Test | |
public void ShouldHaveNoInputs() { | |
ledger(ledgerServices, (ledger -> { | |
ledger.transaction(tx -> { | |
tx.input(TEMPLATE_CONTRACT_ID, new TemplateState(sandra.getParty(), BANK_ACCOUNT_NUMBER, 1000000)); |
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
public class ContractTests { | |
MockServices ledgerServices = new MockServices(); | |
static private TestIdentity sandra = new TestIdentity( | |
new CordaX500Name("Sandra Bullocks", "London", "GB")); | |
@Test | |
public void dummyTest() { | |
} | |
} |
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, (ledger -> { | |
ledger.transaction(tx -> { | |
/* Create transaction */ | |
return null; | |
}); | |
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) { | |
requireThat(require -> { | |
require.using("There should be no input states", tx.getInputs().isEmpty()); | |
// require.using("Exception message to be thrown", Boolean expression here!); | |
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
@Test | |
public void ShouldNotHaveMoreThanOneOutput() { | |
ledger(ledgerServices, (ledger -> { | |
ledger.transaction(tx -> { | |
tx.output(TEMPLATE_CONTRACT_ID, new TemplateState(sandra.getParty(), BANK_ACCOUNT_NUMBER, 1000000)); | |
tx.output(TEMPLATE_CONTRACT_ID, new TemplateState(sandra.getParty(), BANK_ACCOUNT_NUMBER, 3000000)); | |
tx.command(ImmutableList.of(sandra.getPublicKey()), new TemplateContract.Commands.Action()); | |
tx.failsWith("There should be exactly 1 output state."); | |
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
/* | |
Contract verification rules | |
*/ | |
requireThat(require -> { | |
require.using("There should be no input states", tx.getInputs().isEmpty()); | |
require.using("There should be exactly 1 output state.", tx.getOutputs().size() == 1); | |
require.using("There should be exactly 1 required signers.", command.getSigners().size() == 1); | |
TemplateState output = (TemplateState) tx.getOutputStates().get(0); | |
require.using("Must be signed by party referenced in output state.", command.getSigners().contains(output.getMe().getOwningKey())); | |
require.using("Account Balance must be greater than zero.", output.getBankAccountBalance() > 0); |
OlderNewer