Skip to content

Commit

Permalink
Merge pull request #26 from LovelaceAcademy/add-m10
Browse files Browse the repository at this point in the history
Add M10 - A Vesting Contract I
  • Loading branch information
klarkc authored Aug 15, 2023
2 parents 17d1e36 + d3d8382 commit aac390c
Show file tree
Hide file tree
Showing 26 changed files with 18,209 additions and 9 deletions.
15 changes: 6 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,18 @@ To run anyting on this repository, you'll need to have:
- Untyped vs Typed Validation Scripts
- Testing with Plutip

## Module 10 - A Vesting Contract
## [Module 10 - A Vesting Contract I](modules/M10-a-vesting-contract)

- Script Contexts
- Datums
- Redeemer
- Script Context
- Handling Time (Slots)
- Building the UI
- Building the contract and tests

## Module 11 - High Ground
## Module 11 - A Vesting Contract II

- Metaprogramming in Haskell
- Parameterized contracts
- Running the parameterized contract in CTL
- Building the UI

## Module 12 - (Non/)Fungible Tokens
- Parameterized contracts (Plutus and CTL)
- Values
- Minting Policy
- NFT's
Expand Down
133 changes: 133 additions & 0 deletions modules/M10-a-vesting-contract/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
---
title: M10 - A Vesting Contract
author: Walker Leite
---
# Introduction

## Getting Started

In this module we'll see how to handle time in the blockchain and build a Vesting Contract.

To run this presentation type (you will need [nix](https://nixos.org)):

```sh
../../slide README.md
```

### Community Support

- [LovelaceAcademy Discord](https://discord.gg/fWP9eGdfZ8)
- [StackExchange](https://cardano.stackexchange.com/) (:bulb: use the tag lovelace-academy)
- [Plutonomicon Discord](https://discord.gg/gGFdGaUE)

[![Module Video](https://img.youtube.com/vi/SiXIclFrc6k/0.jpg)](https://www.youtube.com/watch?v=SiXIclFrc6k&list=PLHJ1yaDcSSads9N7s5grkGZCzVsAjcsez)

## What you should know

- Bulding smart contracts (module 6);
- Cardano-Transaction-Lib (module 8);
- Halogen (modules 7 and 8);
- Although not required, [variants](https://github.com/natefaubion/purescript-variant) will be useful for [halogen-formless](https://github.com/thomashoneyman/purescript-halogen-formless).

# Script Context

## Script Context and Purpose

```haskell
data ScriptContext = ScriptContext
{ scriptContextTxInfo :: TxInfo
scriptContextPurpose :: ScriptPurpose
}

data ScriptPurpose =
Minting CurrencySymbol
| Spending TxOutRef
| Rewarding StakingCredential
| Certifying DCert

data TxOutRef = TxOutRef
{ txOutRefId :: TxId
, txOutRefIdx :: Integer
}

data TxId = TxId { getTxId :: BuiltinByteString }
```

## Transaction Info

```haskell
data TxInfo = TxInfo
{ txInfoInputs :: [TxInInfo]
, txInfoReferenceInputs :: [TxInInfo]
, txInfoOutputs :: [TxOut]
, txInfoFee :: Value
, txInfoMint :: Value
, txInfoDCert :: [DCert]
, txInfoWdrl :: Map StakingCredential Integer
, txInfoValidRange :: POSIXTimeRange
, txInfoSignatories :: [PubKeyHash]
, txInfoRedeemers :: Map ScriptPurpose Redeemer
, txInfoData :: Map DatumHash Datum
, txInfoId :: TxId
}
```

# Handling Time

## Assumptions

- `1 slot = 1 second up to 36 hours` (by protocol design)
- Allowed time range can be provided on transaction creation in POSIX format

## POSIXTimeRange

```haskell
type POSIXTimeRange = Interval POSIXTime

newtype POSIXTime = POSIXTime { getPOSIXTime :: Integer }

data Interval a = Interval
{ ivFrom :: LowerBound a
, ivTo :: UpperBound a
}

data LowerBound a = LowerBound (Extended a) Closure
data UpperBound a = UpperBound (Extended a) Closure

type Closure = Bool

data Extended a = NegInf | Finite a | PosInf
```

## Deadline

![deadline](images/001.png)

## Vesting Contract

- [Vesting Contract](https://plutus-pioneer-program.readthedocs.io/en/latest/pioneer/week3.html#example-vesting)
- [PPP 030304](https://www.youtube.com/watch?v=ae7U_yKIQ0Y)

# Breakthrough: Building the Vesting Contract

## Description

> As a donator I want to lock an ADA value in a contract, to be rewarded to a given beneficiary according a given deadline

> As the beneficiary I want to reclaim the locked ADA value only after the deadline

## Bootstrap

```
mkdir modules/M10-a-vesting-contract/{contract,dapp}

(
cd modules/M10-a-vesting-contract/contract
nix flake init -t github:LovelaceAcademy/nix-templates#hor-plutus
)

(
cd modules/M10-a-vesting-contract/dapp
nix flake init -t github:LovelaceAcademy/nix-templates#pix-ctl-full
)
```
1 change: 1 addition & 0 deletions modules/M10-a-vesting-contract/contract/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/result
217 changes: 217 additions & 0 deletions modules/M10-a-vesting-contract/contract/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit aac390c

Please sign in to comment.