From ac2af74d7021c5932ab68bb2c1c4022c4900523f Mon Sep 17 00:00:00 2001 From: angrybayblade Date: Wed, 3 Apr 2024 20:26:38 +0530 Subject: [PATCH 1/4] feat: make RPC configurable via env vars --- .env.example | 3 ++- frontend/context/UserBalanceProvider.tsx | 9 ++++++--- frontend/service/Services.ts | 5 ++++- operate/ledger/__init__.py | 9 +++++---- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/.env.example b/.env.example index eef6ffae4..5f8e07890 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,4 @@ FORK_URL= NODE_ENV= -STAKING_TEST_KEYS_PATH= \ No newline at end of file +STAKING_TEST_KEYS_PATH= +DEV_RPC= \ No newline at end of file diff --git a/frontend/context/UserBalanceProvider.tsx b/frontend/context/UserBalanceProvider.tsx index 9946c5f25..74c75006f 100644 --- a/frontend/context/UserBalanceProvider.tsx +++ b/frontend/context/UserBalanceProvider.tsx @@ -3,6 +3,9 @@ import { useInterval } from 'usehooks-ts'; import { useAppInfo } from '@/hooks'; import { EthersService } from '@/service'; +import { env } from "process"; + +const RPC = env.DEV_RPC ? env.DEV_RPC : "https://rpc.gnosischain.com"; export const UserBalanceContext = createContext<{ balance: number; @@ -15,10 +18,10 @@ export const UserBalanceProvider = ({ children }: PropsWithChildren) => { const [balance, setBalance] = useState(0); const updateBalance = async () => { - const isRpcValid = await EthersService.checkRpc('http://localhost:8545'); + const isRpcValid = await EthersService.checkRpc(RPC); if (userPublicKey && isRpcValid) - EthersService.getEthBalance(userPublicKey, 'http://localhost:8545').then( - (res) => setBalance(res), + EthersService.getEthBalance(userPublicKey, RPC).then( + (res) => setBalance(res) ); }; diff --git a/frontend/service/Services.ts b/frontend/service/Services.ts index 99d85196c..8377fae87 100644 --- a/frontend/service/Services.ts +++ b/frontend/service/Services.ts @@ -1,5 +1,8 @@ import { Deployment, Service, ServiceHash, ServiceTemplate } from '@/client'; import { BACKEND_URL } from '@/constants'; +import { env } from "process" + +const RPC = env.DEV_RPC ? env.DEV_RPC : "https://rpc.gnosischain.com" /** * Get a single service from the backend @@ -42,7 +45,7 @@ const createService = async ({ deploy, configuration: { ...serviceTemplate.configuration, - rpc: 'http://localhost:8545', + rpc: RPC, }, }), headers: { diff --git a/operate/ledger/__init__.py b/operate/ledger/__init__.py index fe17e50a7..cc6b392db 100644 --- a/operate/ledger/__init__.py +++ b/operate/ledger/__init__.py @@ -19,6 +19,7 @@ """Ledger helpers.""" +import os import typing as t from operate.ledger.base import LedgerHelper @@ -27,10 +28,10 @@ from operate.types import ChainType, LedgerType -ETHEREUM_RPC = "https://ethereum.publicnode.com" -GNOSIS_RPC = "https://rpc.gnosischain.com" -GOERLI_RPC = "https://ethereum-goerli.publicnode.com" -SOLANA_RPC = "https://api.mainnet-beta.solana.com" +ETHEREUM_RPC = os.environ.get("DEV_RPC", "https://ethereum.publicnode.com") +GNOSIS_RPC = os.environ.get("DEV_RPC", "https://rpc.gnosischain.com") +GOERLI_RPC = os.environ.get("DEV_RPC", "https://ethereum-goerli.publicnode.com") +SOLANA_RPC = os.environ.get("DEV_RPC", "https://api.mainnet-beta.solana.com") DEFAULT_RPCS = { From fa39dff9c73e0572ca8d8bb1ce2a660264805cb4 Mon Sep 17 00:00:00 2001 From: josh miller <31908788+truemiller@users.noreply.github.com> Date: Wed, 3 Apr 2024 23:17:08 +0100 Subject: [PATCH 2/4] Delete frontend/service/Services.ts have removed the js changes as will need to use a different workaround to expose only the requested ENV --- frontend/service/Services.ts | 104 ----------------------------------- 1 file changed, 104 deletions(-) delete mode 100644 frontend/service/Services.ts diff --git a/frontend/service/Services.ts b/frontend/service/Services.ts deleted file mode 100644 index 8377fae87..000000000 --- a/frontend/service/Services.ts +++ /dev/null @@ -1,104 +0,0 @@ -import { Deployment, Service, ServiceHash, ServiceTemplate } from '@/client'; -import { BACKEND_URL } from '@/constants'; -import { env } from "process" - -const RPC = env.DEV_RPC ? env.DEV_RPC : "https://rpc.gnosischain.com" - -/** - * Get a single service from the backend - * @param serviceHash - * @returns - */ -const getService = async (serviceHash: ServiceHash): Promise => - fetch(`${BACKEND_URL}/services/${serviceHash}`).then((response) => - response.json(), - ); - -/** - * Gets an array of services from the backend - * @returns An array of services - */ -const getServices = async (): Promise => - fetch(`${BACKEND_URL}/services`, { - method: 'GET', - headers: { - 'Content-Type': 'application/json', - }, - }).then((response) => response.json()); - -/** - * Creates a service - * @param serviceTemplate - * @returns Promise - */ -const createService = async ({ - serviceTemplate, - deploy, -}: { - serviceTemplate: ServiceTemplate; - deploy: boolean; -}): Promise => - fetch(`${BACKEND_URL}/services`, { - method: 'POST', - body: JSON.stringify({ - ...serviceTemplate, - deploy, - configuration: { - ...serviceTemplate.configuration, - rpc: RPC, - }, - }), - headers: { - 'Content-Type': 'application/json', - }, - }).then((response) => response.json()); - -const deployOnChain = async (serviceHash: ServiceHash): Promise => - fetch(`${BACKEND_URL}/services/${serviceHash}/onchain/deploy`, { - method: 'POST', - }).then((response) => response.json()); - -const stopOnChain = async (serviceHash: ServiceHash): Promise => - fetch(`${BACKEND_URL}/services/${serviceHash}/onchain/stop`, { - method: 'POST', - }).then((response) => response.json()); - -const buildDeployment = async (serviceHash: ServiceHash): Promise => - fetch(`${BACKEND_URL}/services/${serviceHash}/deployment/build`, { - method: 'POST', - }).then((response) => response.json()); - -const startDeployment = async (serviceHash: ServiceHash): Promise => - fetch(`${BACKEND_URL}/services/${serviceHash}/deployment/start`, { - method: 'POST', - }).then((response) => response.json()); - -const stopDeployment = async (serviceHash: ServiceHash): Promise => - fetch(`${BACKEND_URL}/services/${serviceHash}/deployment/stop`, { - method: 'POST', - }).then((response) => response.json()); - -const deleteDeployment = async ( - serviceHash: ServiceHash, -): Promise => - fetch(`${BACKEND_URL}/services/${serviceHash}/deployment/delete`, { - method: 'POST', - }).then((response) => response.json()); - -const getDeployment = async (serviceHash: ServiceHash): Promise => - fetch(`${BACKEND_URL}/services/${serviceHash}/deployment`).then((response) => - response.json(), - ); - -export const ServicesService = { - getService, - getServices, - getDeployment, - createService, - deployOnChain, - stopOnChain, - buildDeployment, - startDeployment, - stopDeployment, - deleteDeployment, -}; From 87ca3b08a3d6d366fd450eead115dbbc44b208fb Mon Sep 17 00:00:00 2001 From: josh miller <31908788+truemiller@users.noreply.github.com> Date: Wed, 3 Apr 2024 23:17:27 +0100 Subject: [PATCH 3/4] Delete frontend/context/UserBalanceProvider.tsx --- frontend/context/UserBalanceProvider.tsx | 37 ------------------------ 1 file changed, 37 deletions(-) delete mode 100644 frontend/context/UserBalanceProvider.tsx diff --git a/frontend/context/UserBalanceProvider.tsx b/frontend/context/UserBalanceProvider.tsx deleted file mode 100644 index 74c75006f..000000000 --- a/frontend/context/UserBalanceProvider.tsx +++ /dev/null @@ -1,37 +0,0 @@ -import { createContext, PropsWithChildren, useState } from 'react'; -import { useInterval } from 'usehooks-ts'; - -import { useAppInfo } from '@/hooks'; -import { EthersService } from '@/service'; -import { env } from "process"; - -const RPC = env.DEV_RPC ? env.DEV_RPC : "https://rpc.gnosischain.com"; - -export const UserBalanceContext = createContext<{ - balance: number; -}>({ - balance: 0, -}); - -export const UserBalanceProvider = ({ children }: PropsWithChildren) => { - const { userPublicKey } = useAppInfo(); - const [balance, setBalance] = useState(0); - - const updateBalance = async () => { - const isRpcValid = await EthersService.checkRpc(RPC); - if (userPublicKey && isRpcValid) - EthersService.getEthBalance(userPublicKey, RPC).then( - (res) => setBalance(res) - ); - }; - - useInterval(() => { - updateBalance(); - }, 5000); - - return ( - - {children} - - ); -}; From 0eb827a225dd328b939007d6b2804022516e5a5b Mon Sep 17 00:00:00 2001 From: Josh Miller Date: Thu, 4 Apr 2024 09:04:16 +0100 Subject: [PATCH 4/4] Refactor imports and add UserBalanceProvider and ServicesService --- frontend/client/types.ts | 3 +- frontend/context/UserBalanceProvider.tsx | 34 ++++++++ frontend/service/Services.ts | 101 +++++++++++++++++++++++ 3 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 frontend/context/UserBalanceProvider.tsx create mode 100644 frontend/service/Services.ts diff --git a/frontend/client/types.ts b/frontend/client/types.ts index 18c603d1b..8da82cbd4 100644 --- a/frontend/client/types.ts +++ b/frontend/client/types.ts @@ -1,5 +1,6 @@ import { Address } from '@/types'; -import { Ledger, Chain, DeploymentStatus } from './enums'; + +import { Chain, DeploymentStatus, Ledger } from './enums'; export type ServiceHash = string; diff --git a/frontend/context/UserBalanceProvider.tsx b/frontend/context/UserBalanceProvider.tsx new file mode 100644 index 000000000..9946c5f25 --- /dev/null +++ b/frontend/context/UserBalanceProvider.tsx @@ -0,0 +1,34 @@ +import { createContext, PropsWithChildren, useState } from 'react'; +import { useInterval } from 'usehooks-ts'; + +import { useAppInfo } from '@/hooks'; +import { EthersService } from '@/service'; + +export const UserBalanceContext = createContext<{ + balance: number; +}>({ + balance: 0, +}); + +export const UserBalanceProvider = ({ children }: PropsWithChildren) => { + const { userPublicKey } = useAppInfo(); + const [balance, setBalance] = useState(0); + + const updateBalance = async () => { + const isRpcValid = await EthersService.checkRpc('http://localhost:8545'); + if (userPublicKey && isRpcValid) + EthersService.getEthBalance(userPublicKey, 'http://localhost:8545').then( + (res) => setBalance(res), + ); + }; + + useInterval(() => { + updateBalance(); + }, 5000); + + return ( + + {children} + + ); +}; diff --git a/frontend/service/Services.ts b/frontend/service/Services.ts new file mode 100644 index 000000000..99d85196c --- /dev/null +++ b/frontend/service/Services.ts @@ -0,0 +1,101 @@ +import { Deployment, Service, ServiceHash, ServiceTemplate } from '@/client'; +import { BACKEND_URL } from '@/constants'; + +/** + * Get a single service from the backend + * @param serviceHash + * @returns + */ +const getService = async (serviceHash: ServiceHash): Promise => + fetch(`${BACKEND_URL}/services/${serviceHash}`).then((response) => + response.json(), + ); + +/** + * Gets an array of services from the backend + * @returns An array of services + */ +const getServices = async (): Promise => + fetch(`${BACKEND_URL}/services`, { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + }, + }).then((response) => response.json()); + +/** + * Creates a service + * @param serviceTemplate + * @returns Promise + */ +const createService = async ({ + serviceTemplate, + deploy, +}: { + serviceTemplate: ServiceTemplate; + deploy: boolean; +}): Promise => + fetch(`${BACKEND_URL}/services`, { + method: 'POST', + body: JSON.stringify({ + ...serviceTemplate, + deploy, + configuration: { + ...serviceTemplate.configuration, + rpc: 'http://localhost:8545', + }, + }), + headers: { + 'Content-Type': 'application/json', + }, + }).then((response) => response.json()); + +const deployOnChain = async (serviceHash: ServiceHash): Promise => + fetch(`${BACKEND_URL}/services/${serviceHash}/onchain/deploy`, { + method: 'POST', + }).then((response) => response.json()); + +const stopOnChain = async (serviceHash: ServiceHash): Promise => + fetch(`${BACKEND_URL}/services/${serviceHash}/onchain/stop`, { + method: 'POST', + }).then((response) => response.json()); + +const buildDeployment = async (serviceHash: ServiceHash): Promise => + fetch(`${BACKEND_URL}/services/${serviceHash}/deployment/build`, { + method: 'POST', + }).then((response) => response.json()); + +const startDeployment = async (serviceHash: ServiceHash): Promise => + fetch(`${BACKEND_URL}/services/${serviceHash}/deployment/start`, { + method: 'POST', + }).then((response) => response.json()); + +const stopDeployment = async (serviceHash: ServiceHash): Promise => + fetch(`${BACKEND_URL}/services/${serviceHash}/deployment/stop`, { + method: 'POST', + }).then((response) => response.json()); + +const deleteDeployment = async ( + serviceHash: ServiceHash, +): Promise => + fetch(`${BACKEND_URL}/services/${serviceHash}/deployment/delete`, { + method: 'POST', + }).then((response) => response.json()); + +const getDeployment = async (serviceHash: ServiceHash): Promise => + fetch(`${BACKEND_URL}/services/${serviceHash}/deployment`).then((response) => + response.json(), + ); + +export const ServicesService = { + getService, + getServices, + getDeployment, + createService, + deployOnChain, + stopOnChain, + buildDeployment, + startDeployment, + stopDeployment, + deleteDeployment, +};