Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ That project requires [docker](https://docker.com) to be installed so the backen

| Variables | Default | Description |
| :-------------------------: | :--------------------------------------: | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: |
| `ARCHIVE_GATEWAY_API_KEY` | `NONE` | **Required** API Key to access the SQD archive nodes on supported chains. Check [SQD portal app](https://portal.sqd.dev/app) |
| `CHAIN_IDS` | `31337` | Comma separated supported chain ids to be indexed |
| `RPC_URL_1` | `https://rpc.ankr.com/eth` | Endpoint for Mainnet RPC node |
| `RPC_URL_11155111` | `https://rpc.ankr.com/eth_sepolia` | Endpoint for Sepolia RPC node |
Expand Down
928 changes: 580 additions & 348 deletions package-lock.json

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@
"@cartesi/rollups-v2": "npm:@cartesi/rollups@2.1.1",
"@subsquid/evm-abi": "^0.3.1",
"@subsquid/evm-codec": "^0.3.0",
"@subsquid/evm-processor": "^1.4.0",
"@subsquid/evm-processor": "^1.30.1",
"@subsquid/file-store": "^2.0.0",
"@subsquid/graphql-server": "^4.2.0",
"@subsquid/logger": "^1.3.0",
"@subsquid/typeorm-migration": "^1.2.0",
"@subsquid/typeorm-store": "^1.2.0",
"@subsquid/graphql-server": "^4.12.0",
"@subsquid/logger": "^1.6.0",
"@subsquid/typeorm-migration": "^1.3.1",
"@subsquid/typeorm-store": "^1.9.1",
"abitype": "^1.0.6",
"dotenv": "^16.1.4",
"ethers": "^6.5.1",
Expand All @@ -55,8 +55,8 @@
},
"devDependencies": {
"@subsquid/cli": "latest",
"@subsquid/evm-typegen": "^4.3.0",
"@subsquid/typeorm-codegen": "^2.0.2",
"@subsquid/evm-typegen": "^4.6.0",
"@subsquid/typeorm-codegen": "^2.2.0",
"@types/node": "^22.1.0",
"@usecannon/cli": "^2.21.5",
"@vitest/coverage-v8": "^2",
Expand Down
26 changes: 13 additions & 13 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ import {
optimismSepolia,
sepolia,
} from 'viem/chains';
import { contracts as MainnetContracts } from './deployments/1/contracts.json';
import { contracts as OptimismContracts } from './deployments/10/contracts.json';
import { contracts as SepoliaContracts } from './deployments/11155111/contracts.json';
import { contracts as OptSepoliaContracts } from './deployments/11155420/contracts.json';
import { contracts as CannonContracts } from './deployments/13370/contracts.json';
import { contracts as ArbitrumContracts } from './deployments/42161/contracts.json';
import { contracts as ArbSepoliaContracts } from './deployments/421614/contracts.json';
import { contracts as BaseSepoliaContracts } from './deployments/84532/contracts.json';
import { contracts as MainnetContracts } from './deployments/1/contracts.json';
import { contracts as OptimismContracts } from './deployments/10/contracts.json';
import { contracts as BaseContracts } from './deployments/8453/contracts.json';
import { contracts as ArbitrumContracts } from './deployments/42161/contracts.json';
import { archiveNodes } from './gateways';
import { contracts as BaseSepoliaContracts } from './deployments/84532/contracts.json';
import { getArchiveGateway } from './gateways';
import { parseIntOr, smallerOf } from './utils';

// addresses from deployment/13370. (Probably) the addresses will be the same on all chains
Expand Down Expand Up @@ -110,7 +110,7 @@ export const getConfig = (chainId: number): ProcessorConfig => {
case mainnet.id:
return {
dataSource: {
archive: archiveNodes.mainnet,
archive: getArchiveGateway('mainnet'),
rpcEndpoint: {
url: process.env[RPC_URL] ?? 'https://rpc.ankr.com/eth',
rateLimit: rateLimit,
Expand All @@ -135,7 +135,7 @@ export const getConfig = (chainId: number): ProcessorConfig => {
case sepolia.id:
return {
dataSource: {
archive: archiveNodes.sepolia,
archive: getArchiveGateway('sepolia'),
rpcEndpoint: {
url:
process.env[RPC_URL] ??
Expand All @@ -162,7 +162,7 @@ export const getConfig = (chainId: number): ProcessorConfig => {
case optimism.id:
return {
dataSource: {
archive: archiveNodes.optimism,
archive: getArchiveGateway('optimism'),
rpcEndpoint: {
url:
process.env[RPC_URL] ??
Expand All @@ -189,7 +189,7 @@ export const getConfig = (chainId: number): ProcessorConfig => {
case optimismSepolia.id: //Optimism-Sepolia
return {
dataSource: {
archive: archiveNodes.optimismSepolia,
archive: getArchiveGateway('optimismSepolia'),
rpcEndpoint: {
url:
process.env[RPC_URL] ??
Expand All @@ -216,7 +216,7 @@ export const getConfig = (chainId: number): ProcessorConfig => {
case base.id:
return {
dataSource: {
archive: archiveNodes.base,
archive: getArchiveGateway('base'),
rpcEndpoint: {
url:
process.env[RPC_URL] ??
Expand All @@ -242,7 +242,7 @@ export const getConfig = (chainId: number): ProcessorConfig => {
case baseSepolia.id:
return {
dataSource: {
archive: archiveNodes.baseSepolia,
archive: getArchiveGateway('baseSepolia'),
rpcEndpoint: {
url:
process.env[RPC_URL] ??
Expand All @@ -269,7 +269,7 @@ export const getConfig = (chainId: number): ProcessorConfig => {
case arbitrum.id:
return {
dataSource: {
archive: archiveNodes.arbitrum,
archive: getArchiveGateway('arbitrum'),
rpcEndpoint: {
url:
process.env[RPC_URL] ??
Expand All @@ -296,7 +296,7 @@ export const getConfig = (chainId: number): ProcessorConfig => {
case arbitrumSepolia.id:
return {
dataSource: {
archive: archiveNodes.arbitrumSepolia,
archive: getArchiveGateway('arbitrumSepolia'),
rpcEndpoint: {
url:
process.env[RPC_URL] ??
Expand Down
33 changes: 33 additions & 0 deletions src/gateways.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { GatewaySettings } from '@subsquid/evm-processor';

/**
* Archive nodes raw gateway URLs more info {@link https://docs.subsquid.io/glossary/#archive-registry}
*
Expand All @@ -15,3 +17,34 @@ export const archiveNodes = {
arbitrum: 'https://v2.archive.subsquid.io/network/arbitrum-one',
arbitrumSepolia: 'https://v2.archive.subsquid.io/network/arbitrum-sepolia',
} as const;

type SupportedGateways = keyof typeof archiveNodes;

/**
* Get the archive gateway settings for a specific network.
* @param gateway The network for which to get the archive gateway settings.
* @returns The gateway settings including the URL and configured API key (required).
*/
export const getArchiveGateway = (
gateway: SupportedGateways,
): GatewaySettings => {
const archiveGatewayApiKey = process.env.ARCHIVE_GATEWAY_API_KEY;
if (!archiveGatewayApiKey) {
throw new Error(
'Required ARCHIVE_GATEWAY_API_KEY environment variable is not set.',
);
}

const archiveUrl = archiveNodes[gateway];

if (!archiveUrl) {
throw new Error(
`Unsupported gateway: ${gateway}.\nSupported archive gateways: ${Object.keys(archiveNodes).join(', ')}`,
);
}

return {
url: archiveUrl,
apiKey: archiveGatewayApiKey,
};
};
61 changes: 59 additions & 2 deletions tests/gateways.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, expect, it } from 'vitest';
import { archiveNodes } from '../src/gateways';
import { afterEach } from 'node:test';
import { describe, expect, it, vi } from 'vitest';
import { archiveNodes, getArchiveGateway } from '../src/gateways';

describe('Gateways (Squid Archive Nodes)', () => {
it('should have a list of all supported chains by name', () => {
Expand Down Expand Up @@ -44,4 +45,60 @@ describe('Gateways (Squid Archive Nodes)', () => {
'https://v2.archive.subsquid.io/network/arbitrum-sepolia',
);
});

describe('getArchiveGateway', () => {
afterEach(() => {
vi.unstubAllEnvs();
});

it('should throw an error if the ARCHIVE_GATEWAY_API_KEY environment variable is not set', () => {
const originalEnv = process.env.ARCHIVE_GATEWAY_API_KEY;
delete process.env.ARCHIVE_GATEWAY_API_KEY;

try {
getArchiveGateway('mainnet');
throw new Error('Expected getArchiveGateway to throw an error');
} catch (error) {
expect((error as Error).message).toEqual(
'Required ARCHIVE_GATEWAY_API_KEY environment variable is not set.',
);
} finally {
process.env.ARCHIVE_GATEWAY_API_KEY = originalEnv;
}
});

it('should return the correct gateway settings for a supported network', () => {
const apiKey = 'test-api-key';
vi.stubEnv('ARCHIVE_GATEWAY_API_KEY', apiKey);

Object.entries(archiveNodes).forEach(([network, url]) => {
const gatewaySettings = getArchiveGateway(
network as keyof typeof archiveNodes,
);
expect(gatewaySettings).toEqual({
url,
apiKey,
});
});
});

describe('Runtime errors', () => {
it('should throw an error if the archive requested is not supported', () => {
const apiKey = 'test-api-key';
vi.stubEnv('ARCHIVE_GATEWAY_API_KEY', apiKey);

try {
// @ts-expect-error: Testing unsupported network
getArchiveGateway('lemonade');
throw new Error(
'Expected getArchiveGateway to throw an error',
);
} catch (error) {
expect((error as Error).message).toEqual(
`Unsupported gateway: lemonade.\nSupported archive gateways: ${Object.keys(archiveNodes).join(', ')}`,
);
}
});
});
});
});
Loading
Loading