Skip to content

Commit

Permalink
Merge branch 'main' into update-get-execution
Browse files Browse the repository at this point in the history
  • Loading branch information
v9n committed Jan 6, 2025
2 parents 682ba52 + c0b75d6 commit b1a1459
Show file tree
Hide file tree
Showing 21 changed files with 211 additions and 164 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ jspm_packages/

# Build outputs
build/
dist/
out/
*.tsbuildinfo

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@avaprotocol/sdk-js",
"version": "1.2.1-dev.6",
"version": "1.2.1-dev.7",
"description": "A JavaScript/TypeScript SDK designed to simplify integration with Ava Protocol's AVS",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
26 changes: 6 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,11 @@ import { getKeyRequestMessage } from "./auth";
import { AggregatorClient } from "../grpc_codegen/avs_grpc_pb";
import * as avs_pb from "../grpc_codegen/avs_pb";
import { BoolValue } from "google-protobuf/google/protobuf/wrappers_pb";
import Workflow, {
WorkflowProps,
WorkflowStatus,
WorkflowStatuses,
} from "./models/workflow";
import Workflow, { WorkflowProps } from "./models/workflow";
import Edge, { EdgeProps } from "./models/edge";
import Execution from "./models/execution";
import NodeFactory from "./models/node/factory";
import TriggerFactory, {
TriggerType,
TriggerTypes,
} from "./models/trigger/factory";
import TriggerFactory from "./models/trigger/factory";

import {
AUTH_KEY_HEADER,
Expand Down Expand Up @@ -322,7 +315,7 @@ export default class Client extends BaseClient {
async getExecutions(
workflows: string[],
options?: GetExecutionsRequest
): Promise<{ cursor: string; result: Execution[], hasMore: boolean; }> {
): Promise<{ cursor: string; result: Execution[]; hasMore: boolean }> {
const request = new avs_pb.ListExecutionsReq();
request.setTaskIdsList(workflows);

Expand Down Expand Up @@ -355,7 +348,7 @@ export default class Client extends BaseClient {
async getExecution(
taskId: string,
executionId: string,
options?: RequestOptions
options?: RequestOptions
): Promise<Execution> {
const request = new avs_pb.ExecutionReq();
request.setTaskId(taskId);
Expand Down Expand Up @@ -493,16 +486,9 @@ export * from "./types";
export * from "./models/node/factory";
export * from "./models/trigger/factory";

export {
Workflow,
WorkflowStatuses,
Edge,
Execution,
NodeFactory,
TriggerFactory,
};
export { Workflow, Edge, Execution, NodeFactory, TriggerFactory };

export type { WorkflowProps, WorkflowStatus, EdgeProps };
export type { WorkflowProps, EdgeProps };

// Add this line at the end of the file
export { getKeyRequestMessage };
7 changes: 4 additions & 3 deletions src/models/trigger/block.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import * as avs_pb from "../../../grpc_codegen/avs_pb";
import Trigger, { TriggerProps, TriggerTypes } from "./interface";
import Trigger, { TriggerProps } from "./interface";
import { TriggerType } from "../../types";

// Required props for constructor: id, name, type and data: { interval }
export type BlockTriggerDataType = avs_pb.BlockCondition.AsObject;
export type BlockTriggerProps = TriggerProps & { data: BlockTriggerDataType };

class BlockTrigger extends Trigger {
constructor(props: BlockTriggerProps) {
super({ ...props, type: TriggerTypes.BLOCK, data: props.data });
super({ ...props, type: TriggerType.Block, data: props.data });
}

toRequest(): avs_pb.TaskTrigger {
Expand All @@ -31,7 +32,7 @@ class BlockTrigger extends Trigger {

return new BlockTrigger({
...obj,
type: TriggerTypes.BLOCK,
type: TriggerType.Block,
data: raw.getBlock()!.toObject() as BlockTriggerDataType,
});
}
Expand Down
10 changes: 5 additions & 5 deletions src/models/trigger/cron.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import * as avs_pb from "../../../grpc_codegen/avs_pb";
import Trigger, { TriggerProps, TriggerTypes } from "./interface";

import Trigger, { TriggerProps } from "./interface";
import { TriggerType } from "../../types";
// Required props for constructor: id, name, type and data: { scheduleList }
export type CronTriggerDataType = avs_pb.CronCondition.AsObject;
export type CronTriggerProps = TriggerProps & { data: CronTriggerDataType };

class CronTrigger extends Trigger {
constructor(props: CronTriggerProps) {
super({ ...props, type: TriggerTypes.CRON, data: props.data });
super({ ...props, type: TriggerType.Cron, data: props.data });

console.log("CronTrigger.constructor.props:", {
...props,
type: TriggerTypes.CRON,
type: TriggerType.Cron,
data: props.data,
});
}
Expand Down Expand Up @@ -40,7 +40,7 @@ class CronTrigger extends Trigger {
console.log("CronTrigger.fromResponse.obj:", obj);
return new CronTrigger({
...obj,
type: TriggerTypes.CRON,
type: TriggerType.Cron,
data: raw.getCron()!.toObject() as CronTriggerDataType,
});
}
Expand Down
10 changes: 5 additions & 5 deletions src/models/trigger/event.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import * as avs_pb from "../../../grpc_codegen/avs_pb";
import Trigger, { TriggerProps, TriggerTypes } from "./interface";

import Trigger, { TriggerProps } from "./interface";
import { TriggerType } from "../../types";
// Required props for constructor: id, name, type and data: { expression }
export type EventTriggerDataType = avs_pb.EventCondition.AsObject;
export type EventTriggerProps = TriggerProps & { data: EventTriggerDataType };

class EventTrigger extends Trigger {
constructor(props: EventTriggerProps) {
super({ ...props, type: TriggerTypes.EVENT, data: props.data });
super({ ...props, type: TriggerType.Event, data: props.data });

console.log("EventTrigger.constructor.props:", {
...props,
type: TriggerTypes.EVENT,
type: TriggerType.Event,
data: props.data,
});
}
Expand Down Expand Up @@ -40,7 +40,7 @@ class EventTrigger extends Trigger {
console.log("EventTrigger.fromResponse.obj:", obj);
return new EventTrigger({
...obj,
type: TriggerTypes.EVENT,
type: TriggerType.Event,
data: raw.getEvent()!.toObject() as EventTriggerDataType,
});
}
Expand Down
15 changes: 7 additions & 8 deletions src/models/trigger/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { BlockTriggerProps } from "./block";
import CronTrigger, { CronTriggerProps } from "./cron";
import EventTrigger, { EventTriggerProps } from "./event";
import FixedTimeTrigger, { FixedTimeTriggerProps } from "./fixedTime";
import Trigger, { TriggerType, TriggerTypes } from "./interface";
import Trigger from "./interface";
import { TriggerProps } from "./interface";

import { TriggerType } from "../../types";
class TriggerFactory {
/**
* Static factory method to create Trigger instances
Expand All @@ -15,13 +15,13 @@ class TriggerFactory {
*/
static create(props: TriggerProps): Trigger {
switch (props.type) {
case TriggerTypes.BLOCK:
case TriggerType.Block:
return new BlockTrigger(props as BlockTriggerProps);
case TriggerTypes.CRON:
case TriggerType.Cron:
return new CronTrigger(props as CronTriggerProps);
case TriggerTypes.EVENT:
case TriggerType.Event:
return new EventTrigger(props as EventTriggerProps);
case TriggerTypes.FIXED_TIME:
case TriggerType.FixedTime:
return new FixedTimeTrigger(props as FixedTimeTriggerProps);
}

Expand Down Expand Up @@ -54,7 +54,7 @@ export default TriggerFactory;

export {
Trigger,
TriggerTypes,
TriggerType,
BlockTrigger,
CronTrigger,
EventTrigger,
Expand All @@ -63,7 +63,6 @@ export {

export type {
TriggerProps,
TriggerType,
BlockTriggerProps,
CronTriggerProps,
EventTriggerProps,
Expand Down
9 changes: 5 additions & 4 deletions src/models/trigger/fixedTime.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as avs_pb from "../../../grpc_codegen/avs_pb";
import Trigger, { TriggerProps, TriggerTypes } from "./interface";
import Trigger, { TriggerProps } from "./interface";
import { TriggerType } from "../../types";

// Required props for constructor: id, name,type and data: { epoch }
export type FixedTimeTriggerDataType = avs_pb.FixedTimeCondition.AsObject;
Expand All @@ -9,11 +10,11 @@ export type FixedTimeTriggerProps = TriggerProps & {

class FixedTimeTrigger extends Trigger {
constructor(props: FixedTimeTriggerProps) {
super({ ...props, type: TriggerTypes.FIXED_TIME, data: props.data });
super({ ...props, type: TriggerType.FixedTime, data: props.data });

console.log("FixedTimeTrigger.constructor.props:", {
...props,
type: TriggerTypes.FIXED_TIME,
type: TriggerType.FixedTime,
data: props.data,
});
}
Expand Down Expand Up @@ -42,7 +43,7 @@ class FixedTimeTrigger extends Trigger {
console.log("FixedTimeTrigger.fromResponse.obj:", obj);
return new FixedTimeTrigger({
...obj,
type: TriggerTypes.FIXED_TIME,
type: TriggerType.FixedTime,
data: raw.getFixedTime()!.toObject() as FixedTimeTriggerDataType,
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/models/trigger/interface.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as avs_pb from "../../../grpc_codegen/avs_pb";
export const TriggerTypes = avs_pb.TaskTrigger.TriggerTypeCase;
export type TriggerType = avs_pb.TaskTrigger.TriggerTypeCase;
import { TriggerType } from "../../types";

export type TriggerData =
| avs_pb.FixedTimeCondition.AsObject
| avs_pb.CronCondition.AsObject
Expand Down
70 changes: 50 additions & 20 deletions src/models/trigger/metadata.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,38 @@
import * as avs_pb from "../../../grpc_codegen/avs_pb";
import type { TriggerType } from "./interface";
import { TriggerTypes } from "./interface";
import { TriggerType } from "../../types";
import _ from "lodash";

// Union type for all possible trigger data
export type TriggerMetadataProps =
| { type: avs_pb.TaskTrigger.TriggerTypeCase.FIXED_TIME; epoch: number }
| { type: avs_pb.TaskTrigger.TriggerTypeCase.CRON; epoch: number }
| { type: avs_pb.TaskTrigger.TriggerTypeCase.BLOCK; blockNumber: number }
| { type: TriggerType.FixedTime; epoch: number }
| { type: TriggerType.Cron; epoch: number }
| { type: TriggerType.Block; blockNumber: number }
| {
type: avs_pb.TaskTrigger.TriggerTypeCase.EVENT;
type: TriggerType.Event;
blockNumber: number;
logIndex: number;
txHash: string;
};
}
| { type: TriggerType.Manual }
| { type: TriggerType.Unset };

// Convert the number values of gRPC TriggerType to string values of TriggerType
const convertTriggerType = (
grpcType: avs_pb.TriggerMetadata.TriggerType
): TriggerType => {
const conversionMap: {
[key in avs_pb.TriggerMetadata.TriggerType]: TriggerType;
} = {
[avs_pb.TriggerMetadata.TriggerType.FIXEDTIME]: TriggerType.FixedTime,
[avs_pb.TriggerMetadata.TriggerType.CRON]: TriggerType.Cron,
[avs_pb.TriggerMetadata.TriggerType.BLOCK]: TriggerType.Block,
[avs_pb.TriggerMetadata.TriggerType.EVENT]: TriggerType.Event,
[avs_pb.TriggerMetadata.TriggerType.MANUAL]: TriggerType.Manual,
[avs_pb.TriggerMetadata.TriggerType.UNSET]: TriggerType.Unset,
};

return conversionMap[grpcType];
};

class TriggerMetadata {
type: TriggerType;
Expand All @@ -24,16 +43,17 @@ class TriggerMetadata {

constructor(props: TriggerMetadataProps) {
this.type = props.type;

// Configure metadata based on trigger type
switch (props.type) {
case TriggerTypes.FIXED_TIME:
case TriggerTypes.CRON:
case TriggerType.FixedTime:
case TriggerType.Cron:
this.epoch = props.epoch;
break;
case TriggerTypes.BLOCK:
case TriggerType.Block:
this.blockNumber = props.blockNumber;
break;
case TriggerTypes.EVENT:
case TriggerType.Event:
this.blockNumber = props.blockNumber;
this.logIndex = props.logIndex;
this.txHash = props.txHash;
Expand All @@ -49,13 +69,9 @@ class TriggerMetadata {
if (!data) {
return undefined;
}
let type = data.getType() as unknown as avs_pb.TaskTrigger.TriggerTypeCase;
if (type != avs_pb.TaskTrigger.TriggerTypeCase.FIXED_TIME && type != avs_pb.TaskTrigger.TriggerTypeCase.CRON && type != avs_pb.TaskTrigger.TriggerTypeCase.BLOCK && type !=avs_pb.TaskTrigger.TriggerTypeCase.EVENT) {
throw new Error("Unable to determine trigger type from response");
}

return new TriggerMetadata({
type,
type: convertTriggerType(data.getType()),
blockNumber: data.getBlockNumber(),
epoch: data.getEpoch(),
logIndex: data.getLogIndex(),
Expand All @@ -67,18 +83,26 @@ class TriggerMetadata {
const request = new avs_pb.TriggerMetadata();

switch (this.type) {
case TriggerTypes.FIXED_TIME:
case TriggerTypes.CRON:
case TriggerType.FixedTime:
request.setType(avs_pb.TriggerMetadata.TriggerType.FIXEDTIME);
if (this.epoch) {
request.setEpoch(this.epoch);
}
break;
case TriggerTypes.BLOCK:
case TriggerType.Cron:
request.setType(avs_pb.TriggerMetadata.TriggerType.CRON);
if (this.epoch) {
request.setEpoch(this.epoch);
}
break;
case TriggerType.Block:
request.setType(avs_pb.TriggerMetadata.TriggerType.BLOCK);
if (this.blockNumber) {
request.setBlockNumber(this.blockNumber);
}
break;
case TriggerTypes.EVENT:
case TriggerType.Event:
request.setType(avs_pb.TriggerMetadata.TriggerType.EVENT);
if (this.blockNumber) {
request.setBlockNumber(this.blockNumber);
}
Expand All @@ -89,6 +113,12 @@ class TriggerMetadata {
request.setTxHash(this.txHash);
}
break;
case TriggerType.Manual:
request.setType(avs_pb.TriggerMetadata.TriggerType.MANUAL);
break;
case TriggerType.Unset:
request.setType(avs_pb.TriggerMetadata.TriggerType.UNSET);
break;
default:
throw new Error("Unsupported trigger type");
}
Expand Down
Loading

0 comments on commit b1a1459

Please sign in to comment.