vishal gawde
vishal gawde

Reputation: 168

corda transactionBuilder accept list of State as Input State

I am trying to add List> as inputState in to TransactionBuilder by adding

TransactionBuilder(notary = notary).
                                    withItems(list of states,
                                    StateAndContract(output State, CONTRACT_ID),
                                    Command)

getting Wrong argument type: class java.util.ArrayList, how can i pass list of States as input to TransactionBuilder?

Upvotes: 0

Views: 298

Answers (2)

Joel
Joel

Reputation: 23140

Instead of calling withItems(listOfStates), try withItems(*listOfStates.toTypedArray()).

Upvotes: 2

user8299582
user8299582

Reputation:

As you can see here, withItems pattern matches states based on the class type.

/** 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
}

It is clear from this code snippet that withItems does not accept a list of states. Thus, if you wish to add input states, you would have to input them individually (i.e. withItems(state1, state2, state3, ...)). Be careful to add the correct state types (input states are of type StateAndRef and output states are of type TransactionState.

Upvotes: 2

Related Questions