In Corda, how to get the timestamp of when a transaction happened?

浪子不回头ぞ 提交于 2019-12-08 10:05:22

问题


I am using Corda 3.2. Given a SignedTransaction, how can I establish when it was recorded?


回答1:


There is no direct API for determining when a transaction was recorded. However, you can achieve this by checking either:

  1. When one of the transaction's inputs was consumed:

    val inputStateRef = signedTx.inputs[0]
    val queryCriteria = QueryCriteria.VaultQueryCriteria(stateRefs = listOf(inputStateRef))
    val results = serviceHub.vaultService.queryBy<ContractState>(queryCriteria)
    val consumedTime = results.statesMetadata.single().consumedTime!!
    
  2. When one of the transaction's outputs was recorded:

    val ledgerTx = signedTx.toLedgerTransaction(serviceHub)
    val outputStateRef = StateRef(signedTx.id, 0)
    val queryCriteria = QueryCriteria.VaultQueryCriteria(stateRefs = listOf(outputStateRef))
    val results = serviceHub.vaultService.queryBy<ContractState>(queryCriteria)
    val recordedTime = results.statesMetadata.single().recordedTime
    


来源:https://stackoverflow.com/questions/51855450/in-corda-how-to-get-the-timestamp-of-when-a-transaction-happened

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!