Changes for tutorials.

This commit is contained in:
Sander Spies 2019-09-30 19:23:30 +02:00
parent 23c406e415
commit e56e62b255
8 changed files with 57 additions and 54 deletions

View File

@ -13,7 +13,7 @@ Contract below is effectively an empty contract, that takes a `unit` as a parame
<!--DOCUSAURUS_CODE_TABS--> <!--DOCUSAURUS_CODE_TABS-->
<!--Pascaligo--> <!--Pascaligo-->
```Pascal ```pascaligo
function main (const p : unit ; const s : unit) : (list(operation) * unit) is function main (const p : unit ; const s : unit) : (list(operation) * unit) is
block {skip} with ((nil : list(operation)), s) block {skip} with ((nil : list(operation)), s)
``` ```
@ -27,7 +27,7 @@ In the example below we have a simple counter contract, that can be either `Incr
<!--DOCUSAURUS_CODE_TABS--> <!--DOCUSAURUS_CODE_TABS-->
<!--Pascaligo--> <!--Pascaligo-->
```Pascal ```pascaligo
// variant defining pseudo multi-entrypoint actions // variant defining pseudo multi-entrypoint actions
type action is type action is
| Increment of int | Increment of int

View File

@ -9,7 +9,7 @@ Body of a function consists of two parts, the first part (**`block {}`** or **`b
<!--DOCUSAURUS_CODE_TABS--> <!--DOCUSAURUS_CODE_TABS-->
<!--Pascaligo--> <!--Pascaligo-->
```Pascal ```pascaligo
const availableSupply: nat = 15n; const availableSupply: nat = 15n;
const totalSupply: nat = 100n; const totalSupply: nat = 100n;
@ -30,7 +30,7 @@ A short hand syntax for the same function as above can inline the price calculat
While this approach can have it's benefits, it can decrease readability. While this approach can have it's benefits, it can decrease readability.
<!--DOCUSAURUS_CODE_TABS--> <!--DOCUSAURUS_CODE_TABS-->
<!--Pascaligo--> <!--Pascaligo-->
```Pascal ```pascaligo
const availableSupply: nat = 15n; const availableSupply: nat = 15n;
const totalSupply: nat = 100n; const totalSupply: nat = 100n;

View File

@ -13,7 +13,7 @@ Type aliasing is a great choice when working towards a readable / maintainable s
<!--DOCUSAURUS_CODE_TABS--> <!--DOCUSAURUS_CODE_TABS-->
<!--Pascaligo--> <!--Pascaligo-->
```Pascal ```pascaligo
type animalBreed is string; type animalBreed is string;
const dogBreed: animalBreed = "Saluki"; const dogBreed: animalBreed = "Saluki";

View File

@ -17,14 +17,15 @@ In the [previous tutorial](tutorials/get-started/tezos-taco-shop-smart-contract.
## Analyzing the current contract ## Analyzing the current contract
### **`taco-shop.ligo`** ### **`taco-shop.ligo`**
``` ```pascaligo
type taco_supply is record type taco_supply is record
current_stock : nat; current_stock : nat;
max_price : tez; max_price : tez;
end end
type taco_shop_storage is map(nat, taco_supply); type taco_shop_storage is map(nat, taco_supply);
function buy_taco (const taco_kind_index: nat ; var taco_shop_storage : taco_shop_storage) : (list(operation) * taco_shop_storage) is function buy_taco (const taco_kind_index: nat ; var taco_shop_storage : taco_shop_storage) :
(list(operation) * taco_shop_storage) is
begin begin
// Retrieve the taco_kind from the contract's storage // Retrieve the taco_kind from the contract's storage
const taco_kind : taco_supply = get_force(taco_kind_index, taco_shop_storage); const taco_kind : taco_supply = get_force(taco_kind_index, taco_shop_storage);
@ -46,7 +47,7 @@ function buy_taco (const taco_kind_index: nat ; var taco_shop_storage : taco_sho
### Purchase price formula ### Purchase price formula
Pedro's Taco Shop contract currently enables customers to buy tacos, at a computed price based on a simple formula. Pedro's Taco Shop contract currently enables customers to buy tacos, at a computed price based on a simple formula.
``` ```pascaligo
const current_purchase_price : tez = taco_kind.max_price / taco_kind.current_stock; const current_purchase_price : tez = taco_kind.max_price / taco_kind.current_stock;
``` ```
@ -66,7 +67,7 @@ This means that after all the *purchase conditions* of our contract are met - e.
### Defining the recipient ### Defining the recipient
In order to send tokens, we will need a receiver address - which in our case will be Pedro's personal account. Additionally we'll wrap the given address as a *`contract(unit)`* - which represents either a contract with no parameters, or an implicit account. In order to send tokens, we will need a receiver address - which in our case will be Pedro's personal account. Additionally we'll wrap the given address as a *`contract(unit)`* - which represents either a contract with no parameters, or an implicit account.
``` ```pascaligo
const ownerAddress : address = "tz1TGu6TN5GSez2ndXXeDX6LgUDvLzPLqgYV"; const ownerAddress : address = "tz1TGu6TN5GSez2ndXXeDX6LgUDvLzPLqgYV";
const receiver : contract(unit) = get_contract(ownerAddress); const receiver : contract(unit) = get_contract(ownerAddress);
``` ```
@ -77,7 +78,7 @@ const receiver : contract(unit) = get_contract(ownerAddress);
Now we can transfer the `amount` received by `buy_taco` to Pedro's `ownerAddress`. We will do so by forging a `transaction(unit, amount, receiver)` within a list of operations returned at the end of our contract. Now we can transfer the `amount` received by `buy_taco` to Pedro's `ownerAddress`. We will do so by forging a `transaction(unit, amount, receiver)` within a list of operations returned at the end of our contract.
``` ```pascaligo
const payoutOperation : operation = transaction(unit, amount, receiver) ; const payoutOperation : operation = transaction(unit, amount, receiver) ;
const operations : list(operation) = list const operations : list(operation) = list
payoutOperation payoutOperation
@ -89,7 +90,7 @@ end;
## Finalizing the contract ## Finalizing the contract
### **`taco-shop.ligo`** ### **`taco-shop.ligo`**
``` ```pascaligo
type taco_supply is record type taco_supply is record
current_stock : nat; current_stock : nat;
max_price : tez; max_price : tez;
@ -129,7 +130,7 @@ function buy_taco (const taco_kind_index: nat ; var taco_shop_storage : taco_sho
To confirm that our contract is valid, we can dry run it. As a result we see a *new operation* in the list of returned operations to be executed subsequently. To confirm that our contract is valid, we can dry run it. As a result we see a *new operation* in the list of returned operations to be executed subsequently.
``` ```pascaligo
ligo dry-run taco-shop.ligo --syntax pascaligo --amount 1 buy_taco 1n "map ligo dry-run taco-shop.ligo --syntax pascaligo --amount 1 buy_taco 1n "map
1n -> record 1n -> record
current_stock = 50n; current_stock = 50n;
@ -157,12 +158,12 @@ end"
Because Pedro is a member of the (STA) Specialty Taco Association, he has decided to donate **10%** of the earnings to the STA. We'll just add a `donationAddress` to the contract, and compute a 10% donation sum from each taco purchase. Because Pedro is a member of the (STA) Specialty Taco Association, he has decided to donate **10%** of the earnings to the STA. We'll just add a `donationAddress` to the contract, and compute a 10% donation sum from each taco purchase.
``` ```pascaligo
const ownerAddress: address = "tz1TGu6TN5GSez2ndXXeDX6LgUDvLzPLqgYV"; const ownerAddress: address = "tz1TGu6TN5GSez2ndXXeDX6LgUDvLzPLqgYV";
const donationAddress: address = "tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx"; const donationAddress: address = "tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx";
``` ```
``` ```pascaligo
const receiver : contract(unit) = get_contract(ownerAddress); const receiver : contract(unit) = get_contract(ownerAddress);
const donationReceiver : contract(unit) = get_contract(donationAddress); const donationReceiver : contract(unit) = get_contract(donationAddress);

View File

@ -35,7 +35,7 @@ Each taco kind, has its own `max_price` that it sells for, and a finite supply f
Current purchase price is calculated with the following equation: Current purchase price is calculated with the following equation:
``` ```pascaligo
current_purchase_price = max_price / available_stock current_purchase_price = max_price / available_stock
``` ```
@ -71,8 +71,8 @@ The best way to install the dockerized LIGO is as a **global executable** throug
To begin implementing our smart contract, we need an entry point. We'll call it `main` and it'll specify our contract's storage (`int`) and input parameter (`int`). Of course this is not the final storage/parameter of our contract, but it's something to get us started and test our LIGO installation as well. To begin implementing our smart contract, we need an entry point. We'll call it `main` and it'll specify our contract's storage (`int`) and input parameter (`int`). Of course this is not the final storage/parameter of our contract, but it's something to get us started and test our LIGO installation as well.
### `taco-shop.ligo` ### `taco-shop.ligo`
```Pascal ```pascaligo
function main (const parameter : int; const contractStorage : int) : (list(operation) * int) is function main (const parameter: int; const contractStorage: int) : (list(operation) * int) is
block {skip} with ((nil : list(operation)), contractStorage + parameter) block {skip} with ((nil : list(operation)), contractStorage + parameter)
``` ```
@ -129,7 +129,7 @@ ligo dry-run taco-shop.ligo --syntax pascaligo main 4 3
We know that Pedro's Taco Shop serves two kinds of tacos, so we'll need to manage stock individually, per kind. Let's define a type, that will keep the `stock` & `max_price` per kind - in a record with two fields. Additionally, we'll want to combine our `taco_supply` type into a map, consisting of the entire offer of Pedro's shop. We know that Pedro's Taco Shop serves two kinds of tacos, so we'll need to manage stock individually, per kind. Let's define a type, that will keep the `stock` & `max_price` per kind - in a record with two fields. Additionally, we'll want to combine our `taco_supply` type into a map, consisting of the entire offer of Pedro's shop.
**Taco shop's storage** **Taco shop's storage**
```Pascal ```pascaligo
type taco_supply is record type taco_supply is record
current_stock : nat; current_stock : nat;
max_price : tez; max_price : tez;
@ -141,7 +141,7 @@ type taco_shop_storage is map(nat, taco_supply);
Next step is to update the `main` entry point to include `taco_shop_storage` as its storage - while doing that let's set the `parameter` to `unit` as well to clear things up. Next step is to update the `main` entry point to include `taco_shop_storage` as its storage - while doing that let's set the `parameter` to `unit` as well to clear things up.
**`taco-shop.ligo`** **`taco-shop.ligo`**
```Pascal ```pascaligo
type taco_supply is record type taco_supply is record
current_stock : nat; current_stock : nat;
max_price : tez; max_price : tez;
@ -208,7 +208,7 @@ Let's start by customizing our contract a bit, we will:
- change `taco_shop_storage` to a `var` instead of a `const`, because we'll want to modify it - change `taco_shop_storage` to a `var` instead of a `const`, because we'll want to modify it
**`taco-shop.ligo`** **`taco-shop.ligo`**
```Pascal ```pascaligo
type taco_supply is record type taco_supply is record
current_stock : nat; current_stock : nat;
max_price : tez; max_price : tez;
@ -231,7 +231,7 @@ In order to decrease the stock in our contract's storage for a specific taco kin
**`taco-shop.ligo`** **`taco-shop.ligo`**
```Pascal ```pascaligo
type taco_supply is record type taco_supply is record
current_stock : nat; current_stock : nat;
max_price : tez; max_price : tez;
@ -266,7 +266,7 @@ To make sure we get paid, we will:
- if yes, stock for the given `taco_kind` will be decreased and the payment accepted - if yes, stock for the given `taco_kind` will be decreased and the payment accepted
**`taco-shop.ligo`** **`taco-shop.ligo`**
```Pascal ```pascaligo
type taco_supply is record type taco_supply is record
current_stock : nat; current_stock : nat;
max_price : tez; max_price : tez;
@ -327,11 +327,11 @@ end"
If you'd like to accept tips in your contract as well, simply change the following line, depending on which behavior do you prefer. If you'd like to accept tips in your contract as well, simply change the following line, depending on which behavior do you prefer.
**Without tips** **Without tips**
```Pascal ```pascaligo
if amount =/= current_purchase_price then if amount =/= current_purchase_price then
``` ```
**With tips** **With tips**
```Pascal ```pascaligo
if amount >= current_purchase_price then if amount >= current_purchase_price then
``` ```

View File

@ -15,35 +15,6 @@ const MarkdownBlock = CompLibrary.MarkdownBlock; /* Used to read markdown */
const Container = CompLibrary.Container; const Container = CompLibrary.Container;
const GridBlock = CompLibrary.GridBlock; const GridBlock = CompLibrary.GridBlock;
function pascaligo(hljs) {
return {
// case_insensitive: true,
beginKeywords: '',
keywords: {
keyword: 'and begin block case const contains down else end fail for ' +
'from function if in is list map mod nil not of or patch ' +
'procedure record remove set skip step then to type var while with',
literal: 'true false unit int string some none bool nat list'
},
lexemes: '[a-zA-Z][a-zA-Z0-9_]*',
contains: [
hljs.C_LINE_COMMENT_MODE,
{
className: 'type',
begin: /[A-Z][a-z]+/
},
{
begin: /[*+-:;\(\)\{\}|\>\<]/,
// className: 'ignore'
}
]
}
}
hljs.registerLanguage('pascaligo', pascaligo);
const pre = "```"; const pre = "```";
const pascaligoExampleSmall = `${pre}pascaligo const pascaligoExampleSmall = `${pre}pascaligo

View File

@ -144,7 +144,34 @@ const siteConfig = {
highlight: { highlight: {
// Highlight.js theme to use for syntax highlighting in code blocks. // Highlight.js theme to use for syntax highlighting in code blocks.
theme: "default" theme: "default",
hljs: function (hljs) {
hljs.registerLanguage('pascaligo', function (hljs) {
return {
// case_insensitive: true,
beginKeywords: '',
keywords: {
keyword: 'and begin block case const contains down else end fail for ' +
'from function if in is list map mod nil not of or patch ' +
'procedure record remove set skip step then to type var while with',
literal: 'true false unit int string some none bool nat list'
},
lexemes: '[a-zA-Z][a-zA-Z0-9_]*',
contains: [
hljs.C_LINE_COMMENT_MODE,
{
className: 'type',
begin: /[A-Z][a-z]+/
},
{
begin: /[*+-:;\(\)\{\}|\>\<]/,
// className: 'ignore'
}
]
}
});
}
}, },
// Add custom scripts here that would be placed in <script> tags. // Add custom scripts here that would be placed in <script> tags.

View File

@ -817,3 +817,7 @@ input#search_input_react::placeholder {
font-weight:bold font-weight:bold
} }
.docMainWrapper .hljs {
border: solid 1px lightgray;
overflow-x: auto;
}