You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
6.0 KiB

import { toNano, Address } from "ton";
import { ContractSystem } from "ton-emulator";
2 years ago
import { TONB } from '../output/jetton_TONB';
import { default_content } from '../utils/config';
import { TONBWallet } from '../output/jetton_TONBWallet';
2 years ago
import { beginCell } from 'ton-core';
import { PseudoStaking } from '../output/jetton_PseudoStaking';
import { logEvents } from "../utils/helpers";
describe('jetton', () => {
2 years ago
it('should deploy and deposit the wallet with the correct sum of money + withdraw should be working', async () => {
// Create jetton
let system = await ContractSystem.create();
let owner = system.treasure('owner');
let contract = system.open(await TONB.fromInit(owner.address, default_content, null));
let tracker = system.track(contract.address);
// Mint
await contract.send(owner, { value: toNano('100.2') }, { $$type: 'Deposit', amount: toNano('100') });
2 years ago
let log = await system.run();
let events = tracker.events();
expect(events).toMatchSnapshot();
2 years ago
let addr = Address.parse((events[4] as any).messages[0].to);
let wallet_contract = system.contract(addr);
let wdata = ((await wallet_contract.get(97026, [])) as any).stack.items;
expect(wdata[0].value).toEqual(100000000000n);
2 years ago
let linker_contract = system.contract(Address.parse((events[3] as any).messages[0].to));
let linker_owner = ((await linker_contract.get('owner', [])) as any).stack.items[0];
expect(linker_owner.cell.toBoc()).toEqual(beginCell().storeAddress(addr).endCell().toBoc());
// Check owner
expect((await contract.getOwner()).toString()).toEqual(owner.address.toString());
// Withdraw
await contract.send(owner, { value: toNano('0.15') }, { $$type: 'Withdraw', amount: toNano('20') });
log = await system.run();
events = tracker.events();
expect((events[5] as any).messages[0].value).toBeGreaterThan(toNano('19'));
});
it('should work correctly with the staking', async () => {
// Create jetton
let system = await ContractSystem.create();
let owner = system.treasure('owner');
let pseudostaking_contract = system.open(await PseudoStaking.fromInit());
let contract = system.open(await TONB.fromInit(owner.address, default_content, pseudostaking_contract.address));
2 years ago
await pseudostaking_contract.send(owner, { value: toNano('10000') }, "Deposit");
await contract.send(owner, { value: toNano('0.1') }, { $$type: "SetStakingPool", staking_pool: pseudostaking_contract.address });
await system.run();
let tracker = system.track(contract.address);
// Mint - The contract should stake the money
await contract.send(owner, { value: toNano('100.2') }, { $$type: 'Deposit', amount: toNano('100') });
await system.run();
let events = tracker.events();
// console.log(events)
expect(events).toMatchSnapshot();
expect((events[4] as any).messages[0].value).toBeGreaterThan(99000000000n);
// Try to withdraw
await contract.send(owner, { value: toNano('0.5') }, { $$type: 'Withdraw', amount: toNano('50') });
let log = await system.run();
events = tracker.events();
// console.log(log)
// console.log((events[7] as any).messages[0])
let addressBook_rev = {'wallet': (events[6] as any).messages[0].to,
'staking': (events[7] as any).messages[0].to, 'tonb': (events[0] as any).message.to, 'owner': (events[0] as any).message.from};
// reverse keys and values
let addressBook = Object.fromEntries(Object.entries(addressBook_rev).map(([key, value]) => [value, key]));
// console.log("wallet:", (events[6] as any).messages[0].to)
// console.log("staking:", (events[7] as any).messages[0].to)
logEvents(events, addressBook);
let found_transaction = false;
for (let i = 0; i < events.length; i++) {
if (events[i].type == 'sent') {
if ((events[i] as any).messages[0].to == addressBook_rev['owner']) {
if ((events[i] as any).messages[0].value>49000000000n) {
found_transaction = true;
}
}
}
}
expect(found_transaction).toBeTruthy();
await contract.send(owner, { value: toNano('0.5') }, { $$type: 'Unstake', amount: toNano('0') });
log = await system.run();
events = tracker.events();
logEvents(events, addressBook);
expect((events[events.length-1] as any).messages[0].value).toBeGreaterThan(toNano('4.9'));
expect((events[events.length-1] as any).messages[0].to).toEqual(addressBook_rev['owner']);
});
2 years ago
it('should correctly work with transfers', async () => {
let system = await ContractSystem.create();
let owner = system.treasure('owner');
let contract = system.open(await TONB.fromInit(owner.address, default_content, null));
let dude = system.treasure('dude')
// Mint
await contract.send(owner, { value: toNano('100.2') }, { $$type: 'Deposit', amount: toNano('100') });
await system.run();
// let addr = Address.parse((events[4] as any).messages[0].to);
// let wallet_contract = system.contract(addr);
let wallet_contract = system.open(await TONBWallet.fromInit(contract.address, owner.address))
let wallet_contract_2 = system.open(await TONBWallet.fromInit(contract.address, dude.address))
let tracker = system.track(wallet_contract.address);
let tracker_2 = system.track(wallet_contract_2.address);
await wallet_contract.send(owner, { value: toNano("0.2") }, {
amount: toNano(10), destination: dude.address, queryId: 0n,
forwardTonAmount: 0n, $$type: 'TokenTransfer',
forwardPayload: beginCell().endCell(),
responseDestination: null, customPayload: null
});
await system.run();
expect((await wallet_contract_2.getGetWalletData()).balance).toEqual(10000000000n);
});
});