1+ /*
2+ * Copyright IBM Corp. All Rights Reserved.
3+ *
4+ * SPDX-License-Identifier: Apache-2.0
5+ */
6+
7+ package com.cordaSimpleApplication.client
8+
9+ import com.cordaSimpleApplication.flow.IssueAssetState
10+ import com.cordaSimpleApplication.flow.GetStatesByTokenType
11+ import com.cordaSimpleApplication.flow.IssueAssetStateFromStateRef
12+ import com.cordaSimpleApplication.flow.DeleteAssetState
13+ import com.cordaSimpleApplication.flow.GetAssetStateByLinearId
14+ import com.cordaSimpleApplication.flow.RetrieveStateAndRef
15+ import com.cordaSimpleApplication.flow.MergeAssetStates
16+ import com.cordaSimpleApplication.flow.SplitAssetState
17+
18+ import com.cordaSimpleApplication.flow.TransferAssetStateInitiator
19+ import com.cordaSimpleApplication.state.AssetState
20+ import com.github.ajalt.clikt.core.CliktCommand
21+ import com.github.ajalt.clikt.core.requireObject
22+ import com.github.ajalt.clikt.parameters.arguments.argument
23+ import net.corda.core.identity.CordaX500Name
24+ import java.lang.Exception
25+ import net.corda.core.messaging.startFlow
26+
27+ /* *
28+ * The CLI command used to trigger a CreateState flow.
29+ *
30+ * @property quantity The numberOfTokens for the [AssetState].
31+ * @property tokenType The tokenType for the [AssetState].
32+ */
33+ class IssueAssetStateCommand : CliktCommand (help = " Invokes the IssueAssetState flow. Requires a key and value" ) {
34+ private val quantity: String by argument()
35+ private val tokenType: String by argument()
36+ val config by requireObject<Map <String , String >>()
37+ override fun run () {
38+ issueAssetStateHelper(quantity.toLong(), tokenType, config)
39+ }
40+ }
41+
42+ /* *
43+ * Helper function used by IssueAssetStateCommand
44+ */
45+ fun issueAssetStateHelper (numberOfTokens : Long , tokenType : String , config : Map <String , String >) {
46+ val rpc = NodeRPCConnection (
47+ host = config[" CORDA_HOST" ]!! ,
48+ username = " clientUser1" ,
49+ password = " test" ,
50+ rpcPort = config[" CORDA_PORT" ]!! .toInt())
51+ try {
52+ println (" IssueAsset flow with arguments $numberOfTokens , $tokenType " )
53+ val proxy = rpc.proxy
54+ val createdState = proxy.startFlow(::IssueAssetState , numberOfTokens, tokenType)
55+ .returnValue.get().tx.outputStates.first() as AssetState
56+ println (createdState)
57+ } catch (e: Exception ) {
58+ println (e.toString())
59+ } finally {
60+ rpc.close()
61+ }
62+ }
63+
64+ class IssueAssetStateFromStateRefCommand : CliktCommand (help = " Invokes the IssueAssetStateFromStateRef flow. Requires a linearId" ) {
65+ private val linearId: String by argument()
66+ val config by requireObject<Map <String , String >>()
67+ override fun run () {
68+ val rpc = NodeRPCConnection (
69+ host = config[" CORDA_HOST" ]!! ,
70+ username = " clientUser1" ,
71+ password = " test" ,
72+ rpcPort = config[" CORDA_PORT" ]!! .toInt())
73+ try {
74+ println (" IssueAsset flow with arguments $linearId " )
75+ val proxy = rpc.proxy
76+ val createdState = proxy.startFlow(::IssueAssetStateFromStateRef , linearId)
77+ .returnValue.get().tx.outputStates.first() as AssetState
78+ println (createdState)
79+ } catch (e: Exception ) {
80+ println (e.toString())
81+ } finally {
82+ rpc.close()
83+ }
84+ }
85+ }
86+
87+ /* *
88+ * The CLI command used to trigger a GetStatesByTokenType flow.
89+ *
90+ * @property tokenType The filter criteria for the [AssetState]s to be retrieved.
91+ */
92+ class GetAssetStatesByTypeCommand : CliktCommand (help = " Get asset states by token type. Requires a token type" ) {
93+ private val tokenType: String by argument()
94+ val config by requireObject<Map <String , String >>()
95+ override fun run () {
96+ println (" Get states with type $tokenType " )
97+ val rpc = NodeRPCConnection (
98+ host = config[" CORDA_HOST" ]!! ,
99+ username = " clientUser1" ,
100+ password = " test" ,
101+ rpcPort = config[" CORDA_PORT" ]!! .toInt())
102+ try {
103+ val proxy = rpc.proxy
104+ val states = proxy.startFlow(::GetStatesByTokenType , tokenType)
105+ .returnValue.get()
106+ println (states.toString(Charsets .UTF_8 ))
107+ } catch (e: Exception ) {
108+ println (e.toString())
109+ } finally {
110+ rpc.close()
111+ }
112+ }
113+ }
114+
115+ /* *
116+ * The CLI command used to trigger a GetAssetStateByLinearId flow.
117+ *
118+ * @property linearId The linearId for the [AssetState] to be retrieved.
119+ */
120+ class GetAssetStateByLinearIdCommand : CliktCommand (help = " Gets asset state by linearId. Requires a linearId" ) {
121+ private val linearId: String by argument()
122+ val config by requireObject<Map <String , String >>()
123+ override fun run () {
124+ println (" Get state with linearId $linearId " )
125+ val rpc = NodeRPCConnection (
126+ host = config[" CORDA_HOST" ]!! ,
127+ username = " clientUser1" ,
128+ password = " test" ,
129+ rpcPort = config[" CORDA_PORT" ]!! .toInt())
130+ try {
131+ val proxy = rpc.proxy
132+ val state = proxy.startFlow(::GetAssetStateByLinearId , linearId)
133+ .returnValue.get()
134+ println (state)
135+ } catch (e: Exception ) {
136+ println (e.toString())
137+ } finally {
138+ rpc.close()
139+ }
140+ }
141+ }
142+
143+ /* *
144+ * The CLI command used to trigger a DeleteAssetState flow.
145+ *
146+ * @property linearId The filter for the [AssetState] to be deleted.
147+ */
148+ class DeleteAssetStateCommand : CliktCommand (help = " Invokes the DeleteAssetState flow. Requires a linearId" ) {
149+ private val linearId: String by argument()
150+ val config by requireObject<Map <String , String >>()
151+ override fun run () {
152+ println (" DeleteAssetState flow with linearId $linearId " )
153+ val rpc = NodeRPCConnection (
154+ host = config[" CORDA_HOST" ]!! ,
155+ username = " clientUser1" ,
156+ password = " test" ,
157+ rpcPort = config[" CORDA_PORT" ]!! .toInt())
158+ try {
159+ val proxy = rpc.proxy
160+ val deletedState = proxy.startFlow(::DeleteAssetState , linearId)
161+ .returnValue.get().inputs.first()
162+ println (deletedState)
163+ } catch (e: Exception ) {
164+ println (e.toString())
165+ } finally {
166+ rpc.close()
167+ }
168+ }
169+ }
170+
171+ /* *
172+ * The CLI command used to trigger a MergeAssetStates flow.
173+ *
174+ * @property linearId1 The filter for the first token asset [AssetState] used in the merge operation.
175+ * @property linearId2 The filter for the second token asset [AssetState] used in the merge operation.
176+ */
177+ class MergeAssetStatesCommand : CliktCommand (help = " Invokes the MergeAssetStates flow. Requires two linearIds" ) {
178+ private val linearId1: String by argument()
179+ private val linearId2: String by argument()
180+ val config by requireObject<Map <String , String >>()
181+ override fun run () {
182+ println (" MergeAssetStates flow with linearIds $linearId1 and $linearId2 " )
183+ val rpc = NodeRPCConnection (
184+ host = config[" CORDA_HOST" ]!! ,
185+ username = " clientUser1" ,
186+ password = " test" ,
187+ rpcPort = config[" CORDA_PORT" ]!! .toInt())
188+ try {
189+ val proxy = rpc.proxy
190+ val mergedState = proxy.startFlow(::MergeAssetStates , linearId1, linearId2)
191+ .returnValue.get().inputs.first()
192+ println (mergedState)
193+ } catch (e: Exception ) {
194+ println (e.toString())
195+ } finally {
196+ rpc.close()
197+ }
198+ }
199+ }
200+
201+ /* *
202+ * The CLI command used to trigger a RetrieveStateAndRef flow.
203+ *
204+ * @property tokenType The token type of [AssetState] to be retrieved.
205+ * @property quantity The number of fungible tokens in [AssetState] to be retrieved.
206+ */
207+ class RetrieveAssetStateAndRefCommand : CliktCommand (help = " Invokes the RetrieveStateAndRef flow. Requires tokenType and quantity" ) {
208+ val tokenType: String by argument()
209+ val quantity: String by argument()
210+ val config by requireObject<Map <String , String >>()
211+ override fun run () {
212+ println (" RetrieveStateAndRef flow with tokenType $tokenType and $quantity " )
213+ val rpc = NodeRPCConnection (
214+ host = config[" CORDA_HOST" ]!! ,
215+ username = " clientUser1" ,
216+ password = " test" ,
217+ rpcPort = config[" CORDA_PORT" ]!! .toInt())
218+ try {
219+ val proxy = rpc.proxy
220+ val stateAndRef = proxy.startFlow(::RetrieveStateAndRef , tokenType, quantity.toLong())
221+ .returnValue.get()
222+ println (stateAndRef.toString())
223+ } catch (e: Exception ) {
224+ println (e.toString())
225+ } finally {
226+ rpc.close()
227+ }
228+ }
229+ }
230+
231+
232+ /* *
233+ * The CLI command used to trigger a SplitAssetState flow.
234+ *
235+ * @property linearId The filter for the [AssetState] to be split.
236+ * @property quantity1 The number of fungible tokens in the first [AssetState] created after split.
237+ * @property quantity2 The number of fungible tokens in the second [AssetState] created after split.
238+ */
239+ class SplitAssetStateCommand : CliktCommand (help = " Invokes the SplitAssetState flow. Requires a linearId" ) {
240+ private val linearId: String by argument()
241+ private val quantity1: String by argument()
242+ private val quantity2: String by argument()
243+ val config by requireObject<Map <String , String >>()
244+ override fun run () {
245+ println (" SplitAssetState flow with linearId $linearId " )
246+ val rpc = NodeRPCConnection (
247+ host = config[" CORDA_HOST" ]!! ,
248+ username = " clientUser1" ,
249+ password = " test" ,
250+ rpcPort = config[" CORDA_PORT" ]!! .toInt())
251+ try {
252+ val proxy = rpc.proxy
253+ val outputStates = proxy.startFlow(::SplitAssetState , linearId, quantity1.toLong(), quantity2.toLong())
254+ .returnValue.get().inputs
255+ println (outputStates)
256+ } catch (e: Exception ) {
257+ println (e.toString())
258+ } finally {
259+ rpc.close()
260+ }
261+ }
262+ }
263+
264+ /* *
265+ * The CLI command used to trigger a TransferAssetStateInitiator flow.
266+ *
267+ * @property linearId The filter for the [AssetState] to be transferred.
268+ * @property otherParty The Party to whom the [AssetState] will be transferred to
269+ */
270+ class TransferAssetStateCommand : CliktCommand (help = " Invokes the TransferAssetState flow. Requires a linearId and otherParty" ) {
271+ private val linearId: String by argument()
272+ private val partyName: String by argument()
273+ val config by requireObject<Map <String , String >>()
274+ override fun run () {
275+ println (" TransferAssetState flow with linearId $linearId " )
276+ val rpc = NodeRPCConnection (
277+ host = config[" CORDA_HOST" ]!! ,
278+ username = " clientUser1" ,
279+ password = " test" ,
280+ rpcPort = config[" CORDA_PORT" ]!! .toInt())
281+ try {
282+ val proxy = rpc.proxy
283+ val partyX500Name = CordaX500Name .parse(partyName)
284+ val otherParty = proxy.wellKnownPartyFromX500Name(partyX500Name) ? : return println (" Party named $partyName cannot be found.\n " )
285+ val outputStates = proxy.startFlow(::TransferAssetStateInitiator , linearId, otherParty)
286+ .returnValue.get().inputs
287+ println (outputStates)
288+ } catch (e: Exception ) {
289+ println (e.toString())
290+ } finally {
291+ rpc.close()
292+ }
293+ }
294+ }
0 commit comments