|
1 | 1 | // SPDX-License-Identifier: MIT |
2 | 2 | // Copyright © 2025 TON Studio |
3 | 3 |
|
4 | | -import {Blockchain} from "@ton/sandbox" |
| 4 | +import {Blockchain, internal} from "@ton/sandbox" |
5 | 5 | import {createJetton, createTonVault} from "../utils/environment" |
6 | | -import {beginCell} from "@ton/core" |
7 | | -import {findTransactionRequired, flattenTransaction} from "@ton/test-utils" |
| 6 | +import {beginCell, toNano} from "@ton/core" |
| 7 | +import {findTransactionRequired, flattenTransaction, randomAddress} from "@ton/test-utils" |
8 | 8 | import {randomInt} from "node:crypto" |
9 | | -import {TonVault} from "../output/DEX_TonVault" |
| 9 | +import {loadPayoutFromTonVault, storePayoutFromPool, TonVault} from "../output/DEX_TonVault" |
| 10 | +import {AmmPool} from "../output/DEX_AmmPool" |
| 11 | +import {sortAddresses} from "../utils/deployUtils" |
10 | 12 |
|
11 | 13 | describe("TON Vault", () => { |
12 | 14 | test("Jettons are returned if sent to TON Vault", async () => { |
@@ -46,4 +48,65 @@ describe("TON Vault", () => { |
46 | 48 | const finalJettonBalance = await jetton.wallet.getJettonBalance() |
47 | 49 | expect(finalJettonBalance).toEqual(initialBalance) |
48 | 50 | }) |
| 51 | + test("TON Vault successfully transfers swap payload", async () => { |
| 52 | + const blockchain = await Blockchain.create() |
| 53 | + |
| 54 | + const tonVaultContract = await TonVault.fromInit() |
| 55 | + const openedTonVault = blockchain.openContract(tonVaultContract) |
| 56 | + const deployer = await blockchain.treasury("deployer") |
| 57 | + // Deploy contract |
| 58 | + const deployRes = await openedTonVault.send( |
| 59 | + deployer.getSender(), |
| 60 | + {value: toNano(0.1)}, |
| 61 | + null, |
| 62 | + ) |
| 63 | + expect(deployRes.transactions).toHaveTransaction({ |
| 64 | + on: tonVaultContract.address, |
| 65 | + deploy: true, |
| 66 | + }) |
| 67 | + |
| 68 | + const otherVaultAddress = randomAddress(0) |
| 69 | + const sortedAddresses = sortAddresses(tonVaultContract.address, otherVaultAddress, 0n, 0n) |
| 70 | + const randomAmmPool = await AmmPool.fromInit( |
| 71 | + sortedAddresses.lower, |
| 72 | + sortedAddresses.higher, |
| 73 | + 0n, |
| 74 | + 0n, |
| 75 | + 0n, |
| 76 | + null, |
| 77 | + ) |
| 78 | + const tonVaultObject = await blockchain.getContract(tonVaultContract.address) |
| 79 | + |
| 80 | + const randomReceiver = randomAddress(0) |
| 81 | + const payloadToForward = beginCell() |
| 82 | + .storeStringTail("Random quite big payload. User can encode anything here") |
| 83 | + .endCell() |
| 84 | + const res = await tonVaultObject.receiveMessage( |
| 85 | + internal({ |
| 86 | + from: randomAmmPool.address, |
| 87 | + to: tonVaultContract.address, |
| 88 | + value: toNano(0.1), |
| 89 | + body: beginCell() |
| 90 | + .store( |
| 91 | + storePayoutFromPool({ |
| 92 | + $$type: "PayoutFromPool", |
| 93 | + amount: 0n, |
| 94 | + otherVault: otherVaultAddress, |
| 95 | + receiver: randomReceiver, |
| 96 | + payloadToForward: payloadToForward, |
| 97 | + }), |
| 98 | + ) |
| 99 | + .endCell(), |
| 100 | + }), |
| 101 | + ) |
| 102 | + const flatTx = flattenTransaction(res) |
| 103 | + expect(flatTx.exitCode).toEqual(0) |
| 104 | + expect(flatTx.actionResultCode).toEqual(0) |
| 105 | + expect(res.outMessagesCount).toEqual(1) |
| 106 | + const payoutBody = res.outMessages.get(0)?.body |
| 107 | + expect(payoutBody).toBeDefined() |
| 108 | + const parsedPayout = loadPayoutFromTonVault(payoutBody!.beginParse()) |
| 109 | + expect(parsedPayout.$$type).toEqual("PayoutFromTonVault") |
| 110 | + expect(parsedPayout.body).toEqualCell(payloadToForward) |
| 111 | + }) |
49 | 112 | }) |
0 commit comments