From 73bc7c71326940221f90cb709aefb8fc788972ff Mon Sep 17 00:00:00 2001 From: vignesha22 <82584664+vignesha22@users.noreply.github.com> Date: Tue, 19 Sep 2023 12:44:40 +0530 Subject: [PATCH] Dynamic Supported Networks (#10) * removed from codebase and fetched from json * added ability to set supported chains via env --- backend/.gitignore | 1 + backend/config.json.default | 121 +++++++++++++++++++++++++++++ backend/src/constants/Etherspot.ts | 93 ---------------------- backend/src/plugins/config.ts | 4 +- backend/src/routes/index.ts | 29 +++++-- 5 files changed, 146 insertions(+), 102 deletions(-) create mode 100644 backend/config.json.default delete mode 100644 backend/src/constants/Etherspot.ts diff --git a/backend/.gitignore b/backend/.gitignore index a87b91c..3c3c87c 100644 --- a/backend/.gitignore +++ b/backend/.gitignore @@ -17,6 +17,7 @@ npm-debug.log* coverage .env +config.json package-lock.json pnpm-lock.yaml \ No newline at end of file diff --git a/backend/config.json.default b/backend/config.json.default new file mode 100644 index 0000000..94f8fb1 --- /dev/null +++ b/backend/config.json.default @@ -0,0 +1,121 @@ +[ + { + "chainId": 1, + "bundler": "https://ethereum-bundler.etherspot.io/", + "contracts": { + "etherspotPaymasterAddress": "0x7F690e93CecFca5A31E6e1dF50A33F6d3059048c" + } + }, + { + "chainId": 5, + "bundler": "https://goerli-bundler.etherspot.io", + "contracts": { + "etherspotPaymasterAddress": "0xcaDBADcFeD5530A49762DFc9d1d712CcD6b09b25" + } + }, + { + "chainId": 10, + "bundler": "https://optimism-bundler.etherspot.io", + "contracts": { + "etherspotPaymasterAddress": "0x805650ce74561C85baA44a8Bd13E19633Fd0F79d" + } + }, + { + "chainId": 56, + "bundler": "https://bnb-bundler.etherspot.io/", + "contracts": { + "etherspotPaymasterAddress": "0xEA5ecE95D3A28f9faB161779d20128b449F9EC9C" + } + }, + { + "chainId": 100, + "bundler": "https://gnosis-bundler.etherspot.io/", + "contracts": { + "etherspotPaymasterAddress": "0x373aBcF1EA9e5802778E32870e7f72C8A6a90349" + } + }, + { + "chainId": 122, + "bundler": "https://fuse-bundler.etherspot.io", + "contracts": { + "etherspotPaymasterAddress": "0xEC2EE24E79C73DB13Dd9bC782856a5296626b7eb" + } + }, + { + "chainId": 137, + "bundler": "https://polygon-bundler.etherspot.io", + "contracts": { + "etherspotPaymasterAddress": "0x26FeC24b0D467C9de105217B483931e8f944ff50" + } + }, + { + "chainId": 5000, + "bundler": "https://mantle-bundler.etherspot.io/", + "contracts": { + "etherspotPaymasterAddress": "0x8A41594e5c6Fe492e437414c24eA6f401186b8d2" + } + }, + { + "chainId": 8453, + "bundler": "https://base-bundler.etherspot.io/", + "contracts": { + "etherspotPaymasterAddress": "0x810FA4C915015b703db0878CF2B9344bEB254a40" + } + }, + { + "chainId": 42161, + "bundler": "https://arbitrum-bundler.etherspot.io", + "contracts": { + "etherspotPaymasterAddress": "0xEC2EE24E79C73DB13Dd9bC782856a5296626b7eb" + } + }, + { + "chainId": 43114, + "bundler": "https://avalanche-bundler.etherspot.io/", + "contracts": { + "etherspotPaymasterAddress": "0x527569794781671319f20374A050BDbef4181aB3" + } + }, + { + "chainId": 59144, + "bundler": "https://linea-bundler.etherspot.io/", + "contracts": { + "etherspotPaymasterAddress": "0xB3AD9B9B06c6016f81404ee8FcCD0526F018Cf0C" + } + }, + { + "chainId": 80001, + "bundler": "https://mumbai-bundler.etherspot.io", + "contracts": { + "etherspotPaymasterAddress": "0x8350355c08aDAC387b443782124A30A8942BeC2e" + } + }, + { + "chainId": 84531, + "bundler": "https://basegoerli-bundler.etherspot.io", + "contracts": { + "etherspotPaymasterAddress": "0x898c530A5fA37720DcF1843AeCC34b6B0cBaEB8a" + } + }, + { + "chainId": 421613, + "bundler": "https://arbitrumgoerli-bundler.etherspot.io", + "contracts": { + "etherspotPaymasterAddress": "0x898c530A5fA37720DcF1843AeCC34b6B0cBaEB8a" + } + }, + { + "chainId": 534351, + "bundler": "https://scrollsepolia-bundler.etherspot.io/", + "contracts": { + "etherspotPaymasterAddress": "0xe893A26DD53b325BffAacDfA224692EfF4C448c4" + } + }, + { + "chainId": 11155111, + "bundler": "https://sepolia-bundler.etherspot.io", + "contracts": { + "etherspotPaymasterAddress": "0xcaDBADcFeD5530A49762DFc9d1d712CcD6b09b25" + } + } +] \ No newline at end of file diff --git a/backend/src/constants/Etherspot.ts b/backend/src/constants/Etherspot.ts deleted file mode 100644 index 26e68d6..0000000 --- a/backend/src/constants/Etherspot.ts +++ /dev/null @@ -1,93 +0,0 @@ -interface NetworkConfig { - chainId: number; - bundler: string; - contracts: { - etherspotPaymasterAddress: string; - }; -} - -const Networks: { - [key: string]: NetworkConfig -} = { - [5]: { - chainId: 5, - bundler: 'https://goerli-bundler.etherspot.io', - contracts: { - etherspotPaymasterAddress: '0xcaDBADcFeD5530A49762DFc9d1d712CcD6b09b25', - } - }, - [80001]: { - chainId: 80001, - bundler: 'https://mumbai-bundler.etherspot.io', - contracts: { - etherspotPaymasterAddress: '0x8350355c08aDAC387b443782124A30A8942BeC2e', - } - }, - [84531]: { - chainId: 84531, - bundler: 'https://basegoerli-bundler.etherspot.io', - contracts: { - etherspotPaymasterAddress: '0x898c530A5fA37720DcF1843AeCC34b6B0cBaEB8a', - } - }, - [11155111]: { - chainId: 11155111, - bundler: 'https://sepolia-bundler.etherspot.io', - contracts: { - etherspotPaymasterAddress: '0xcaDBADcFeD5530A49762DFc9d1d712CcD6b09b25', - } - }, - [10]: { - chainId: 10, - bundler: 'https://optimism-bundler.etherspot.io', - contracts: { - etherspotPaymasterAddress: '0x805650ce74561C85baA44a8Bd13E19633Fd0F79d', - } - }, - [137]: { - chainId: 137, - bundler: 'https://polygon-bundler.etherspot.io', - contracts: { - etherspotPaymasterAddress: '0x26FeC24b0D467C9de105217B483931e8f944ff50', - } - }, - [42161]: { - chainId: 42161, - bundler: 'https://arbitrum-bundler.etherspot.io', - contracts: { - etherspotPaymasterAddress: '0xEC2EE24E79C73DB13Dd9bC782856a5296626b7eb', - } - }, - [1]: { - chainId: 1, - bundler: 'https://ethereum-bundler.etherspot.io/', - contracts: { - etherspotPaymasterAddress: '0x7F690e93CecFca5A31E6e1dF50A33F6d3059048c', - } - }, - [421613]: { - chainId: 421613, - bundler: 'https://arbitrumgoerli-bundler.etherspot.io', - contracts: { - etherspotPaymasterAddress: '0x898c530A5fA37720DcF1843AeCC34b6B0cBaEB8a', - } - }, - [122]: { - chainId: 122, - bundler: 'https://fuse-bundler.etherspot.io', - contracts: { - etherspotPaymasterAddress: '0xEC2EE24E79C73DB13Dd9bC782856a5296626b7eb', - } - }, - [5000]: { - chainId: 5000, - bundler: 'https://mantle-bundler.etherspot.io/', - contracts: { - etherspotPaymasterAddress: '0x8A41594e5c6Fe492e437414c24eA6f401186b8d2', - } - }, -}; - -export function getNetworkConfig(key: number) { - return Networks[key]; -} \ No newline at end of file diff --git a/backend/src/plugins/config.ts b/backend/src/plugins/config.ts index 9cd011a..74065af 100644 --- a/backend/src/plugins/config.ts +++ b/backend/src/plugins/config.ts @@ -19,6 +19,7 @@ const ConfigSchema = Type.Strict( PAYMASTER_PRIVATE_KEY: Type.String(), STACKUP_API_KEY: Type.String() || undefined, API_KEY: Type.String(), + SUPPORTED_NETWORKS: Type.String() || undefined, }) ); @@ -48,9 +49,8 @@ const configPlugin: FastifyPluginAsync = async (server) => { API_HOST: process.env.API_HOST ?? '', API_KEY: process.env.API_KEY ?? '', PAYMASTER_PRIVATE_KEY: process.env.PAYMASTER_PRIVATE_KEY ?? '', - PIMLICO_API_KEY: process.env.PIMLICO_API_KEY ?? '', STACKUP_API_KEY: process.env.STACKUP_API_KEY ?? '', - PIMLICO_CHAIN_ID: process.env.PIMLICO_CHAIN_ID ?? '', + SUPPORTED_NETWORKS: process.env.SUPPORTED_NETWORKS ?? '', } server.decorate("config", config); diff --git a/backend/src/routes/index.ts b/backend/src/routes/index.ts index 20557d2..a07fdf4 100644 --- a/backend/src/routes/index.ts +++ b/backend/src/routes/index.ts @@ -3,7 +3,7 @@ import { Type } from "@sinclair/typebox"; import { FastifyPluginAsync } from "fastify"; import { ethers } from "ethers"; import { Paymaster } from "../paymaster/index.js"; -import { getNetworkConfig } from "../constants/Etherspot.js"; +import SupportedNetworks from "../../config.json" assert { type: "json" }; import { TOKEN_ADDRESS } from "../constants/Pimlico.js"; import ErrorMessage from "../constants/ErrorMessage.js"; @@ -13,6 +13,15 @@ const routes: FastifyPluginAsync = async (server) => { server.config.STACKUP_API_KEY, ); + function getNetworkConfig(key: any) { + if (server.config.SUPPORTED_NETWORKS !== '') { + const buffer = Buffer.from(server.config.SUPPORTED_NETWORKS, 'base64'); + const supportedNetworks = JSON.parse(buffer.toString()) + return supportedNetworks.find((chain: any) => { chain["chainId"] == key }); + } else + return SupportedNetworks.find((chain) => chain.chainId == key); + } + const whitelistResponseSchema = { schema: { response: { @@ -57,7 +66,7 @@ const routes: FastifyPluginAsync = async (server) => { ) { return reply.code(400).send({ error: ErrorMessage.INVALID_DATA }); } - if (!getNetworkConfig(chainId)) { + if (server.config.SUPPORTED_NETWORKS == '' && !SupportedNetworks) { return reply.code(400).send({ error: ErrorMessage.UNSUPPORTED_NETWORK }); } const hex = (Number((date.valueOf() / 1000).toFixed(0)) + 6000).toString(16); @@ -67,6 +76,7 @@ const routes: FastifyPluginAsync = async (server) => { } str += hex; const networkConfig = getNetworkConfig(chainId); + if (!networkConfig) return reply.code(400).send({ error: ErrorMessage.UNSUPPORTED_NETWORK }); const result = await paymaster.sign(userOp, str, "0x0000000000001234", entryPoint, networkConfig.contracts.etherspotPaymasterAddress, networkConfig.bundler); if (body.jsonrpc) return reply.code(200).send({ jsonrpc: body.jsonrpc, id: body.id, result, error: null }) @@ -102,10 +112,11 @@ const routes: FastifyPluginAsync = async (server) => { ) { return reply.code(400).send({ error: ErrorMessage.INVALID_DATA }); } - if (!getNetworkConfig(chainId)) { + if (server.config.SUPPORTED_NETWORKS == '' && !SupportedNetworks) { return reply.code(400).send({ error: ErrorMessage.UNSUPPORTED_NETWORK }); } const networkConfig = getNetworkConfig(chainId); + if (!networkConfig) return reply.code(400).send({ error: ErrorMessage.UNSUPPORTED_NETWORK }); if (!TOKEN_ADDRESS[chainId][gasToken]) return reply.code(400).send({ error: ErrorMessage.UNSUPPORTED_NETWORK_TOKEN }) const result = await paymaster.pimlico(userOp, gasToken, networkConfig.bundler, entryPoint); if (body.jsonrpc) @@ -140,10 +151,11 @@ const routes: FastifyPluginAsync = async (server) => { ) { return reply.code(400).send({ error: ErrorMessage.INVALID_DATA }); } - if (!getNetworkConfig(chainId)) { + if (server.config.SUPPORTED_NETWORKS == '' && !SupportedNetworks) { return reply.code(400).send({ error: ErrorMessage.UNSUPPORTED_NETWORK }); } const networkConfig = getNetworkConfig(chainId); + if (!networkConfig) return reply.code(400).send({ error: ErrorMessage.UNSUPPORTED_NETWORK }); if (!TOKEN_ADDRESS[chainId][gasToken]) return reply.code(400).send({ error: "Invalid network/token" }) const result = await paymaster.pimlicoAddress(gasToken, networkConfig.bundler, entryPoint); if (body.jsonrpc) @@ -208,10 +220,11 @@ const routes: FastifyPluginAsync = async (server) => { ) { return reply.code(400).send({ error: ErrorMessage.INVALID_DATA }); } - if (!getNetworkConfig(chainId)) { + if (server.config.SUPPORTED_NETWORKS == '' && !SupportedNetworks) { return reply.code(400).send({ error: ErrorMessage.UNSUPPORTED_NETWORK }); } const networkConfig = getNetworkConfig(chainId); + if (!networkConfig) return reply.code(400).send({ error: ErrorMessage.UNSUPPORTED_NETWORK }); const validAddresses = await address.every(ethers.utils.isAddress); if (!validAddresses) return reply.code(400).send({ error: "Invalid Address passed" }); const result = await paymaster.whitelistAddresses(address, networkConfig.contracts.etherspotPaymasterAddress, networkConfig.bundler); @@ -248,10 +261,11 @@ const routes: FastifyPluginAsync = async (server) => { ) { return reply.code(400).send({ error: ErrorMessage.INVALID_DATA }); } - if (!getNetworkConfig(chainId)) { + if (server.config.SUPPORTED_NETWORKS == '' && !SupportedNetworks) { return reply.code(400).send({ error: ErrorMessage.UNSUPPORTED_NETWORK }); } const networkConfig = getNetworkConfig(chainId); + if (!networkConfig) return reply.code(400).send({ error: ErrorMessage.UNSUPPORTED_NETWORK }); const response = await paymaster.checkWhitelistAddress(sponsorAddress, accountAddress, networkConfig.contracts.etherspotPaymasterAddress, networkConfig.bundler); if (body.jsonrpc) return reply.code(200).send({ jsonrpc: body.jsonrpc, id: body.id, result: { message: response === true ? 'Already added' : 'Not added yet' }, error: null }) @@ -283,10 +297,11 @@ const routes: FastifyPluginAsync = async (server) => { ) { return reply.code(400).send({ error: ErrorMessage.INVALID_DATA }); } - if (!getNetworkConfig(chainId)) { + if (server.config.SUPPORTED_NETWORKS == '' && !SupportedNetworks) { return reply.code(400).send({ error: ErrorMessage.UNSUPPORTED_NETWORK }); } const networkConfig = getNetworkConfig(chainId); + if (!networkConfig) return reply.code(400).send({ error: ErrorMessage.UNSUPPORTED_NETWORK }); return await paymaster.deposit(amount, networkConfig.contracts.etherspotPaymasterAddress, networkConfig.bundler); } catch (err: any) { request.log.error(err);