Skip to content

Commit

Permalink
feat: support filecoin
Browse files Browse the repository at this point in the history
  • Loading branch information
jtourkos committed Aug 15, 2024
1 parent 8b68b26 commit 5db3985
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 35 deletions.
32 changes: 15 additions & 17 deletions .env.template
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
PORT=string
NETWORK=string # See 'SupportedNetwork' type.
PUBLIC_API_KEYS=string # comma-separated list of strings
DRIPS_API_KEY=string
INFURA_API_KEY=string
POSTGRES_CONNECTION_STRING=string
REPO_DRIVER_ADDRESS=string # optional, defaults to mainnet address of latest deployment
DRIPS_ADDRESS=string # optional, defaults to mainnet address of latest deployment
RPC_URL=string # optional, defaults to infura mainnet
PRETEND_ALL_REPOS_EXIST=boolean # true or false. If true, app will always assume all GitHub repos exist. Used in E2E tests. Defaults to false.
NODE_ENV=string # 'development' or 'production'.
RATE_LIMIT_WINDOW_IN_MINUTES=number # optional, defaults to 2.
RATE_LIMIT_MAX_REQUESTS_PER_WINDOW=number # optional, defaults to 1000.
MAX_QUERY_DEPTH=number # optional, defaults to 4.
TIMEOUT_IN_SECONDS=number # optional, defaults to 20.
ADDRESS_DRIVER_ADDRESS=string # required, address of the `AddressDriver` contract.
IPFS_GATEWAY_URL=string # required, URL of the IPFS gateway.
PORT=string # Optional. The port the server will listen on. Defaults to 8080.
NETWORK=string # Required. The network to connect to. See `SupportedNetworks` in `types.ts` for supported networks.
PUBLIC_API_KEYS=string # Required. A comma-separated list of API keys that are allowed to access the server.
INFURA_API_KEY=string # Required only if RPC_URL is not specified. The Infura API key to use for connecting to the Ethereum network.
POSTGRES_CONNECTION_STRING=string # Required. The connection string for the database.
REPO_DRIVER_ADDRESS=string # Optional. Defaults to mainnet address of latest deployment
DRIPS_ADDRESS=string # Optional. Defaults to mainnet address of latest deployment
RPC_URL=string # Optional. The RPC URL to connect to for retrieving blockchain events. If not specified, the server will use Infura. In this case, INFURA_API_KEY must be specified.
PRETEND_ALL_REPOS_EXIST=boolean # Optional. If true, app will always assume all GitHub repos exist. Used in E2E tests. Defaults to false.
RATE_LIMIT_WINDOW_IN_MINUTES=number # Optional. Defaults to 2.
RATE_LIMIT_MAX_REQUESTS_PER_WINDOW=number # Optional. Defaults to 1000.
MAX_QUERY_DEPTH=number # Optional. Defaults to 4.
TIMEOUT_IN_SECONDS=number # Optional. Defaults to 20.
ADDRESS_DRIVER_ADDRESS=string # Required. the address of the `AddressDriver` contract.
IPFS_GATEWAY_URL=string # Required. The URL of the IPFS gateway.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ web_modules/
.yarn-integrity

# dotenv environment variable files
.env
.env.*
!.env.template

Expand Down
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
"build:graphql": "graphql-codegen",
"build:contracts": "typechain --target=ethers-v6 --out-dir ./src/generated/contracts ./src/abi/**.json",
"build": "npm run build:contracts && npm run build:graphql && tsc",
"start:local": "ENV=local npx nodemon",
"start:goerli": "ENV=goerli npx nodemon",
"start:sepolia": "ENV=sepolia npx nodemon",
"start:mainnet": "export ENV=mainnet && npm run build && node dist/index.js",
"dev": "npx nodemon",
"start": "npm run build && node dist/index.js",
"check": "tsc --noEmit"
},
Expand Down
10 changes: 7 additions & 3 deletions src/common/appSettings.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import dotenv from 'dotenv';
import shouldNeverHappen from '../utils/shouldNeverHappen';

dotenv.config({ path: `.env.${process.env.ENV}` });
dotenv.config();

function missingEnvVar(name: string): never {
throw new Error(`Missing ${name} in .env file.`);
Expand All @@ -11,7 +12,9 @@ export default {
network: process.env.NETWORK,
environment: process.env.ENV ?? 'local',
infuraApiKey: process.env.INFURA_API_KEY,
publicApiKeys: process.env.PUBLIC_API_KEYS?.split(',') || [],
publicApiKeys:
process.env.PUBLIC_API_KEYS?.split(',') ||
shouldNeverHappen('PUBLIC_API_KEYS is not set.'),
dripsApiKey: process.env.DRIPS_API_KEY,
postgresConnectionString: process.env.POSTGRES_CONNECTION_STRING,
rpcUrl:
Expand Down Expand Up @@ -39,4 +42,5 @@ export default {
process.env.ADDRESS_DRIVER_ADDRESS ||
missingEnvVar('ADDRESS_DRIVER_ADDRESS'),
ipfsGatewayUrl: process.env.IPFS_GATEWAY_URL,
};
glifToken: process.env.GLIF_TOKEN,
} as const;
30 changes: 30 additions & 0 deletions src/common/getProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type { Provider } from 'ethers';
import { FetchRequest, JsonRpcProvider, WebSocketProvider } from 'ethers';
import shouldNeverHappen from '../utils/shouldNeverHappen';
import appSettings from './appSettings';

let providerInstance: Provider | null = null;

export default function getProvider(): Provider {
if (!providerInstance) {
const { rpcUrl, glifToken } = appSettings;

if (rpcUrl.includes('node.glif.io')) {
const fetchRequest = new FetchRequest(rpcUrl);

fetchRequest.method = 'POST';
fetchRequest.setHeader('Content-Type', 'application/json');
fetchRequest.setHeader('Authorization', `Bearer ${glifToken}`);

providerInstance = new JsonRpcProvider(fetchRequest, undefined, {});
} else if (rpcUrl.startsWith('http')) {
providerInstance = new JsonRpcProvider(rpcUrl, undefined, {});
} else if (rpcUrl.startsWith('wss')) {
providerInstance = new WebSocketProvider(rpcUrl);
} else {
shouldNeverHappen(`Invalid RPC URL: ${rpcUrl}`);
}
}

return providerInstance;
}
4 changes: 0 additions & 4 deletions src/common/provider.ts

This file was deleted.

6 changes: 3 additions & 3 deletions src/project/projectUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ProjectVerificationStatus } from './ProjectModel';
import { RepoDriver__factory } from '../generated/contracts';
import assert from '../utils/assert';
import appSettings from '../common/appSettings';
import provider from '../common/provider';
import getProvider from '../common/getProvider';

export function splitProjectName(projectName: string): {
ownerName: string;
Expand Down Expand Up @@ -109,7 +109,7 @@ export async function toFakeUnclaimedProjectFromUrl(url: string) {

const repoDriver = RepoDriver__factory.connect(
appSettings.repoDriverAddress,
provider,
getProvider(),
);

const nameAsBytesLike = ethers.toUtf8Bytes(`${ownerName}/${repoName}`);
Expand Down Expand Up @@ -145,7 +145,7 @@ export async function toFakeUnclaimedProject(

const repoDriver = RepoDriver__factory.connect(
appSettings.repoDriverAddress,
provider,
getProvider(),
);

const nameAsBytesLike = ethers.toUtf8Bytes(`${ownerName}/${repoName}`);
Expand Down
4 changes: 2 additions & 2 deletions src/user/UserDataSource.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import appSettings from '../common/appSettings';
import provider from '../common/provider';
import type { Address, AddressDriverId } from '../common/types';
import { AddressDriver__factory } from '../generated/contracts';
import { Driver } from '../generated/graphql';
import type { User } from '../generated/graphql';
import getUserAddress from '../utils/getUserAddress';
import getUserAccount from '../utils/getUserAccount';
import getProvider from '../common/getProvider';

export default class UsersDataSource {
public async getUserAccount(accountId: AddressDriverId) {
Expand Down Expand Up @@ -34,7 +34,7 @@ export default class UsersDataSource {
public async getUserByAddress(address: Address): Promise<User> {
const addressDriver = AddressDriver__factory.connect(
appSettings.addressDriverAddress,
provider,
getProvider(),
);

const accountId = (await addressDriver.calcAccountId(address)).toString();
Expand Down
4 changes: 2 additions & 2 deletions src/utils/getWithdrawableBalances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import StreamReceiverSeenEventModel from '../models/StreamReceiverSeenEventModel
import StreamsSetEventModel from '../models/StreamsSetEventModel';
import appSettings from '../common/appSettings';
import { Drips__factory } from '../generated/contracts';
import provider from '../common/provider';
import getProvider from '../common/getProvider';

const drips = Drips__factory.connect(appSettings.dripsAddress, provider);
const drips = Drips__factory.connect(appSettings.dripsAddress, getProvider());

export async function getRelevantTokens(accountId: AccountId) {
const streamReceiverSeenEventsForUser =
Expand Down

0 comments on commit 5db3985

Please sign in to comment.