如何在Amount类中表示负数?

时间:2018-01-18 19:09:25

标签: corda

根据文档https://docs.corda.net/api/kotlin/corda/net.corda.core.contracts/-amount/

Amount类不允许使用负数

如果ContractState类的Amount字段可以为负值(例如可以多付的余额),那么表示负数的最佳方式是什么?

2 个答案:

答案 0 :(得分:2)

您在Corda中无法获得负数,因为您无法支付负余额或将负余额存入帐户。

但是你可以发布义务(iou),你可以在这里查看r3 Corda样本:https://github.com/roger3cev/obligation-cordapp

答案 1 :(得分:1)

Amount旨在不允许负数。通过以下init块阻止它这样做:

init {
    // Amount represents a static balance of physical assets as managed by the distributed ledger and is not allowed
    // to become negative a rule further maintained by the Contract verify method.
    // N.B. If concepts such as an account overdraft are required this should be modelled separately via Obligations,
    // or similar second order smart contract concepts.
    require(quantity >= 0) { "Negative amounts are not allowed: $quantity" }
}

AmountTransfer可用于对负转移进行建模。或者,您只需复制排除此Amount块的init类。