Contracts: add an issue crafting function to the cash contract.

This commit is contained in:
Mike Hearn 2015-11-16 20:50:31 +01:00
parent aecc1de0cf
commit 28bd2053cc
3 changed files with 26 additions and 0 deletions

View File

@ -136,6 +136,16 @@ object Cash : Contract {
}
}
/**
* Puts together an issuance transaction for the specified amount that starts out being owned by the given pubkey.
*/
fun craftIssue(tx: PartialTransaction, amount: Amount, at: InstitutionReference, owner: PublicKey) {
check(tx.inputStates().isEmpty())
check(tx.outputStates().sumCashOrNull() == null)
tx.addOutputState(Cash.State(at, amount, owner))
tx.addArg(WireCommand(Cash.Commands.Issue(), listOf(at.institution.owningKey)))
}
/**
* Generate a transaction that consumes one or more of the given input states to move money to the given pubkey.
* Note that the wallet list is not updated: it's up to you to do that.

View File

@ -108,6 +108,11 @@ class PartialTransaction(private val inputStates: MutableList<ContractStateRef>
check(currentSigs.isEmpty())
args.add(arg)
}
// Accessors that yield immutable snapshots.
fun inputStates(): List<ContractStateRef> = ArrayList(inputStates)
fun outputStates(): List<ContractState> = ArrayList(outputStates)
fun args(): List<WireCommand> = ArrayList(args)
}

View File

@ -8,6 +8,7 @@ import java.security.PublicKey
import java.util.*
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertTrue
class CashTests {
val inState = Cash.State(
@ -87,6 +88,16 @@ class CashTests {
arg(MINI_CORP_KEY) { Cash.Commands.Issue() }
this.accepts()
}
val ptx = PartialTransaction()
Cash.craftIssue(ptx, 100.DOLLARS, InstitutionReference(MINI_CORP, OpaqueBytes.of(12, 34)), owner = DUMMY_PUBKEY_1)
assertTrue(ptx.inputStates().isEmpty())
val s = ptx.outputStates()[0] as Cash.State
assertEquals(100.DOLLARS, s.amount)
assertEquals(MINI_CORP, s.deposit.institution)
assertEquals(DUMMY_PUBKEY_1, s.owner)
assertTrue(ptx.args()[0].command is Cash.Commands.Issue)
assertEquals(MINI_CORP_KEY, ptx.args()[0].pubkeys[0])
}
@Test