Skip to content

Commit

Permalink
feat: Add config to set RPC node based on chain id.
Browse files Browse the repository at this point in the history
  • Loading branch information
brunomenezes committed Oct 4, 2023
1 parent 15580dd commit ba36330
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 7 deletions.
4 changes: 3 additions & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -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=
3 changes: 2 additions & 1 deletion squid-mainnet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion squid-sepolia.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
Expand All @@ -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,
};
Expand Down
40 changes: 40 additions & 0 deletions tests/processor.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { afterEach } from 'node:test';
import { MockInstance, beforeEach, describe, expect, test, vi } from 'vitest';
import { createProcessor } from '../src/processor';

Expand Down Expand Up @@ -28,6 +29,10 @@ describe('Processor creation', () => {
vi.clearAllMocks();
});

afterEach(() => {
vi.unstubAllEnvs();
});

test('Throw error for unsupported chains', () => {
try {
const processor = createProcessor(999);
Expand Down Expand Up @@ -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',
});
});
});

0 comments on commit ba36330

Please sign in to comment.