You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Corda中FungibleAsset何时子类化OnLedgerAsset?自定义资产需继承吗?

Custom Fungible Assets in Corda: OnLedgerAsset vs FungibleAsset

Let’s start by clarifying the relationship between these two components first:

  • FungibleAsset is the core interface that defines the minimum requirements for a splittable/mergeable asset on Corda. It enforces properties like owner, amount, and methods to handle basic asset interactions.
  • OnLedgerAsset is an abstract base class from the finance package that implements most of the common logic for fungible assets—think issuance, splitting, merging, exiting, and standard ledger interactions like generating commands or handling state participants.

Should I inherit from OnLedgerAsset when creating a custom fungible asset?

In 90% of cases, yes—this is the recommended approach.

OnLedgerAsset saves you from reinventing the wheel. It encapsulates all the boilerplate logic for standard fungible asset operations, so you can focus entirely on your asset’s unique business rules and properties. For example:

  • If you’re building a custom reward points asset, you only need to add fields like validUntil or merchantId, then implement the minimal abstract methods (like getOwner(), generateIssue()) to tie your custom logic into the existing framework.
  • It ensures your asset behaves consistently with Corda’s built-in fungible assets (like Cash), making it easier for other nodes or developers to integrate with your solution.

The only time you’d skip inheriting from OnLedgerAsset is if your asset has extremely specialized logic that completely deviates from standard fungible asset behavior—for example, a split/merge rule that can’t be adapted to OnLedgerAsset’s existing implementation. Even then, you’d still implement the FungibleAsset interface directly, but this is a rare edge case.

When should I subclass OnLedgerAsset for FungibleAsset?

Subclass OnLedgerAsset whenever you want to leverage its pre-built ledger interaction capabilities. Specific scenarios include:

  • You need standard issuance, transfer, split, merge, or exit workflows for your asset—OnLedgerAsset already handles the heavy lifting of command generation, state participant management, and basic contract validation.
  • You want your asset to align with Corda’s finance package design patterns, ensuring compatibility with existing tooling and integrations.
  • You don’t want to write repetitive code for handling state lifecycle events (like generating move commands or updating participant lists).

Quick Example

Here’s a simplified snippet of how a custom reward points state might inherit from OnLedgerAsset:

data class RewardPointsState(
    override val amount: Amount<Issued<RewardPointsType>>,
    override val owner: AbstractParty,
    val validUntil: Instant,
    val merchantId: String
) : OnLedgerAsset<RewardPointsState>() {

    override fun withNewOwner(newOwner: AbstractParty): CommandAndState {
        return CommandAndState(MoveCommand(), copy(owner = newOwner))
    }

    override fun generateIssue(): CommandAndState {
        return CommandAndState(IssueCommand(), this)
    }

    // Implement other required abstract methods...
}

This way, you get all the standard fungible asset functionality out of the box, while adding your custom fields and rules.

内容的提问来源于stack exchange,提问作者Joel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:20:20