Plutus: Ada

πŸ’Έ Plutus Tutorial: Working with Ada in Smart Contracts


πŸ—‚οΈ Table of Contents

  1. πŸ” Introduction

  2. πŸ’° What is Ada and Lovelace

  3. πŸͺ™ Currency Symbol and Token Name

  4. πŸ—οΈ Creating and Extracting Ada

  5. πŸ“¦ Working with Value

  6. βž— Arithmetic Operations on Ada

  7. πŸ§ͺ Utility Functions

  8. πŸ“š Glossary

  9. πŸ”— 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 newtype

  • Easy constructors and extractors

  • Helpers for conversion and arithmetic


2. πŸ’° What is Ada and Lovelace

newtype Ada = Lovelace { getLovelace :: Integer }
  • Ada is just a wrapped integer called Lovelace

  • 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

🧱 Construct Ada values

lovelaceOf :: Integer -> Ada
adaOf      :: Micro   -> Ada
Function
Meaning

lovelaceOf 1000000

β†’ 1 Ada (raw integer)

adaOf (MkFixed 1)

β†’ 1 Ada (fixed-point)

πŸ” Extract amount

getAda :: Ada -> Micro

5. πŸ“¦ Working with Value

Plutus transactions use the Value type to represent tokens and Ada.

πŸ“€ To convert Ada β†’ Value:

toValue :: Ada -> Value

πŸ“₯ To extract 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

Since Ada derives Num, Semigroup, Monoid, etc., you can:

a + b
a - b
a * 2

πŸ”Έ Divide two Ada values

divide :: 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

Term
Definition

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