Skip to content

Commit 173a145

Browse files
committed
Split the receiver example to binary messages and textual messages
1 parent 3eb44b7 commit 173a145

File tree

10 files changed

+550
-24
lines changed

10 files changed

+550
-24
lines changed

src/routes/(examples)/03-receivers/+page.svelte

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import { Receivers } from "./Receivers";
1010
1111
let sender: Sender;
12-
let sender2: Sender;
1312
let contract: SandboxContract<Receivers>;
1413
1514
$store = {

src/routes/(examples)/03-receivers/Receivers.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -521,15 +521,9 @@ export class Receivers implements Contract {
521521
provider: ContractProvider,
522522
via: Sender,
523523
args: { value: bigint; bounce?: boolean | null | undefined },
524-
message: "increment" | "decrement" | Add | Subtract | MultiMath | Deploy,
524+
message: Add | Subtract | MultiMath | "increment" | "decrement" | Deploy,
525525
) {
526526
let body: Cell | null = null;
527-
if (message === "increment") {
528-
body = beginCell().storeUint(0, 32).storeStringTail(message).endCell();
529-
}
530-
if (message === "decrement") {
531-
body = beginCell().storeUint(0, 32).storeStringTail(message).endCell();
532-
}
533527
if (message && typeof message === "object" && !(message instanceof Slice) && message.$$type === "Add") {
534528
body = beginCell().store(storeAdd(message)).endCell();
535529
}
@@ -539,6 +533,12 @@ export class Receivers implements Contract {
539533
if (message && typeof message === "object" && !(message instanceof Slice) && message.$$type === "MultiMath") {
540534
body = beginCell().store(storeMultiMath(message)).endCell();
541535
}
536+
if (message === "increment") {
537+
body = beginCell().storeUint(0, 32).storeStringTail(message).endCell();
538+
}
539+
if (message === "decrement") {
540+
body = beginCell().storeUint(0, 32).storeStringTail(message).endCell();
541+
}
542542
if (message && typeof message === "object" && !(message instanceof Slice) && message.$$type === "Deploy") {
543543
body = beginCell().store(storeDeploy(message)).endCell();
544544
}

src/routes/(examples)/03-receivers/content.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ Sending a message to a contract costs gas and is processed in the course of a tr
88

99
## Receivers
1010

11-
Contract methods named `receive()` are the handlers that process each incoming message type. Tact will automatically route the correct message to the correct receiver listening for it.
11+
When designing your contract, make a list of every operation that your contract supports, then, define a message for each operation, and finally, implement a handler for each message containing the logic of what to do when it arrives.
1212

13-
## Hardware wallets and blind signing
13+
Contract methods named `receive()` are the handlers that process each incoming message type. Tact will automatically route every incoming message to the correct receiver listening for it according to its type.
1414

15-
When working with dangerous contracts that handle a lot of money, users are encouraged to use hardware wallets like [Ledger](https://www.ledger.com/). Hardware wallets cannot decode binary messages to confirm to the user what they're actually signing. Tact supports textual messages for this reason, since they can easily be confirmed with users, eliminating phishing risks.
15+
Messages are defined using the `message` keyword. They can carry input arguments. Notice that for integers, you must define the encoding size, just like in state variables. When somebody sends the message, they serialize it over the wire.

src/routes/(examples)/03-receivers/contract.tact

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import "@stdlib/deploy";
22

3+
// this message will cause our contract to add an amount to the counter
34
message Add {
45
amount: Int as uint32;
56
}
67

8+
// this message will cause our contract to subtract an amount from the counter
79
message Subtract {
810
amount: Int as uint32;
911
}
1012

13+
// this message will cause our contract to do a complex math operation on the counter
1114
message MultiMath {
1215
add: Int as uint32;
1316
subtract: Int as uint32;
@@ -21,33 +24,33 @@ contract Receivers with Deployable {
2124
init() {
2225
self.val = 0;
2326
}
24-
25-
// a textual string message, using the comment field of a transfer (great for Ledger)
26-
receive("increment") {
27-
self.val = self.val + 1;
28-
}
29-
30-
// a different textual string message, you can have as many as you want
31-
receive("decrement") {
32-
self.val = self.val - 1;
33-
}
3427

35-
// binary message that can carry input arguments
28+
// handler for the "Add" message - this is a binary message that has an input argument (amount)
3629
receive(msg: Add) {
3730
self.val = self.val + msg.amount;
3831
}
3932

40-
// a different binary message, although its format is identical
33+
// handler for the "Subtract" message - this is a different binary message although its format is identical
4134
receive(msg: Subtract) {
4235
self.val = self.val - msg.amount;
4336
}
4437

45-
// binary messages can hold multiple arguments
38+
// handler for the "MultiMath" message - this is a binary message that holds multiple input arguments
4639
receive(msg: MultiMath) {
4740
self.val = self.val + msg.add;
4841
self.val = self.val - msg.subtract;
4942
self.val = self.val * msg.multiply;
5043
}
44+
45+
// handler for "increment" textual message - this is a textual string message, these cannot carry input arguments
46+
receive("increment") {
47+
self.val = self.val + 1;
48+
}
49+
50+
// handler for "decrement" textual message - this is a different textual string message, you can have as many as you want
51+
receive("decrement") {
52+
self.val = self.val - 1;
53+
}
5154

5255
get fun value(): Int {
5356
return self.val;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<script lang="ts">
2+
import { toNano, type Sender } from "ton-core";
3+
import { Blockchain, type SandboxContract } from "@ton-community/sandbox";
4+
import { getExample } from "$lib/helpers";
5+
import store from "$lib/store";
6+
7+
import markdown from "./content.md?raw";
8+
import tactCode from "./contract.tact?raw";
9+
import { Receivers } from "./Receivers";
10+
11+
let sender: Sender;
12+
let contract: SandboxContract<Receivers>;
13+
14+
$store = {
15+
markdown,
16+
tactCode,
17+
deploy: async () => {
18+
const blockchain = await Blockchain.create();
19+
const deployer = await blockchain.treasury("deployer");
20+
sender = deployer.getSender();
21+
contract = blockchain.openContract(await Receivers.fromInit());
22+
const addresses = {
23+
[deployer.address.toString()]: "deployer",
24+
[contract.address.toString()]: "contract",
25+
};
26+
return [[contract], addresses, [await contract.send(deployer.getSender(), { value: toNano(1) }, { $$type: "Deploy", queryId: 0n })]];
27+
},
28+
messages: {
29+
increment: async () => {
30+
return [await contract.send(sender, { value: toNano(1) }, "increment")];
31+
},
32+
decrement: async () => {
33+
return [await contract.send(sender, { value: toNano(1) }, "decrement")];
34+
},
35+
"increment by 2": async () => {
36+
return [await contract.send(sender, { value: toNano(1) }, "increment by 2")];
37+
},
38+
"increment by 3": async () => {
39+
return [await contract.send(sender, { value: toNano(1) }, "increment by 3")];
40+
},
41+
},
42+
getters: {
43+
value: async () => {
44+
return await contract.getValue();
45+
},
46+
},
47+
prev: getExample(import.meta.url).prev,
48+
next: getExample(import.meta.url).next,
49+
};
50+
</script>

0 commit comments

Comments
 (0)