77import { Web3Connection } from '@taikai/dappkit' ;
88import { ContractEventListener , EventListenerHandler , Web3Event } from '@/types/events' ;
99import log from '@/services/log' ;
10- import { ContractCastType } from '@prisma/client' ;
10+ import { ContractCastType , ContractCastStatus } from '@prisma/client' ;
1111import { chainsSupported } from '@/constants/chains' ;
1212import { InstructionMap , Program , VirtualMachine } from '@/types/vm' ;
13- import { CastInfo , ContractCast , ContractCastStatusEnum , SecretManager , SecretMap } from '../types' ;
13+ import { CastInfo , ContractCast , SecretManager , SecretMap } from '../types' ;
1414import db from '@/services/prisma' ;
1515import { ContractListenerFactory } from './contract-listener-factory' ;
1616import EVMContractListener from './contract-listener' ;
@@ -57,7 +57,7 @@ export class EVMContractCast<VM extends VirtualMachine, T extends SecretManager>
5757 /** Web3 connection to interact with the blockchain */
5858 private _web3Con : Web3Connection ;
5959 /** Current status of the contract cast */
60- private _status : ContractCastStatusEnum = ContractCastStatusEnum . IDLE ;
60+ private _status : ContractCastStatus = ContractCastStatus . IDLE ;
6161 /** Manager for handling secrets needed by the contract cast */
6262 private _secretManager : T ;
6363
@@ -142,7 +142,7 @@ export class EVMContractCast<VM extends VirtualMachine, T extends SecretManager>
142142 *
143143 * @returns The current status enum value
144144 */
145- getStatus ( ) : ContractCastStatusEnum {
145+ getStatus ( ) : ContractCastStatus {
146146 return this . _status ;
147147 }
148148
@@ -209,6 +209,20 @@ export class EVMContractCast<VM extends VirtualMachine, T extends SecretManager>
209209 this . _vm . loadProgram ( program ) ;
210210 }
211211
212+ async setStatus ( status : ContractCastStatus ) {
213+ this . _status = status ;
214+ await db . contractCast . update ( {
215+ where : {
216+ id : this . _id ,
217+ } ,
218+ data : {
219+ status : status ,
220+ } ,
221+ } ) ;
222+ }
223+
224+
225+
212226 /**
213227 * Starts the Contract Cast by recovering past events and setting up event listening.
214228 *
@@ -221,14 +235,14 @@ export class EVMContractCast<VM extends VirtualMachine, T extends SecretManager>
221235 async start ( ) {
222236 log . d ( `Starting the Contract Cast=[${ this . getName ( ) } ]` ) ;
223237 try {
224- this . _status = ContractCastStatusEnum . RECOVERING ;
238+ await this . setStatus ( ContractCastStatus . RECOVERING ) ;
225239 log . d ( `Starting Recovering Cast=[${ this . getName ( ) } ]` ) ;
226240
227241 await this . _recoverEvents ( ) ;
228242 log . d ( `Stopping Recovering Cast=[${ this . getName ( ) } ]` ) ;
229- if ( ( this . getStatus ( ) as number ) !== ContractCastStatusEnum . TERMINATED ) {
243+ if ( ( this . getStatus ( ) ) !== ContractCastStatus . TERMINATED ) {
230244 await this . _startContractListening ( ) ;
231- this . _status = ContractCastStatusEnum . LISTENING ;
245+ await this . setStatus ( ContractCastStatus . LISTENING ) ;
232246 }
233247 } catch ( e : Error | any ) {
234248 log . e ( `Failed to start Contract Cast=[${ this . getName ( ) } ] ${ e . message } ${ e . stack } ` ) ;
@@ -253,7 +267,7 @@ export class EVMContractCast<VM extends VirtualMachine, T extends SecretManager>
253267 * If the Cast is in recovery state the recover process will save the
254268 * latest block position and transaction Index;
255269 * */
256- if ( this . getStatus ( ) !== ContractCastStatusEnum . RECOVERING ) {
270+ if ( this . getStatus ( ) !== ContractCastStatus . RECOVERING ) {
257271 const currentBlock = await this . _web3Con . eth . getBlockNumber ( ) ;
258272 const txCount = await this . _web3Con . eth . getBlockTransactionCount ( currentBlock ) ;
259273 if (
@@ -271,7 +285,7 @@ export class EVMContractCast<VM extends VirtualMachine, T extends SecretManager>
271285 log . e ( `Failed to stop Contract Cast=[${ this . getName ( ) } ]${ e . message } ${ e . stack } ` ) ;
272286 }
273287 } else {
274- this . _status = ContractCastStatusEnum . TERMINATED ;
288+ await this . setStatus ( ContractCastStatus . TERMINATED ) ;
275289 }
276290 }
277291
@@ -316,7 +330,7 @@ export class EVMContractCast<VM extends VirtualMachine, T extends SecretManager>
316330 * @returns True if the status is TERMINATED, false otherwise
317331 */
318332 shouldStop ( ) : boolean {
319- return this . getStatus ( ) === ContractCastStatusEnum . TERMINATED ;
333+ return this . getStatus ( ) === ContractCastStatus . TERMINATED ;
320334 }
321335
322336 /**
0 commit comments