Skip to content
This repository was archived by the owner on Dec 12, 2024. It is now read-only.

feat: Privilege check in the Cookbook #140

Merged
merged 5 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion pages/book/cookbook.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -535,4 +535,40 @@ let itemAddress: Address = contractAddress(self.getNftItemInit(itemIndex));
[Tact collection and item contracts example](https://github.com/howardpen9/nft-template-in-tact/blob/tutorial/sources/contract.tact)\
[FunC collection and item contracts example](https://github.com/Cosmodude/TAP/tree/main/contracts)

</Callout>
</Callout>

## Receiving messages

### How to check sender privileges using Ownable trait

```tact
// Ownable has to be imported from stdlib, as well as Deployable, for convenience:
import "@stdlib/ownable";
import "@stdlib/deploy";

message FooBarMsg {
newVal: Int as uint32;
}

// Ownable trait can limit certain actions to the owner only
contract SenderChecker with Deployable, Ownable {
owner: Address; // Ownable trait requires you to add this exact state variable
val: Int as uint32; // some value

init() {
// we can initialize owner to any value we want, the deployer in this case:
self.owner = sender();
self.val = 0;
}

receive("inc") {
require(self.owner == sender(), "Only the owner can increase the value!");
self.val += 1;
}

receive(msg: FooBarMsg) {
require(self.owner == sender(), "Only the owner can set the value!");
self.val = msg.newVal;
}
}
```
15 changes: 6 additions & 9 deletions pages/book/receive.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,17 @@ TON is a distributed blockchain which means that communication between contracts

## Receive internal messages

To receive a message of the required type, you need to declare a receiver function, for example, `receive("increment")`. This notation means the declaration of a `receiver` function that will be called when a text with the value `"increment"` is sent to the contract. The function body can modify the state of the contract and send messages to other contracts. It is impossible to call `receiver` directly. If you need to reuse some logic you can declare a function and call it from `receiver`.
To receive a message of the required type, you need to declare a receiver function, for example, `receive("increment"){:tact}`. This notation means the declaration of a receiver function that will be called when a text with the value `"increment"{:tact}` is sent to the contract. The function body can modify the state of the contract and send messages to other contracts. It is impossible to call a receiver directly. If you need to reuse some logic you can declare a function and call it from the receiver.

There are several receiver functions. All receiver functions are processed in the order they are listed below:
* `receive()` - called when an empty message is sent to the contract
* `receive("message")` - called when a text message with a specific comment is sent to the contract
* `receive(str: String)` - called when an arbitrary text message is sent to the contract
* `receive(msg: MyMessage)` - called when a binary message of type `MyMessage` is sent to the contract
* `receive(msg: Slice)` - called when binary message of unknown type is sent to the contract


## Example
* `receive(){:tact}` - called when an empty message is sent to the contract
* `receive("message"){:tact}` - called when a text message with a specific comment is sent to the contract
* `receive(str: String){:tact}` - called when an arbitrary text message is sent to the contract
* `receive(msg: MyMessage){:tact}` - called when a binary message of type `MyMessage` is sent to the contract
* `receive(msg: Slice){:tact}` - called when binary message of unknown type is sent to the contract

```tact

message MyMessage {
value: Int;
}
Expand Down
Loading