Skip to content

Instantly share code, notes, and snippets.

@brenden-t-r
brenden-t-r / TemplateState.java
Created May 30, 2018 15:46
Original template state
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.
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.
@brenden-t-r
brenden-t-r / TemplateContract.java
Created May 30, 2018 15:55
TemplateContract.java
@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.");
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;
}
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));
public class ContractTests {
MockServices ledgerServices = new MockServices();
static private TestIdentity sandra = new TestIdentity(
new CordaX500Name("Sandra Bullocks", "London", "GB"));
@Test
public void dummyTest() {
}
}
ledger(ledgerServices, (ledger -> {
ledger.transaction(tx -> {
/* Create transaction */
return null;
});
return null;
}));
@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;
});
@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;
});
/*
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);