-
Notifications
You must be signed in to change notification settings - Fork 4
/
ExampleContractTest.kt
115 lines (105 loc) · 4.26 KB
/
ExampleContractTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package com.example.contract.token
import com.example.contract.token.commands.IssuePrivate
import com.example.contract.token.commands.MovePrivate
import com.example.contract.token.commands.RedeemPrivate
import com.example.contract.token.commands.SplitPrivate
import com.ing.zkflow.testing.dsl.zkLedger
import net.corda.testing.core.TestIdentity
import net.corda.testing.node.MockServices
import org.junit.jupiter.api.Test
import java.time.Instant
class ExampleContractTest {
private val services = MockServices(listOf("com.example.contract"))
private val ecb = TestIdentity.fresh("ECB").party
private val alice = TestIdentity.fresh("Alice").party.anonymise()
private val bob = TestIdentity.fresh("Bob").party.anonymise()
private val alicesEuro = digitalEuro(1.00, issuer = ecb, holder = alice)
@Test
fun `Issue one EUR to Alice`() {
services.zkLedger {
transaction {
output(ExampleTokenContract.ID, alicesEuro)
timeWindow(Instant.now())
tweak {
command(listOf(bob.owningKey), IssuePrivate()) // wrong signer
fails()
}
command(listOf(ecb.owningKey), IssuePrivate())
verifies()
}
}
}
@Test
fun `Issue to Alice and move to Bob`() {
services.zkLedger {
val issueTx = transaction {
output(ExampleTokenContract.ID, alicesEuro)
command(listOf(ecb.owningKey), IssuePrivate())
timeWindow(Instant.now())
verifies()
}
transaction {
input(issueTx.outRef<ExampleToken>(0).ref)
command(listOf(alice.owningKey), MovePrivate())
timeWindow(Instant.now())
tweak {
output(ExampleTokenContract.ID, alicesEuro.withNewHolder(bob, 0.9))
`fails with`("Amounts of input and output must equal")
}
tweak {
output(ExampleTokenContract.ID, alicesEuro.withNewHolder(bob))
output(ExampleTokenContract.ID, alicesEuro.withNewHolder(bob))
`fails with`("There should be no additional 'public only' outputs")
}
output(ExampleTokenContract.ID, alicesEuro.withNewHolder(bob))
verifies()
}
}
}
@Test
fun `issue and split`() {
services.zkLedger {
val issueTx = transaction {
output(ExampleTokenContract.ID, alicesEuro)
command(listOf(ecb.owningKey), IssuePrivate())
timeWindow(Instant.now())
verifies()
}
val half = alicesEuro.copy(amount = alicesEuro.amount.splitEvenly(2).first())
transaction {
input(issueTx.outRef<ExampleToken>(0).ref)
output(ExampleTokenContract.ID, half) // change to self
command(listOf(alice.owningKey), SplitPrivate())
timeWindow(Instant.now())
tweak {
output(ExampleTokenContract.ID, half.withNewHolder(bob, 0.1)) // amount total not conserved
`fails with`("Amounts of funds must be constant")
}
output(ExampleTokenContract.ID, half.withNewHolder(bob))
verifies()
}
}
}
@Test
fun `issue and redeem`() {
services.zkLedger {
val issueTx = transaction {
output(ExampleTokenContract.ID, alicesEuro)
command(listOf(ecb.owningKey), IssuePrivate())
timeWindow(Instant.now())
verifies()
}
val half = alicesEuro.copy(amount = alicesEuro.amount.splitEvenly(2).first())
transaction {
input(issueTx.outRef<ExampleToken>(0).ref)
command(listOf(alice.owningKey, ecb.owningKey), RedeemPrivate())
timeWindow(Instant.now())
tweak {
output(ExampleTokenContract.ID, half.withNewHolder(bob, 0.1))
`fails with`("There should be no additional 'public only' outputs")
}
verifies()
}
}
}
}