Skip to content

Commit eca5f2d

Browse files
committed
Update GetTokenBalance
1 parent 9e25ef6 commit eca5f2d

File tree

1 file changed

+21
-10
lines changed
  • Tokens/fungiblehousetoken/workflows/src/main/java/net/corda/examples/tokenizedhouse/flows

1 file changed

+21
-10
lines changed

Tokens/fungiblehousetoken/workflows/src/main/java/net/corda/examples/tokenizedhouse/flows/QueryTokens.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.r3.corda.lib.tokens.contracts.types.TokenPointer;
55
import com.r3.corda.lib.tokens.contracts.types.TokenType;
66
import com.r3.corda.lib.tokens.workflows.utilities.QueryUtilities;
7+
import net.corda.core.contracts.TransactionState;
78
import net.corda.examples.tokenizedhouse.states.FungibleHouseTokenState;
89
import net.corda.core.contracts.Amount;
910
import net.corda.core.contracts.StateAndRef;
@@ -13,6 +14,9 @@
1314
import net.corda.core.flows.StartableByRPC;
1415
import net.corda.core.utilities.ProgressTracker;
1516

17+
import java.util.Set;
18+
import java.util.stream.Collectors;
19+
1620
public 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

Comments
 (0)