Merge branch 'hp-improvements-4' into 'dev'

Changes for tutorials.

See merge request ligolang/ligo!113
This commit is contained in:
Sander 2019-09-30 18:22:43 +00:00
commit 757c5433fd
8 changed files with 60 additions and 57 deletions

View File

@ -13,7 +13,7 @@ Contract below is effectively an empty contract, that takes a `unit` as a parame
<!--DOCUSAURUS_CODE_TABS-->
<!--Pascaligo-->
```Pascal
```pascaligo
function main (const p : unit ; const s : unit) : (list(operation) * unit) is
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-->
<!--Pascaligo-->
```Pascal
```pascaligo
// variant defining pseudo multi-entrypoint actions
type action is
| 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-->
<!--Pascaligo-->
```Pascal
```pascaligo
const availableSupply: nat = 15n;
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.
<!--DOCUSAURUS_CODE_TABS-->
<!--Pascaligo-->
```Pascal
```pascaligo
const availableSupply: nat = 15n;
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-->
<!--Pascaligo-->
```Pascal
```pascaligo
type animalBreed is string;
const dogBreed: animalBreed = "Saluki";
@ -26,7 +26,7 @@ const dogBreed: animalBreed = "Saluki";
### Simple types
<!--DOCUSAURUS_CODE_TABS-->
<!--Pascaligo-->
```Pascal
```pascaligo
// accountBalances is a simple type, a map of address <-> tez
type accountBalances is map(address, tez);
@ -46,7 +46,7 @@ In the example below you can see definition of data types for a ledger, that kee
<!--DOCUSAURUS_CODE_TABS-->
<!--Pascaligo-->
```Pascal
```pascaligo
// alias two types
type account is address;
type numberOfTransactions is nat;

View File

@ -17,14 +17,15 @@ In the [previous tutorial](tutorials/get-started/tezos-taco-shop-smart-contract.
## Analyzing the current contract
### **`taco-shop.ligo`**
```
```pascaligo
type taco_supply is record
current_stock : nat;
max_price : tez;
end
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
// Retrieve the taco_kind from the contract's 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
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;
```
@ -66,7 +67,7 @@ This means that after all the *purchase conditions* of our contract are met - e.
### 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.
```
```pascaligo
const ownerAddress : address = "tz1TGu6TN5GSez2ndXXeDX6LgUDvLzPLqgYV";
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.
```
```pascaligo
const payoutOperation : operation = transaction(unit, amount, receiver) ;
const operations : list(operation) = list
payoutOperation
@ -89,7 +90,7 @@ end;
## Finalizing the contract
### **`taco-shop.ligo`**
```
```pascaligo
type taco_supply is record
current_stock : nat;
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.
```
```pascaligo
ligo dry-run taco-shop.ligo --syntax pascaligo --amount 1 buy_taco 1n "map
1n -> record
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.
```
```pascaligo
const ownerAddress: address = "tz1TGu6TN5GSez2ndXXeDX6LgUDvLzPLqgYV";
const donationAddress: address = "tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx";
```
```
```pascaligo
const receiver : contract(unit) = get_contract(ownerAddress);
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:
```
```pascaligo
current_purchase_price = max_price / available_stock
```
@ -71,7 +71,7 @@ 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.
### `taco-shop.ligo`
```Pascal
```pascaligo
function main (const parameter: int; const contractStorage: int) : (list(operation) * int) is
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.
**Taco shop's storage**
```Pascal
```pascaligo
type taco_supply is record
current_stock : nat;
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.
**`taco-shop.ligo`**
```Pascal
```pascaligo
type taco_supply is record
current_stock : nat;
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
**`taco-shop.ligo`**
```Pascal
```pascaligo
type taco_supply is record
current_stock : nat;
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`**
```Pascal
```pascaligo
type taco_supply is record
current_stock : nat;
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
**`taco-shop.ligo`**
```Pascal
```pascaligo
type taco_supply is record
current_stock : nat;
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.
**Without tips**
```Pascal
```pascaligo
if amount =/= current_purchase_price then
```
**With tips**
```Pascal
```pascaligo
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 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 pascaligoExampleSmall = `${pre}pascaligo

View File

@ -144,7 +144,34 @@ const siteConfig = {
highlight: {
// 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.

View File

@ -648,7 +648,7 @@ body
color: #3AA0FF;
}
.tab-pane {
.home-container .tab-pane {
height: 500px;
}
@ -817,3 +817,7 @@ input#search_input_react::placeholder {
font-weight:bold
}
.docMainWrapper .hljs {
border: solid 1px lightgray;
overflow-x: auto;
}