Minor: define contracts as regular classes instead of using the kotlin singleton syntax

This commit is contained in:
Mike Hearn 2015-11-24 15:22:19 +01:00
parent af4aa3dedc
commit 5285d3bb47
5 changed files with 22 additions and 9 deletions

View File

@ -28,7 +28,7 @@ class InsufficientBalanceException(val amountMissing: Amount) : Exception()
* At the same time, other contracts that just want money and don't care much who is currently holding it in their
* vaults can ignore the issuer/depositRefs and just examine the amount fields.
*/
object Cash : Contract {
class Cash : Contract {
/**
* TODO:
* 1) hash should be of the contents, not the URI

View File

@ -6,9 +6,9 @@ import java.time.Instant
/**
* This is an ultra-trivial implementation of commercial paper, which is essentially a simpler version of a corporate
* bond. It can be seen as a company-specific currency. A company issues CP with a particular face value, like $100
* bond. It can be seen as a company-specific currency. A company issues CP with a particular face value, say $100,
* but sells it for less, say $90. The paper can be redeemed for cash at a given date in the future. Thus this example
* would have a 10% interest rate with a single repayment. Commercial paper is often rolled out (redeemed and the
* would have a 10% interest rate with a single repayment. Commercial paper is often rolled over (redeemed and the
* money used to immediately rebuy).
*
* This contract is not intended to realistically model CP. It is here only to act as a next step up above cash in
@ -16,13 +16,15 @@ import java.time.Instant
*
* Open issues:
* - In this model, you cannot merge or split CP. Can you do this normally? We could model CP as a specialised form
* of cash, or reuse some of the cash code?
* of cash, or reuse some of the cash code? Waiting on response from Ayoub and Rajar about whether CP can always
* be split/merged or only in secondary markets. Even if current systems can't do this, would it be a desirable
* feature to have anyway?
*/
val CP_PROGRAM_ID = SecureHash.sha256("replace-me-later-with-bytecode-hash")
// TODO: Generalise the notion of an owned instrument into a superclass/supercontract. Consider composition vs inheritance.
object CommercialPaper : Contract {
class CommercialPaper : Contract {
// TODO: should reference the content of the legal agreement, not its URI
override val legalContractReference: SecureHash = SecureHash.sha256("https://en.wikipedia.org/wiki/Commercial_paper")

View File

@ -91,7 +91,7 @@ class CashTests {
}
val ptx = PartialTransaction()
Cash.craftIssue(ptx, 100.DOLLARS, InstitutionReference(MINI_CORP, OpaqueBytes.of(12, 34)), owner = DUMMY_PUBKEY_1)
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)
@ -300,7 +300,7 @@ class CashTests {
fun makeSpend(amount: Amount, dest: PublicKey): WireTransaction {
val tx = PartialTransaction()
Cash.craftSpend(tx, amount, dest, WALLET)
Cash().craftSpend(tx, amount, dest, WALLET)
return tx.toWireTransaction()
}

View File

@ -22,6 +22,17 @@ class CommercialPaperTests {
val CASH_2 = CASH_1.copy(owner = DUMMY_PUBKEY_2)
val CASH_3 = CASH_1.copy(owner = DUMMY_PUBKEY_1)
val A_THOUSAND_DOLLARS = Cash.State(InstitutionReference(MINI_CORP, OpaqueBytes.of(1,2,3)), 1000.DOLLARS, ALICE)
@Test
fun move2() {
transactionGroup {
transaction {
output { A_THOUSAND_DOLLARS `owned by` MINI_CORP_KEY }
}
}
}
@Test
fun move() {
transaction {

View File

@ -36,8 +36,8 @@ val TEST_TX_TIME = Instant.parse("2015-04-17T12:00:00.00Z")
// In a real system this would be a persistent map of hash to bytecode and we'd instantiate the object as needed inside
// a sandbox. For now we just instantiate right at the start of the program.
val TEST_PROGRAM_MAP: Map<SecureHash, Contract> = mapOf(
CASH_PROGRAM_ID to Cash,
CP_PROGRAM_ID to CommercialPaper,
CASH_PROGRAM_ID to Cash(),
CP_PROGRAM_ID to CommercialPaper(),
DUMMY_PROGRAM_ID to DummyContract
)