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

Commit 36942f1

Browse files
committedMar 25, 2024
feat: Added a privilege check example to Cookbook
1 parent d8ec002 commit 36942f1

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed
 

‎pages/book/cookbook.mdx

+37-1
Original file line numberDiff line numberDiff line change
@@ -535,4 +535,40 @@ let itemAddress: Address = contractAddress(self.getNftItemInit(itemIndex));
535535
[Tact collection and item contracts example](https://github.com/howardpen9/nft-template-in-tact/blob/tutorial/sources/contract.tact)\
536536
[FunC collection and item contracts example](https://github.com/Cosmodude/TAP/tree/main/contracts)
537537

538-
</Callout>
538+
</Callout>
539+
540+
## Receiving messages
541+
542+
### How to check sender privileges using Ownable trait
543+
544+
```tact
545+
// Ownable has to be imported from stdlib, as well as Deployable, for convenience:
546+
import "@stdlib/ownable";
547+
import "@stdlib/deploy";
548+
549+
message FooBarMsg {
550+
newVal: Int as uint32;
551+
}
552+
553+
// Ownable trait can limit certain actions to the owner only
554+
contract SenderChecker with Deployable, Ownable {
555+
owner: Address; // Ownable trait requires you to add this exact state variable
556+
val: Int as uint32; // some value
557+
558+
init() {
559+
// we can initialize owner to any value we want, the deployer in this case:
560+
self.owner = sender();
561+
self.val = 0;
562+
}
563+
564+
receive("inc") {
565+
require(self.owner == sender(), "Only the owner can increase the value!");
566+
self.val += 1;
567+
}
568+
569+
receive(msg: FooBarMsg) {
570+
require(self.owner == sender(), "Only the owner can set the value!");
571+
self.val = msg.newVal;
572+
}
573+
}
574+
```

‎pages/book/receive.mdx

+6-9
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,17 @@ TON is a distributed blockchain which means that communication between contracts
44

55
## Receive internal messages
66

7-
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`.
7+
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.
88

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

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

1917
```tact
20-
2118
message MyMessage {
2219
value: Int;
2320
}

0 commit comments

Comments
 (0)