Skip to content

Commit ff4a87c

Browse files
Update tutorial for JsLIGO v2
1 parent 73c066e commit ff4a87c

File tree

3 files changed

+118
-124
lines changed

3 files changed

+118
-124
lines changed

gitlab-pages/docs/tutorials/taco-shop/getting-payouts.md

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ As it is, this contract cannot change the administrator address after it is depl
3030

3131
```jsligo skip
3232
// Create contract object that represents the target account
33-
const receiver_contract = match(Tezos.get_contract_opt(storage.admin_address)) {
34-
when(Some(contract)): contract;
35-
when(None): failwith("Couldn't find account");
36-
};
33+
const receiver_contract = $match(Tezos.get_contract_opt(storage.admin_address), {
34+
"Some": (contract) => contract,
35+
"None": () => failwith("Couldn't find account"),
36+
});
3737
3838
// Create operation to send tez
3939
const payout_operation = Tezos.Operation.transaction(unit, Tezos.get_balance(), receiver_contract);
@@ -78,7 +78,7 @@ As it is, this contract cannot change the administrator address after it is depl
7878
The complete entrypoint looks like this:
7979

8080
```jsligo skip
81-
@entry
81+
// @entry
8282
const payout = (_u: unit, storage: storage): [
8383
list<operation>,
8484
storage
@@ -90,10 +90,10 @@ const payout = (_u: unit, storage: storage): [
9090
}
9191
9292
// Create contract object that represents the target account
93-
const receiver_contract = match(Tezos.get_contract_opt(storage.admin_address)) {
94-
when(Some(contract)): contract;
95-
when(None): failwith("Couldn't find account");
96-
};
93+
const receiver_contract = $match(Tezos.get_contract_opt(storage.admin_address), {
94+
"Some": (contract) => contract,
95+
"None": () => failwith("Couldn't find account"),
96+
});
9797
9898
// Create operation to send tez
9999
const payout_operation = Tezos.Operation.transaction(unit, Tezos.get_balance(), receiver_contract);
@@ -230,29 +230,28 @@ Of course, after you implement the `payout` entrypoint, you should add tests for
230230
Test.Contract.transfer(
231231
Test.Typed_address.get_entrypoint("payout", contract.taddr),
232232
unit,
233-
0tez
233+
0 as tez
234234
);
235-
match(payout_result) {
236-
when(Success(_s)):
237-
do {
235+
$match(payout_result, {
236+
"Success": (_s) => (() => {
238237
const storage = Test.Typed_address.get_storage(contract.taddr);
239238
// Check that the stock has been reset
240239
Assert.assert(
241240
eq_in_map(
242-
Map.find(1n, TacoShop.default_taco_data),
241+
Map.find(1 as nat, TacoShop.default_taco_data),
243242
storage.taco_data,
244-
1n
243+
1 as nat
245244
));
246245
Assert.assert(
247246
eq_in_map(
248-
Map.find(2n, TacoShop.default_taco_data),
247+
Map.find(2 as nat, TacoShop.default_taco_data),
249248
storage.taco_data,
250-
2n
249+
2 as nat
251250
));
252251
Test.IO.log("Successfully reset taco storage");
253-
}
254-
when(Fail(_err)): failwith("Failed to reset taco storage");
255-
};
252+
})(),
253+
"Fail": (_err) => failwith("Failed to reset taco storage"),
254+
});
256255
```
257256

258257
1. Add this code to verify that Pedro's account received the tez from the contract:
@@ -269,18 +268,18 @@ Of course, after you implement the `payout` entrypoint, you should add tests for
269268

270269
```jsligo skip
271270
// Verify that the entrypoint fails if called by someone else
272-
const other_user_account = Test.Account.address(1n);
271+
const other_user_account = Test.Account.address(1 as nat);
273272
Test.State.set_source(other_user_account);
274273
const failed_payout_result =
275274
Test.Contract.transfer(
276275
Test.Typed_address.get_entrypoint("payout", contract.taddr),
277276
unit,
278-
0tez
277+
0 as tez
279278
);
280-
match(failed_payout_result) {
281-
when(Success(_s)): failwith("A non-admin user was able to call the payout entrypoint");
282-
when(Fail(_err)): Test.IO.log("Successfully prevented a non-admin user from calling the payout entrypoint");
283-
};
279+
$match(failed_payout_result, {
280+
"Success": (_s) => failwith("A non-admin user was able to call the payout entrypoint"),
281+
"Fail": (_err) => Test.IO.log("Successfully prevented a non-admin user from calling the payout entrypoint"),
282+
});
284283
```
285284

286285
1. Run the test with `ligo run test taco_shop.jsligo` and verify that the test runs successfully.

gitlab-pages/docs/tutorials/taco-shop/selling-tacos.md

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,13 @@ In this case, the contract needs to store the taco data map and the administrato
138138

139139
```jsligo skip
140140
export const default_taco_data: taco_data = Map.literal([
141-
[1n, { current_stock: 50n, max_price: 50tez }],
142-
[2n, { current_stock: 20n, max_price: 75tez }]
141+
[1 as nat, { current_stock: 50 as nat, max_price: 50 as tez }],
142+
[2 as nat, { current_stock: 20 as nat, max_price: 75 as tez }]
143143
]);
144144
```
145145

146-
Note that the natural numbers are indicated with an `n` after the number; otherwise, LIGO assumes that numbers are integers.
147-
Similarly, the maximum prices of the tacos are suffixed with `tez` to indicate that they are amounts of tez.
146+
Note that the natural numbers are indicated with an `as nat` after the number; otherwise, LIGO assumes that numbers are integers.
147+
Similarly, the maximum prices of the tacos have `as tez` to indicate that they are amounts of tez.
148148

149149
1. To keep the code for the contract organized, put the types and values in a namespace named `TacoShop`.
150150
The contract looks like this so far:
@@ -159,9 +159,9 @@ The contract looks like this so far:
159159
taco_data: taco_data,
160160
};
161161
162-
export const default_taco_data: taco_data = Map.literal ([
163-
[1n, { current_stock: 50n, max_price: 50tez }],
164-
[2n, { current_stock: 20n, max_price: 75tez }]
162+
export const default_taco_data: taco_data = Map.literal([
163+
[1 as nat, { current_stock: 50 as nat, max_price: 50 as tez }],
164+
[2 as nat, { current_stock: 20 as nat, max_price: 75 as tez }]
165165
]);
166166
167167
};
@@ -254,10 +254,10 @@ Add this function inside the namespace, immediately after the `default_taco_data
254254
// Internal function to get the price of a taco
255255
const get_taco_price_internal = (taco_kind_index: nat, taco_data: taco_data): tez => {
256256
const taco_kind: taco_supply =
257-
match (Map.find_opt(taco_kind_index, taco_data)) {
258-
when(Some(kind)): kind;
259-
when(None()): failwith("Unknown kind of taco")
260-
};
257+
$match (Map.find_opt(taco_kind_index, taco_data), {
258+
"Some": (kind) => kind,
259+
"None": () => failwith("Unknown kind of taco"),
260+
});
261261
return taco_kind.max_price / taco_kind.current_stock;
262262
}
263263
```
@@ -303,15 +303,15 @@ As described in [Entrypoints](../../syntax/contracts/entrypoints), entrypoints m
303303

304304
<Syntax syntax="jsligo">
305305

306-
- Entrypoints are functions marked with the `@entry` decorator
306+
- Entrypoints are functions marked with the `@entry` decorator, which (when used in a namespace) must be in a comment immediately before the function
307307
- Entrypoints receive a parameter from the caller and the current state of the contract storage
308308
- Entrypoints return a tuple consisting of a list of operations to run (such as calls to other smart contracts or transfers of tez) and the new state of the contract storage
309309

310310
1. In the smart contract file, within the `TacoShop` namespace, add this stub of an entrypoint:
311311

312312
```jsligo skip
313313
// Buy a taco
314-
@entry
314+
// @entry
315315
const buy_taco = (taco_kind_index: nat, storage: storage): [
316316
list<operation>,
317317
storage
@@ -341,10 +341,10 @@ As described in [Entrypoints](../../syntax/contracts/entrypoints), entrypoints m
341341
```jsligo skip
342342
// Retrieve the kind of taco from the contracts storage or fail
343343
const taco_kind: taco_supply =
344-
match (Map.find_opt(taco_kind_index, taco_data)) {
345-
when(Some(kind)): kind;
346-
when(None()): failwith("Unknown kind of taco");
347-
};
344+
$match (Map.find_opt(taco_kind_index, taco_data), {
345+
"Some": (kind) => kind,
346+
"None": () => failwith("Unknown kind of taco"),
347+
});
348348
```
349349

350350
1. After the code you just added, add this code to get the current price of a taco:
@@ -368,7 +368,7 @@ It uses the `Tezos.get_amount()` function, which returns the amount of tez that
368368

369369
```jsligo skip
370370
// Verify that there is at least one of this type of taco
371-
if (taco_kind.current_stock == 0n) {
371+
if (taco_kind.current_stock == 0 as nat) {
372372
return failwith("Sorry, we are out of this type of taco");
373373
}
374374
```
@@ -379,7 +379,7 @@ It uses the `Tezos.get_amount()` function, which returns the amount of tez that
379379
// Update the storage with the new quantity of tacos
380380
const updated_taco_data: taco_data = Map.update(
381381
taco_kind_index,
382-
(Some (({...taco_kind, current_stock: abs(taco_kind.current_stock - 1n) }))),
382+
["Some" as "Some", {...taco_kind, current_stock: abs(taco_kind.current_stock - 1) }],
383383
taco_data);
384384
```
385385

@@ -404,7 +404,7 @@ It uses the `Tezos.get_amount()` function, which returns the amount of tez that
404404
1. After the code for the `buy_taco` entrypoint, stub in the code for the entrypoint that allows Pedro to retrieve the tez in the contract, which you will add in a later section:
405405

406406
```jsligo skip
407-
@entry
407+
// @entry
408408
const payout = (_u: unit, storage: storage): [
409409
list<operation>,
410410
storage
@@ -545,7 +545,7 @@ Unlike entrypoints, they return a single value to the caller instead of a list o
545545
Add this view to the contract, after the `get_taco_price_internal` function and somewhere within the namespace:
546546

547547
```jsligo skip
548-
@view
548+
// @view
549549
const get_taco_price = (taco_kind_index: nat, storage: storage): tez =>
550550
get_taco_price_internal(taco_kind_index, storage.taco_data);
551551
```
@@ -567,26 +567,26 @@ namespace TacoShop {
567567
};
568568
569569
export const default_taco_data: taco_data = Map.literal([
570-
[1n, { current_stock: 50n, max_price: 50tez }],
571-
[2n, { current_stock: 20n, max_price: 75tez }]
570+
[1 as nat, { current_stock: 50 as nat, max_price: 50 as tez }],
571+
[2 as nat, { current_stock: 20 as nat, max_price: 75 as tez }]
572572
]);
573573
574574
// Internal function to get the price of a taco
575575
const get_taco_price_internal = (taco_kind_index: nat, taco_data: taco_data): tez => {
576576
const taco_kind: taco_supply =
577-
match (Map.find_opt(taco_kind_index, taco_data)) {
578-
when(Some(kind)): kind;
579-
when(None()): failwith("Unknown kind of taco")
580-
};
577+
$match (Map.find_opt(taco_kind_index, taco_data), {
578+
"Some": (kind) => kind,
579+
"None": () => failwith("Unknown kind of taco"),
580+
});
581581
return taco_kind.max_price / taco_kind.current_stock;
582582
}
583583
584-
@view
584+
// @view
585585
const get_taco_price = (taco_kind_index: nat, storage: storage): tez =>
586586
get_taco_price_internal(taco_kind_index, storage.taco_data);
587587
588588
// Buy a taco
589-
@entry
589+
// @entry
590590
const buy_taco = (taco_kind_index: nat, storage: storage): [
591591
list<operation>,
592592
storage
@@ -596,10 +596,10 @@ namespace TacoShop {
596596
597597
// Retrieve the kind of taco from the contracts storage or fail
598598
const taco_kind: taco_supply =
599-
match (Map.find_opt(taco_kind_index, taco_data)) {
600-
when(Some(kind)): kind;
601-
when(None()): failwith("Unknown kind of taco");
602-
};
599+
$match (Map.find_opt(taco_kind_index, taco_data), {
600+
"Some": (kind) => kind,
601+
"None": () => failwith("Unknown kind of taco"),
602+
});
603603
604604
// Get the current price of this type of taco
605605
const current_purchase_price = get_taco_price_internal(taco_kind_index, taco_data);
@@ -610,14 +610,14 @@ namespace TacoShop {
610610
}
611611
612612
// Verify that there is at least one of this type of taco
613-
if (taco_kind.current_stock == 0n) {
613+
if (taco_kind.current_stock == (0 as nat)) {
614614
return failwith("Sorry, we are out of this type of taco");
615615
}
616616
617617
// Update the storage with the new quantity of tacos
618618
const updated_taco_data: taco_data = Map.update(
619619
taco_kind_index,
620-
(Some (({...taco_kind, current_stock: abs (taco_kind.current_stock - 1n) }))),
620+
["Some" as "Some", {...taco_kind, current_stock: abs(taco_kind.current_stock - 1) }],
621621
taco_data);
622622
623623
const updated_storage: storage = {
@@ -628,7 +628,7 @@ namespace TacoShop {
628628
return [[], updated_storage];
629629
}
630630
631-
@entry
631+
// @entry
632632
const payout = (_u: unit, storage: storage): [
633633
list<operation>,
634634
storage
@@ -638,6 +638,7 @@ namespace TacoShop {
638638
639639
return [[], storage];
640640
}
641+
641642
};
642643
```
643644

0 commit comments

Comments
 (0)