@@ -14,6 +14,7 @@ import { ReserveData, UserReserveData } from 'typechain/types-returns/lending_po
1414import BlockTimestampProvider from '../../../typechain/contracts/block_timestamp_provider' ;
1515import { TestEnv } from './make-suite' ;
1616import { Psp22Ownable } from '@abaxfinance/contract-helpers' ;
17+ import { customHandleReturnType } from 'scripts/typechain/query' ;
1718
1819export const LINE_SEPARATOR = '=' . repeat ( process . stdout . columns ) ;
1920
@@ -35,52 +36,56 @@ export const createEnumChecker = <T extends string, TEnumValue extends string>(e
3536export type AnyAbaxContractEventEnumLiteral < T extends AnyAbaxContractEvent > = `${T } `;
3637export type AnyAbaxContract = LendingPool | VToken | AToken | SToken | PSP22Emitable ;
3738
38- export const subscribeOnEvent = async < TEvent extends AnyAbaxContractEventEnumLiteral < AnyAbaxContractEvent > > (
39+ const subscribeOnEvent = async < TEvent extends AnyAbaxContractEventEnumLiteral < AnyAbaxContractEvent > > (
3940 contract : AnyAbaxContract ,
40- eventName : TEvent ,
41- callback : ( event : TEvent , timestamp : number ) => void ,
42- ) => {
43- const callbackWrapper = ( args : any [ ] , event : AbiEvent , timestamp : number ) => {
44- const _event : Record < string , any > = { } ;
45-
46- for ( let i = 0 ; i < args . length ; i ++ ) {
47- _event [ event . args [ i ] . name ] = args [ i ] . toJSON ( ) ;
48- }
49-
50- callback ( handleEventReturn ( _event , getEventTypeDescription ( eventName , contract . name ) ) as TEvent , timestamp ) ;
51- } ;
52- return __subscribeOnEvent ( contract , callbackWrapper , ( name : string ) => name === eventName ) ;
53- } ;
54-
55- const __subscribeOnEvent = async (
56- contract : AnyAbaxContract ,
57- callback : ( args : any [ ] , event : AbiEvent , timestamp : number ) => void ,
58- filter : ( eventName : string ) => boolean = ( ) => true ,
41+ eventName : string ,
42+ cb : ( event : TEvent , timestamp : number ) => void ,
5943) => {
6044 const api = await apiProviderWrapper . getAndWaitForReady ( ) ;
6145 // @ts -ignore
62- return api . query . system . events ( async ( events ) => {
63- for ( const record of events ) {
64- const { event } = record ;
65-
66- if ( event . method === 'ContractEmitted' ) {
67- const [ address , data ] = record . event . data ;
68-
69- if ( address . toString ( ) === contract . address . toString ( ) ) {
70- const { args, event : ev } = contract . abi . decodeEvent ( data ) ;
71-
72- if ( filter ( ev . identifier . toString ( ) ) ) {
73- const timestamp = await api . query . timestamp . now ( ) ;
74- // console.table({ eventName: ev.identifier.toString(), timestamp: timestamp.toString() });
75- callback ( args , ev , parseInt ( timestamp . toString ( ) ) ) ;
46+ return api . query . system . events ( ( events ) => {
47+ try {
48+ for ( const record of events ) {
49+ const { event } = record ;
50+
51+ if ( event . method === 'ContractEmitted' ) {
52+ const [ address , data ] = record . event . data ;
53+
54+ if ( address . toString ( ) === contract . address . toString ( ) ) {
55+ const eventDecoded = contract . abi . decodeEvent ( data ) ;
56+
57+ if ( eventDecoded . event . identifier . toString ( ) === eventName ) {
58+ api . query . timestamp . now ( ) . then ( ( timestamp ) => {
59+ try {
60+ // console.table({ eventName: eventDecoded.event.identifier.toString(), timestamp: timestamp.toString() });
61+
62+ const _event : Record < string , any > = { } ;
63+ for ( let i = 0 ; i < eventDecoded . args . length ; i ++ ) {
64+ _event [ eventDecoded . event . args [ i ] . name ] = eventDecoded . args [ i ] . toJSON ( ) ;
65+ }
66+
67+ const eventParsed = handleEventReturn (
68+ _event ,
69+ // eslint-disable-next-line @typescript-eslint/no-var-requires
70+ getEventTypeDescription ( eventName , require ( `typechain/event-data/${ contract . name } .json` ) ) ,
71+ ) as TEvent ;
72+ const timestampParsed = parseInt ( timestamp . toString ( ) ) ;
73+ cb ( eventParsed , timestampParsed ) ;
74+ } catch ( e ) {
75+ console . error ( 'Fatal error during processing events from api.query.system.events' , 'api.query.timestamp.now' , e ) ;
76+ }
77+ } ) ;
78+ }
7679 }
7780 }
7881 }
82+ } catch ( e ) {
83+ console . error ( 'Fatal error during processing events from api.query.system.events' , e ) ;
7984 }
8085 } ) ;
8186} ;
8287
83- export const subscribeOnEvents = (
88+ export const subscribeOnEvents = async (
8489 testEnv : TestEnv ,
8590 reserveName : string ,
8691 callback : ( eventName : string , event : AnyAbaxContractEvent , emitingContract : AnyAbaxContract , timestamp : number ) => void ,
@@ -89,8 +94,10 @@ export const subscribeOnEvents = (
8994 const reserve = reserves [ reserveName ] ;
9095
9196 const subscribePromises : Promise < any > [ ] = [ ] ;
92- const callbackDecorator = ( eventName : string , emitingContract : AnyAbaxContract ) => ( event : AnyAbaxContractEvent , timestamp : number ) =>
93- callback ( eventName , event , emitingContract , timestamp ) ;
97+ const callbackDecorator = ( eventName : string , emitingContract : AnyAbaxContract ) => ( event : AnyAbaxContractEvent , timestamp : number ) => {
98+ // console.log('callbackDecorator', { eventName, event, emitingContract, timestamp });
99+ return callback ( eventName , event , emitingContract , timestamp ) ;
100+ } ;
94101
95102 for ( const event of Object . values ( ContractsEvents . LendingPoolEvent ) ) {
96103 subscribePromises . push ( subscribeOnEvent ( lendingPool , event , callbackDecorator ( event , lendingPool ) ) ) ;
0 commit comments