@@ -9,13 +9,14 @@ import {UserService} from "../user/user.service";
99import { DataSource } from "typeorm" ;
1010import * as TokenFactoryABI from "../abi/TokenFactory.json" ;
1111import { AppService } from "../app.service" ;
12+ import { Cron , CronExpression } from "@nestjs/schedule" ;
1213
1314@Injectable ( )
1415export class IndexerService {
1516 private readonly logger = new Logger ( IndexerService . name ) ;
1617 private readonly web3 : Web3
18+ private readonly accountAddress : string
1719 private readonly tokenFactoryContract : Contract < ContractAbi >
18- private readonly tokenContract : Contract < ContractAbi >
1920 private readonly blocksIndexingRange = 1000
2021
2122 constructor (
@@ -25,28 +26,32 @@ export class IndexerService {
2526 private dataSource : DataSource ,
2627 ) {
2728 const rpcUrl = configService . get ( 'RPC_URL' )
28- const contractAddress = configService . get ( 'PUMP_FUN_CONTRACT_ADDRESS ' )
29- const initialBlockNumber = configService . get ( 'PUMP_FUN_INITIAL_BLOCK_NUMBER ' )
29+ const contractAddress = configService . get ( 'TOKEN_FACTORY_ADDRESS ' )
30+ const initialBlockNumber = configService . get ( 'INDEXER_INITIAL_BLOCK_NUMBER ' )
3031
3132 if ( ! contractAddress ) {
32- this . logger . error ( `[PUMP_FUN_CONTRACT_ADDRESS ] is missing but required, exit` )
33+ this . logger . error ( `[TOKEN_FACTORY_ADDRESS ] is missing but required, exit` )
3334 process . exit ( 1 )
3435 }
3536
3637 if ( ! initialBlockNumber ) {
37- this . logger . error ( `[PUMP_FUN_INITIAL_BLOCK_NUMBER ] is missing but required, exit` )
38+ this . logger . error ( `[INDEXER_INITIAL_BLOCK_NUMBER ] is missing but required, exit` )
3839 process . exit ( 1 )
3940 }
4041
4142 this . logger . log ( `Starting app service, RPC_URL=${
4243 rpcUrl
43- } , PUMP_FUN_CONTRACT_ADDRESS =${
44+ } , TOKEN_FACTORY_ADDRESS =${
4445 contractAddress
45- } , PUMP_FUN_INITIAL_BLOCK_NUMBER =${
46+ } , INDEXER_INITIAL_BLOCK_NUMBER =${
4647 initialBlockNumber
4748 } `)
4849
4950 this . web3 = new Web3 ( rpcUrl ) ;
51+ const account = this . web3 . eth . accounts . privateKeyToAccount ( configService . get ( 'SERVICE_PRIVATE_KEY' ) )
52+ this . accountAddress = account . address
53+ this . web3 . eth . accounts . wallet . add ( account ) ;
54+ this . logger . log ( `Service account address=${ account . address } ` )
5055 this . tokenFactoryContract = new this . web3 . eth . Contract ( TokenFactoryABI , contractAddress ) ;
5156 this . bootstrap ( ) . then (
5257 ( ) => {
@@ -62,9 +67,9 @@ export class IndexerService {
6267 where : { }
6368 } )
6469 if ( ! indexerState ) {
65- const blockNumber = + this . configService . get < number > ( 'PUMP_FUN_INITIAL_BLOCK_NUMBER ' )
70+ const blockNumber = + this . configService . get < number > ( 'INDEXER_INITIAL_BLOCK_NUMBER ' )
6671 if ( ! blockNumber ) {
67- this . logger . error ( '[PUMP_FUN_INITIAL_BLOCK_NUMBER ] is empty but required, exit' )
72+ this . logger . error ( '[INDEXER_INITIAL_BLOCK_NUMBER ] is empty but required, exit' )
6873 process . exit ( 1 )
6974 }
7075 await this . dataSource . manager . insert ( IndexerState , {
@@ -307,4 +312,41 @@ export class IndexerService {
307312
308313 this . eventsTrackingLoop ( )
309314 }
315+
316+ @Cron ( CronExpression . EVERY_DAY_AT_MIDNIGHT )
317+ async callSetWinner ( ) {
318+ let txnHash = ''
319+ let gasFees = 0n
320+
321+ for ( let i = 0 ; i < 3 ; i ++ ) {
322+ try {
323+ gasFees = await this . tokenFactoryContract . methods
324+ . setWinner ( )
325+ . estimateGas ( { from : this . accountAddress } ) ;
326+ const gasPrice = await this . web3 . eth . getGasPrice ( ) ;
327+ const tx = {
328+ from : this . accountAddress ,
329+ to : this . configService . get ( 'TOKEN_FACTORY_ADDRESS' ) ,
330+ gas : gasFees ,
331+ gasPrice,
332+ data : this . tokenFactoryContract . methods . setWinner ( ) . encodeABI ( ) ,
333+ } ;
334+ const signPromise = await this . web3 . eth . accounts . signTransaction ( tx , this . configService . get ( 'SERVICE_PRIVATE_KEY' ) ) ;
335+ const sendTxn =
336+ await this . web3 . eth . sendSignedTransaction (
337+ signPromise . rawTransaction ,
338+ ) ;
339+ txnHash = sendTxn . transactionHash . toString ( )
340+ break ;
341+ } catch ( e ) {
342+ this . logger . warn ( `Failed to send setWinner transaction, attempt: ${ ( i + 1 ) } / 3:` , e )
343+ }
344+ }
345+
346+ if ( txnHash ) {
347+ this . logger . log ( `[setWinner] successfully called, transaction hash=${ txnHash } , gasFees=${ gasFees } ` )
348+ } else {
349+ this . logger . error ( 'Failed to call setWinner!' )
350+ }
351+ }
310352}
0 commit comments