MohamedLEGH
MohamedLEGH

Reputation: 311

Find a state by unique ID in a cordapp api/flow

Can I find a state with a txhash ? I want something like this : val state = rpcOps.findStateFromTXhash(txhash)

I have found that there is a type of state called linearState that have a linearId property . There is also a hash property but I don't know if it is what I search.

Upvotes: 0

Views: 286

Answers (2)

Joel
Joel

Reputation: 23140

There is no RPC operation to load a transaction's states given a transaction ID.

However, you can write a flow to do this, as follows, then invoke this flow via RPC:

@InitiatingFlow
@StartableByRPC
class GetStatesFromTransactionFlow(val transactionID: SecureHash) : FlowLogic<List<ContractState>>() {

    @Suspendable
    override fun call(): List<ContractState> {
        val signedTransaction = serviceHub.validatedTransactions.getTransaction(transactionID)

        if (signedTransaction == null) {
            throw FlowException("Transaction does not exist in node's transaction storage.")
        }

        val ledgerTransaction = signedTransaction.toLedgerTransaction(serviceHub)
        val inputs = ledgerTransaction.inputs.map { it.state.data }
        val outputs = ledgerTransaction.outputs.map { it.data }

        return inputs + outputs
    }
}

Upvotes: 0

Kid101
Kid101

Reputation: 1470

In your flows, you can do getServiceHub().loadState() here you can pass in the securehash to get your state. not sure if we can do something like that directly from CordaRpcConnection object.

Your state will have a linearId if it's a type of linear state. You can search your state using the linearId easily. read here. I'd recommend you read more about states to see what best suits your requirement. Link

Upvotes: 1

Related Questions