From 92c1d3597c88064158741606f582b30ec6dc3e5d Mon Sep 17 00:00:00 2001 From: Brandon Chuah Date: Wed, 28 Aug 2024 14:46:05 +0800 Subject: [PATCH 1/7] chore: fetch automate from w3f api --- src/constants/index.ts | 1 + src/index.test.ts | 2 +- src/lib/AutomateSDK.ts | 57 ++++++++++++++++++------------ src/types/W3fNetworks.interface.ts | 4 +++ src/utils/index.ts | 33 ++++++++++++++--- 5 files changed, 68 insertions(+), 29 deletions(-) create mode 100644 src/types/W3fNetworks.interface.ts diff --git a/src/constants/index.ts b/src/constants/index.ts index 233f9ee..670ae96 100644 --- a/src/constants/index.ts +++ b/src/constants/index.ts @@ -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"; 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 = diff --git a/src/index.test.ts b/src/index.test.ts index ad04d55..ffe635a 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -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", diff --git a/src/lib/AutomateSDK.ts b/src/lib/AutomateSDK.ts index baa2d99..9fb132f 100644 --- a/src/lib/AutomateSDK.ts +++ b/src/lib/AutomateSDK.ts @@ -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"; @@ -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, - ) { + ): Promise { 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!; } 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 { diff --git a/src/types/W3fNetworks.interface.ts b/src/types/W3fNetworks.interface.ts new file mode 100644 index 0000000..faa07b8 --- /dev/null +++ b/src/types/W3fNetworks.interface.ts @@ -0,0 +1,4 @@ +export interface W3fNetwork { + automate: string; +} +export type W3fNetworks = Record; diff --git a/src/utils/index.ts b/src/utils/index.ts index 9ad706e..df2746a 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -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 { + try { + const response = await axios.get<{ network: W3fNetwork }>( + `${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 { + return (await getNetwork(chainId)) !== null; } -export function isAutomateDevSupported(chainId: number): boolean { +export async function isAutomateDevSupported( + chainId: number, +): Promise { return ( - isAutomateSupported(chainId) && + (await isAutomateSupported(chainId)) && Boolean(GELATO_ADDRESSES[chainId].automateDev) ); } From ee06c3d34484f055958d1ce9539eff82b6bbe0bd Mon Sep 17 00:00:00 2001 From: Brandon Chuah Date: Wed, 28 Aug 2024 14:46:12 +0800 Subject: [PATCH 2/7] chore: update readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index fc3b193..720ba08 100644 --- a/README.md +++ b/README.md @@ -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; } @@ -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); ``` 4. Create an automation task: From 0fd5bd0bd47363a5b2f270b7cd10205fd2724ac4 Mon Sep 17 00:00:00 2001 From: Brandon Chuah Date: Thu, 29 Aug 2024 11:57:50 +0800 Subject: [PATCH 3/7] fix: w3f api endpoint --- src/utils/index.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/utils/index.ts b/src/utils/index.ts index df2746a..c3e6ca2 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -4,11 +4,11 @@ import { W3fNetwork } from "../types/W3FNetworks.interface"; export async function getNetwork(chainId: number): Promise { try { - const response = await axios.get<{ network: W3fNetwork }>( - `${W3F_API_ENDPOINT}/networks/${chainId}`, + const response = await axios.get<{ networks: W3fNetwork }>( + `${W3F_API_ENDPOINT}/${chainId}`, ); - return response.data.network; + return response.data.networks; } catch (error) { if (axios.isAxiosError(error)) { if (error.response?.status !== 404) { From 37e33620eaf945b2205d8b83f497e12dab5ff2d9 Mon Sep 17 00:00:00 2001 From: Brandon Chuah Date: Thu, 29 Aug 2024 15:39:17 +0800 Subject: [PATCH 4/7] chore: update w3f api data --- src/utils/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/index.ts b/src/utils/index.ts index c3e6ca2..ffc0c29 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -4,11 +4,11 @@ import { W3fNetwork } from "../types/W3FNetworks.interface"; export async function getNetwork(chainId: number): Promise { try { - const response = await axios.get<{ networks: W3fNetwork }>( + const response = await axios.get<{ network: W3fNetwork }>( `${W3F_API_ENDPOINT}/${chainId}`, ); - return response.data.networks; + return response.data.network; } catch (error) { if (axios.isAxiosError(error)) { if (error.response?.status !== 404) { From e33d431ee937a58d632fdc0188a6cea7eb3d68c6 Mon Sep 17 00:00:00 2001 From: Brandon Chuah Date: Fri, 30 Aug 2024 14:16:11 +0800 Subject: [PATCH 5/7] chore: use w3f api dev --- src/constants/index.ts | 2 ++ src/lib/AutomateSDK.ts | 4 +-- src/utils/index.ts | 61 ++++++++++++++++++++++++++---------------- 3 files changed, 42 insertions(+), 25 deletions(-) diff --git a/src/constants/index.ts b/src/constants/index.ts index 670ae96..ed47175 100644 --- a/src/constants/index.ts +++ b/src/constants/index.ts @@ -2,6 +2,8 @@ import { GelatoAddressBook } from "../types"; export const W3F_API_ENDPOINT = "https://api.gelato.digital/w3f/networks"; +export const W3F_API_ENDPOINT_DEV = + "https://api.dev.gelato.digital/w3f/networks"; 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 = diff --git a/src/lib/AutomateSDK.ts b/src/lib/AutomateSDK.ts index 9fb132f..1d2961c 100644 --- a/src/lib/AutomateSDK.ts +++ b/src/lib/AutomateSDK.ts @@ -37,7 +37,7 @@ import { TaskTransaction, } from "../types"; import { Module, ModuleData } from "../types/Module.interface"; -import { errorMessage, getNetwork, isAutomateDevSupported } from "../utils"; +import { errorMessage, isAutomateDevSupported, w3fApi } from "../utils"; import { AutomateModule } from "./AutomateModule"; import { Signature } from "./Signature"; @@ -79,7 +79,7 @@ export class AutomateSDK { automateAddress = GELATO_ADDRESSES[chainId].automateDev!; } else { - const network = await getNetwork(chainId); + const network = await w3fApi.getNetwork(chainId, config?.isDevelopment); if (!network) { throw new Error(`Automate is not available on chainId:${chainId}`); } diff --git a/src/utils/index.ts b/src/utils/index.ts index ffc0c29..eeae990 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,38 +1,53 @@ import axios, { AxiosError } from "axios"; -import { GELATO_ADDRESSES, W3F_API_ENDPOINT } from "../constants"; -import { W3fNetwork } from "../types/W3FNetworks.interface"; - -export async function getNetwork(chainId: number): Promise { - try { - const response = await axios.get<{ network: W3fNetwork }>( - `${W3F_API_ENDPOINT}/${chainId}`, - ); - - return response.data.network; - } catch (error) { - if (axios.isAxiosError(error)) { - if (error.response?.status !== 404) { - console.error("Error getting network:", error.message); +import { W3F_API_ENDPOINT, W3F_API_ENDPOINT_DEV } from "../constants"; +import { W3fNetwork } from "../types/W3fNetworks.interface"; + +class W3FApi { + private _network: Map = new Map(); + + public async getNetwork( + chainId: number, + isDevelopment?: boolean, + ): Promise { + try { + const cacheNetwork = this._network.get(chainId); + if (cacheNetwork !== undefined) { + return JSON.parse(cacheNetwork) as W3fNetwork; } - } else { - console.error("Unexpected error getting network:", error); - } - return null; + const endpoint = isDevelopment ? W3F_API_ENDPOINT_DEV : W3F_API_ENDPOINT; + + const response = await axios.get<{ network: W3fNetwork }>( + `${endpoint}/${chainId}`, + ); + + this._network.set(chainId, JSON.stringify(response.data.network)); + + 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 const w3fApi = new W3FApi(); + export async function isAutomateSupported(chainId: number): Promise { - return (await getNetwork(chainId)) !== null; + return (await w3fApi.getNetwork(chainId)) !== null; } export async function isAutomateDevSupported( chainId: number, ): Promise { - return ( - (await isAutomateSupported(chainId)) && - Boolean(GELATO_ADDRESSES[chainId].automateDev) - ); + return (await w3fApi.getNetwork(chainId, true)) !== null; } export function errorMessage(err: Error | AxiosError) { From f001b64b95a1f0459572cc08be9299a2a700eef2 Mon Sep 17 00:00:00 2001 From: Brandon Chuah Date: Fri, 30 Aug 2024 14:16:41 +0800 Subject: [PATCH 6/7] chore: major version 4.0.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 120c450..ad3604e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@gelatonetwork/automate-sdk", - "version": "3.0.19", + "version": "4.0.0", "description": "SDK to create Automate tasks", "url": "https://github.com/gelatodigital/automate-sdk", "main": "dist/index.js", From 423632c325f92ede4b215a21985c03b1afbc9a2d Mon Sep 17 00:00:00 2001 From: Brandon Chuah Date: Thu, 5 Sep 2024 12:38:16 +0800 Subject: [PATCH 7/7] chore: remove static address --- src/constants/index.ts | 206 ----------------------------------------- src/lib/AutomateSDK.ts | 20 ++-- src/utils/index.ts | 7 +- 3 files changed, 11 insertions(+), 222 deletions(-) diff --git a/src/constants/index.ts b/src/constants/index.ts index ed47175..caf58d7 100644 --- a/src/constants/index.ts +++ b/src/constants/index.ts @@ -1,5 +1,4 @@ /* eslint-disable @typescript-eslint/naming-convention */ -import { GelatoAddressBook } from "../types"; export const W3F_API_ENDPOINT = "https://api.gelato.digital/w3f/networks"; export const W3F_API_ENDPOINT_DEV = @@ -13,208 +12,3 @@ export const AUTOMATE_TASKS_DEV_API = export const ETH = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"; export const ZERO_ADD = "0x0000000000000000000000000000000000000000"; - -export const CHAIN_ID = { - MAINNET: 1, - GOERLI: 5, - OPTIMISM: 10, - CRONOS: 25, - OPTIMISTIC_GOERLI: 420, - BSC: 56, - GNOSIS: 100, - POLYGON: 137, - FANTOM: 250, - MOONBEAM: 1284, - MOONRIVER: 1285, - ARBITRUM: 42161, - AVALANCHE: 43114, - MUMBAI: 80001, - ZKSYNC: 324, - LINEA: 59144, - BASE: 8453, - POLYGONZK: 1101, - ZKATANA: 1261120, - SEPOLIA: 11155111, - UNREAL: 18233, - OSEPOLIA: 11155420, - ARBSEPOLIA: 421614, - GELOPCELESTIATESTNET: 123420111, - BASESEPOLIA: 84532, - METIS: 1088, - LISKSEPOLIA: 4202, - BLASTSEPOLIA: 168587773, - MODE: 34443, - ASTARZKEVM: 3776, - ASTARZKYOTO: 6038361, - BLAST: 81457, - GELATOORBITTESTNET: 88153591557, - REYACRONOS: 89346162, - REYA: 1729, - PLAYBLOCK: 1829, - BLACKBERRY: 94_204_209, - AMOY: 80002, - REAL: 111188, - CONNEXTSEPOLIA: 6398, - ANOMALY_ANDROMEDA: 241120, - ALEPHZEROTESTNET: 2039, - LISK: 1135, - COREDAO: 1116, - ROOTSTOCK: 30, - NOVASTROTESTNET: 560098, - OPENCAMPUSCODEX: 656476, - CAMPNETWORKTESTNET: 325000, - RIDOTTOTETROMINO: 4444, -}; - -export const GELATO_ADDRESSES: GelatoAddressBook = { - [CHAIN_ID.MAINNET]: { - automate: "0x2A6C106ae13B558BB9E2Ec64Bd2f1f7BEFF3A5E0", - }, - [CHAIN_ID.GOERLI]: { - automate: "0x2A6C106ae13B558BB9E2Ec64Bd2f1f7BEFF3A5E0", - }, - [CHAIN_ID.OPTIMISTIC_GOERLI]: { - automate: "0x2A6C106ae13B558BB9E2Ec64Bd2f1f7BEFF3A5E0", - }, - [CHAIN_ID.POLYGON]: { - automate: "0x2A6C106ae13B558BB9E2Ec64Bd2f1f7BEFF3A5E0", - }, - [CHAIN_ID.MUMBAI]: { - automate: "0x2A6C106ae13B558BB9E2Ec64Bd2f1f7BEFF3A5E0", - automateDev: "0xC97aBa1B6cf4d8aCa19dFba68e99befaDA9aeFE3", - }, - [CHAIN_ID.FANTOM]: { - automate: "0x2A6C106ae13B558BB9E2Ec64Bd2f1f7BEFF3A5E0", - }, - [CHAIN_ID.ARBITRUM]: { - automate: "0x2A6C106ae13B558BB9E2Ec64Bd2f1f7BEFF3A5E0", - }, - [CHAIN_ID.AVALANCHE]: { - automate: "0x2A6C106ae13B558BB9E2Ec64Bd2f1f7BEFF3A5E0", - }, - [CHAIN_ID.BASE]: { - automate: "0x2A6C106ae13B558BB9E2Ec64Bd2f1f7BEFF3A5E0", - }, - [CHAIN_ID.BSC]: { - automate: "0x2A6C106ae13B558BB9E2Ec64Bd2f1f7BEFF3A5E0", - }, - [CHAIN_ID.GNOSIS]: { - automate: "0x2A6C106ae13B558BB9E2Ec64Bd2f1f7BEFF3A5E0", - }, - [CHAIN_ID.OPTIMISM]: { - automate: "0x2A6C106ae13B558BB9E2Ec64Bd2f1f7BEFF3A5E0", - }, - [CHAIN_ID.POLYGONZK]: { - automate: "0x2A6C106ae13B558BB9E2Ec64Bd2f1f7BEFF3A5E0", - }, - [CHAIN_ID.MOONBEAM]: { - automate: "", - }, - [CHAIN_ID.MOONRIVER]: { - automate: "", - }, - [CHAIN_ID.CRONOS]: { - automate: "", - }, - [CHAIN_ID.ZKSYNC]: { - automate: "0xF27e0dfD58B423b1e1B90a554001d0561917602F", - }, - [CHAIN_ID.LINEA]: { - automate: "0x2A6C106ae13B558BB9E2Ec64Bd2f1f7BEFF3A5E0", - }, - [CHAIN_ID.ZKATANA]: { - automate: "0x2A6C106ae13B558BB9E2Ec64Bd2f1f7BEFF3A5E0", - }, - [CHAIN_ID.SEPOLIA]: { - automate: "0x2A6C106ae13B558BB9E2Ec64Bd2f1f7BEFF3A5E0", - }, - [CHAIN_ID.UNREAL]: { - automate: "0x2A6C106ae13B558BB9E2Ec64Bd2f1f7BEFF3A5E0", - }, - [CHAIN_ID.OSEPOLIA]: { - automate: "0x2A6C106ae13B558BB9E2Ec64Bd2f1f7BEFF3A5E0", - }, - [CHAIN_ID.ARBSEPOLIA]: { - automate: "0x2A6C106ae13B558BB9E2Ec64Bd2f1f7BEFF3A5E0", - }, - [CHAIN_ID.GELOPCELESTIATESTNET]: { - automate: "0x2A6C106ae13B558BB9E2Ec64Bd2f1f7BEFF3A5E0", - }, - [CHAIN_ID.BASESEPOLIA]: { - automate: "0x2A6C106ae13B558BB9E2Ec64Bd2f1f7BEFF3A5E0", - }, - [CHAIN_ID.METIS]: { - automate: "0x2A6C106ae13B558BB9E2Ec64Bd2f1f7BEFF3A5E0", - }, - [CHAIN_ID.LISKSEPOLIA]: { - automate: "0x2A6C106ae13B558BB9E2Ec64Bd2f1f7BEFF3A5E0", - }, - [CHAIN_ID.BLASTSEPOLIA]: { - automate: "0x2A6C106ae13B558BB9E2Ec64Bd2f1f7BEFF3A5E0", - }, - [CHAIN_ID.MODE]: { - automate: "0x2A6C106ae13B558BB9E2Ec64Bd2f1f7BEFF3A5E0", - }, - [CHAIN_ID.ASTARZKEVM]: { - automate: "0x2A6C106ae13B558BB9E2Ec64Bd2f1f7BEFF3A5E0", - }, - [CHAIN_ID.ASTARZKYOTO]: { - automate: "0x2A6C106ae13B558BB9E2Ec64Bd2f1f7BEFF3A5E0", - }, - [CHAIN_ID.BLAST]: { - automate: "0x2A6C106ae13B558BB9E2Ec64Bd2f1f7BEFF3A5E0", - }, - [CHAIN_ID.GELATOORBITTESTNET]: { - automate: "0x2A6C106ae13B558BB9E2Ec64Bd2f1f7BEFF3A5E0", - automateDev: "0xd84ED255d7ee2B032f66AeCfB41b51044d409aDC", - }, - [CHAIN_ID.REYACRONOS]: { - automate: "0x2A6C106ae13B558BB9E2Ec64Bd2f1f7BEFF3A5E0", - }, - [CHAIN_ID.REYA]: { - automate: "0x2A6C106ae13B558BB9E2Ec64Bd2f1f7BEFF3A5E0", - }, - [CHAIN_ID.PLAYBLOCK]: { - automate: "0x2A6C106ae13B558BB9E2Ec64Bd2f1f7BEFF3A5E0", - }, - [CHAIN_ID.BLACKBERRY]: { - automate: "0x2A6C106ae13B558BB9E2Ec64Bd2f1f7BEFF3A5E0", - }, - [CHAIN_ID.AMOY]: { - automate: "0x2A6C106ae13B558BB9E2Ec64Bd2f1f7BEFF3A5E0", - automateDev: "0xd84ED255d7ee2B032f66AeCfB41b51044d409aDC", - }, - [CHAIN_ID.REAL]: { - automate: "0x2A6C106ae13B558BB9E2Ec64Bd2f1f7BEFF3A5E0", - }, - [CHAIN_ID.CONNEXTSEPOLIA]: { - automate: "0x2A6C106ae13B558BB9E2Ec64Bd2f1f7BEFF3A5E0", - }, - [CHAIN_ID.ANOMALY_ANDROMEDA]: { - automate: "0x2A6C106ae13B558BB9E2Ec64Bd2f1f7BEFF3A5E0", - }, - [CHAIN_ID.ALEPHZEROTESTNET]: { - automate: "0x2A6C106ae13B558BB9E2Ec64Bd2f1f7BEFF3A5E0", - }, - [CHAIN_ID.LISK]: { - automate: "0x2A6C106ae13B558BB9E2Ec64Bd2f1f7BEFF3A5E0", - }, - [CHAIN_ID.COREDAO]: { - automate: "0x2A6C106ae13B558BB9E2Ec64Bd2f1f7BEFF3A5E0", - }, - [CHAIN_ID.ROOTSTOCK]: { - automate: "0x2A6C106ae13B558BB9E2Ec64Bd2f1f7BEFF3A5E0", - }, - [CHAIN_ID.NOVASTROTESTNET]: { - automate: "0x2A6C106ae13B558BB9E2Ec64Bd2f1f7BEFF3A5E0", - }, - [CHAIN_ID.OPENCAMPUSCODEX]: { - automate: "0x2A6C106ae13B558BB9E2Ec64Bd2f1f7BEFF3A5E0", - }, - [CHAIN_ID.CAMPNETWORKTESTNET]: { - automate: "0x2A6C106ae13B558BB9E2Ec64Bd2f1f7BEFF3A5E0", - }, - [CHAIN_ID.RIDOTTOTETROMINO]: { - automate: "0x2A6C106ae13B558BB9E2Ec64Bd2f1f7BEFF3A5E0", - }, -}; diff --git a/src/lib/AutomateSDK.ts b/src/lib/AutomateSDK.ts index 1d2961c..c11a427 100644 --- a/src/lib/AutomateSDK.ts +++ b/src/lib/AutomateSDK.ts @@ -14,7 +14,6 @@ import { AUTOMATE_TASKS_API, AUTOMATE_TASKS_DEV_API, ETH, - GELATO_ADDRESSES, ZERO_ADD, } from "../constants"; import { @@ -37,7 +36,7 @@ import { TaskTransaction, } from "../types"; import { Module, ModuleData } from "../types/Module.interface"; -import { errorMessage, isAutomateDevSupported, w3fApi } from "../utils"; +import { errorMessage, w3fApi } from "../utils"; import { AutomateModule } from "./AutomateModule"; import { Signature } from "./Signature"; @@ -71,22 +70,17 @@ export class AutomateSDK { signatureMessage?: string, config?: Partial, ): Promise { - let automateAddress: string; - if (config && config.isDevelopment) { - if (!(await isAutomateDevSupported(chainId))) { + const network = await w3fApi.getNetwork(chainId, config?.isDevelopment); + if (!network) { + if (config?.isDevelopment) { throw new Error(`AutomateDev is not available on chainId:${chainId}`); - } - - automateAddress = GELATO_ADDRESSES[chainId].automateDev!; - } else { - const network = await w3fApi.getNetwork(chainId, config?.isDevelopment); - if (!network) { + } else { throw new Error(`Automate is not available on chainId:${chainId}`); } - - automateAddress = network.automate; } + const automateAddress = network.automate; + if (!Signer.isSigner(signer)) { throw new Error(`Invalid Automate signer`); } diff --git a/src/utils/index.ts b/src/utils/index.ts index eeae990..4369073 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -3,14 +3,15 @@ import { W3F_API_ENDPOINT, W3F_API_ENDPOINT_DEV } from "../constants"; import { W3fNetwork } from "../types/W3fNetworks.interface"; class W3FApi { - private _network: Map = new Map(); + private _network: Map = new Map(); public async getNetwork( chainId: number, isDevelopment?: boolean, ): Promise { try { - const cacheNetwork = this._network.get(chainId); + const key = `${chainId}_${isDevelopment ? "dev" : "prod"}`; + const cacheNetwork = this._network.get(key); if (cacheNetwork !== undefined) { return JSON.parse(cacheNetwork) as W3fNetwork; } @@ -21,7 +22,7 @@ class W3FApi { `${endpoint}/${chainId}`, ); - this._network.set(chainId, JSON.stringify(response.data.network)); + this._network.set(key, JSON.stringify(response.data.network)); return response.data.network; } catch (error) {