Skip to content

Commit

Permalink
feat: block trigger (#72)
Browse files Browse the repository at this point in the history
* chore: add event trigger to README

* feat: add block trigger GN-4308

* chore: bump version
  • Loading branch information
mkykadir authored Oct 26, 2023
1 parent 8de3cfc commit cb192d9
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 13 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,18 @@ interface CreateTaskOptions {
type: TriggerType.CRON; // cron trigger
cron: string; // cron expression
};
| {
type: TriggerType.EVENT; // event trigger
filter: {
address: string; // address to listen events for
topics: Array<Array<string | null>>; // topics to listen for check Ethers.js doc (https://docs.ethers.org/v5/concepts/events/#events--filters)
};
blockConfirmations: number; // number of blocks to confirm event before triggering
};
|
{
type: TriggerType.BLOCK; // block trigger
};
}

const params: CreateTaskOptions = {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gelatonetwork/automate-sdk",
"version": "2.19.2-beta",
"version": "2.19.3-beta",
"description": "SDK to create Automate tasks",
"url": "https://github.com/gelatodigital/automate-sdk",
"main": "dist/index.js",
Expand Down
14 changes: 11 additions & 3 deletions src/lib/AutomateModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,9 @@ export class AutomateModule {
): Promise<string> => {
try {
if (!web3FunctionArgsHex && web3FunctionArgs) {
const { types, keys } =
await this.getAbiTypesAndKeysFromSchema(web3FunctionHash);
const { types, keys } = await this.getAbiTypesAndKeysFromSchema(
web3FunctionHash,
);
// ensure all userArgs are provided & encoded in same order as they are defined in the schema
const values: (
| string
Expand Down Expand Up @@ -263,7 +264,7 @@ export class AutomateModule {
["uint8", "bytes"],
[Number(TriggerType.CRON), triggerBytes],
);
} else {
} else if (trigger.type === TriggerType.EVENT) {
const triggerBytes = ethers.utils.defaultAbiCoder.encode(
["address", "bytes32[][]", "uint256"],
[
Expand All @@ -277,6 +278,11 @@ export class AutomateModule {
["uint8", "bytes"],
[Number(TriggerType.EVENT), triggerBytes],
);
} else {
triggerArgs = ethers.utils.defaultAbiCoder.encode(
["uint8"],
[Number(TriggerType.BLOCK)],
);
}

return triggerArgs;
Expand Down Expand Up @@ -338,6 +344,8 @@ export class AutomateModule {
blockConfirmations: blockConfirmations.toNumber(),
};
}
} else if (type === TriggerType.BLOCK) {
trigger = { type };
}
}
} catch {}
Expand Down
20 changes: 12 additions & 8 deletions src/lib/AutomateSDK.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,9 @@ export class AutomateSDK {
isDeployed: boolean;
}> {
const opsProxyFactory = await this._getOpsProxyFactory();
const [address, isDeployed] =
await opsProxyFactory.getProxyOf(creatorAddress);
const [address, isDeployed] = await opsProxyFactory.getProxyOf(
creatorAddress,
);

return { address, isDeployed };
}
Expand Down Expand Up @@ -239,8 +240,9 @@ export class AutomateSDK {
creatorAddress?: string,
): Promise<CreateTaskOptions> {
creatorAddress = creatorAddress ?? (await this._signer.getAddress());
const { address: execAddress } =
await this._getDedicatedMsgSender(creatorAddress);
const { address: execAddress } = await this._getDedicatedMsgSender(
creatorAddress,
);

const automateProxyInterface = AutomateProxy__factory.createInterface();

Expand Down Expand Up @@ -312,8 +314,9 @@ export class AutomateSDK {
tx: unsignedTx,
} = await this.prepareTask(_args, overrides);

const tx: ContractTransaction =
await this._signer.sendTransaction(unsignedTx);
const tx: ContractTransaction = await this._signer.sendTransaction(
unsignedTx,
);
await this._finalizeTaskCreation(taskId, args, authToken);
return { taskId, tx };
}
Expand Down Expand Up @@ -380,8 +383,9 @@ export class AutomateSDK {
): Promise<TaskTransaction> {
const { tx: unsignedTx } = await this.prepareCancelTask(taskId, overrides);

const tx: ContractTransaction =
await this._signer.sendTransaction(unsignedTx);
const tx: ContractTransaction = await this._signer.sendTransaction(
unsignedTx,
);
return { taskId, tx };
}

Expand Down
11 changes: 10 additions & 1 deletion src/types/Trigger.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export enum TriggerType {
TIME,
CRON,
EVENT,
BLOCK,
}

export type TimeTrigger = {
Expand All @@ -21,4 +22,12 @@ export type EventTrigger = {
blockConfirmations: number;
};

export type TriggerConfig = TimeTrigger | CronTrigger | EventTrigger;
export type BlockTrigger = {
type: TriggerType.BLOCK;
};

export type TriggerConfig =
| TimeTrigger
| CronTrigger
| EventTrigger
| BlockTrigger;

0 comments on commit cb192d9

Please sign in to comment.