Skip to content

Commit

Permalink
Fix event subscriptions in tests (#32)
Browse files Browse the repository at this point in the history
* do not use async function

* add another try catch

* remove console log

* use handleEventReturn
  • Loading branch information
WookashWackomy authored Aug 22, 2023
1 parent 6f61a66 commit 9465f56
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 40 deletions.
7 changes: 4 additions & 3 deletions tests/scenarios/scenariosRunner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import { apiProviderWrapper } from 'tests/setup/helpers';

const scenarioFolder = path.join(__dirname, 'stories');

const selectedFiles: string[] = [];
const selectedScenarios: string[] = [];
const selectedFiles: string[] = []; //['deposit'];
const selectedScenarios: string[] = []; //['User 0 Deposits 1000 DAI in an empty reserve'];
const skipScenarios = false;
const forceOnlyScenarios = false;

const forceItOnly = selectedFiles.length > 0 || selectedScenarios.length > 0;
const forceItOnly = forceOnlyScenarios || selectedFiles.length > 0 || selectedScenarios.length > 0;
fs.readdirSync(scenarioFolder).forEach((file) => {
if (selectedFiles.length > 0 && !selectedFiles.map((n) => n.replace('.json', '')).includes(file.replace('.json', ''))) return;

Expand Down
81 changes: 44 additions & 37 deletions tests/scenarios/utils/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { ReserveData, UserReserveData } from 'typechain/types-returns/lending_po
import BlockTimestampProvider from '../../../typechain/contracts/block_timestamp_provider';
import { TestEnv } from './make-suite';
import { Psp22Ownable } from '@abaxfinance/contract-helpers';
import { customHandleReturnType } from 'scripts/typechain/query';

export const LINE_SEPARATOR = '='.repeat(process.stdout.columns);

Expand All @@ -35,52 +36,56 @@ export const createEnumChecker = <T extends string, TEnumValue extends string>(e
export type AnyAbaxContractEventEnumLiteral<T extends AnyAbaxContractEvent> = `${T}`;
export type AnyAbaxContract = LendingPool | VToken | AToken | SToken | PSP22Emitable;

export const subscribeOnEvent = async <TEvent extends AnyAbaxContractEventEnumLiteral<AnyAbaxContractEvent>>(
const subscribeOnEvent = async <TEvent extends AnyAbaxContractEventEnumLiteral<AnyAbaxContractEvent>>(
contract: AnyAbaxContract,
eventName: TEvent,
callback: (event: TEvent, timestamp: number) => void,
) => {
const callbackWrapper = (args: any[], event: AbiEvent, timestamp: number) => {
const _event: Record<string, any> = {};

for (let i = 0; i < args.length; i++) {
_event[event.args[i].name] = args[i].toJSON();
}

callback(handleEventReturn(_event, getEventTypeDescription(eventName, contract.name)) as TEvent, timestamp);
};
return __subscribeOnEvent(contract, callbackWrapper, (name: string) => name === eventName);
};

const __subscribeOnEvent = async (
contract: AnyAbaxContract,
callback: (args: any[], event: AbiEvent, timestamp: number) => void,
filter: (eventName: string) => boolean = () => true,
eventName: string,
cb: (event: TEvent, timestamp: number) => void,
) => {
const api = await apiProviderWrapper.getAndWaitForReady();
// @ts-ignore
return api.query.system.events(async (events) => {
for (const record of events) {
const { event } = record;

if (event.method === 'ContractEmitted') {
const [address, data] = record.event.data;

if (address.toString() === contract.address.toString()) {
const { args, event: ev } = contract.abi.decodeEvent(data);

if (filter(ev.identifier.toString())) {
const timestamp = await api.query.timestamp.now();
// console.table({ eventName: ev.identifier.toString(), timestamp: timestamp.toString() });
callback(args, ev, parseInt(timestamp.toString()));
return api.query.system.events((events) => {
try {
for (const record of events) {
const { event } = record;

if (event.method === 'ContractEmitted') {
const [address, data] = record.event.data;

if (address.toString() === contract.address.toString()) {
const eventDecoded = contract.abi.decodeEvent(data);

if (eventDecoded.event.identifier.toString() === eventName) {
api.query.timestamp.now().then((timestamp) => {
try {
// console.table({ eventName: eventDecoded.event.identifier.toString(), timestamp: timestamp.toString() });

const _event: Record<string, any> = {};
for (let i = 0; i < eventDecoded.args.length; i++) {
_event[eventDecoded.event.args[i].name] = eventDecoded.args[i].toJSON();
}

const eventParsed = handleEventReturn(
_event,
// eslint-disable-next-line @typescript-eslint/no-var-requires
getEventTypeDescription(eventName, require(`typechain/event-data/${contract.name}.json`)),
) as TEvent;
const timestampParsed = parseInt(timestamp.toString());
cb(eventParsed, timestampParsed);
} catch (e) {
console.error('Fatal error during processing events from api.query.system.events', 'api.query.timestamp.now', e);
}
});
}
}
}
}
} catch (e) {
console.error('Fatal error during processing events from api.query.system.events', e);
}
});
};

export const subscribeOnEvents = (
export const subscribeOnEvents = async (
testEnv: TestEnv,
reserveName: string,
callback: (eventName: string, event: AnyAbaxContractEvent, emitingContract: AnyAbaxContract, timestamp: number) => void,
Expand All @@ -89,8 +94,10 @@ export const subscribeOnEvents = (
const reserve = reserves[reserveName];

const subscribePromises: Promise<any>[] = [];
const callbackDecorator = (eventName: string, emitingContract: AnyAbaxContract) => (event: AnyAbaxContractEvent, timestamp: number) =>
callback(eventName, event, emitingContract, timestamp);
const callbackDecorator = (eventName: string, emitingContract: AnyAbaxContract) => (event: AnyAbaxContractEvent, timestamp: number) => {
// console.log('callbackDecorator', { eventName, event, emitingContract, timestamp });
return callback(eventName, event, emitingContract, timestamp);
};

for (const event of Object.values(ContractsEvents.LendingPoolEvent)) {
subscribePromises.push(subscribeOnEvent(lendingPool, event, callbackDecorator(event, lendingPool)));
Expand Down

0 comments on commit 9465f56

Please sign in to comment.