mirror of
https://github.com/corda/corda.git
synced 2025-05-02 08:43:15 +00:00
Minor: define contracts as regular classes instead of using the kotlin singleton syntax
This commit is contained in:
parent
af4aa3dedc
commit
5285d3bb47
@ -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
|
* 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.
|
* vaults can ignore the issuer/depositRefs and just examine the amount fields.
|
||||||
*/
|
*/
|
||||||
object Cash : Contract {
|
class Cash : Contract {
|
||||||
/**
|
/**
|
||||||
* TODO:
|
* TODO:
|
||||||
* 1) hash should be of the contents, not the URI
|
* 1) hash should be of the contents, not the URI
|
||||||
|
@ -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
|
* 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
|
* 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).
|
* 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
|
* 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:
|
* Open issues:
|
||||||
* - In this model, you cannot merge or split CP. Can you do this normally? We could model CP as a specialised form
|
* - 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")
|
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.
|
// 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
|
// 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")
|
override val legalContractReference: SecureHash = SecureHash.sha256("https://en.wikipedia.org/wiki/Commercial_paper")
|
||||||
|
|
||||||
|
@ -91,7 +91,7 @@ class CashTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
val ptx = PartialTransaction()
|
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())
|
assertTrue(ptx.inputStates().isEmpty())
|
||||||
val s = ptx.outputStates()[0] as Cash.State
|
val s = ptx.outputStates()[0] as Cash.State
|
||||||
assertEquals(100.DOLLARS, s.amount)
|
assertEquals(100.DOLLARS, s.amount)
|
||||||
@ -300,7 +300,7 @@ class CashTests {
|
|||||||
|
|
||||||
fun makeSpend(amount: Amount, dest: PublicKey): WireTransaction {
|
fun makeSpend(amount: Amount, dest: PublicKey): WireTransaction {
|
||||||
val tx = PartialTransaction()
|
val tx = PartialTransaction()
|
||||||
Cash.craftSpend(tx, amount, dest, WALLET)
|
Cash().craftSpend(tx, amount, dest, WALLET)
|
||||||
return tx.toWireTransaction()
|
return tx.toWireTransaction()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,6 +22,17 @@ class CommercialPaperTests {
|
|||||||
val CASH_2 = CASH_1.copy(owner = DUMMY_PUBKEY_2)
|
val CASH_2 = CASH_1.copy(owner = DUMMY_PUBKEY_2)
|
||||||
val CASH_3 = CASH_1.copy(owner = DUMMY_PUBKEY_1)
|
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
|
@Test
|
||||||
fun move() {
|
fun move() {
|
||||||
transaction {
|
transaction {
|
||||||
|
@ -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
|
// 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.
|
// a sandbox. For now we just instantiate right at the start of the program.
|
||||||
val TEST_PROGRAM_MAP: Map<SecureHash, Contract> = mapOf(
|
val TEST_PROGRAM_MAP: Map<SecureHash, Contract> = mapOf(
|
||||||
CASH_PROGRAM_ID to Cash,
|
CASH_PROGRAM_ID to Cash(),
|
||||||
CP_PROGRAM_ID to CommercialPaper,
|
CP_PROGRAM_ID to CommercialPaper(),
|
||||||
DUMMY_PROGRAM_ID to DummyContract
|
DUMMY_PROGRAM_ID to DummyContract
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user