44import com .r3 .corda .lib .tokens .contracts .types .TokenPointer ;
55import com .r3 .corda .lib .tokens .contracts .types .TokenType ;
66import com .r3 .corda .lib .tokens .workflows .utilities .QueryUtilities ;
7+ import net .corda .core .contracts .TransactionState ;
78import net .corda .examples .tokenizedhouse .states .FungibleHouseTokenState ;
89import net .corda .core .contracts .Amount ;
910import net .corda .core .contracts .StateAndRef ;
1314import net .corda .core .flows .StartableByRPC ;
1415import net .corda .core .utilities .ProgressTracker ;
1516
17+ import java .util .Set ;
18+ import java .util .stream .Collectors ;
19+
1620public class QueryTokens {
1721
1822 @ InitiatingFlow
@@ -34,20 +38,27 @@ public ProgressTracker getProgressTracker() {
3438 @ Override
3539 @ Suspendable
3640 public String call () throws FlowException {
37- //get house states on ledger with uuid as input tokenId
38- StateAndRef <FungibleHouseTokenState > stateAndRef = getServiceHub ().getVaultService ().
41+ //get a set of the RealEstateEvolvableTokenType object on ledger with uuid as input tokenId
42+ Set <FungibleHouseTokenState > evolvableTokenTypeSet = getServiceHub ().getVaultService ().
3943 queryBy (FungibleHouseTokenState .class ).getStates ().stream ()
40- .filter (sf ->sf .getState ().getData ().getSymbol ().equals (symbol )).findAny ()
41- .orElseThrow (()-> new IllegalArgumentException ("FungibleHouseTokenState symbol=\" " +symbol +"\" not found from vault" ));
44+ .filter (sf ->sf .getState ().getData ().getSymbol ().equals (symbol )).map (StateAndRef ::getState )
45+ .map (TransactionState ::getData ).collect (Collectors .toSet ());
46+ if (evolvableTokenTypeSet .isEmpty ()){
47+ throw new IllegalArgumentException ("FungibleHouseTokenState symbol=\" " +symbol +"\" not found from vault" );
48+ }
4249
43- //get the RealEstateEvolvableTokenType object
44- FungibleHouseTokenState evolvableTokenType = stateAndRef . getState (). getData () ;
50+ // Save the result
51+ String result = "" ;
4552
46- //get the pointer pointer to the house
47- TokenPointer <FungibleHouseTokenState > tokenPointer = evolvableTokenType .toPointer (FungibleHouseTokenState .class );
53+ for (FungibleHouseTokenState evolvableTokenType : evolvableTokenTypeSet ){
54+ //get the pointer pointer to the house
55+ TokenPointer <FungibleHouseTokenState > tokenPointer = evolvableTokenType .toPointer (FungibleHouseTokenState .class );
56+ //query balance or each different Token
57+ Amount <TokenType > amount = QueryUtilities .tokenBalance (getServiceHub ().getVaultService (), tokenPointer );
58+ result += "\n You currently have " + amount .getQuantity ()+ " " + symbol + " Tokens issued by " +evolvableTokenType .getMaintainer ()+"\n " ;
4859
49- Amount < TokenType > amount = QueryUtilities . tokenBalance ( getServiceHub (). getVaultService (), tokenPointer );
50- return " \n You currently have " + amount . getQuantity ()+ " " + symbol + " Tokens \n " ;
60+ }
61+ return result ;
5162 }
5263 }
5364
0 commit comments