Skip to content

Commit

Permalink
Merge branch 'main' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
bard committed Sep 7, 2023
2 parents f74a47d + c5a4012 commit 8ec8687
Show file tree
Hide file tree
Showing 15 changed files with 288 additions and 201 deletions.
6 changes: 4 additions & 2 deletions src/calculator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import csv from "csv-parser";
import { linearQF, Contribution, Calculation } from "pluralistic";
import type { PassportProvider } from "../passport/index.js";
import { PriceProvider } from "../prices/provider.js";
import { Chain, tokenDecimals } from "../config.js";
import { Chain, getDecimalsForToken } from "../config.js";
import type { Round, Application, Vote } from "../indexer/types.js";
import { getVotesWithCoefficients } from "./votes.js";
import {
Expand Down Expand Up @@ -169,7 +169,9 @@ export default class Calculator {
}

const matchAmount = BigInt(round.matchAmount);
const matchTokenDecimals = BigInt(tokenDecimals[this.chainId][round.token]);
const matchTokenDecimals = BigInt(
getDecimalsForToken(this.chainId, round.token)
);

let matchingCapAmount = this.matchingCapAmount;

Expand Down
132 changes: 71 additions & 61 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import "dotenv/config";
import { ethers } from "ethers";
import { parseArgs } from "node:util";
import { ToBlock } from "chainsauce";
import { z } from "zod";
import * as abis from "./indexer/abis/index.js";

type ChainId = number;
type CoingeckoSupportedChainId = 1 | 10 | 250 | 42161;
Expand All @@ -16,9 +18,9 @@ export type Token = {

export type Subscription = {
address: string;
abi: string;
abi: ethers.ContractInterface;
fromBlock?: number;
events?: Record<string, string>;
eventsRenames?: Record<string, string>;
};

export type Chain = {
Expand All @@ -30,12 +32,13 @@ export type Chain = {
subscriptions: Subscription[];
};

export const CHAINS: Chain[] = [
const rpcUrl = z.string().url();

const CHAINS: Chain[] = [
{
id: 1,
name: "mainnet",
rpc: z
.string()
rpc: rpcUrl
.default("https://mainnet.infura.io/v3/")
.parse(process.env.MAINNET_RPC_URL),
pricesFromTimestamp: Date.UTC(2022, 11, 1, 0, 0, 0),
Expand Down Expand Up @@ -71,25 +74,24 @@ export const CHAINS: Chain[] = [
subscriptions: [
{
address: "0x03506eD3f57892C85DB20C36846e9c808aFe9ef4",
abi: "#abis/v2/ProjectRegistry.json",
abi: abis.v2.ProjectRegistry,
},
{
address: "0x9Cb7f434aD3250d1656854A9eC7A71EceC6eE1EF",
abi: "#abis/v2/RoundFactory.json",
abi: abis.v2.RoundFactory,
fromBlock: 16994474,
},
{
address: "0x4a850F463D1C4842937c5Bc9540dBc803D744c9F",
abi: "#abis/v2/QuadraticFundingVotingStrategyFactory.json",
abi: abis.v2.QuadraticFundingVotingStrategyFactory,
fromBlock: 16994526,
},
],
},
{
id: 5,
name: "goerli",
rpc: z
.string()
rpc: rpcUrl
.default("https://goerli.infura.io/v3/")
.parse(process.env.GOERLI_RPC_URL),
pricesFromTimestamp: Date.UTC(2022, 11, 1, 0, 0, 0),
Expand Down Expand Up @@ -125,42 +127,41 @@ export const CHAINS: Chain[] = [
subscriptions: [
{
address: "0x832c5391dc7931312CbdBc1046669c9c3A4A28d5",
abi: "#abis/v1/ProjectRegistry.json",
abi: abis.v1.ProjectRegistry,
},
{
address: "0x5770b7a57BD252FC4bB28c9a70C9572aE6400E48",
abi: "#abis/v1/RoundFactory.json",
events: {
abi: abis.v1.RoundFactory,
eventsRenames: {
RoundCreated: "RoundCreatedV1",
},
},
{
address: "0xa71864fAd36439C50924359ECfF23Bb185FFDf21",
abi: "#abis/v2/ProjectRegistry.json",
abi: abis.v2.ProjectRegistry,
fromBlock: 8738420,
},
{
address: "0x24F9EBFAdf095e0afe3d98635ee83CD72e49B5B0",
abi: "#abis/v2/RoundFactory.json",
abi: abis.v2.RoundFactory,
fromBlock: 8738430,
},
{
address: "0x06A6Cc566c5A88E77B1353Cdc3110C2e6c828e38",
abi: "#abis/v2/QuadraticFundingVotingStrategyFactory.json",
abi: abis.v2.QuadraticFundingVotingStrategyFactory,
fromBlock: 8790265,
},
{
address: "0x0077551e24bfB910aBABedC4336246e34B5fB0A2",
abi: "#abis/v2/DirectPayoutStrategyFactory.json",
abi: abis.v2.DirectPayoutStrategyFactory,
fromBlock: 9284367,
},
],
},
{
id: 10,
name: "optimism",
rpc: z
.string()
rpc: rpcUrl
.default("https://opt-mainnet.g.alchemy.com/v2/")
.parse(process.env.OPTIMISM_RPC_URL),
pricesFromTimestamp: Date.UTC(2022, 11, 1, 0, 0, 0),
Expand Down Expand Up @@ -196,25 +197,24 @@ export const CHAINS: Chain[] = [
subscriptions: [
{
address: "0x8e1bD5Da87C14dd8e08F7ecc2aBf9D1d558ea174",
abi: "#abis/v2/ProjectRegistry.json",
abi: abis.v2.ProjectRegistry,
},
{
address: "0x04E753cFB8c8D1D7f776f7d7A033740961b6AEC2",
abi: "#abis/v2/RoundFactory.json",
abi: abis.v2.RoundFactory,
fromBlock: 87169287,
},
{
address: "0x838C5e10dcc1e54d62761d994722367BA167AC22",
abi: "#abis/v2/QuadraticFundingVotingStrategyFactory.json",
abi: abis.v2.QuadraticFundingVotingStrategyFactory,
fromBlock: 87168143,
},
],
},
{
id: 250,
name: "fantom",
rpc: z
.string()
rpc: rpcUrl
.default("https://rpcapi.fantom.network")
.parse(process.env.FANTOM_RPC_URL),
pricesFromTimestamp: Date.UTC(2022, 11, 1, 0, 0, 0),
Expand Down Expand Up @@ -260,25 +260,24 @@ export const CHAINS: Chain[] = [
subscriptions: [
{
address: "0x8e1bD5Da87C14dd8e08F7ecc2aBf9D1d558ea174",
abi: "#abis/v2/ProjectRegistry.json",
abi: abis.v2.ProjectRegistry,
},
{
address: "0xfb08d1fD3a7c693677eB096E722ABf4Ae63B0B95",
abi: "#abis/v2/RoundFactory.json",
abi: abis.v2.RoundFactory,
fromBlock: 66509340,
},
{
address: "0x534d2AAc03dCd0Cb3905B591BAf04C14A95426AB",
abi: "#abis/v2/QuadraticFundingVotingStrategyFactory.json",
abi: abis.v2.QuadraticFundingVotingStrategyFactory,
fromBlock: 66509340,
},
],
},
{
id: 58008,
name: "pgn-testnet",
rpc: z
.string()
rpc: rpcUrl
.default("https://sepolia.publicgoods.network")
.parse(process.env.PGN_TESTNET_RPC_URL),
pricesFromTimestamp: Date.UTC(2023, 6, 12, 0, 0, 0),
Expand All @@ -305,25 +304,24 @@ export const CHAINS: Chain[] = [
subscriptions: [
{
address: "0x6294bed5B884Ae18bf737793Ef9415069Bf4bc11",
abi: "#abis/v2/ProjectRegistry.json",
abi: abis.v2.ProjectRegistry,
},
{
address: "0x0479b9DA9f287539FEBd597350B1eBaEBF7479ac",
abi: "#abis/v2/RoundFactory.json",
abi: abis.v2.RoundFactory,
fromBlock: 0,
},
{
address: "0xE8027a807Bb85e57da4B7A5ecE65b0aBDf231ce8",
abi: "#abis/v2/QuadraticFundingVotingStrategyFactory.json",
abi: abis.v2.QuadraticFundingVotingStrategyFactory,
fromBlock: 0,
},
],
},
{
id: 424,
name: "pgn-mainnet",
rpc: z
.string()
rpc: rpcUrl
.default("https://rpc.publicgoods.network")
.parse(process.env.PGN_RPC_URL),
pricesFromTimestamp: Date.UTC(2023, 6, 12, 0, 0, 0),
Expand All @@ -341,25 +339,24 @@ export const CHAINS: Chain[] = [
subscriptions: [
{
address: "0xDF9BF58Aa1A1B73F0e214d79C652a7dd37a6074e",
abi: "#abis/v2/ProjectRegistry.json",
abi: abis.v2.ProjectRegistry,
},
{
address: "0x8AdFcF226dfb2fA73788Ad711C958Ba251369cb3",
abi: "#abis/v2/RoundFactory.json",
abi: abis.v2.RoundFactory,
fromBlock: 0,
},
{
address: "0x2AFA4bE0f2468347A2F086c2167630fb1E58b725",
abi: "#abis/v2/QuadraticFundingVotingStrategyFactory.json",
abi: abis.v2.QuadraticFundingVotingStrategyFactory,
fromBlock: 0,
},
],
},
{
id: 42161,
name: "arbitrum",
rpc: z
.string()
rpc: rpcUrl
.default("https://arb-mainnet.g.alchemy.com/v2/")
.parse(process.env.ARBITRUM_RPC_URL),
pricesFromTimestamp: Date.UTC(2023, 7, 1, 0, 0, 0),
Expand Down Expand Up @@ -404,23 +401,22 @@ export const CHAINS: Chain[] = [
subscriptions: [
{
address: "0x73AB205af1476Dc22104A6B8b3d4c273B58C6E27",
abi: "#abis/v2/ProjectRegistry.json",
abi: abis.v2.ProjectRegistry,
},
{
address: "0xF2a07728107B04266015E67b1468cA0a536956C8",
abi: "#abis/v2/RoundFactory.json",
abi: abis.v2.RoundFactory,
},
{
address: "0xC3A195EEa198e74D67671732E1B8F8A23781D735",
abi: "#abis/v2/QuadraticFundingVotingStrategyFactory.json",
abi: abis.v2.QuadraticFundingVotingStrategyFactory,
},
],
},
{
id: 421613,
name: "arbitrum-goerli",
rpc: z
.string()
rpc: rpcUrl
.default("https://arb-goerli.g.alchemy.com/v2/")
.parse(process.env.ARBITRUM_GOERLI_RPC_URL),
pricesFromTimestamp: Date.UTC(2023, 7, 1, 0, 0, 0),
Expand Down Expand Up @@ -465,34 +461,48 @@ export const CHAINS: Chain[] = [
subscriptions: [
{
address: "0x0CD135777dEaB6D0Bb150bDB0592aC9Baa4d0871",
abi: "#abis/v2/ProjectRegistry.json",
abi: abis.v2.ProjectRegistry,
},
{
address: "0xdf25423c9ec15347197Aa5D3a41c2ebE27587D59",
abi: "#abis/v2/RoundFactory.json",
abi: abis.v2.RoundFactory,
},
{
address: "0x0BFA0AAF5f2D81f859e85C8E82A3fc5b624fc6E8",
abi: "#abis/v2/QuadraticFundingVotingStrategyFactory.json",
abi: abis.v2.QuadraticFundingVotingStrategyFactory,
},
],
},
];

// mapping of chain id => token address => decimals
export const tokenDecimals = Object.fromEntries(
CHAINS.map((chain) => {
return [
chain.id,
Object.fromEntries(
chain.tokens.map((token) => [
token.address.toLowerCase(),
token.decimals,
])
),
];
})
);
export const getDecimalsForToken = (
chainId: ChainId,
tokenAddress: string
): number => {
const chain = CHAINS.find((c) => c.id === chainId);
if (chain === undefined) {
throw new Error(`No such chain: ${chainId}`);
}

const token = chain.tokens.find(
(t) => t.address.toLowerCase() === tokenAddress
);
if (token === undefined) {
throw new Error(
`No such token: ${tokenAddress} configured for chain ${chainId}`
);
}

return token.decimals;
};

export const getChainConfigById = (chainId: ChainId): Chain => {
const chain = CHAINS.find((c) => c.id === chainId);
if (chain === undefined) {
throw new Error(`Chain not configured: ${chainId}`);
}
return chain;
};

export type Config = {
buildTag: string | null;
Expand Down
8 changes: 3 additions & 5 deletions src/database.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CHAINS } from "./config.js";
import { getChainConfigById } from "./config.js";
import path from "node:path";
import { JsonStorage } from "chainsauce";

Expand All @@ -7,11 +7,9 @@ export default function load(
chainId?: number
): JsonStorage {
if (chainId) {
if (CHAINS.find((chain) => chain.id === chainId) === undefined) {
throw new Error(`Chain ${chainId} not foound`);
}
const chain = getChainConfigById(chainId);

storageDir = path.join(storageDir, chainId.toString());
storageDir = path.join(storageDir, chain.id.toString());
}

return new JsonStorage(storageDir);
Expand Down
Loading

0 comments on commit 8ec8687

Please sign in to comment.