From 55f3e56dbc18f0645cf9dcc54cb455f1510a7298 Mon Sep 17 00:00:00 2001 From: Fabco Date: Tue, 9 Jul 2024 10:23:19 +0200 Subject: [PATCH] New test suite, 10.000 transfers --- README.md | 1 + tests/Arithmetic.spec.ts | 2 +- tests/Fibonacci.spec.ts | 2 +- tests/TenThousandsTransfers.spec.ts | 62 +++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 tests/TenThousandsTransfers.spec.ts diff --git a/README.md b/README.md index 16a7f12..4ec8673 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ A set of low, high value TON blockchain (Telegram Open Network) FunC smart contr - **Arithmetic** : TON Smart contract that performs basic add/substract/multiply operations on a integer - **Fibonacci** : TON Smart contract that stores two integers, and continues the fibonacci sequence everytime it is touched. +- **Ten thousands transfers** : Two blockchain users exchange TON back and forth, 10.000 times, balance is checked after each transfer. ```sh npm i diff --git a/tests/Arithmetic.spec.ts b/tests/Arithmetic.spec.ts index 58fba9f..bb2c8cd 100644 --- a/tests/Arithmetic.spec.ts +++ b/tests/Arithmetic.spec.ts @@ -16,7 +16,7 @@ describe('[Arithmetic]', () => { let blockchain: Blockchain; let deployer: SandboxContract; let arithmeticContract: SandboxContract; - let user1: any = null; + let user1: null | SandboxContract = null; let provider: null | ContractProvider = null; beforeAll(async () => { diff --git a/tests/Fibonacci.spec.ts b/tests/Fibonacci.spec.ts index b397c60..994d1db 100644 --- a/tests/Fibonacci.spec.ts +++ b/tests/Fibonacci.spec.ts @@ -10,7 +10,7 @@ describe('[Fibonacci]', () => { let blockchain: Blockchain; let deployer: SandboxContract; let contract: SandboxContract; - let user1: any = null; + let user1: null | SandboxContract = null; beforeAll(async () => { code = await compile('Fibonacci'); diff --git a/tests/TenThousandsTransfers.spec.ts b/tests/TenThousandsTransfers.spec.ts new file mode 100644 index 0000000..1d27cd5 --- /dev/null +++ b/tests/TenThousandsTransfers.spec.ts @@ -0,0 +1,62 @@ +import { Blockchain, SandboxContract, TreasuryContract, printTransactionFees } from '@ton/sandbox'; +import { Cell, toNano } from '@ton/core'; +import '@ton/test-utils'; +import { compile } from '@ton/blueprint'; + +describe('[Ten thousands transfers]', () => { + let blockchain: Blockchain; + let deployer: SandboxContract; + let user1: null | SandboxContract = null; + let user2: null | SandboxContract = null; + const transfers: { + sender: SandboxContract; + receiver: SandboxContract; + value: bigint; + }[] = []; + + beforeAll(async () => { + blockchain = await Blockchain.create(); + user1 = await blockchain.treasury('user1'); + user2 = await blockchain.treasury('user2'); + for (let i = 0; i < 10000; i += 1) { + if (Math.random() > 0.5) { + transfers.push({ + sender: user1 as unknown as SandboxContract, + receiver: user2 as unknown as SandboxContract, + value: BigInt(Math.round(Math.random() * 100)), + }); + } else { + transfers.push({ + sender: user2 as unknown as SandboxContract, + receiver: user1 as unknown as SandboxContract, + value: BigInt(Math.round(Math.random() * 100)), + }); + } + } + }); + + it('[Ten thousands transfers] user1 and user2 transfer money 10.000 times', async () => { + let i = 1; + for (const transfer of transfers) { + const balanceReceiverBefore = await transfer.receiver.getBalance(); + await transfer.sender.send({ + value: transfer.value, + to: transfer.receiver.address, + }); + const expectedBalance = balanceReceiverBefore + transfer.value; + /* + balance is not always exact, there may be a +1 or + -1 rounding/else error + */ + const expectedBalances = [expectedBalance - 1n, expectedBalance, expectedBalance + 1n]; + const balanceReceiverAfter = await transfer.receiver.getBalance(); + expect(expectedBalances.includes(balanceReceiverAfter)).toBe(true); + i += 1; + if (i === 100 || i === 1000 || i % 1000 === 0) { + console.log( + `ok transfer no${i} ${transfer.sender.address} -> ${transfer.receiver.address} (${transfer.value})`, + ); + } + } + }); +});