Skip to content

Commit

Permalink
Write chapter 8
Browse files Browse the repository at this point in the history
  • Loading branch information
AymericBethencourt committed Mar 26, 2020
1 parent cbcde14 commit dbe36b0
Show file tree
Hide file tree
Showing 29 changed files with 153 additions and 75 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<td width="255px"><img src="./public/readme.gif" alt="serverless mern" align="center" style="width: 255px" /></td>
<td>
Tezos Academy is a fun interactive tutorial develloped by <a href="https://octo.com/" target="_blank">OCTO Technology</a> on how to code smart contracts for <a href="https://tezos.com/" target="_blank">Tezos</a>. You are about to create a spaceship battle smart contract!<br /><br />
By default, Tezos smart contracts are written in <a href="https://tezos.gitlab.io/whitedoc/michelson.html" target="_blank">Michaelson</a>, but it is an hard to learn low level formal language. That is why we decide for this tutorial to use <a href="https://ligolang.org/" target="_blank">Pascaligo</a> instead. The syntax is high level, close to Pascal and tranpiles to Michaelson.<br /><br />
By default, Tezos smart contracts are written in <a href="https://tezos.gitlab.io/whitedoc/michelson.html" target="_blank">Michelson</a>, but it is an hard to learn low level formal language. That is why we decide for this tutorial to use <a href="https://ligolang.org/" target="_blank">PascaLIGO</a> instead. The syntax is high level, close to Pascal and tranpiles to Michelson.<br /><br />
In this tutorial, you are going to build a "Ship Factory" to build an army of spaceships, then we will add more and more functionalities as you progress through the tutorial, such as going into space battles!
</td>
</tr>
Expand Down
1 change: 1 addition & 0 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<link href="https://fonts.googleapis.com/css?family=Electrolize&display=swap" rel="stylesheet" />
<link href="https://fonts.googleapis.com/css?family=Roboto&display=swap" rel="stylesheet" />
<title>Tezos Academy</title>
</head>
<body>
Expand Down
12 changes: 10 additions & 2 deletions src/Chapter/Chapter.controller.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,16 @@ export const Chapter = () => {
setValidatorState(PENDING);
} else {
setShowDiff(true);
if (data.exercise === data.solution) setValidatorState(RIGHT);
else setValidatorState(WRONG);
if (data.exercise && data.solution) {
if (
// @ts-ignore
data.exercise.replace(/\s+|\/\/ Type your solution below/g, "") ===
// @ts-ignore
data.solution.replace(/\s+|\/\/ Type your solution below/g, "")
)
setValidatorState(RIGHT);
else setValidatorState(WRONG);
} else setValidatorState(WRONG);
}
};

Expand Down
8 changes: 4 additions & 4 deletions src/Chapter/Chapter.view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as React from "react";
import { CardBottomCorners, CardTopCorners } from "../Card/Card.style";
import { PENDING, RIGHT, WRONG } from "../Chapters/ChapterAbout/ChapterAbout.constants";
//prettier-ignore
import { Button, ButtonBorder, ButtonText, ChapterCourse, ChapterGrid, ChapterH1, ChapterH2, ChapterMonaco, ChapterStyled, ChapterValidator, ChapterValidatorContent, ChapterValidatorContentWrapper, ChapterValidatorInside, ChapterValidatorTitle, ChapterItalic } from "../Chapters/ChapterAbout/ChapterAbout.style";
import { Button, ButtonBorder, ButtonText, ChapterCourse, ChapterGrid, ChapterH1, ChapterH2, ChapterItalic, ChapterMonaco, ChapterStyled, ChapterValidator, ChapterValidatorContent, ChapterValidatorContentWrapper, ChapterValidatorInside, ChapterValidatorTitle } from "../Chapters/ChapterAbout/ChapterAbout.style";

const MonacoReadOnly = ({ children }: any) => {
const height = children.split("\n").length * 22;
Expand All @@ -25,7 +25,7 @@ const MonacoReadOnly = ({ children }: any) => {
folding: false,
readOnly: true,
fontSize: 14,
fontFamily: "Electrolize"
fontFamily: "Roboto"
}}
/>
</div>
Expand All @@ -49,7 +49,7 @@ const MonacoEditor = ({ proposedSolution, proposedSolutionCallback }: any) => {
folding: true,
readOnly: false,
fontSize: 14,
fontFamily: "Electrolize"
fontFamily: "Roboto"
}}
/>
</div>
Expand All @@ -74,7 +74,7 @@ const MonacoDiff = ({ solution, proposedSolution }: any) => {
folding: true,
readOnly: false,
fontSize: 14,
fontFamily: "Electrolize",
fontFamily: "Roboto",
renderSideBySide: false
}}
/>
Expand Down
4 changes: 2 additions & 2 deletions src/Chapters/ChapterAbout/ChapterAbout.data.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export const data = `#Chapter 1 : About Tezos Academy
Tezos Academy is a fun interactive tutorial develloped by <a href="https://octo.com/" target="_blank">OCTO Technology</a> on how to code smart contracts for <a href="https://tezos.com/" target="_blank">Tezos</a>. You are about to create a spaceship battle smart contract!
By default, Tezos smart contracts are written in <a href="https://tezos.gitlab.io/whitedoc/michelson.html" target="_blank">Michaelson</a>, but it is an hard to learn low level formal language. That is why we decide for this tutorial to use <a href="https://ligolang.org/" target="_blank">Pascaligo</a> instead. The syntax is high level, close to Pascal and tranpiles to Michaelson.
By default, Tezos smart contracts are written in <a href="https://tezos.gitlab.io/whitedoc/michelson.html" target="_blank">Michelson</a>, but it is an hard to learn low level formal language. That is why we decide for this tutorial to use <a href="https://ligolang.org/" target="_blank">PascaLIGO</a> instead. The syntax is high level, close to Pascal and tranpiles to Michelson.
## Part I : The ship factory
Expand All @@ -16,7 +16,7 @@ We will add more and more functionalities as you progress through this tutorial,
## How Ship ID works
Ship's appearance is based on its unique ID, a 8 digits integer such as
Ship's appearance is based on its unique ID, a 6 digits integer such as
<code height="20px" value="101301">
Ship ID is similar to how DNA works. Parts of it correspond to parts of its appearance. The first digit corresponds to the class of the ship, the second to the cabin design, third to the engine design, etc...
Expand Down
2 changes: 1 addition & 1 deletion src/Chapters/ChapterAbout/ChapterAbout.style.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export const ChapterMonaco = styled.div`

export const ChapterItalic = styled.em`
color: #42edf8 !important;
text-shadow: 0px 0px 25px rgba(11, 183, 226, 0.65), 0px 0px 15px rgba(0, 112, 202, 0.6);
/* text-shadow: 0px 0px 25px rgba(11, 183, 226, 0.65), 0px 0px 15px rgba(0, 112, 202, 0.6); */
text-transform: none;
font-style: normal;
`;
4 changes: 2 additions & 2 deletions src/Chapters/ChapterAbout/ChapterAbout.view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ShipSelector } from "../../ShipSelector/ShipSelector.controller";
import { PENDING, RIGHT, WRONG } from "./ChapterAbout.constants";
import { data } from "./ChapterAbout.data";
//prettier-ignore
import { Button, ButtonBorder, ButtonText, ChapterCourse, ChapterGrid, ChapterStyled, ChapterH1, ChapterH2, ChapterValidator, ChapterValidatorContent, ChapterValidatorContentWrapper, ChapterValidatorInside, ChapterValidatorTitle } from "./ChapterAbout.style";
import { Button, ButtonBorder, ButtonText, ChapterCourse, ChapterGrid, ChapterH1, ChapterH2, ChapterStyled, ChapterValidator, ChapterValidatorContent, ChapterValidatorContentWrapper, ChapterValidatorInside, ChapterValidatorTitle } from "./ChapterAbout.style";

monaco
.init()
Expand Down Expand Up @@ -50,7 +50,7 @@ const MonacoReadOnly = ({ height, value }: any) => {
folding: false,
readOnly: true,
fontSize: 14,
fontFamily: "Electrolize"
fontFamily: "Roboto"
}}
/>
</div>
Expand Down
12 changes: 6 additions & 6 deletions src/Chapters/ChapterConditionals/course.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

Booleans are typed _bool_ in LIGO :

```js
const a: boolean = true; // or false
```
const a: bool = true // or false
```

# Comparing Values

Only values of the same type can be natively compared, i.e. int, nat, string, tez, timestamp, address, etc... However some values of the same type are not natively comparable, i.e. maps, sets or lists. You will have to write your own comparison functions for those.

```js
```
// Comparing Strings
const a : string = "Alice"
const b : string = "Alice"
Expand All @@ -35,18 +35,18 @@ const c : bool = (a = b) // false

Conditional logic enables forking the control flow depending on the state.

```js
```
function isSmall (const n : nat) : bool is
if n < 10n then true else false
```

⚠️ When the branches of the conditional are not a single expression, as above, we need a block:

```js
```
if x < y then
block {
x := x + 1;
y := y - 1;
y := y - 1
}
else skip;
```
Expand Down
2 changes: 1 addition & 1 deletion src/Chapters/ChapterConditionals/exercise.ligo
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ const my_ship_price : tez = 3tez * 1.20
function modify_ship (const my_ship : ship_code) : ship_code is
block {
// Type your solution below
const modified_ship := String.slice(0n, 2n, my_ship) ^ "1" ^ String.slice(4n, 3n, my_ship)
const modified_ship := String.slice(0n, 2n, my_ship) ^ "1" ^ String.slice(3n, 3n, my_ship)
} with modified_ship
5 changes: 3 additions & 2 deletions src/Chapters/ChapterConditionals/solution.ligo
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const my_ship_price : tez = 3tez * 1.20
function modify_ship (const my_ship : ship_code) : ship_code is
block {
// Type your solution below
if String.slice(2n, 1n, name) = "2" then
const modified_ship := String.slice(0n, 2n, my_ship) ^ "1" ^ String.slice(4n, 3n, my_ship)
var modified_ship : ship_code := my_ship;
if String.slice(2n, 1n, my_ship) = "2" then
modified_ship = String.slice(0n, 2n, my_ship) ^ "1" ^ String.slice(3n, 3n, my_ship)
else skip;
} with modified_ship
20 changes: 10 additions & 10 deletions src/Chapters/ChapterFunctions/course.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Chapter 6 : Functions

LIGO functions are the basic building block of contracts. Each entrypoint of a contract is a functions and each smart contract must have at least one function named _main_ that dispatches controls to the other functions.
LIGO functions are the basic building block of contracts. Each entrypoint of a contract is a function and each smart contract must have at least one function named _main_ that dispatches controls to the other functions.

When calling a function, LIGO makes a copy of the arguments but also the environment variables. Therefore any modification to these will not be reflected outside the scope of the function and will be lost if not explicitly returned by the function.

Expand All @@ -10,25 +10,25 @@ There are 2 types of functions in PascaLIGO, Block Functions and Blockless Funct

In PascaLIGO, blocks allows for the sequential composition of instructions into an isolated scope. Each block needs to include at least one instruction.

```js
```
block { a := a + 1 }
```

If we need a placeholder, we use the instruction _skip_ which leaves the state unchanged. The rationale for skip instead of a truly empty block is that it prevents you from writing an empty block by mistake.

```js
```
block { skip }
```

Blocks can also include declarations of values :

```js
```
block { const a : int = 1 }
```

Functions in PascaLIGO are defined using the following syntax :

```js
```
function <name> (<parameters>) : <return_type> is
block {
<operations and instructions>
Expand All @@ -37,7 +37,7 @@ block {

For instance :

```js
```
function add (const a : int; const b : int) : int is
block {
const sum : int = a + b
Expand All @@ -48,23 +48,23 @@ function add (const a : int; const b : int) : int is

Functions that can contain all of their logic into a single expression can be defined without the need of a block. The add function above can be re-written as a blockless function:

```js
```
function add (const a: int; const b : int) : int is a + b
```

## Anonymous functions (a.k.a. lambdas)

It is possible to define functions without assigning them a name. They are useful when you want to pass them as arguments, or assign them to a key in a record or a map.

```js
```
function increment (const b : int) : int is
(function (const a : int) : int is a + 1) (b)
const a : int = increment (1); // a = 2
```

If the example above seems contrived, here is a more common design pattern for lambdas: to be used as parameters to functions. Consider the use case of having a list of integers and mapping the increment function to all its elements.

```js
```
function incr_map (const l : list (int)) : list (int) is
List.map (function (const i : int) : int is i + 1, l)
```
Expand All @@ -77,7 +77,7 @@ At the moment, recursive function are limited to one (possibly tupled) parameter

In PascaLigo recursive functions are defined using the _recursive_ keyword

```js
```
recursive function sum (const n : int; const acc: int) : int is
if n<1 then acc else sum(n-1,acc+n)
```
Expand Down
2 changes: 1 addition & 1 deletion src/Chapters/ChapterFunctions/exercise.ligo
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ var my_ship : ship_code := "020433"
my_ship := "222031"
const my_ship_price : tez = 3tez * 1.20
// Type your solution below
my_ship := String.slice(0n, 2n, my_ship) ^ "1" ^ String.slice(4n, 3n, my_ship)
my_ship := String.slice(0n, 2n, my_ship) ^ "1" ^ String.slice(3n, 3n, my_ship)
2 changes: 1 addition & 1 deletion src/Chapters/ChapterFunctions/solution.ligo
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ const my_ship_price : tez = 3tez * 1.20
// Type your solution below
function modify_ship (const my_ship : ship_code) : ship_code is
block {
const modified_ship := String.slice(0n, 2n, my_ship) ^ "1" ^ String.slice(4n, 3n, my_ship)
const modified_ship = String.slice(0n, 2n, my_ship) ^ "1" ^ String.slice(3n, 3n, my_ship)
} with modified_ship
2 changes: 1 addition & 1 deletion src/Chapters/ChapterLists/course.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Chapter 10 : Lists

Comming soon ...
Coming soon ...
2 changes: 1 addition & 1 deletion src/Chapters/ChapterLists/exercise.ligo
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ var my_ship : ship_code := "020433"
my_ship := "222031"
const my_ship_price : tez = 3tez * 1.20
// Type your solution below
my_ship := String.slice(0n, 2n, my_ship) ^ "1" ^ String.slice(4n, 3n, my_ship)
my_ship := String.slice(0n, 2n, my_ship) ^ "1" ^ String.slice(3n, 3n, my_ship)
2 changes: 1 addition & 1 deletion src/Chapters/ChapterLists/solution.ligo
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ const my_ship_price : tez = 3tez * 1.20
// Type your solution below
function modify_ship (const my_ship : ship_code) : ship_code is
block {
const modified_ship := String.slice(0n, 2n, my_ship) ^ "1" ^ String.slice(4n, 3n, my_ship)
const modified_ship := String.slice(0n, 2n, my_ship) ^ "1" ^ String.slice(3n, 3n, my_ship)
} with modified_ship
56 changes: 55 additions & 1 deletion src/Chapters/ChapterLoops/course.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,57 @@
# Chapter 8 : Loops

Comming soon ...
LIGO integrate 2 kinds of loops. General while iterations and bounded for loops.

## While loops

While loops are define as follows :

```
while <condition> block {
<operations>
}
```

⚠️ If the while condition is never met, the block will will repeatedly be evaluated until the contract run out of gas or fails.

## For Loops

For-loops iterates over bounded intervals :

```
for <variable assignment> to <upper bound> block {
<operations>
}
```

For instance :

```
var acc : int := 0;
for i := 1 to 10 block {
acc := acc + i
}
```

For-loops can also iterate through the contents of a collection, that is, a list, a set or a map. This is done with :

```
for <element var> in <collection type> <collection var> block {
<operations>
}
```

Here is an example where the integers in a list are summed up.

```
function sum_list (var l : list (int)) : int is block {
var total : int := 0;
for i in list l block {
total := total + i
}
} with total
```

## Your mission

Modify the contract so that the function iterates over all attributes of the ship and changes all occurences of "2" by "1".
11 changes: 9 additions & 2 deletions src/Chapters/ChapterLoops/exercise.ligo
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,12 @@ type ship_code is string
var my_ship : ship_code := "020433"
my_ship := "222031"
const my_ship_price : tez = 3tez * 1.20
// Type your solution below
my_ship := String.slice(0n, 2n, my_ship) ^ "1" ^ String.slice(4n, 3n, my_ship)

function modify_ship (const my_ship : ship_code) : ship_code is
block {
var modified_ship : ship_code := my_ship;
// Type your solution below
if String.slice(2n, 1n, name) = "2" then
modified_ship = String.slice(0n, 2n, my_ship) ^ "1" ^ String.slice(3n, 3n, my_ship)
else skip;
} with modified_ship
12 changes: 9 additions & 3 deletions src/Chapters/ChapterLoops/solution.ligo
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ type ship_code is string
var my_ship : ship_code := "020433"
my_ship := "222031"
const my_ship_price : tez = 3tez * 1.20
// Type your solution below

function modify_ship (const my_ship : ship_code) : ship_code is
block {
const modified_ship := String.slice(0n, 2n, my_ship) ^ "1" ^ String.slice(4n, 3n, my_ship)
} with modified_ship
var modified_ship : ship_code := my_ship;
// Type your solution below
for i := 0n to 5n block {
if String.slice(i, 1n, name) = "2" then
modified_ship = String.slice(0n, i, my_ship) ^ "1" ^ String.slice(i + 2n, 5n - i, my_ship)
else skip;
}
} with modified_ship
Loading

0 comments on commit dbe36b0

Please sign in to comment.