Skip to content

Commit

Permalink
New test suite, 10.000 transfers
Browse files Browse the repository at this point in the history
  • Loading branch information
fabcotech committed Jul 9, 2024
1 parent 97c1a88 commit 55f3e56
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/Arithmetic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('[Arithmetic]', () => {
let blockchain: Blockchain;
let deployer: SandboxContract<TreasuryContract>;
let arithmeticContract: SandboxContract<Arithmetic>;
let user1: any = null;
let user1: null | SandboxContract<TreasuryContract> = null;
let provider: null | ContractProvider = null;

beforeAll(async () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/Fibonacci.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('[Fibonacci]', () => {
let blockchain: Blockchain;
let deployer: SandboxContract<TreasuryContract>;
let contract: SandboxContract<Fibonacci>;
let user1: any = null;
let user1: null | SandboxContract<TreasuryContract> = null;

beforeAll(async () => {
code = await compile('Fibonacci');
Expand Down
62 changes: 62 additions & 0 deletions tests/TenThousandsTransfers.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { Blockchain, SandboxContract, TreasuryContract, printTransactionFees } from '@ton/sandbox';

Check failure on line 1 in tests/TenThousandsTransfers.spec.ts

View workflow job for this annotation

GitHub Actions / lintandcheck

'printTransactionFees' is defined but never used
import { Cell, toNano } from '@ton/core';

Check failure on line 2 in tests/TenThousandsTransfers.spec.ts

View workflow job for this annotation

GitHub Actions / lintandcheck

'Cell' is defined but never used

Check failure on line 2 in tests/TenThousandsTransfers.spec.ts

View workflow job for this annotation

GitHub Actions / lintandcheck

'toNano' is defined but never used
import '@ton/test-utils';
import { compile } from '@ton/blueprint';

Check failure on line 4 in tests/TenThousandsTransfers.spec.ts

View workflow job for this annotation

GitHub Actions / lintandcheck

'compile' is defined but never used

describe('[Ten thousands transfers]', () => {
let blockchain: Blockchain;
let deployer: SandboxContract<TreasuryContract>;

Check failure on line 8 in tests/TenThousandsTransfers.spec.ts

View workflow job for this annotation

GitHub Actions / lintandcheck

'deployer' is defined but never used
let user1: null | SandboxContract<TreasuryContract> = null;
let user2: null | SandboxContract<TreasuryContract> = null;
const transfers: {
sender: SandboxContract<TreasuryContract>;
receiver: SandboxContract<TreasuryContract>;
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<TreasuryContract>,
receiver: user2 as unknown as SandboxContract<TreasuryContract>,
value: BigInt(Math.round(Math.random() * 100)),
});
} else {
transfers.push({
sender: user2 as unknown as SandboxContract<TreasuryContract>,
receiver: user1 as unknown as SandboxContract<TreasuryContract>,
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})`,
);
}
}
});
});

0 comments on commit 55f3e56

Please sign in to comment.