Plutus: Ada
πΈ Plutus Tutorial: Working with Ada
in Smart Contracts
Ada
in Smart ContractsποΈ Table of Contents
π Introduction
π° What is
Ada
andLovelace
πͺ Currency Symbol and Token Name
ποΈ Creating and Extracting
Ada
π¦ Working with
Value
β Arithmetic Operations on
Ada
π§ͺ Utility Functions
π Glossary
π References
1. π Introduction
Plutus contracts often need to handle ADA β the native currency of Cardano. This module simplifies how you represent, create, and manipulate Ada
values and convert them to/from Value
.
The module gives you:
Strong type safety via the
Ada
newtypeEasy constructors and extractors
Helpers for conversion and arithmetic
2. π° What is Ada
and Lovelace
Ada
and Lovelace
newtype Ada = Lovelace { getLovelace :: Integer }
Ada
is just a wrapped integer calledLovelace
1 Ada = 1,000,000 Lovelace
β
Ada
derives:
Eq
,Ord
,Show
Num
,Real
,Integral
FromData
,ToData
, etc.
π§ Example:
Lovelace 1000000 -- 1 Ada
3. πͺ Currency Symbol and Token Name
These two identify Ada in a Value
:
adaSymbol :: CurrencySymbol -- ""
adaToken :: TokenName -- ""
They're both empty, because Ada is the default native currency.
π See in Haddock
4. ποΈ Creating and Extracting Ada
Ada
π§± Construct Ada
values
Ada
valueslovelaceOf :: Integer -> Ada
adaOf :: Micro -> Ada
lovelaceOf 1000000
β 1 Ada (raw integer)
adaOf (MkFixed 1)
β 1 Ada (fixed-point)
π Extract amount
getAda :: Ada -> Micro
5. π¦ Working with Value
Value
Plutus transactions use the Value
type to represent tokens and Ada.
π€ To convert Ada β Value
:
Ada β Value
:toValue :: Ada -> Value
π₯ To extract Ada β Value
:
Ada β Value
:fromValue :: Value -> Ada
π§ͺ Value constructors
lovelaceValueOf :: Integer -> Value
adaValueOf :: Micro -> Value
π‘ Tip:
lovelaceValueOf == toValue . lovelaceOf
adaValueOf == toValue . adaOf
6. β Arithmetic Operations on Ada
Ada
Since Ada
derives Num
, Semigroup
, Monoid
, etc., you can:
a + b
a - b
a * 2
πΈ Divide two Ada
values
Ada
valuesdivide :: Ada -> Ada -> Ada
divide (Lovelace a) (Lovelace b) = Lovelace (P.divide a b)
π Example:
divide (Lovelace 5000000) (Lovelace 1000000) == Lovelace 5
7. π§ͺ Utility Functions
β Is Ada value zero?
isZero :: Ada -> Bool
Examples:
isZero (Lovelace 0) -- β
True
isZero (Lovelace 100000) -- β False
8. π Glossary
Ada
Native Cardano currency type
Lovelace
Smallest unit of Ada (1 Ada = 1,000,000 Lovelace)
CurrencySymbol
Empty for Ada, identifies custom currencies
TokenName
Empty for Ada, identifies specific tokens
Value
Multi-asset value container in Plutus
Micro
Fixed-point decimal used for precise Ada amounts
MkFixed
Constructor for Micro
, wraps an Integer
getLovelace
Access raw integer from Ada
9. π References
Last updated