Skip to content

Commit f9e403a

Browse files
authored
Merge pull request #8 from baker-fi/develop
Merge on Master
2 parents 40b7eff + 42da1a8 commit f9e403a

File tree

10 files changed

+57
-18
lines changed

10 files changed

+57
-18
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-- CreateEnum
2+
CREATE TYPE "ContractCastStatus" AS ENUM ('IDLE', 'RECOVERING', 'LISTENING', 'TERMINATED');
3+
4+
-- AlterTable
5+
ALTER TABLE "ContractCast" ADD COLUMN "status" "ContractCastStatus" NOT NULL DEFAULT 'IDLE';

prisma/schema.prisma

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,18 @@ enum ContractCastType {
2020
ERC1155
2121
}
2222

23+
enum ContractCastStatus {
24+
IDLE
25+
RECOVERING
26+
LISTENING
27+
TERMINATED
28+
}
29+
2330
model ContractCast {
2431
id String @id @default(cuid()) @db.VarChar(30)
2532
name String? @unique
2633
type ContractCastType
34+
status ContractCastStatus @default(IDLE)
2735
abi String
2836
address String
2937
chainId Int

src/graphql/resolvers/contract-cast/contract-cast.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export const contractCast: Resolver<ContractCast, ContractCastArgType> = async (
3434
name: true,
3535
address: true,
3636
blockNumber: true,
37+
status: true,
3738
transactionIndex: true,
3839
abi: true,
3940
chainId: true,

src/graphql/resolvers/contract-cast/contract-casts.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export const contractCasts: Resolver<ContractCast[], ContractCastsArgType> = asy
5454
name: true,
5555
address: true,
5656
blockNumber: true,
57+
status: true,
5758
transactionIndex: true,
5859
abi: true,
5960
chainId: true,

src/graphql/resolvers/contract-cast/create.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ const createContractCast: Resolver<ContractCast, CreateContractCastArgType> = as
8686
name: true,
8787
abi: true,
8888
program: true,
89+
status: true,
8990
blockNumber: true,
9091
transactionIndex: true,
9192
chainId: true,

src/graphql/resolvers/contract-cast/delete.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export const deleteContractCast: Resolver<ContractCast, DeleteContractCastArgTyp
3333
name: true,
3434
blockNumber: true,
3535
chainId: true,
36+
status: true,
3637
createdAt: true,
3738
program: true,
3839
type: true,

src/graphql/resolvers/contract-cast/update.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const updateContractCast: Resolver<ContractCast, UpdateContractCastArgType> = as
4545
program: true,
4646
blockNumber: true,
4747
transactionIndex: true,
48+
status: true,
4849
abi: true,
4950
chainId: true,
5051
createdAt: true,

src/graphql/types/contract-cast.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import { builder } from '../builder';
2-
import { ContractCastType } from '@prisma/client';
2+
import { ContractCastType, ContractCastStatus } from '@prisma/client';
33

44
export const ContractCastTypeEnum = builder.enumType('ContractCastType', {
55
values: Object.values(ContractCastType),
66
});
77

8+
export const ContractCastStatusEnum = builder.enumType('ContractCastStatusEnum', {
9+
values: Object.values(ContractCastStatus),
10+
});
11+
812
builder.prismaObject('ContractCast', {
913
fields: (t) => ({
1014
id: t.exposeID('id'),
@@ -13,11 +17,14 @@ builder.prismaObject('ContractCast', {
1317
}),
1418
address: t.exposeString('address'),
1519
chainId: t.exposeInt('chainId'),
16-
blockNumber: t.exposeInt('chainId'),
20+
blockNumber: t.exposeInt('blockNumber'),
1721
transactionIndex: t.exposeInt('transactionIndex'),
1822
type: t.expose('type', {
1923
type: ContractCastTypeEnum,
2024
}),
25+
status: t.expose('status', {
26+
type: ContractCastStatusEnum,
27+
}),
2128
program: t.exposeString('program'),
2229
createdAt: t.expose('createdAt', {
2330
type: 'Date',

src/lib/contract-cast.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
import { Web3Connection } from '@taikai/dappkit';
88
import { ContractEventListener, EventListenerHandler, Web3Event } from '@/types/events';
99
import log from '@/services/log';
10-
import { ContractCastType } from '@prisma/client';
10+
import { ContractCastType, ContractCastStatus } from '@prisma/client';
1111
import { chainsSupported } from '@/constants/chains';
1212
import { InstructionMap, Program, VirtualMachine } from '@/types/vm';
13-
import { CastInfo, ContractCast, ContractCastStatusEnum, SecretManager, SecretMap } from '../types';
13+
import { CastInfo, ContractCast, SecretManager, SecretMap } from '../types';
1414
import db from '@/services/prisma';
1515
import { ContractListenerFactory } from './contract-listener-factory';
1616
import 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
/**

src/types/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ChainCastManager } from '@/services/chaincast-manager';
2-
import { ContractCastType, PrismaClient } from '@prisma/client';
2+
import { ContractCastType, ContractCastStatus, PrismaClient } from '@prisma/client';
33
import LogService, { LogLevel } from '@taikai/scribal';
44
import { InstructionMap, Program, VirtualMachine } from './vm';
55
import { EventListenerHandler, Web3Event } from './events';
@@ -103,14 +103,14 @@ export type CastInfo = {
103103
};
104104

105105
export enum ContractCastStatusEnum {
106-
IDLE,
107-
RECOVERING,
108-
LISTENING,
109-
TERMINATED,
106+
IDLE = 'IDLE',
107+
RECOVERING = 'RECOVERING',
108+
LISTENING = 'LISTENING',
109+
TERMINATED = 'TERMINATED',
110110
}
111111

112112
export type ContractCast = {
113-
getStatus(): ContractCastStatusEnum;
113+
getStatus(): ContractCastStatus;
114114
loadProgram(program: Program): Promise<void>;
115115
loadSecrets(secrets: SecretMap): Promise<void>;
116116
getSecretsManager(): SecretManager;

0 commit comments

Comments
 (0)