Skip to content

Commit cbc774b

Browse files
committed
test
1 parent 84932e3 commit cbc774b

File tree

3 files changed

+51
-152
lines changed

3 files changed

+51
-152
lines changed

docs/v3/guidelines/quick-start/blockchain-interaction/reading-from-network.mdx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ async function main() {
7474
endpoint: 'https://testnet.toncenter.com/api/v2/jsonRPC',
7575
});
7676

77-
const accountAddress = Address.parse('0QD-SuoCHsCL2pIZfE8IAKsjc0aDpDUQAoo-ALHl2mje04A-'); // Replace with any address
77+
// Replace with any address
78+
const accountAddress = Address.parse('0QD-SuoCHsCL2pIZfE8IAKsjc0aDpDUQAoo-ALHl2mje04A-');
7879

7980
// Calling method on http api
8081
const state = await tonClient.getContractState(accountAddress);
@@ -127,6 +128,7 @@ async function main() {
127128
const builder = new TupleBuilder();
128129
builder.writeAddress(Address.parse('0QD-SuoCHsCL2pIZfE8IAKsjc0aDpDUQAoo-ALHl2mje04A-'));
129130

131+
// Replace with any address
130132
const accountAddress = Address.parse('kQD0GKBM8ZbryVk2aESmzfU6b9b_8era_IkvBSELujFZPsyy')
131133

132134
// Calling http api to run get method on specific contract

docs/v3/guidelines/quick-start/blockchain-interaction/writing-to-network.mdx

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,18 @@ Versions of `node` and `npm` should be at least `v20` and `v10` correspondingly.
2727

2828
Let's set up our project structure:
2929

30-
1. Create a new directory for your project
31-
2. Initialize a Node.js project
32-
3. Install the required dependencies
30+
1. Create a new directory for your project and navigate into it.
31+
2. Initialize a Node.js project.
32+
3. Install the required dependencies.
33+
4. Initialize TypeScript configuration.
3334

3435
Run these commands in your terminal:
3536

3637
```bash
37-
mkdir writing-to-ton && cd writing-to-ton # create new directory and cd to it
38-
npm init -y # initialize Node.js project
39-
npm install typescript ts-node @ton/ton @ton/core @ton/crypto #install required dependecies
40-
npx tsc --init # initialize typesctipt
38+
mkdir writing-to-ton && cd writing-to-ton
39+
npm init -y
40+
npm install typescript ts-node @ton/ton @ton/core @ton/crypto
41+
npx tsc --init
4142
```
4243

4344
To run scripts, use the following command:
@@ -136,8 +137,8 @@ Navigate to [Tonviewer](https://testnet.tonviewer.com/) and paste your address i
136137
## Sending NFTs
137138

138139
[Non-fungible tokens](/v3/guidelines/dapps/asset-processing/nft-processing/nfts/) (NFTs) are assets like a piece of art, digital content, or video that have been tokenized via a blockchain. In TON, NFTs are represented via a collection of smart contracts:
139-
- **NFT Collection**: Stores information about the NFT collection.
140-
- **NFT Item**: Stores information about the NFT item that the user owns.
140+
- **NFT Collection**: stores information about the NFT collection.
141+
- **NFT Item**: stores information about the NFT item that the user owns.
141142

142143
To send an NFT, we should first acquire one. The easiest way to do that is to create and deploy your own NFT through [TON Tools](https://ton-collection-edit.vercel.app/deploy-nft-single). Note that the `owner` addresses of your NFT must be your wallet address to be able to perform operations on it.
143144

docs/v3/guidelines/quick-start/developing-smart-contracts/tact-folder/tact-storage-and-get-methods.mdx

Lines changed: 38 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ For more details, refer to the [Tact documentation](https://docs.tact-lang.org/#
1212
:::
1313

1414

15-
Smart contracts often need to store data, like counters or ownership information, and provide a way to read or update it through messages. In this section, you’ll learn how to define and initialize contract storage, receive and handle incoming messages, restrict access using traits, and create getter functions to read contract states outside the blockchain.
15+
Smart contracts often need to store data, like counters or ownership information, and provide a way to read or update it through messages. In this section, you’ll learn how to define and initialize contract storage, receive and handle incoming messages, and create getter functions to read contract states outside the blockchain.
1616

1717
Let's create and modify our smart contract following the standard steps described in the previous [Blueprint overview](/v3/guidelines/quick-start/developing-smart-contracts/tact-folder/tact-blueprint-sdk-overview/) section.
1818

@@ -63,7 +63,6 @@ message(0x7e8764ef) Add {
6363

6464

6565

66-
6766
#### Defining the contract
6867

6968
The [contract](https://docs.tact-lang.org/book/contracts/) definition in Tact follows an object-oriented programming style:
@@ -84,12 +83,14 @@ counter: Int as uint32;
8483
```
8584
These fields are serialized similarly to structures and stored in the contract's data register.
8685

86+
Use `self.id` and `self.counter` to access them within contract functions.
87+
8788
#### Initializing the contract
8889

89-
Define an [`init()`](https://docs.tact-lang.org/book/contracts/#init-function/) function to do this:
90+
Define an [`init()`](https://docs.tact-lang.org/book/contracts/#init-function/) function to set initial values:
9091

9192
```tact title="/contracts/hello_world.tact"
92-
init(id: Int, owner: Address) {
93+
init(id: Int) {
9394
self.id = id;
9495
self.counter = 0;
9596
}
@@ -114,32 +115,6 @@ receive() {
114115
}
115116
```
116117

117-
#### Restricting access
118-
119-
Tact also provides handy ways to share the same logic through [traits](https://docs.tact-lang.org/book/types/#traits) reusable code blocks similar to abstract classes in other languages. Tact doesn’t use classical inheritance, but traits let you define shared logic without duplicating code. They look like contracts but can’t store persistent state.
120-
For example, you can use the `Ownable` trait, which provides built-in ownership checks, to ensure that only the contract owner can send messages.
121-
122-
```tact title="/contracts/hello_world.tact"
123-
// Import library to use trait
124-
import "@stdlib/ownable";
125-
126-
// Ownable trait introduced here
127-
contract HelloWorld with Ownable {
128-
129-
...
130-
131-
receive(msg: Add) {
132-
self.requireOwner();
133-
self.counter += msg.amount;
134-
self.notify("Cashback".asComment());
135-
}
136-
}
137-
```
138-
139-
:::info
140-
[Identity validation](https://docs.tact-lang.org/book/security-best-practices/#identity-validation) plays a key role in secure contract interactions. You can read more about identity validation and its importance in the linked documentation.
141-
:::
142-
143118
#### Getter functions
144119

145120
Tact supports [getter functions](https://docs.tact-lang.org/book/functions/#getter-functions) for retrieving contract state off-chain:
@@ -154,32 +129,26 @@ get fun counter(): Int {
154129
}
155130
```
156131

157-
Note, that the `owner` getter is automatically defined via the `Ownable` trait.
158-
159132
#### Complete contract
160133

161134
```tact title="/contracts/hello_world.tact"
162-
import "@stdlib/ownable";
163-
164135
// message with opcode
165136
message(0x7e8764ef) Add {
166137
queryId: Int as uint64;
167138
amount: Int as uint32;
168139
}
169140
170-
// Contract defenition. `Ownable` is a trait to share functionality.
171-
contract HelloWorld with Ownable {
141+
// Contract defenition
142+
contract HelloWorld {
172143
173144
// storage variables
174145
id: Int as uint32;
175146
counter: Int as uint32;
176-
owner: Address;
177147
178148
// init function.
179-
init(id: Int, owner: Address) {
149+
init(id: Int) {
180150
self.id = id;
181151
self.counter = 0;
182-
self.owner = owner;
183152
}
184153
185154
// default(null) recieve for deploy
@@ -189,8 +158,6 @@ contract HelloWorld with Ownable {
189158
190159
// function to recive messages from other contracts
191160
receive(msg: Add) {
192-
// function from `Ownable` trait to assert, that only owner may call this
193-
self.requireOwner();
194161
self.counter += msg.amount;
195162
196163
// Notify the caller that the receiver was executed and forward remaining value back
@@ -246,10 +213,12 @@ This code exports everything inside the `tact_HelloWorld.ts` file in the build f
246213
<details>
247214
<summary><b>Updating tests</b></summary>
248215

249-
Now let's ensure that our smart contract code fails when we try to send `add` message from non-owner:
250-
- Create `HelloWorld` with some owner.
251-
- Create another smart contract that will have different address - `nonOwner`.
252-
- Try to send an internal message to `HelloWorld` and enusre that it fails with expected `exitCode` and counter field remains the same.
216+
Now let's ensure that our smart contract correctly updates the counter:
217+
- Deploy the `HelloWorld` contract with an initial ID.
218+
- Check that the initial counter value is `0`.
219+
- Send an `Add` message to increment the counter.
220+
- Verify that the counter value increases by the expected amount.
221+
253222

254223
Implementation of test should look like this:
255224

@@ -262,130 +231,61 @@ This code exports everything inside the `tact_HelloWorld.ts` file in the build f
262231

263232
describe('HelloWorld Basic Tests', () => {
264233
let blockchain: Blockchain;
265-
let helloWorld: SandboxContract<HelloWorld >;
266-
let owner: SandboxContract<TreasuryContract>;
234+
let helloWorld: SandboxContract<HelloWorld>;
235+
let sender: SandboxContract<TreasuryContract>;
267236

268237
beforeEach(async () => {
269-
// Create a new blockchain instance
270238
blockchain = await Blockchain.create();
239+
sender = await blockchain.treasury('user');
271240

272-
// Create an owner wallet
273-
owner = await blockchain.treasury('owner');
274-
275-
// Deploy the contract
276241
helloWorld = blockchain.openContract(
277-
await HelloWorld .fromInit(0n, owner.address)
242+
// init with id = 0
243+
await HelloWorld.fromInit(0n)
278244
);
279245

280-
// Send deploy transaction
281246
const deployResult = await helloWorld.send(
282-
owner.getSender(),
283-
{ value: toNano('1.00') },
247+
sender.getSender(),
248+
{ value: toNano('1') },
284249
null
285250
);
286251

287-
// Verify deployment was successful
288252
expect(deployResult.transactions).toHaveTransaction({
289-
from: owner.address,
253+
from: sender.address,
290254
to: helloWorld.address,
291255
deploy: true,
292256
success: true
293257
});
294258
});
295259

296-
it('should initialize with correct values', async () => {
297-
// Check initial counter value
298-
const initialCounter = await helloWorld.getCounter();
299-
expect(initialCounter).toBe(0n);
300-
301-
// Check initial ID
260+
it('should initialize with id = 0 and counter = 0', async () => {
302261
const id = await helloWorld.getId();
262+
const counter = await helloWorld.getCounter();
303263
expect(id).toBe(0n);
264+
expect(counter).toBe(0n);
304265
});
305266

306-
it('should allow owner to increment counter', async () => {
307-
// Get initial counter value
308-
const initialCounter = await helloWorld.getCounter();
309-
310-
// Increment counter by 5
311-
const incrementAmount = 5n;
267+
it('should increase counter by given amount', async () => {
312268
const result = await helloWorld.send(
313-
owner.getSender(),
314-
{ value: toNano('0.05') },
269+
sender.getSender(),
270+
{ value: toNano('0.1') },
315271
{
316272
$$type: 'Add',
317-
amount: incrementAmount,
318-
queryId: 0n
273+
queryId: 0n,
274+
amount: 10n
319275
}
320276
);
321277

322-
// Verify transaction was successful
323278
expect(result.transactions).toHaveTransaction({
324-
from: owner.address,
279+
from: sender.address,
325280
to: helloWorld.address,
326281
success: true
327282
});
328283

329-
// Check counter was incremented correctly
330-
const newCounter = await helloWorld.getCounter();
331-
expect(newCounter).toBe(initialCounter + incrementAmount);
332-
});
333-
334-
it('should prevent non-owner from incrementing counter', async () => {
335-
// Create a non-owner wallet
336-
const nonOwner = await blockchain.treasury('nonOwner');
337-
338-
// Get initial counter value
339-
const initialCounter = await helloWorld.getCounter();
340-
341-
// Try to increment counter as non-owner
342-
const result = await helloWorld.send(
343-
nonOwner.getSender(),
344-
{ value: toNano('0.05') },
345-
{
346-
$$type: 'Add',
347-
amount: 5n,
348-
queryId: 0n
349-
}
350-
);
351-
352-
// Verify transaction failed
353-
expect(result.transactions).toHaveTransaction({
354-
from: nonOwner.address,
355-
to: helloWorld.address,
356-
success: false
357-
});
358-
359-
// Verify counter was not changed
360-
const newCounter = await helloWorld.getCounter();
361-
expect(newCounter).toBe(initialCounter);
362-
});
363-
364-
it('should handle multiple increments correctly', async () => {
365-
// Perform multiple increments
366-
const increments = [3n, 7n, 2n];
367-
let expectedCounter = 0n;
368-
369-
for (const amount of increments) {
370-
await helloWorld.send(
371-
owner.getSender(),
372-
{ value: toNano('0.05') },
373-
{
374-
$$type: 'Add',
375-
amount: amount,
376-
queryId: 0n
377-
}
378-
);
379-
expectedCounter += amount;
380-
}
381-
382-
// Verify final counter value
383-
const finalCounter = await helloWorld.getCounter();
384-
expect(finalCounter).toBe(expectedCounter);
284+
const counter = await helloWorld.getCounter();
285+
expect(counter).toBe(10n);
385286
});
386287
});
387288

388-
389289
```
390290

391291
Don't forget to verify the example is correct by running test script:
@@ -400,21 +300,17 @@ This code exports everything inside the `tact_HelloWorld.ts` file in the build f
400300

401301
PASS tests/HelloWorld.spec.ts
402302
HelloWorld Basic Tests
403-
✓ should initialize with correct values (211 ms)
404-
✓ should allow owner to increment counter (100 ms)
405-
✓ should prevent non-owner from incrementing counter (152 ms)
406-
✓ should handle multiple increments correctly (112 ms)
303+
✓ should initialize with id = 0 and counter = 0 (305 ms)
304+
✓ should increase counter by given amount (120 ms)
407305

408306
Test Suites: 1 passed, 1 total
409-
Tests: 4 passed, 4 total
307+
Tests: 2 passed, 2 total
410308
Snapshots: 0 total
411-
Time: 1.193 s, estimated 2 s
309+
Time: 1.399 s
412310
Ran all test suites.
413311

414-
```
415-
416-
---
417312

313+
```
418314
</details>
419315

420316

0 commit comments

Comments
 (0)