@@ -2,18 +2,18 @@ import {Injectable, Logger} from '@nestjs/common';
22import { ConfigService } from "@nestjs/config" ;
33import { Contract , ContractAbi , EventLog , Web3 } from "web3" ;
44import * as TokenFactoryABI from './abi/TokenFactory.json'
5- import * as TokenABI from './abi/Token.json'
65import { Between , DataSource } from "typeorm" ;
7- import { IndexerState , Token , UserAccount } from "./entities" ;
6+ import { Comment , IndexerState , Token , UserAccount } from "./entities" ;
87import { AddCommentDto , GetCommentsDto } from "./dto/comment.dto" ;
9- import { Comment } from "./entities" ;
108import { GetTokensDto } from "./dto/token.dto" ;
119import * as process from "node:process" ;
12- import { Trade , TradeType } from "./entities/trade.entity " ;
10+ import { Trade } from "./entities" ;
1311import { Cron , CronExpression } from "@nestjs/schedule" ;
1412import * as moment from "moment" ;
1513import { GetTradesDto } from "./dto/trade.dto" ;
1614import { UserService } from "./user/user.service" ;
15+ import axios from "axios" ;
16+ import { TokenMetadata , TradeEventLog , TradeType } from "./types" ;
1717
1818@Injectable ( )
1919export class AppService {
@@ -90,26 +90,27 @@ export class AppService {
9090 }
9191 }
9292
93- private async processTradeEvents ( events : EventLog [ ] , tradeType : TradeType ) {
93+ private async processTradeEvents ( events : TradeEventLog [ ] ) {
9494 for ( const event of events ) {
95- const txnHash = event . transactionHash
96- const blockNumber = Number ( event . blockNumber )
97- const values = event . returnValues
98- const tokenAddress = values [ 'token' ] as string
95+ const { data, type } = event
96+ const txnHash = data . transactionHash . toLowerCase ( )
97+ const blockNumber = Number ( data . blockNumber )
98+ const values = data . returnValues
99+ const tokenAddress = ( values [ 'token' ] as string ) . toLowerCase ( )
99100 const amountIn = String ( values [ 'amount0In' ] as bigint )
100101 const amountOut = String ( values [ 'amount0Out' ] as bigint )
101102 const fee = String ( values [ 'fee' ] as bigint )
102103 const timestamp = Number ( values [ 'timestamp' ] as bigint )
103104
104105 const token = await this . getTokenByAddress ( tokenAddress )
105106 if ( ! token ) {
106- this . logger . error ( `swap event: failed to get token by address="${ tokenAddress } ", event tx hash="${ event . transactionHash } ", exit` )
107+ this . logger . error ( `Trade event: failed to get token by address="${ tokenAddress } ", event tx hash="${ data . transactionHash } ", exit` )
107108 process . exit ( 1 )
108109 }
109110
110111 try {
111112 await this . dataSource . manager . insert ( Trade , {
112- type : tradeType ,
113+ type,
113114 txnHash,
114115 blockNumber,
115116 token,
@@ -118,9 +119,9 @@ export class AppService {
118119 fee,
119120 timestamp
120121 } ) ;
121- this . logger . log ( `Trade [${ tradeType } ]: token=${ tokenAddress } , amountIn=${ amountIn } , amountOut=${ amountOut } , fee=${ fee } ` )
122+ this . logger . log ( `Trade [${ type } ]: token=${ tokenAddress } , amountIn=${ amountIn } , amountOut=${ amountOut } , fee=${ fee } , timestamp= ${ timestamp } , txnHash= ${ txnHash } ` )
122123 } catch ( e ) {
123- this . logger . error ( `Failed to process swap token=${ tokenAddress } txnHash=${ txnHash } ` , e )
124+ this . logger . error ( `Failed to process trade [ ${ type } ]: token=${ tokenAddress } txnHash=${ txnHash } ` , e )
124125 throw new Error ( e ) ;
125126 }
126127 }
@@ -163,7 +164,7 @@ export class AppService {
163164 fromBlock,
164165 toBlock,
165166 topics : [
166- this . web3 . utils . sha3 ( 'TokenCreated(address,uint256)' ) ,
167+ this . web3 . utils . sha3 ( 'TokenCreated(address,string,string,string,address, uint256)' ) ,
167168 ] ,
168169 } ) as EventLog [ ] ;
169170
@@ -183,38 +184,60 @@ export class AppService {
183184 ] ,
184185 } ) as EventLog [ ] ;
185186
187+ const tradeEvents : TradeEventLog [ ] = [ ...buyEvents ] . map ( data => {
188+ return {
189+ type : TradeType . buy ,
190+ data
191+ }
192+ } ) . concat ( [ ...sellEvents ] . map ( data => {
193+ return {
194+ type : TradeType . sell ,
195+ data
196+ }
197+ } ) ) . sort ( ( a , b ) => {
198+ return + ( a . data . returnValues . timestamp . toString ( ) ) - + ( b . data . returnValues . timestamp . toString ( ) )
199+ } )
200+
186201 for ( const tokenCreated of tokenCreatedEvents ) {
187- const txnHash = tokenCreated . transactionHash
202+ const txnHash = tokenCreated . transactionHash . toLowerCase ( )
188203 const values = tokenCreated . returnValues
189- const tokenAddress = values [ 'token' ] as string
204+ const tokenAddress = ( values [ 'token' ] as string ) . toLowerCase ( )
205+ const name = values [ 'name' ] as string
206+ const symbol = values [ 'symbol' ] as string
207+ const uri = values [ 'uri' ] as string
208+ const creatorAddress = ( values [ 'creator' ] as string ) . toLowerCase ( )
190209 const timestamp = Number ( values [ 'timestamp' ] as bigint )
191210
192- const tx = await this . web3 . eth . getTransaction ( txnHash )
193- const userAddress = tx . from
211+ let image = null
212+ let uriData = null
213+ try {
214+ const { data } = await axios . get < TokenMetadata > ( uri )
215+ uriData = data
216+ } catch ( e ) {
217+ this . logger . error ( `Failed to get token uri data, uri=${ uri } , tokenAddress=${ tokenAddress } ` , e )
218+ }
219+
194220 const user = await this . dataSource . manager . findOne ( UserAccount , {
195221 where : {
196- address : userAddress . toLowerCase ( )
222+ address : creatorAddress
197223 }
198224 } )
199225
200- const tokenContract = new this . web3 . eth . Contract ( TokenABI , tokenAddress ) ;
201- const name = await tokenContract . methods . name ( ) . call ( ) as string
202- const symbol = await tokenContract . methods . symbol ( ) . call ( ) as string
203-
204226 await this . dataSource . manager . insert ( Token , {
205227 txnHash,
206228 address : tokenAddress ,
207229 blockNumber : Number ( tokenCreated . blockNumber ) ,
208230 name,
209231 symbol,
210232 timestamp,
211- user
233+ user,
234+ uri,
235+ uriData,
212236 } ) ;
213- this . logger . log ( `New token: address=${ tokenAddress } , name=${ name } , symbol=${ symbol } , user =${ userAddress } , txnHash=${ txnHash } ` ) ;
237+ this . logger . log ( `Create token: address=${ tokenAddress } , name=${ name } , symbol=${ symbol } , uri =${ uri } , creator= ${ creatorAddress } , txnHash=${ txnHash } ` ) ;
214238 }
215239
216- await this . processTradeEvents ( buyEvents , TradeType . buy )
217- await this . processTradeEvents ( sellEvents , TradeType . sell )
240+ await this . processTradeEvents ( tradeEvents )
218241
219242 this . logger . log ( `[${ fromBlock } -${ toBlock } ] (${ ( ( toBlock - fromBlock + 1 ) ) } blocks), new tokens=${ tokenCreatedEvents . length } , trade=${ [ ...buyEvents , ...sellEvents ] . length } (buy=${ buyEvents . length } , sell=${ sellEvents . length } )` )
220243 } else {
0 commit comments