corda transactionBuilder接受状态列表作为输入状态

时间:2018-08-22 16:13:07

标签: corda

我试图通过添加

将List>作为inputState添加到TransactionBuilder中
TransactionBuilder(notary = notary).
                                    withItems(list of states,
                                    StateAndContract(output State, CONTRACT_ID),
                                    Command)

获取错误的参数类型:类java.util.ArrayList,如何将状态列表作为输入传递给TransactionBuilder?

2 个答案:

答案 0 :(得分:2)

尝试调用withItems(listOfStates),而不是调用withItems(*listOfStates.toTypedArray())

答案 1 :(得分:1)

您可以看到herewithItems模式根据类类型匹配状态。

/** A more convenient way to add items to this transaction that calls the add* methods for you based on type */
fun withItems(vararg items: Any): TransactionBuilder {
    for (t in items) {
        when (t) {
            is StateAndRef<*> -> addInputState(t)
            is SecureHash -> addAttachment(t)
            is TransactionState<*> -> addOutputState(t)
            is StateAndContract -> addOutputState(t.state, t.contract)
            is ContractState -> throw UnsupportedOperationException("Removed as of V1: please use a StateAndContract instead")
            is Command<*> -> addCommand(t)
            is CommandData -> throw IllegalArgumentException("You passed an instance of CommandData, but that lacks the pubkey. You need to wrap it in a Command object first.")
            is TimeWindow -> setTimeWindow(t)
            is PrivacySalt -> setPrivacySalt(t)
            else -> throw IllegalArgumentException("Wrong argument type: ${t.javaClass}")
        }
    }
    return this
}

从此代码段中可以明显看出,withItems不接受状态列表。因此,如果要添加输入状态,则必须分别输入它们(即withItems(state1, state2, state3, ...))。请小心添加正确的状态类型(输入状态为StateAndRef类型,输出状态为TransactionState类型。