问题描述
我是第一次尝试使用令牌 SDK,但无法从特定 Corda 帐户兑换正确数量的令牌。
我如何发行代币:
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()))
我还实现了函数 report() 来查询 Vault 以获取每个帐户的代币数量:
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
如果我为特定帐户发行 10 个代币,我会在调用我的报告函数的 GET 上收到此信息:
{
"value": {
"quantity": 10,"displayTokenSize": 1,"token": {
"tokenIdentifier": "MyToken","fractionDigits": 0,"customTokenType": false,"regularTokenType": true,"tokenClass": "com.r3.corda.lib.tokens.contracts.types.TokenType","pointer": false
}
},"message": "Tokens in account."
}
这表明我实际上在发行代币,因为代币的数量是我刚刚发行的 10 个代币。
我在兑换流程中所做的事情:
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))
但是当我执行 GET 来运行我的 report() 函数时,它只是给了我这个作为响应:
{
"value": {
"quantity": 0,"message": "Tokens in account."
}
这表明我的兑换流程不起作用,因为它不仅兑换了 10 个代币,而且兑换了该帐户的所有代币,因为数量为零。
关于如何解决这个问题有什么想法吗?
非常感谢
解决方法
我相信答案是在您调用的 RedeemFungibleTokens 子流中添加一个 changeHolder。