Skip to content

Commit

Permalink
Merge pull request #2 from erivas-ligo/fix-multiparameter-funcs
Browse files Browse the repository at this point in the history
Use multiparameter functions
  • Loading branch information
Marigold-infrabot authored Feb 3, 2023
2 parents d4c12f5 + a1ecdbb commit abec34e
Show file tree
Hide file tree
Showing 20 changed files with 79 additions and 79 deletions.
16 changes: 8 additions & 8 deletions src/frontend/src/pages/Chapters/JS/ChapterBuiltIns/course.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ A LIGO smart contract can query part of the state of the Tezos blockchain by mea

## A few built-ins

_Tezos.balance_ : Get the balance for the contract.
<!-- prettier-ignore -->*Tezos.get\_balance* : Get the balance for the contract.

_Tezos.amount_ : Get the amount of tez provided by the sender to complete this transaction.
<!-- prettier-ignore -->*Tezos.get\_amount* : Get the amount of tez provided by the sender to complete this transaction.

_Tezos.sender_ : Get the address that initiated the current transaction.
<!-- prettier-ignore -->*Tezos.get\_sender* : Get the address that initiated the current transaction.

<!-- prettier-ignore -->*Tezos.self\_address* : Get the address of the currently running contract.

_Tezos.source_ : Get the originator (address) of the current transaction. That is, if a chain of transactions led to the current execution get the address that began the chain. Not to be confused with Tezos.sender, which gives the address of the contract or user which directly caused the current transaction.
<!-- prettier-ignore -->*Tezos.get\_source* : Get the originator (address) of the current transaction. That is, if a chain of transactions led to the current execution get the address that began the chain. Not to be confused with Tezos.get_sender, which gives the address of the contract or user which directly caused the current transaction.

<!-- prettier-ignore -->*Tezos.chain\_id* : Get the identifier of the chain to distinguish between main and test chains.
<!-- prettier-ignore -->*Tezos.get\_chain\_id* : Get the identifier of the chain to distinguish between main and test chains.

ℹ️ A more complete list is available on <a href="https://ligolang.org/docs/reference/current-reference" target="_blank">ligolang.org</a>

Expand All @@ -32,16 +32,16 @@ _<string_message>_ must be a string value

## Access Control

This example shows how Tezos.source can be used to deny access to an entrypoint.
This example shows how Tezos.get_source can be used to deny access to an entrypoint.

```
const owner: address = ("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx" as address);
type storage = unit;
type parameter = unit;
type return_ = [list <operation>, storage];
let main = ([action, store]: [parameter, storage]) : return_ => {
if (Tezos.source != owner) {
let main = (action: parameter, store: storage) : return_ => {
if (Tezos.get_source() != owner) {
failwith ("Access denied.") as return_;
} else {
return [(list([]) as list<operation>), store];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ let purchase = (purchase_price : tez) : bool => {
const vendor_address: address = ("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx" as address);

// Type your solution below
if (Tezos.source != ship_address) {
if (Tezos.get_source() != ship_address) {
failwith ("Access denied");
} else {
unit;
};
if (Tezos.amount != purchase_price) {
if (Tezos.get_amount() != purchase_price) {
failwith ("Incorrect amount");
} else {
unit;
Expand Down
10 changes: 5 additions & 5 deletions src/frontend/src/pages/Chapters/JS/ChapterFA12/course.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ type action =
const transfer = (p: transfer, s: storage) : return_ => {
const new_allowances = (() : allowances => {
if (Tezos.sender == p.address_from) {
if (Tezos.get_sender() == p.address_from) {
return s.allowances;
} else {
const authorized_value = match(Big_map.find_opt(
[Tezos.sender, p.address_from], s.allowances), {
[Tezos.get_sender(), p.address_from], s.allowances), {
Some: (value: nat) => value,
None: () => 0 as nat
}
Expand All @@ -122,7 +122,7 @@ const transfer = (p: transfer, s: storage) : return_ => {
failwith("Not Enough Allowance");
} else {
return Big_map.update(
[Tezos.sender, p.address_from],
[Tezos.get_sender(), p.address_from],
Some(abs(authorized_value - p.value)),
s.allowances
);
Expand Down Expand Up @@ -150,14 +150,14 @@ const transfer = (p: transfer, s: storage) : return_ => {
};
const approve = (p: approve, s: storage) : return_ => {
let previous_value = match(Big_map.find_opt([p.spender, Tezos.sender], s.allowances), {
let previous_value = match(Big_map.find_opt([p.spender, Tezos.get_sender()], s.allowances), {
Some: (value: nat) => value,
None: () => 0 as nat
});
if (previous_value > (0 as nat) && p.value > (0 as nat)) {
failwith("Unsafe Allowance Change") as return_;
} else {
const new_allowances = Big_map.update([p.spender, Tezos.sender], Some(p.value), s.allowances);
const new_allowances = Big_map.update([p.spender, Tezos.get_sender()], Some(p.value), s.allowances);
return [list([]) as list<operation>, { ...s, allowances: new_allowances}];
};
};
Expand Down
8 changes: 4 additions & 4 deletions src/frontend/src/pages/Chapters/JS/ChapterFunctions/course.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ Functions in JsLIGO are defined using the let or const keyword, like other value
Here is how you define a basic function that sums two integers:

```
let add = ([a, b]: [int, int]): int => a + b;
let add = (a: int, b: int): int => a + b;
```

If the body contains more than a single expression, you use block between braces:

```
let myFun = ([x, y]: [int, int]): int => {
let myFun = (x: int, y: int): int => {
let doubleX = x + x;
let doubleY = y + y;
return doubleX + doubleY;
Expand All @@ -29,13 +29,13 @@ Note that JsLIGO, like JavaScript, requires the return keyword to indicate what
<!-- prettier-ignore -->By default, LIGO will warn about unused arguments inside functions. In case we do not use an argument, we can use the wildcard _\__ to prevent warnings. Either use _\__ instead of the argument identifier:

```
let k = ([x, _] : [int, int]) : int => x;
let k = (x: int, _: int) : int => x;
```

or use an identifier starting with wildcard:

```
let k = ([x, _y] : [int, int]) : int => x;
let k = (x: int, _y: int) : int => x;
```

## Anonymous functions (a.k.a. lambdas)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type parameter =
type return_ = [list<operation>, storage];
let main = ([action, store] : [parameter, storage]) : return_ => {
let main = (action: parameter, store: storage) : return_ => {
return [
(list([]) as list <operation>),
(match (action, {
Expand Down Expand Up @@ -106,7 +106,7 @@ type parameter =
type return_ = [list<operation>, storage];
let main = ([action, _store] : [parameter, storage]) : return_ => {
let main = (action: parameter, _store: storage) : return_ => {
return [
(list([]) as list <operation>),
(match (action, {
Expand Down
12 changes: 6 additions & 6 deletions src/frontend/src/pages/Chapters/JS/ChapterMainFunction/course.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type parameter = nat;
type storage = nat;
type return_ = [list<operation>, storage];
let save =([action, store]: [parameter, storage]) : return_ =>
let save =(action: parameter, store: storage) : return_ =>
[(list([]) as list<operation>), store]
```

Expand All @@ -50,16 +50,16 @@ type storage = {
type return_ = [list<operation>, storage];
let entry_A = ([input_string, store]: [string, storage]): return_ =>
let entry_A = (input_string: string, store: storage): return_ =>
[(list([]) as list<operation>), {...store, stored_string_A: input_string}];
let entry_B = ([input_string, store]: [string, storage]): return_ =>
let entry_B = (input_string: string, store: storage): return_ =>
[(list([]) as list<operation>), {...store, stored_string_B: input_string}];
let main = ([action, store]: [parameter, storage]): return_ =>
let main = (action: parameter, store: storage): return_ =>
match(action, {
Action_A: (input_string: string) => entry_A([input_string, store]),
Action_B: (input_string: string) => entry_B([input_string, store])
Action_A: (input_string: string) => entry_A(input_string, store),
Action_B: (input_string: string) => entry_B(input_string, store)
});
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ type storage = {

type return_ = [list<operation>, storage];

let action_A = ([input_string, store]: [string, storage]): return_ =>
let action_A = (input_string: string, store: storage): return_ =>
[(list([]) as list<operation>), {...store, stored_string_A: input_string}];

let action_B = ([input_string, store]: [string, storage]): return_ =>
let action_B = (input_string: string, store: storage): return_ =>
[(list([]) as list<operation>), {...store, stored_string_B: input_string}];

let main = ([action, store]: [parameter, storage]): return_ =>
let main = (action: parameter, store: storage): return_ =>
match(action, {
Action_A: (input_string: string) => action_A([input_string, store]),
Action_B: (input_string: string) => action_B([input_string, store])
Action_A: (input_string: string) => action_A(input_string, store),
Action_B: (input_string: string) => action_B(input_string, store)
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ type storage = {

type return_ = [list<operation>, storage];

let set_ship_code = ([input_string, store]: [string, storage]): return_ =>
let set_ship_code = (input_string: string, store: storage): return_ =>
[(list([]) as list<operation>), {...store, ship_code: input_string}];

let go_to = ([input_string, store]: [string, storage]): return_ =>
let go_to = (input_string: string, store: storage): return_ =>
[(list([]) as list<operation>), {...store, destination: input_string}];

let main = ([action, store]: [parameter, storage]): return_ =>
let main = (action: parameter, store: storage): return_ =>
match(action, {
Set_ship_code: (input_string: string) => set_ship_code(input_string, store),
Go_to: (input_string: string) => go_to(input_string, store)
Expand Down
30 changes: 15 additions & 15 deletions src/frontend/src/pages/Chapters/JS/ChapterMultisig/course.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const execute_action = (str: string, s: storage) : list<operation> => {
const send = (param: message, s: storage) : return_ => {
// check sender against the authorized addresses
if (!Set.mem(Tezos.sender, s.authorized_addresses)) {
if (!Set.mem(Tezos.get_sender(), s.authorized_addresses)) {
failwith("Unauthorized address");
} else {
unit;
Expand All @@ -104,28 +104,28 @@ const send = (param: message, s: storage) : return_ => {
const [new_store, proposal_counters_updated] : [addr_set, proposal_counters] = match(voters_opt, {
Some: (voters: addr_set) => {
// The message is already stored. Increment the counter only if the sender is not already associated with the message.
if (Set.mem(Tezos.sender, voters)) {
if (Set.mem(Tezos.get_sender(), voters)) {
return [Set.empty as addr_set, s.proposal_counters];
} else {
const updated: proposal_counters = match(Map.find_opt(Tezos.sender, s.proposal_counters), {
Some: (count: nat) => Map.update(Tezos.sender, Some(count + (1 as nat)), s.proposal_counters),
None: () => Map.add(Tezos.sender, 1 as nat, s.proposal_counters)
const updated: proposal_counters = match(Map.find_opt(Tezos.get_sender(), s.proposal_counters), {
Some: (count: nat) => Map.update(Tezos.get_sender(), Some(count + (1 as nat)), s.proposal_counters),
None: () => Map.add(Tezos.get_sender(), 1 as nat, s.proposal_counters)
});
return [Set.add(Tezos.sender, voters), updated];
return [Set.add(Tezos.get_sender(), voters), updated];
};
},
None: () => {
// the message has never been received before
const updated: proposal_counters = match(Map.find_opt(Tezos.sender, s.proposal_counters), {
Some: (count: nat) => Map.update(Tezos.sender, Some(count + (1 as nat)), s.proposal_counters),
None: () => Map.add(Tezos.sender, 1 as nat, s.proposal_counters)
const updated: proposal_counters = match(Map.find_opt(Tezos.get_sender(), s.proposal_counters), {
Some: (count: nat) => Map.update(Tezos.get_sender(), Some(count + (1 as nat)), s.proposal_counters),
None: () => Map.add(Tezos.get_sender(), 1 as nat, s.proposal_counters)
});
return [Set.add(Tezos.sender, Set.empty as addr_set), updated];
return [Set.add(Tezos.get_sender(), Set.empty as addr_set), updated];
}
});
// check sender counters against the maximum number of proposal
const sender_proposal_counter: nat = match(Map.find_opt(Tezos.sender, proposal_counters_updated), {
const sender_proposal_counter: nat = match(Map.find_opt(Tezos.get_sender(), proposal_counters_updated), {
Some: (count: nat) => count,
None: () => 0 as nat
});
Expand Down Expand Up @@ -173,14 +173,14 @@ const withdraw = (param: message, s: storage) : return_ => {
return match(Map.find_opt(packed_msg, s.message_store), {
Some: (voters: addr_set) => { // The message is stored
const new_set: addr_set = Set.remove(Tezos.sender, voters);
const new_set: addr_set = Set.remove(Tezos.get_sender(), voters);
// Decrement the counter only if the sender was already associated with the message
const proposal_counters_updated: proposal_counters = (() : proposal_counters => {
if (Set.size(voters) != Set.size(new_set)) {
return match(Map.find_opt(Tezos.sender, s.proposal_counters), {
Some: (count: nat) => Map.update(Tezos.sender, Some(abs(count - (1 as nat))), s.proposal_counters),
None: () => Map.add(Tezos.sender, 1 as nat, s.proposal_counters)
return match(Map.find_opt(Tezos.get_sender(), s.proposal_counters), {
Some: (count: nat) => Map.update(Tezos.get_sender(), Some(abs(count - (1 as nat))), s.proposal_counters),
None: () => Map.add(Tezos.get_sender(), 1 as nat, s.proposal_counters)
});
} else { return s.proposal_counters; };
})();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const execute_action = (str: string, s: storage) : list<operation> => {

const send = (param: message, s: storage) : return_ => {
// check sender against the authorized addresses
if (!Set.mem(Tezos.sender, s.authorized_addresses)) {
if (!Set.mem(Tezos.get_sender(), s.authorized_addresses)) {
failwith("Unauthorized address");
} else {
unit;
Expand All @@ -54,14 +54,14 @@ const send = (param: message, s: storage) : return_ => {
const new_voters : addr_set = match(voters_opt, {
Some: (voters: addr_set) => {
// The message is already stored. Increment the counter only if the sender is not already associated with the message.
if (Set.mem(Tezos.sender, voters)) {
if (Set.mem(Tezos.get_sender(), voters)) {
return Set.empty as addr_set;
} else {
return Set.add(Tezos.sender, voters);
return Set.add(Tezos.get_sender(), voters);
};
},
None: () => {
return Set.add(Tezos.sender, Set.empty as addr_set);
return Set.add(Tezos.get_sender(), Set.empty as addr_set);
}
});

Expand Down Expand Up @@ -94,7 +94,7 @@ const withdraw = (param: message, s: storage) : return_ => {

return match(Map.find_opt(packed_msg, s.message_store), {
Some: (voters: addr_set) => { // The message is stored
const new_set: addr_set = Set.remove(Tezos.sender, voters);
const new_set: addr_set = Set.remove(Tezos.get_sender(), voters);

// If the message is left without any associated addresses, remove the corresponding message_store field
const message_store_updated: message_store = (() : message_store => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const execute_action = (str: string, s: storage) : list<operation> => {

const send = (param: message, s: storage) : return_ => {
// check sender against the authorized addresses
if (!Set.mem(Tezos.sender, s.authorized_addresses)) {
if (!Set.mem(Tezos.get_sender(), s.authorized_addresses)) {
failwith("Unauthorized address");
} else {
unit;
Expand All @@ -54,14 +54,14 @@ const send = (param: message, s: storage) : return_ => {
const new_voters : addr_set = match(voters_opt, {
Some: (voters: addr_set) => {
// The message is already stored. Increment the counter only if the sender is not already associated with the message.
if (Set.mem(Tezos.sender, voters)) {
if (Set.mem(Tezos.get_sender(), voters)) {
return Set.empty as addr_set;
} else {
return Set.add(Tezos.sender, voters);
return Set.add(Tezos.get_sender(), voters);
};
},
None: () => {
return Set.add(Tezos.sender, Set.empty as addr_set);
return Set.add(Tezos.get_sender(), Set.empty as addr_set);
}
});

Expand Down Expand Up @@ -97,7 +97,7 @@ const withdraw = (param: message, s: storage) : return_ => {

return match(Map.find_opt(packed_msg, s.message_store), {
Some: (voters: addr_set) => { // The message is stored
const new_set: addr_set = Set.remove(Tezos.sender, voters);
const new_set: addr_set = Set.remove(Tezos.get_sender(), voters);

// If the message is left without any associated addresses, remove the corresponding message_store field
const message_store_updated: message_store = (() : message_store => {
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/src/pages/Chapters/JS/ChapterOption/course.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ The _option_ type is a predefined variant type that is used to express whether t
An example in arithmetic is the division operation:

```
let div = ([a, b]: [nat, nat]): option<nat> => {
let div = (a: nat, b: nat): option<nat> => {
if(b == (0 as nat)){
return (None() as option <nat>);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
type weapon_power = map<string, int>;

let main = (_p: unit, _store: weapon_power) : ([list <operation>, weapon_power]) => {
let main = (_p: unit, _store: weapon_power) : [list <operation>, weapon_power] => {
let weapons: weapon_power =
Map.literal(list([
["Main Laser", 5],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
type weapon_power = map<string, int>;

let main = (_p: unit, _store: weapon_power) : ([list <operation>, weapon_power]) => {
let main = (_p: unit, _store: weapon_power) : [list <operation>, weapon_power] => {
let weapons: weapon_power =
Map.literal(list([
["Main Laser", 5],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let doSomethingInventory = (str: string) : string => {

type param = | ["Apply", string];

const main = ([p, _store]: [param, string]) : [list<operation>, string] => ([
const main = (p: param, _store: string) : [list<operation>, string] => ([
list([]) as list<operation>,
match(p, {
Apply: (str: string) => doSomethingInventory(str)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ let doSomethingInventory = (str: string) : string => {

type param = | ["Apply", string];

const main = ([p, _store]: [param, string]) : [list<operation>, string] => ([
const main = (p: param, _store: string) : [list<operation>, string] => ([
list([]) as list<operation>,
match(p, {
Apply: (str: string) => doSomethingInventory(str)
Expand Down
Loading

0 comments on commit abec34e

Please sign in to comment.