如何在“金额”类中表示负数?
根据文档,在Amount
类中不允许使用负数: https : //docs.corda.net/api/kotlin/corda/net.corda.core.contracts/-amount/
当一个ContractState
类的Amount
字段可以变为负值时(例如可以多付的余额),表示负数的最好方法是什么?
通过以下init
块阻止Amount
建模负值:
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. // NB 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" } }
如果你想创建一个像Amount
这样允许负值的类,只需要制作一个不包含这个init
块的Amount
类的副本。
Corda中不能有负值,因为您无法支付负值余额或在账户中持有负值余额。
但是你可以发出一个义务(iou),你可以在这里看看r3 Corda Sample: https : //github.com/roger3cev/obligation-cordapp