Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fetch automate from w3f api #99

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { AutomateSDK } from "@gelatonetwork/automate-sdk";
```typescript
import { isAutomateSupported } from "@gelatonetwork/automate-sdk";

if (!isAutomateSupported(chainId)) {
if (!(await isAutomateSupported(chainId))) {
console.log(`Automate network not supported (${chainId})`);
return;
}
Expand All @@ -40,7 +40,7 @@ if (!isAutomateSupported(chainId)) {
3. Instantiate Automate using your signer:

```typescript
const automate = new AutomateSDK(chainId, signer);
const automate = await AutomateSDK.create(chainId, signer);
goums marked this conversation as resolved.
Show resolved Hide resolved
```

4. Create an automation task:
Expand Down
1 change: 1 addition & 0 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { GelatoAddressBook } from "../types";

export const W3F_API_ENDPOINT = "https://api.gelato.digital/w3f/networks";
goums marked this conversation as resolved.
Show resolved Hide resolved
export const AUTOMATE_USER_API = "https://api.gelato.digital/automate/users";
export const AUTOMATE_TASKS_API = "https://api.gelato.digital/automate/tasks";
export const AUTOMATE_USER_DEV_API =
Expand Down
2 changes: 1 addition & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const main = async () => {
const provider = new ethers.providers.JsonRpcProvider(providerUrl);

const wallet = new ethers.Wallet(pk as string, provider);
const sdk = new AutomateSDK(chainId, wallet);
const sdk = await AutomateSDK.create(chainId, wallet);

const { taskId, tx } = await sdk.createTask({
name: "AutomateSdkTest",
Expand Down
57 changes: 34 additions & 23 deletions src/lib/AutomateSDK.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,7 @@ import {
TaskTransaction,
} from "../types";
import { Module, ModuleData } from "../types/Module.interface";
import {
errorMessage,
isAutomateDevSupported,
isAutomateSupported,
} from "../utils";
import { errorMessage, getNetwork, isAutomateDevSupported } from "../utils";
import { AutomateModule } from "./AutomateModule";
import { Signature } from "./Signature";

Expand All @@ -54,50 +50,65 @@ export class AutomateSDK {
private readonly _taskApi: Axios;
private readonly _signature: Signature;

constructor(
private constructor(
chainId: number,
signer: Signer,
automate: Automate,
taskApi: Axios,
signature: Signature,
) {
this._automateModule = new AutomateModule();
this._chainId = chainId;
this._signer = signer;
this._automate = automate;
this._taskApi = taskApi;
this._signature = signature;
}

public static async create(
chainId: number,
signer: Signer,
signatureMessage?: string,
config?: Partial<Config>,
) {
): Promise<AutomateSDK> {
let automateAddress: string;
if (config && config.isDevelopment) {
if (!isAutomateDevSupported(chainId)) {
if (!(await isAutomateDevSupported(chainId))) {
throw new Error(`AutomateDev is not available on chainId:${chainId}`);
}

automateAddress = GELATO_ADDRESSES[chainId].automateDev!;
goums marked this conversation as resolved.
Show resolved Hide resolved
} else {
if (!isAutomateSupported(chainId)) {
const network = await getNetwork(chainId);
if (!network) {
throw new Error(`Automate is not available on chainId:${chainId}`);
}

automateAddress = GELATO_ADDRESSES[chainId].automate;
automateAddress = network.automate;
}

if (!Signer.isSigner(signer)) {
throw new Error(`Invalid Automate signer`);
}

this._automateModule = new AutomateModule();
this._signature = new Signature(
const automate = Automate__factory.connect(automateAddress, signer);

let taskApiUrl: string = AUTOMATE_TASKS_API;
if (config) {
taskApiUrl =
config.taskApi ??
(config.isDevelopment ? AUTOMATE_TASKS_DEV_API : AUTOMATE_TASKS_API);
}
const taskApi = axios.create({ baseURL: taskApiUrl });

const signature = new Signature(
chainId,
signer,
signatureMessage,
config?.signatureDomain,
);
this._chainId = chainId;
this._signer = signer;
this._automate = Automate__factory.connect(automateAddress, this._signer);

let taskApiUrl: string = AUTOMATE_TASKS_API;
if (config) {
taskApiUrl =
config.taskApi ?? config.isDevelopment
? AUTOMATE_TASKS_DEV_API
: AUTOMATE_TASKS_API;
}
this._taskApi = axios.create({ baseURL: taskApiUrl });
return new AutomateSDK(chainId, signer, automate, taskApi, signature);
}

public async getActiveTasks(creatorAddress?: string): Promise<Task[]> {
Expand Down
4 changes: 4 additions & 0 deletions src/types/W3fNetworks.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface W3fNetwork {
automate: string;
}
export type W3fNetworks = Record<number, W3fNetwork>;
33 changes: 28 additions & 5 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,36 @@
import axios, { AxiosError } from "axios";
import { GELATO_ADDRESSES } from "../constants";
import { GELATO_ADDRESSES, W3F_API_ENDPOINT } from "../constants";
import { W3fNetwork } from "../types/W3FNetworks.interface";

export function isAutomateSupported(chainId: number): boolean {
return Boolean(GELATO_ADDRESSES[chainId]);
export async function getNetwork(chainId: number): Promise<W3fNetwork | null> {
try {
const response = await axios.get<{ network: W3fNetwork }>(
goums marked this conversation as resolved.
Show resolved Hide resolved
`${W3F_API_ENDPOINT}/networks/${chainId}`,
);

return response.data.network;
} catch (error) {
if (axios.isAxiosError(error)) {
if (error.response?.status !== 404) {
console.error("Error getting network:", error.message);
}
} else {
console.error("Unexpected error getting network:", error);
}

return null;
}
}

export async function isAutomateSupported(chainId: number): Promise<boolean> {
return (await getNetwork(chainId)) !== null;
}

export function isAutomateDevSupported(chainId: number): boolean {
export async function isAutomateDevSupported(
chainId: number,
): Promise<boolean> {
return (
isAutomateSupported(chainId) &&
(await isAutomateSupported(chainId)) &&
Boolean(GELATO_ADDRESSES[chainId].automateDev)
goums marked this conversation as resolved.
Show resolved Hide resolved
);
}
Expand Down