Skip to content

Commit 8990e9f

Browse files
authored
Merge pull request corda#3170 from corda/CORDA-1459
Corda 1459
2 parents 2907250 + 1c49043 commit 8990e9f

File tree

6 files changed

+50
-192
lines changed

6 files changed

+50
-192
lines changed

.idea/compiler.xml

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

finance/src/main/kotlin/net/corda/finance/contracts/FinanceTypes.kt

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import net.corda.core.flows.FlowException
1616
import net.corda.core.identity.Party
1717
import net.corda.core.serialization.CordaSerializable
1818
import net.corda.core.transactions.TransactionBuilder
19-
import net.corda.finance.contracts.asset.CommodityContract
2019
import java.math.BigDecimal
2120
import java.time.DayOfWeek
2221
import java.time.LocalDate

finance/src/main/kotlin/net/corda/finance/contracts/asset/CommodityContract.kt

-169
This file was deleted.

finance/src/main/kotlin/net/corda/finance/utils/StateSummingUtilities.kt

-15
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ import net.corda.core.contracts.ContractState
1010
import net.corda.core.contracts.FungibleAsset
1111
import net.corda.core.contracts.Issued
1212
import net.corda.core.identity.AbstractParty
13-
import net.corda.finance.contracts.Commodity
1413
import net.corda.finance.contracts.asset.Cash
15-
import net.corda.finance.contracts.asset.CommodityContract
1614
import net.corda.finance.contracts.asset.Obligation
1715
import java.util.*
1816

@@ -45,19 +43,6 @@ fun <T : Any> Iterable<ContractState>.sumFungibleOrNull() = filterIsInstance<Fun
4543
/** Sums the asset states in the list, returning zero of the given token if there are none. */
4644
fun <T : Any> Iterable<ContractState>.sumFungibleOrZero(token: Issued<T>) = filterIsInstance<FungibleAsset<T>>().map { it.amount }.sumOrZero(token)
4745

48-
/**
49-
* Sums the cash states in the list, throwing an exception if there are none, or if any of the cash
50-
* states cannot be added together (i.e. are different currencies).
51-
*/
52-
fun Iterable<ContractState>.sumCommodities() = filterIsInstance<CommodityContract.State>().map { it.amount }.sumOrThrow()
53-
54-
/** Sums the cash states in the list, returning null if there are none. */
55-
@Suppress("unused")
56-
fun Iterable<ContractState>.sumCommoditiesOrNull() = filterIsInstance<CommodityContract.State>().map { it.amount }.sumOrNull()
57-
58-
/** Sums the cash states in the list, returning zero of the given currency if there are none. */
59-
fun Iterable<ContractState>.sumCommoditiesOrZero(currency: Issued<Commodity>) = filterIsInstance<CommodityContract.State>().map { it.amount }.sumOrZero(currency)
60-
6146
/**
6247
* Sums the obligation states in the list, throwing an exception if there are none. All state objects in the
6348
* list are presumed to be nettable.

finance/src/test/kotlin/net/corda/finance/contracts/asset/ObligationTests.kt

+4-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import net.corda.testing.core.*
2525
import net.corda.testing.dsl.*
2626
import net.corda.testing.internal.TEST_TX_TIME
2727
import net.corda.testing.internal.rigorousMock
28+
import net.corda.testing.internal.vault.CommodityState
2829
import net.corda.testing.node.MockServices
2930
import net.corda.testing.node.ledger
3031
import net.corda.testing.node.transaction
@@ -574,15 +575,15 @@ class ObligationTests {
574575
unverifiedTransaction {
575576
attachments(Obligation.PROGRAM_ID)
576577
output(Obligation.PROGRAM_ID, "Alice's 1 FCOJ obligation to Bob", oneUnitFcojObligation between Pair(ALICE, BOB))
577-
output(Obligation.PROGRAM_ID, "Alice's 1 FCOJ", CommodityContract.State(oneUnitFcoj, ALICE))
578+
output(Obligation.PROGRAM_ID, "Alice's 1 FCOJ", CommodityState(oneUnitFcoj, ALICE))
578579
}
579580
transaction("Settlement") {
580581
attachments(Obligation.PROGRAM_ID)
581582
input("Alice's 1 FCOJ obligation to Bob")
582583
input("Alice's 1 FCOJ")
583-
output(Obligation.PROGRAM_ID, "Bob's 1 FCOJ", CommodityContract.State(oneUnitFcoj, BOB))
584+
output(Obligation.PROGRAM_ID, "Bob's 1 FCOJ", CommodityState(oneUnitFcoj, BOB))
584585
command(ALICE_PUBKEY, Obligation.Commands.Settle(Amount(oneUnitFcoj.quantity, oneUnitFcojObligation.amount.token)))
585-
command(ALICE_PUBKEY, CommodityContract.Commands.Move(Obligation::class.java))
586+
command(ALICE_PUBKEY, Obligation.Commands.Move(Obligation::class.java))
586587
attachment(attachment(commodityContractBytes.inputStream()))
587588
verifies()
588589
}

testing/test-utils/src/main/kotlin/net/corda/testing/internal/vault/VaultFiller.kt

+36-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import net.corda.core.utilities.getOrThrow
1515
import net.corda.finance.contracts.Commodity
1616
import net.corda.finance.contracts.DealState
1717
import net.corda.finance.contracts.asset.Cash
18-
import net.corda.finance.contracts.asset.CommodityContract
18+
import net.corda.finance.contracts.asset.Obligation
19+
import net.corda.finance.contracts.asset.OnLedgerAsset
1920
import net.corda.testing.core.*
2021
import net.corda.testing.internal.chooseIdentity
2122
import net.corda.testing.internal.chooseIdentityAndCert
@@ -166,19 +167,26 @@ class VaultFiller @JvmOverloads constructor(
166167
return Vault(states)
167168
}
168169

170+
171+
/**
172+
* Puts together an issuance transaction for the specified amount that starts out being owned by the given pubkey.
173+
*/
174+
fun generateCommoditiesIssue(tx: TransactionBuilder, amount: Amount<Issued<Commodity>>, owner: AbstractParty, notary: Party)
175+
= OnLedgerAsset.generateIssue(tx, TransactionState(CommodityState(amount, owner), Obligation.PROGRAM_ID, notary), Obligation.Commands.Issue())
176+
177+
169178
/**
170179
*
171180
* @param issuerServices service hub of the issuer node, which will be used to sign the transaction.
172181
* @return a vault object that represents the generated states (it will NOT be the full vault from the service hub!).
173182
*/
174183
// TODO: need to make all FungibleAsset commands (issue, move, exit) generic
175-
fun fillWithSomeTestCommodity(amount: Amount<Commodity>, issuerServices: ServiceHub, issuedBy: PartyAndReference): Vault<CommodityContract.State> {
184+
fun fillWithSomeTestCommodity(amount: Amount<Commodity>, issuerServices: ServiceHub, issuedBy: PartyAndReference): Vault<CommodityState> {
176185
val myKey: PublicKey = services.myInfo.chooseIdentity().owningKey
177186
val me = AnonymousParty(myKey)
178187

179-
val commodity = CommodityContract()
180188
val issuance = TransactionBuilder(null as Party?)
181-
commodity.generateIssue(issuance, Amount(amount.quantity, Issued(issuedBy, amount.token)), me, altNotary)
189+
generateCommoditiesIssue(issuance, Amount(amount.quantity, Issued(issuedBy, amount.token)), me, altNotary)
182190
val transaction = issuerServices.signInitialTransaction(issuance, issuedBy.party.owningKey)
183191
services.recordTransactions(transaction)
184192
return Vault(setOf(transaction.tx.outRef(0)))
@@ -241,3 +249,27 @@ class VaultFiller @JvmOverloads constructor(
241249
return update.getOrThrow(Duration.ofSeconds(3))
242250
}
243251
}
252+
253+
254+
255+
/** A state representing a commodity claim against some party */
256+
data class CommodityState(
257+
override val amount: Amount<Issued<Commodity>>,
258+
259+
/** There must be a MoveCommand signed by this key to claim the amount */
260+
override val owner: AbstractParty
261+
) : FungibleAsset<Commodity> {
262+
constructor(deposit: PartyAndReference, amount: Amount<Commodity>, owner: AbstractParty)
263+
: this(Amount(amount.quantity, Issued(deposit, amount.token)), owner)
264+
265+
override val exitKeys: Set<PublicKey> = Collections.singleton(owner.owningKey)
266+
override val participants = listOf(owner)
267+
268+
override fun withNewOwnerAndAmount(newAmount: Amount<Issued<Commodity>>, newOwner: AbstractParty): FungibleAsset<Commodity>
269+
= copy(amount = amount.copy(newAmount.quantity), owner = newOwner)
270+
271+
override fun toString() = "Commodity($amount at ${amount.token.issuer} owned by $owner)"
272+
273+
override fun withNewOwner(newOwner: AbstractParty) = CommandAndState(Obligation.Commands.Move(), copy(owner = newOwner))
274+
}
275+

0 commit comments

Comments
 (0)