From c9859e3aeb318d3e4ef9dbd6f475de3a054c527b Mon Sep 17 00:00:00 2001 From: ennucore Date: Sun, 12 Feb 2023 17:06:21 +0100 Subject: [PATCH] Test for profit distribution in the foundation --- sources/tests/foundation.spec.ts | 37 ++++++++++++++++++++++++++------ sources/tests/jetton.spec.ts | 6 ++---- sources/utils/helpers.ts | 8 ++++--- 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/sources/tests/foundation.spec.ts b/sources/tests/foundation.spec.ts index 7b64306..0aa2a2e 100644 --- a/sources/tests/foundation.spec.ts +++ b/sources/tests/foundation.spec.ts @@ -1,23 +1,48 @@ import { toNano } from "ton"; import { ContractSystem } from "ton-emulator"; -import {TONB} from '../output/jetton_TONB'; -import {Foundation} from '../output/jetton_Foundation'; +import { TONB } from '../output/jetton_TONB'; +import { Foundation } from '../output/jetton_Foundation'; import { default_content } from '../utils/config'; import { TONBWallet } from '../output/jetton_TONBWallet'; import { beginCell } from 'ton-core'; import { PseudoStaking } from '../output/jetton_PseudoStaking'; import { createDistribution, createAddressList } from "../utils/interactions"; +import { logEvents } from '../utils/helpers'; describe('jetton', () => { - it('should be able to create a vote', async () => { + it('should be able to distribute profits among admins', async () => { // Create jetton let system = await ContractSystem.create(); let owner = system.treasure('owner'); - let tonb = system.open(await TONB.fromInit(owner.address, default_content, null)); + let someadmin = system.treasure('someadmin'); + let pseudostaking_contract = system.open(await PseudoStaking.fromInit()); + let tonb = system.open(await TONB.fromInit(owner.address, default_content, pseudostaking_contract.address)); let foundation = system.open(await Foundation.fromInit( - createDistribution([owner.address], [100n]), - createAddressList([]), tonb.address)); + createDistribution([owner.address, someadmin.address], [40n, 60n]), + createAddressList([owner.address]), tonb.address)); + let tonb_tracker = system.track(tonb.address); tonb.send(owner, { value: toNano('1') }, { $$type: 'SetOwner', owner: foundation.address }); + await pseudostaking_contract.send(owner, { value: toNano('10000') }, "Deposit"); + await tonb.send(owner, { value: toNano('0.1') }, { $$type: "SetStakingPool", staking_pool: pseudostaking_contract.address }); + await system.run(); + await tonb.send(owner, { value: toNano('100.2') }, { $$type: 'Deposit', amount: toNano('100') }); + await system.run(); + let tonb_events = tonb_tracker.events(); + let addressBook = { 'tonb': (tonb_events[1] as any).message.to, 'owner': (tonb_events[1] as any).message.from, 'staking': (tonb_events[9] as any).messages[0].to, 'someadmin': 'unknown' as any, 'foundation': 'unknown' as any }; + logEvents(tonb_events, addressBook); let tracker = system.track(foundation.address); + await foundation.send(owner, { value: toNano('0.1') }, { $$type: 'RequestUnstake', founderIndex: 0n }); + await system.run(); + await foundation.send(someadmin, { value: toNano('0.1') }, { $$type: 'CollectProfit', adminIndex: 1n }); + await foundation.send(owner, { value: toNano('0.1') }, { $$type: 'CollectProfit', adminIndex: 0n }); + await system.run(); + let foundation_events = tracker.events(); + addressBook['foundation'] = (foundation_events[1] as any).message.to; + logEvents(foundation_events, addressBook); + expect((foundation_events[11] as any).messages[0].value).toBeGreaterThan(toNano('3.9')); + expect((foundation_events[11] as any).messages[0].value).toBeLessThan(toNano('4.1')); + expect((foundation_events[11] as any).messages[0].to).toEqual(addressBook['owner']); + expect((foundation_events[8] as any).messages[0].value).toBeGreaterThan(toNano('5.9')); + expect((foundation_events[8] as any).messages[0].value).toBeLessThan(toNano('6.1')); }); }); \ No newline at end of file diff --git a/sources/tests/jetton.spec.ts b/sources/tests/jetton.spec.ts index 7899416..e3051df 100644 --- a/sources/tests/jetton.spec.ts +++ b/sources/tests/jetton.spec.ts @@ -70,11 +70,9 @@ describe('jetton', () => { // 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); + logEvents(events, addressBook_rev); let found_transaction = false; for (let i = 0; i < events.length; i++) { if (events[i].type == 'sent') { @@ -89,7 +87,7 @@ describe('jetton', () => { await contract.send(owner, { value: toNano('0.5') }, { $$type: 'Unstake', amount: toNano('0') }); log = await system.run(); events = tracker.events(); - logEvents(events, addressBook); + logEvents(events, addressBook_rev); 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']); }); diff --git a/sources/utils/helpers.ts b/sources/utils/helpers.ts index c99e7d7..b5187e1 100644 --- a/sources/utils/helpers.ts +++ b/sources/utils/helpers.ts @@ -136,7 +136,7 @@ export function fmtMessage(message: TrackedMessage, addressBook: any) { // slice first 4 symbols, ..., and last 4 symbols to = to?.slice(0, 4) + '...' + to?.slice(-4); } - return `${from} --> ${to} ${value.toFixed(3)} ${body}`; + return `${from} --> ${to} ${value.toFixed(2)} ${body}`; } export function fmtEvent(ev: any, addressBook: any) { @@ -160,9 +160,11 @@ export function fmtEvent(ev: any, addressBook: any) { } export function logEvents(events: any[], addressBook: any) { + let addressBookRev = Object.fromEntries(Object.entries(addressBook).map(([key, value]) => [value, key])); let res = ''; - for (let ev of events) { - res += fmtEvent(ev, addressBook) + '\n'; + // write events and their indices + for (let i = 0; i < events.length; i++) { + res += `#${i} ${fmtEvent(events[i], addressBookRev)}\n`; } console.log(res) }