Skip to content

Commit

Permalink
Dynamic Supported Networks (#10)
Browse files Browse the repository at this point in the history
* removed from codebase and fetched from json

* added ability to set supported chains via env
  • Loading branch information
vignesha22 authored Sep 19, 2023
1 parent 67949a1 commit 73bc7c7
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 102 deletions.
1 change: 1 addition & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ npm-debug.log*
coverage

.env
config.json

package-lock.json
pnpm-lock.yaml
121 changes: 121 additions & 0 deletions backend/config.json.default
Original file line number Diff line number Diff line change
@@ -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"
}
}
]
93 changes: 0 additions & 93 deletions backend/src/constants/Etherspot.ts

This file was deleted.

4 changes: 2 additions & 2 deletions backend/src/plugins/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
);

Expand Down Expand Up @@ -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);
Expand Down
29 changes: 22 additions & 7 deletions backend/src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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: {
Expand Down Expand Up @@ -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);
Expand All @@ -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 })
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 })
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 73bc7c7

Please sign in to comment.