Skip to content

Commit

Permalink
Tests issues fix
Browse files Browse the repository at this point in the history
Tests issues fix

Tests issues fix

Tests issues fix

Tests issues fix

Tests issues fix

Tests issues fix

Tests issues fix

Tests issues fix

Tests issues fix

Tests issues fix

Tests issues fix

Tests issues fix

Update ton libs (2-4 tutorials) and tests issues fix
  • Loading branch information
sun-rhythms committed Oct 14, 2023
1 parent 08784d3 commit 88127cd
Show file tree
Hide file tree
Showing 33 changed files with 173 additions and 156 deletions.
2 changes: 1 addition & 1 deletion 01-wallet/test/npmton/step7.expected.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
kQBwP1UC3dbBjqOgWHNu3Nfa10FbQI-VGwck7JeOO92qnaEv
UQC9Q5w33dGVyPVVgUct_tRmYQ-JmayerW0IxN0Gf2cTtiQP
workchain: 0
11 changes: 7 additions & 4 deletions 01-wallet/test/npmton/step7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import { mnemonicToWalletKey } from "@ton/crypto";
import { WalletContractV4 } from "@ton/ton";

async function main() {
// use your mnemonic phrase from .end file in the root of the project and change wallet address
// const mnemonic = process.env.MNEMONIC;
// also you need to change your wallet address in step7.expected.txt to run test
const mnemonic = "memory album during buyer copper until arm forest identify race eyebrow bunker dawn phrase butter knock owner thumb click lottery catalog desk trial copper"; // your 24 secret words (replace ... with the rest of the words)
// on GH Actions test uses GH secret as mnemonic
// to run this test locally, you should create the file .env in the project root
// with your mnemonic phrase (according to repo README)
// and change step7.expected wallet address to yours
const mnemonic = process.env.MNEMONIC;

// open wallet v4 (notice the correct wallet version here)
const key = await mnemonicToWalletKey(mnemonic!.split(" "));
const wallet = WalletContractV4.create({ publicKey: key.publicKey, workchain: 0 });

Expand Down
4 changes: 2 additions & 2 deletions 01-wallet/test/npmton/step8.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { mnemonicToWalletKey } from "@ton/crypto";
import { WalletContractV4, TonClient, fromNano } from "@ton/ton";

async function main() {
// open wallet v4 (notice the correct wallet version here)
const mnemonic = process.env.MNEMONIC;; // your 24 secret words (replace ... with the rest of the words)
const mnemonic = process.env.MNEMONIC;

const key = await mnemonicToWalletKey(mnemonic!.split(" "));
const wallet = WalletContractV4.create({ publicKey: key.publicKey, workchain: 0 });

Expand Down
2 changes: 1 addition & 1 deletion 01-wallet/test/npmton/step9.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { mnemonicToWalletKey } from "@ton/crypto";
import { TonClient, WalletContractV4, internal } from "@ton/ton";

async function main() {
// open wallet v4 (notice the correct wallet version here)

const mnemonic = process.env.MNEMONIC;
const key = await mnemonicToWalletKey(mnemonic!.split(" "));
const wallet = WalletContractV4.create({ publicKey: key.publicKey, workchain: 0 });
Expand Down
2 changes: 1 addition & 1 deletion 01-wallet/test/tonweb/step7.expected.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
kQBwP1UC3dbBjqOgWHNu3Nfa10FbQI-VGwck7JeOO92qnaEv
UQC9Q5w33dGVyPVVgUct_tRmYQ-JmayerW0IxN0Gf2cTtiQP
workchain: 0
9 changes: 5 additions & 4 deletions 01-wallet/test/tonweb/step7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import { mnemonicToKeyPair } from "tonweb-mnemonic";
import TonWeb from "tonweb";

async function main() {
// use your mnemonic phrase from .end file in the root of the project and change wallet address
// const mnemonic = process.env.MNEMONIC;
// also you need to change your wallet address in step7.expected.txt to run test
const mnemonic = "memory album during buyer copper until arm forest identify race eyebrow bunker dawn phrase butter knock owner thumb click lottery catalog desk trial copper"; // your 24 secret words (replace ... with the rest of the words)
// on GH Actions test uses GH secret as mnemonic
// to run this test locally, you should create the file .env in the project root
// with your mnemonic phrase (according to repo README)
// and change step7.expected wallet address to yours
const mnemonic = process.env.MNEMONIC;
const key = await mnemonicToKeyPair(mnemonic!.split(" "));

// open wallet v4 (notice the correct wallet version here)
Expand Down
3 changes: 1 addition & 2 deletions 01-wallet/test/tonweb/step8.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import { mnemonicToKeyPair } from "tonweb-mnemonic";
import TonWeb from "tonweb";

async function main() {
// const mnemonic = process.env.MNEMONIC; // use your mnemonic phrase from .end file in the root of the project
const mnemonic = "memory album during buyer copper until arm forest identify race eyebrow bunker dawn phrase butter knock owner thumb click lottery catalog desk trial copper"; // your 24 secret words (replace ... with the rest of the words)
const mnemonic = process.env.MNEMONIC;
const key = await mnemonicToKeyPair(mnemonic!.split(" "));

// initialize ton rpc client on testnet
Expand Down
3 changes: 1 addition & 2 deletions 01-wallet/test/tonweb/step9.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import { mnemonicToKeyPair } from "tonweb-mnemonic";
import TonWeb from "tonweb";

async function main() {
// const mnemonic = process.env.MNEMONIC; // use your mnemonic phrase from .end file in the root of the project
const mnemonic = "memory album during buyer copper until arm forest identify race eyebrow bunker dawn phrase butter knock owner thumb click lottery catalog desk trial copper";
const mnemonic = process.env.MNEMONIC;
const key = await mnemonicToKeyPair(mnemonic!.split(" "));

// initialize ton rpc client on testnet
Expand Down
24 changes: 12 additions & 12 deletions 02-contract/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ The recommended way to interact with contracts is to create a small TypeScript c
Use the following code in `wrappers/Counter.ts` to create the initial data cell for deployment:

```ts
import { Contract, ContractProvider, Sender, Address, Cell, contractAddress, beginCell } from "ton-core";
import { Contract, ContractProvider, Sender, Address, Cell, contractAddress, beginCell } from "@ton/core";

export default class Counter implements Contract {

Expand All @@ -183,7 +183,7 @@ export default class Counter implements Contract {
}
```

Notice a few interesting things about this TypeScript code. First, it depends on the package [ton-core](https://www.npmjs.com/package/ton-core) instead of [ton](https://www.npmjs.com/package/ton), which contains a small subset of base types and is therefore slower to change - an important feature when building a stable interface for our contract. Second, the code that creates the data cell mimics the FunC API and is almost identical to our `save_data()` FunC function. Third, we can see the derivation of the contract address from the code cell and data cell using the function `contractAddress`.
Notice a few interesting things about this TypeScript code. First, it depends on the package [@ton/core](https://www.npmjs.com/package/@ton/core) instead of [ton](https://www.npmjs.com/package/ton), which contains a small subset of base types and is therefore slower to change - an important feature when building a stable interface for our contract. Second, the code that creates the data cell mimics the FunC API and is almost identical to our `save_data()` FunC function. Third, we can see the derivation of the contract address from the code cell and data cell using the function `contractAddress`.

The actual deployment involves sending the first message that will cause our contract to be deployed. We can piggyback any message that is directed towards our contract. This can even be the increment message with op #1, but we will do something simpler. We will just send some TON coins to our contract (an empty message) and piggyback that. Let's make this part of our interface. Add the function `sendDeploy()` to `wrappers/Counter.ts` - this function will send the deployment message:

Expand Down Expand Up @@ -226,8 +226,8 @@ network:testnet
```ts
import * as fs from "fs";
import { getHttpEndpoint } from "@orbs-network/ton-access";
import { mnemonicToWalletKey } from "ton-crypto";
import { TonClient, Cell, WalletContractV4 } from "ton";
import { mnemonicToWalletKey } from "@ton/crypto";
import { TonClient, Cell, WalletContractV4 } from "@ton/ton";
import Counter from "../wrappers/Counter"; // this is the interface class from step 7

export async function run() {
Expand Down Expand Up @@ -286,8 +286,8 @@ network:mainnet
```ts
import * as fs from "fs";
import { getHttpEndpoint } from "@orbs-network/ton-access";
import { mnemonicToWalletKey } from "ton-crypto";
import { TonClient, Cell, WalletContractV4 } from "ton";
import { mnemonicToWalletKey } from "@ton/crypto";
import { TonClient, Cell, WalletContractV4 } from "@ton/ton";
import Counter from "../wrappers/Counter"; // this is the interface class from step 7

export async function run() {
Expand Down Expand Up @@ -406,7 +406,7 @@ network:testnet
---
```ts
import { getHttpEndpoint } from "@orbs-network/ton-access";
import { TonClient, Address } from "ton";
import { TonClient, Address } from "@ton/ton";
import Counter from "../wrappers/Counter"; // this is the interface class we just implemented

export async function run() {
Expand All @@ -432,7 +432,7 @@ network:mainnet
---
```ts
import { getHttpEndpoint } from "@orbs-network/ton-access";
import { TonClient, Address } from "ton";
import { TonClient, Address } from "@ton/ton";
import Counter from "../wrappers/Counter"; // this is the interface class we just implemented

export async function run() {
Expand Down Expand Up @@ -501,8 +501,8 @@ network:testnet
---
```ts
import { getHttpEndpoint } from "@orbs-network/ton-access";
import { mnemonicToWalletKey } from "ton-crypto";
import { TonClient, WalletContractV4, Address } from "ton";
import { mnemonicToWalletKey } from "@ton/crypto";
import { TonClient, Cell, WalletContractV4 } from "@ton/ton";
import Counter from "../wrappers/Counter"; // this is the interface class we just implemented

export async function run() {
Expand Down Expand Up @@ -553,8 +553,8 @@ network:mainnet
---
```ts
import { getHttpEndpoint } from "@orbs-network/ton-access";
import { mnemonicToWalletKey } from "ton-crypto";
import { TonClient, WalletContractV4, Address } from "ton";
import { mnemonicToWalletKey } from "@ton/crypto";
import { TonClient, Cell, WalletContractV4 } from "@ton/ton";
import Counter from "../wrappers/Counter"; // this is the interface class we just implemented

export async function run() {
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions 02-contract/test/step9.ts → 02-contract/test/getCounter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getHttpEndpoint } from "@orbs-network/ton-access";
import { TonClient, Address } from "@ton/ton";
import Counter from "./counter.step9"; // this is the interface class we just implemented

export async function main() {
export async function run() {
// initialize ton rpc client on testnet
const endpoint = await getHttpEndpoint({ network: "testnet" });
const client = new TonClient({ endpoint });
Expand All @@ -23,7 +23,7 @@ export async function main() {
console.log("value:", counterValue >= 1000000000000n ? "more than 1T" : "less than 1T");
}

main()
run()

function sleep(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
Expand Down
8 changes: 4 additions & 4 deletions 02-contract/test/index.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ npm install @ton/ton @ton/core @ton/crypto
npm install @orbs-network/ton-access
npx func-js stdlib.fc counter.fc --boc counter.cell
npx ts-node deploy.step8.ts > deploy.step8.output.txt
COUNTER_ADDRESS=`cut -d : -s -f 2 < deploy.step8.output.txt` npx ts-node step9.ts > step9.output.txt
diff step9.output.txt step9.expected.txt
COUNTER_ADDRESS=`cut -d : -s -f 2 < deploy.step8.output.txt` npx ts-node step10.ts > step10.output.txt
diff step10.output.txt step10.expected.txt
COUNTER_ADDRESS=`cut -d : -s -f 2 < deploy.step8.output.txt` npx ts-node getCounter.ts > getCounter.output.txt
diff getCounter.output.txt getCounter.expected.txt
COUNTER_ADDRESS=`cut -d : -s -f 2 < deploy.step8.output.txt` npx ts-node sendIncrement.ts > sendIncrement.output.txt
diff sendIncrement.output.txt sendIncrement.expected.txt
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { mnemonicToWalletKey } from "@ton/crypto";
import { TonClient, WalletContractV4, Address } from "@ton/ton";
import Counter from "./counter.step10"; // this is the interface class we just implemented

export async function main() {
export async function run() {
// initialize ton rpc client on testnet
const endpoint = await getHttpEndpoint({ network: "testnet" });
const client = new TonClient({ endpoint });
Expand Down Expand Up @@ -43,7 +43,7 @@ export async function main() {
console.log("transaction confirmed!");
}

main()
run()

function sleep(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
Expand Down
8 changes: 4 additions & 4 deletions 03-client/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ npm install
We will need to install a few more packages that will allow us to interact with TON Blockchain. We've seen these packages in action in the previous tutorial. Run the following in terminal:

```console
npm install ton ton-core ton-crypto
npm install @ton/ton @ton/core @ton/crypto
npm install @orbs-network/ton-access
```

Expand Down Expand Up @@ -209,7 +209,7 @@ import { useEffect, useState } from 'react';
import Counter from '../contracts/counter';
import { useTonClient } from './useTonClient';
import { useAsyncInitialize } from './useAsyncInitialize';
import { Address, OpenedContract } from 'ton-core';
import { Address, OpenedContract } from '@ton/core';

export function useCounterContract() {
const client = useTonClient();
Expand Down Expand Up @@ -292,7 +292,7 @@ Create the file `src/hooks/useTonConnect.ts` with the following content:

```ts
import { useTonConnectUI } from '@tonconnect/ui-react';
import { Sender, SenderArguments } from 'ton-core';
import { Sender, SenderArguments } from '@ton/core';

export function useTonConnect(): { sender: Sender; connected: boolean } {
const [tonConnectUI] = useTonConnectUI();
Expand Down Expand Up @@ -327,7 +327,7 @@ import Counter from '../contracts/counter';
import { useTonClient } from './useTonClient';
import { useAsyncInitialize } from './useAsyncInitialize';
import { useTonConnect } from './useTonConnect';
import { Address, OpenedContract } from 'ton-core';
import { Address, OpenedContract } from '@ton/core';

export function useCounterContract() {
const client = useTonClient();
Expand Down
14 changes: 7 additions & 7 deletions 04-testing/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Because testing is such as big deal in smart contract development, there's a sur

3. **Writing tests in FunC** - [toncli](https://github.com/disintar/toncli) is a command-line tool written in Python that runs on your machine and supports [debug](https://github.com/disintar/toncli/blob/master/docs/advanced/transaction_debug.md) and [unit tests](https://github.com/disintar/toncli/blob/master/docs/advanced/func_tests_new.md) for FunC contracts where the tests are also written in FunC ([example](https://github.com/BorysMinaiev/func-contest-1-tests-playground/blob/main/task-1/tests/test.fc)).

4. **Bare-bones TVM with Sandbox** - [Sandbox](https://github.com/ton-community/sandbox) is a bare-bones version of just the [TVM](https://ton-blockchain.github.io/docs/tvm.pdf) running on [WebAssembly](https://webassembly.org/) with a thin JavaScript wrapper that allows test interactions from TypeScript.
4. **Bare-bones TVM with Sandbox** - [Sandbox](https://github.com/ton-org/sandbox) is a bare-bones version of just the [TVM](https://ton-blockchain.github.io/docs/tvm.pdf) running on [WebAssembly](https://webassembly.org/) with a thin JavaScript wrapper that allows test interactions from TypeScript.

5. **Deploying beta contracts to mainnet** - This form of "testing in production" simply deploys alternative beta versions of your contracts to mainnet and uses real (not free) TON coin to play with them in a real environment. If you found a bug, you simply deploy new fixed beta versions and waste a little more money.

Expand Down Expand Up @@ -76,10 +76,10 @@ module.exports = {
And finally, run in terminal:

```console
npm install ton-core @ton-community/sandbox @ton-community/test-utils
npm install @ton/core @ton/sandbox @ton/test-utils
```

This will install [Sandbox](https://github.com/ton-community/sandbox) and its dependencies. Sandbox is our magical library that will emulate TON Blockchain locally by running a bare-bones version of the TVM in process. This will guarantee that our tests will be blazingly fast and completely isolated.
This will install [Sandbox](https://github.com/ton-org/sandbox) and its dependencies. Sandbox is our magical library that will emulate TON Blockchain locally by running a bare-bones version of the TVM in process. This will guarantee that our tests will be blazingly fast and completely isolated.

## Step 2: Load our contract in a test

Expand All @@ -104,8 +104,8 @@ Create the file `step2.spec.ts` with the following content:

```ts
import * as fs from "fs";
import { Cell } from "ton-core";
import { Blockchain, SandboxContract, TreasuryContract } from "@ton-community/sandbox";
import { Cell } from "@ton/core";
import { Blockchain, SandboxContract, TreasuryContract } from "@ton/sandbox";
import Counter from "./counter"; // this is the interface class from tutorial 2

describe("Counter tests", () => {
Expand Down Expand Up @@ -296,7 +296,7 @@ The console output should include something like this:

We can see that the debug messages are printed when the test is running. When we send some TON coin explicitly to the contract (7.123 coins), we can see that the first debug print indeed shows the expected value of `msg_value`. Since the TVM doesn't support floating points, the number is represented internally as a large integer (with 9 decimals, meaning multiplied by 10^9). On the second test, when we send the increment op, we can see both debug prints showing. This is because this message also includes a small amount of coins for gas.

If you would like to see even more verbose log output from running your contracts, you can [increase the verbosity](https://github.com/ton-community/sandbox#viewing-logs) of the `blockchain` object after creating it in beforeEach:
If you would like to see even more verbose log output from running your contracts, you can [increase the verbosity](https://github.com/ton-org/sandbox#viewing-logs) of the `blockchain` object after creating it in beforeEach:

```ts
blockchain.verbosity = {
Expand Down Expand Up @@ -339,7 +339,7 @@ Ready to claim your reward? Simply scan the QR code below or click <a href="ton:

For your convenience, all the code in this tutorial is available in executable form [here](https://github.com/ton-community/tutorials/blob/main/04-testing/test).

In this tutorial we created our project skeleton manually, mostly so we can understand what happens under the hood. When creating a new contract project, you can have an excellent skeleton created automatically by an awesome dev tool called [Blueprint](https://github.com/ton-community/blueprint). To create a new contract project with Blueprint, run in terminal and follow the on-screen instructions:
In this tutorial we created our project skeleton manually, mostly so we can understand what happens under the hood. When creating a new contract project, you can have an excellent skeleton created automatically by an awesome dev tool called [Blueprint](https://github.com/ton-org/blueprint). To create a new contract project with Blueprint, run in terminal and follow the on-screen instructions:

```console
npm create ton@latest
Expand Down
10 changes: 5 additions & 5 deletions docs/01-wallet/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ <h4>TonWeb (JavaScript)</h4>

</div>
<div class="more">
<h3>More Tutorials</h3>
<h3>Tutorials</h3>
<ul>
<li><a href="https://ton-community.github.io/tutorials/01-wallet">01. Working with your first wallet</a></li>
<li><a href="https://ton-community.github.io/tutorials/02-contract">02. Writing your first smart contract</a></li>
<li><a href="https://ton-community.github.io/tutorials/03-client">03. Building your first web client</a></li>
<li><a href="https://ton-community.github.io/tutorials/04-testing">04. Testing your first smart contract</a></li>
<li><a data="tutorial-link" href="https://ton-community.github.io/tutorials/01-wallet">01. Working with your first wallet</a></li>
<li><a data="tutorial-link" href="https://ton-community.github.io/tutorials/02-contract">02. Writing your first smart contract</a></li>
<li><a data="tutorial-link" href="https://ton-community.github.io/tutorials/03-client">03. Building your first web client</a></li>
<li><a data="tutorial-link" href="https://ton-community.github.io/tutorials/04-testing">04. Testing your first smart contract</a></li>
</ul>
</div>
<div class="more">
Expand Down
Loading

0 comments on commit 88127cd

Please sign in to comment.