Corda - Redeem Tokens - Token SDK + Accounts

257 views Asked by At

I'm trying to use the token SDK for the first time and I am not being able to redeem the correct amount of tokens from a specific Corda account.

How I am issuing the tokens:

val accountInfo = accountService.accountInfo(accountId)
          
val accountKey = subFlow(RequestKeyForAccountFlow(accountInfo.state.data,initiateFlow(ourIdentity)))

val tokens = 10 of MyTokenType issuedBy ourIdentity heldBy accountKey

subFlow(IssueTokens(listOf(tokens), emptyList()))

I also implemented the function report() to Query the Vault to GET the amount of tokens for each account:

fun report (accountInfo: StateAndRef<AccountInfo>) : Amount<TokenType> {

    val criteria = QueryCriteria.VaultQueryCriteria(
        status = Vault.StateStatus.UNCONSUMED,
        relevancyStatus = Vault.RelevancyStatus.RELEVANT,
        externalIds = listOf (accountInfo.state.data.identifier.id)
    )

    val exprAggregate=
        builder {
            com.r3.corda.lib.tokens.contracts.internal.schemas.PersistentFungibleToken::amount.sum()
        }

    val aggregateCriteria =
        QueryCriteria.VaultCustomQueryCriteria(exprAggregate)

    val otherResult = serviceHub.vaultService.queryBy(
        criteria = criteria.and(aggregateCriteria),
        contractStateType = FungibleToken::class.java).otherResults[0]

    val sum= if (otherResult == null) 0 else (otherResult as Long)

    return sum.MyToken

If I issue 10 tokens for a specific account I receive this on the GET that calls my report function:

{
"value": {
    "quantity": 10,
    "displayTokenSize": 1,
    "token": {
      "tokenIdentifier": "MyToken",
      "fractionDigits": 0,
      "displayTokenSize": 1,
      "customTokenType": false,
      "regularTokenType": true,
      "tokenClass": "com.r3.corda.lib.tokens.contracts.types.TokenType",
      "pointer": false
    }
  },
  "message": "Tokens in account."
}

This shows me that I am actually Issuing the tokens right, since the quantity of tokens is the 10 tokens that I just issued.

What I am doing in the Redeem flow:

val accountInfo = accountService.accountInfo(accountId)

val tokens: Amount<TokenType> = 10.MyToken

val heldByAccount: QueryCriteria = QueryCriteria.VaultQueryCriteria()
        .withExternalIds(Collections.singletonList(accountInfo.state.data.identifier.id))

subFlow(RedeemFungibleTokens(amount = tokens, issuer = ourIdentity, observers = emptyList(), queryCriteria = heldByAccount))

But When I do the GET to run my report() functin it just gives me this as a response:

   {
  "value": {
    "quantity": 0,
    "displayTokenSize": 1,
    "token": {
      "tokenIdentifier": "MyToken",
      "fractionDigits": 0,
      "displayTokenSize": 1,
      "customTokenType": false,
      "regularTokenType": true,
      "tokenClass": "com.r3.corda.lib.tokens.contracts.types.TokenType",
      "pointer": false
    }
  },
  "message": "Tokens in account."
}

This shows me that my Redeem flow it's not working because it is not only redeeming 10 tokens but all the tokens for that account, since the quantity is equals to zero.

Any ideas on how I can fix this?

Thanks a lot

1

There are 1 answers

0
Marco Carvalho On

I believe the answer is add a changeHolder to the RedeemFungibleTokens sub-flow you are calling.

Take a look at: https://training.corda.net/libraries/accounts-exercise/