Double-spending is a problem that arises when transacting digital currency that involves the same tender being spent multiple times. How does DAML handle double spending problem?
Hi @Rocky
Tender can be represented as a Daml contract, by default, each Daml contract is unique. In addition, there is the concept of the consumption of a contract, meaning the original contract is destroyed and can no longer be accessed. (A user cannot perform a “spend” or “transfer” action twice), and a “spend” or “transfer” action will essentially archive that amount belonging to the user performing the action.
For example the below snippet,
template CBDCv4
with
issuer: Party
owner: Party
amount: Decimal
currency: Text
where
signatory issuer, owner
controller owner can
Transfer -- Creates a new TransferRequest
...
template TransferRequest
with
receiver: Party
digitalCash: CBDCv4 -- This contains data required to create a new
-- CBDCv4, i.e., issuer, amount, currency
where
signatory digitalCash.issuer, digitalCash.owner
controller receiver can
Accept -- Creates a new CBDCv4 with the new owner
...
Once you propose a certain amount to be transferred, the contract that belongs you will you will archived, and a new contract for the recipient will be created. The archival of the contract that belongs to you prevents double spend, because you will not be able to create a transfer from that instance of the contract anymore.
You can read this article that gives more details “Why Daml is Great to represent digital currency”