-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
65 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { Blockchain, SandboxContract, TreasuryContract, printTransactionFees } from '@ton/sandbox'; | ||
import { Cell, toNano } from '@ton/core'; | ||
Check failure on line 2 in tests/TenThousandsTransfers.spec.ts GitHub Actions / lintandcheck
|
||
import '@ton/test-utils'; | ||
import { compile } from '@ton/blueprint'; | ||
|
||
describe('[Ten thousands transfers]', () => { | ||
let blockchain: Blockchain; | ||
let deployer: SandboxContract<TreasuryContract>; | ||
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})`, | ||
); | ||
} | ||
} | ||
}); | ||
}); |