From ba36330c374208eae531407282eab0861f90db4a Mon Sep 17 00:00:00 2001 From: Bruno Menezes Date: Thu, 5 Oct 2023 12:17:05 +1300 Subject: [PATCH] feat: Add config to set RPC node based on chain id. --- .env | 4 +++- squid-mainnet.yaml | 3 ++- squid-sepolia.yaml | 3 ++- src/config.ts | 8 ++++---- tests/processor.test.ts | 40 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 51 insertions(+), 7 deletions(-) diff --git a/.env b/.env index 2970a0b..6c6b52a 100644 --- a/.env +++ b/.env @@ -4,4 +4,6 @@ GQL_PORT=4350 # JSON-RPC node endpoint, both wss and https endpoints are accepted CHAIN_ID="11155111" # possible configurations -# RPC_ENDPOINT= +# RPC_URL_1= +# RPC_URL_11155111= +# RPC_URL_31337= diff --git a/squid-mainnet.yaml b/squid-mainnet.yaml index 5ff1ee1..97a00c6 100644 --- a/squid-mainnet.yaml +++ b/squid-mainnet.yaml @@ -6,10 +6,11 @@ build: deploy: addons: postgres: + secrets: + - RPC_URL_1 processor: env: CHAIN_ID: 1 - RPC_ENDPOINT: https://rpc.ankr.com/eth cmd: - node - lib/main diff --git a/squid-sepolia.yaml b/squid-sepolia.yaml index ed08104..80d9373 100644 --- a/squid-sepolia.yaml +++ b/squid-sepolia.yaml @@ -6,10 +6,11 @@ build: deploy: addons: postgres: + secrets: + - RPC_URL_11155111 processor: env: CHAIN_ID: 11155111 - RPC_ENDPOINT: https://rpc.ankr.com/eth_sepolia cmd: - node - lib/main diff --git a/src/config.ts b/src/config.ts index a8ede7c..5bf74f7 100644 --- a/src/config.ts +++ b/src/config.ts @@ -20,13 +20,13 @@ export type ProcessorConfig = { }; export const getConfig = (chainId: number): ProcessorConfig => { + const RPC_URL = `RPC_URL_${chainId}`; switch (chainId) { case 1: // mainnet return { dataSource: { archive: lookupArchive('eth-mainnet'), - chain: - process.env.RPC_ENDPOINT ?? 'https://rpc.ankr.com/eth', + chain: process.env[RPC_URL] ?? 'https://rpc.ankr.com/eth', }, from: Math.min( CartesiDAppFactoryMainnet.receipt.blockNumber, @@ -38,7 +38,7 @@ export const getConfig = (chainId: number): ProcessorConfig => { dataSource: { archive: lookupArchive('sepolia'), chain: - process.env.RPC_ENDPOINT ?? + process.env[RPC_URL] ?? 'https://rpc.ankr.com/eth_sepolia', }, from: Math.min( @@ -49,7 +49,7 @@ export const getConfig = (chainId: number): ProcessorConfig => { case 31337: // anvil return { dataSource: { - chain: process.env.RPC_ENDPOINT ?? 'http://127.0.0.1:8545', + chain: process.env[RPC_URL] ?? 'http://127.0.0.1:8545', }, from: 0, }; diff --git a/tests/processor.test.ts b/tests/processor.test.ts index 8898389..bea62af 100644 --- a/tests/processor.test.ts +++ b/tests/processor.test.ts @@ -1,3 +1,4 @@ +import { afterEach } from 'node:test'; import { MockInstance, beforeEach, describe, expect, test, vi } from 'vitest'; import { createProcessor } from '../src/processor'; @@ -28,6 +29,10 @@ describe('Processor creation', () => { vi.clearAllMocks(); }); + afterEach(() => { + vi.unstubAllEnvs(); + }); + test('Throw error for unsupported chains', () => { try { const processor = createProcessor(999); @@ -174,4 +179,39 @@ describe('Processor creation', () => { transaction: true, }); }); + + test('Set correct chain for sepolia based on environment var', () => { + const myRPCNodeURL = 'https://my-custom-sepolia-node/v3/api'; + vi.stubEnv('RPC_URL_11155111', myRPCNodeURL); + + const processor = createProcessor(sepolia); + + expect(processor.setDataSource).toHaveBeenCalledWith({ + archive: 'https://v2.archive.subsquid.io/network/ethereum-sepolia', + chain: 'https://my-custom-sepolia-node/v3/api', + }); + }); + + test('Set correct chain for mainnet based on environment var', () => { + const myRPCNodeURL = 'https://my-custom-mainnet-node/v3/api'; + vi.stubEnv('RPC_URL_1', myRPCNodeURL); + + const processor = createProcessor(mainnet); + + expect(processor.setDataSource).toHaveBeenCalledWith({ + archive: 'https://v2.archive.subsquid.io/network/ethereum-mainnet', + chain: 'https://my-custom-mainnet-node/v3/api', + }); + }); + + test('Set correct chain for local/anvil based on environment var', () => { + const myRPCNodeURL = 'https://my-custom-local-node:9000'; + vi.stubEnv('RPC_URL_31337', myRPCNodeURL); + + const processor = createProcessor(local); + + expect(processor.setDataSource).toHaveBeenCalledWith({ + chain: 'https://my-custom-local-node:9000', + }); + }); });