StateLoaderService

Use the StateLoaderService to convert StateRef or StatePointer inputs into StateAndRef objects.

Inject StateLoaderService

StateLoaderService is injectable into services using the CordaInject mechanism.

This is injectable into both Corda Services and flows.

Java

Define a property of type StateLoaderService and annotate with @CordaInject:

@CordaInject
public StateLoaderService stateLoaderService;

Kotlin

Define a lateinit property of type StateLoaderService and annotate with @CordaInject:

@CordaInject
lateinit var stateLoaderService: StateLoaderService

Using the API

load(LinearPointer, LedgerTransaction): StateAndRef

Resolves a LinearPointer to a StateAndRef from inside a LedgerTransaction. All pointed-to states will be included in the transaction as reference states.

load(LinearPointer): StateAndRef

Resolves a LinearPointer to a StateAndRef via a vault query. This method will either return a StateAndRef or return an exception.

load(Set): Set<StateAndRef>

Given a Set of StateRef’s, this loads each referenced transaction and looks up the specified output ContractStates.

load(StateRef): StateAndRef

Given a StateRef, this loads the referenced transaction and looks up the specified output ContractState.

load(StaticPointer, LedgerTransaction): StateAndRef

Resolves a StaticPointer to a StateAndRef from inside a LedgerTransaction. All pointed-to states will be included in the transaction as reference states.

load(StaticPointer): StateAndRef

Resolves a StaticPointer to a StateAndRef via a vault query. This method will either return a StateAndRef or return an exception.

loadOrdered(List): List<StateAndRef>

Given a List of StateRef’s, this loads each referenced transaction and looks up the specified output ContractStates.

Java example

import net.corda.v5.application.flows.Flow;
import net.corda.v5.application.injection.CordaInject;
import net.corda.v5.base.annotations.Suspendable;
import net.corda.v5.ledger.contracts.ContractState;
import net.corda.v5.ledger.contracts.StateAndRef;
import net.corda.v5.ledger.contracts.StateRef;
import net.corda.v5.ledger.services.NotaryLookupService;
import net.corda.v5.ledger.services.StateLoaderService;
import net.corda.v5.ledger.transactions.SignedTransaction;
import net.corda.v5.ledger.transactions.TransactionBuilderFactory;

public class StateLoaderServiceExample implements Flow<String> {

    private StateRef inputStateRef;

    @CordaInject
    private StateLoaderService stateLoaderService;

    @CordaInject
    private TransactionBuilderFactory transactionBuilderFactory;

    @CordaInject
    private NotaryLookupService notaryLookupService;

    public StateLoaderServiceExample(StateRef inputStateRef) {
        this.inputStateRef = inputStateRef;
    }

    @Override
    @Suspendable
    public String call() {

        StateAndRef<ContractState> stateAndRef = stateLoaderService.load(inputStateRef);

        SignedTransaction transaction = transactionBuilderFactory.create()
                .setNotary(notaryLookupService.getNotaryIdentities().get(0))
                .addInputState(stateAndRef)
                .sign();

        return "Example showing stateLoaderService.load";
    }
}

Was this page helpful?

Thanks for your feedback!

Chat with us

Chat with us on our #docs channel on slack. You can also join a lot of other slack channels there and have access to 1-on-1 communication with members of the R3 team and the online community.

Propose documentation improvements directly

Help us to improve the docs by contributing directly. It's simple - just fork this repository and raise a PR of your own - R3's Technical Writers will review it and apply the relevant suggestions.

We're sorry this page wasn't helpful. Let us know how we can make it better!

Chat with us

Chat with us on our #docs channel on slack. You can also join a lot of other slack channels there and have access to 1-on-1 communication with members of the R3 team and the online community.

Create an issue

Create a new GitHub issue in this repository - submit technical feedback, draw attention to a potential documentation bug, or share ideas for improvement and general feedback.

Propose documentation improvements directly

Help us to improve the docs by contributing directly. It's simple - just fork this repository and raise a PR of your own - R3's Technical Writers will review it and apply the relevant suggestions.