Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.magiadigital.cobranzas.contract
- import com.magiadigital.cobranzas.state.DocumentState
- import net.corda.core.contracts.*
- import net.corda.core.crypto.SecureHash
- import net.corda.core.node.ServiceHub
- import net.corda.core.random63BitValue
- /**
- * A implementation of a basic smart contract in Corda.
- *
- * This contract enforces rules regarding the creation of a valid [DocumentState], which in turn encapsulates a [Document].
- *
- * For a new [Document] to be issued onto the ledger, a transaction is required which takes:
- * - Zero input states.
- * - One output state: the new [Document].
- * - An Create() command with the public keys of both the sender and the recipient.
- *
- * All contracts must sub-class the [Contract] interface.
- */
- open class DocumentContract : Contract {
- /**
- * The verify() function of all the states' contracts must not throw an exception for a transaction to be
- * considered valid.
- */
- /** This is a reference to the underlying legal contract template and associated parameters. */
- override val legalContractReference: SecureHash = SecureHash.sha256("Contrato de recaudacion y cobranzas Globokas - Magic Chain")
- /**
- * This contract only implements two command, Create and Collect.
- */
- interface Commands : CommandData {
- class Create : TypeOnlyCommandData(), Commands
- class Collect : TypeOnlyCommandData(), Commands
- }
- override fun verify(tx: TransactionForContract) {
- // Group by everything except owner: any modification to the CP at all is considered changing it fundamentally.
- //val groups = tx.groupStates(DocumentState::linearId)
- println("tx.inputs.size = " + tx.inputs.size)
- println("tx.outputs.size = " + tx.outputs.size)
- val command = tx.commands.requireSingleCommand<DocumentContract.Commands>()
- when (command.value) {
- is Commands.Create -> {
- requireThat {
- // Generic constraints around the Collect transaction.
- "No inputs should be consumed when issuing a Collection Document." by (tx.inputs.isEmpty())
- "Only one output state should be created." by (tx.outputs.size == 1)
- val out = tx.outputs.single() as DocumentState
- "The sender and the recipient cannot be the same entity." by (out.company != out.collector)
- //"All of the participants must be signers." by (command.signers.containsAll(out.participants))
- // Document-specific constraints.
- "The Document's value must be non-negative." by (out.document.amount > 0)
- "The collect document flagCharged must be false." by (out.flagCharged == false)
- }
- }
- is Commands.Collect -> {
- val stateGroups = tx.groupStates(DocumentState::class.java) { it.linearId }
- require(stateGroups.size == 1) { "Must be only a single document collect in transaction" }
- requireThat {
- val input = tx.inputs.single() as DocumentState
- // Generic constraints around the Collect transaction.
- "the collect document is propagated" by (tx.outputs.size == 1)
- "This document its not collect" by (input.flagCharged != true)
- }
- }
- // TODO: Think about how to evolve contracts over time with new commands.
- else -> throw IllegalArgumentException("Unrecognised command $command")
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement