Skip to content

Commit

Permalink
feat: add arbitrum (beta) (#274)
Browse files Browse the repository at this point in the history
* enh: update gitignore

* enh: don't hardcode RPC urls

* feat: add arbitrum support

* enh: remove unused code

* enh: run typecheck before push

* fix: add defaults for RPC urls

* enh: add configuration for arbitrum goerli
  • Loading branch information
bard authored Aug 31, 2023
1 parent 488a03c commit 16e99ae
Show file tree
Hide file tree
Showing 9 changed files with 197 additions and 71 deletions.
15 changes: 10 additions & 5 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
INDEXED_CHAINS=mainnet,optimism,goerli,fantom,pgn-testnet,pgn-mainnet,goerli
INFURA_API_KEY=
ALCHEMY_API_KEY=
PASSPORT_API_KEY=
MAINNET_RPC_URL=https://mainnet.infura.io/v3/
GOERLI_RPC_URL=https://goerli.infura.io/v3/
OPTIMISM_RPC_URL=https://opt-mainnet.g.alchemy.com/v2/
FANTOM_RPC_URL=https://rpcapi.fantom.network
PGN_TESTNET_RPC_URL=https://sepolia.publicgoods.network
PGN_RPC_URL=https://rpc.publicgoods.network
ARBITRUM_RPC_URL=https://arb-mainnet.g.alchemy.com/v2/
ARBITRUM_GOERLI_RPC_URL=https://arb-goerli.g.alchemy.com/v2/
PASSPORT_SCORER_ID=
COINGECKO_API_KEY=
IPFS_GATEWAY=
SENTRY_DSN=
STORAGE_DIR=./data
CACHE_DIR=./.cache
STORAGE_DIR=./storage/data
CACHE_DIR=./storage/cache
DEPLOYMENT_ENVIRONMENT=local
PORT=8080
LOG_LEVEL=debug
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
data
.env
.cache
/storage
dist
node_modules
.idea
Expand Down
3 changes: 3 additions & 0 deletions lefthook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ pre-push:
eslint:
glob: "*.{js,ts,jsx,tsx}"
run: npm run lint {staged_files}
typecheck:
glob: "*.{js,ts,jsx,tsx}"
run: npm run typecheck
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"lint": "eslint src",
"test": "vitest run --reporter verbose",
"test:watch": "vitest watch --reporter verbose",
"typecheck": "tsc --noEmit",
"typecheck:watch": "tsc --noEmit --watch",
"deploy:development": "docker buildx build . -t registry.fly.io/indexer-development:latest && docker push registry.fly.io/indexer-development:latest && flyctl -c fly.development.toml --app indexer-development deploy -i registry.fly.io/indexer-development:latest"
},
Expand Down
55 changes: 0 additions & 55 deletions src/calculator/tokensSettings.ts

This file was deleted.

25 changes: 24 additions & 1 deletion src/calculator/votes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Chain } from "../config.js";
import type { Round, Application, Vote } from "../indexer/types.js";
import type { PassportScore, PassportProvider } from "../passport/index.js";
import { applyVoteCap } from "./tokensSettings.js";

type VoteWithCoefficient = Vote & {
coefficient: number;
Expand Down Expand Up @@ -101,3 +100,27 @@ export async function getVotesWithCoefficients(

return (await Promise.all(votePromises)).flat();
}

export function applyVoteCap(chain: Chain, vote: Vote): Vote {
const tokenConfig = chain.tokens.find(
(t) => t.address.toLowerCase() === vote.token.toLowerCase()
);

if (tokenConfig === undefined) {
throw new Error(`Unknown token: ${vote.token}`);
}

const { voteAmountCap } = tokenConfig;
if (voteAmountCap === undefined) {
return vote;
} else {
// amount : amountRoundToken = voteAmountCap : newAmountRoundToken
const newAmountRoundToken =
(BigInt(vote.amountRoundToken) * voteAmountCap) / BigInt(vote.amount);

return {
...vote,
amountRoundToken: newAmountRoundToken.toString(),
};
}
}
156 changes: 147 additions & 9 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ToBlock } from "chainsauce";
import { z } from "zod";

type ChainId = number;
type CoingeckoSupportedChainId = 1 | 10 | 250;
type CoingeckoSupportedChainId = 1 | 10 | 250 | 42161;

export type Token = {
code: string;
Expand Down Expand Up @@ -34,7 +34,10 @@ export const CHAINS: Chain[] = [
{
id: 1,
name: "mainnet",
rpc: `https://mainnet.infura.io/v3/${process.env.INFURA_API_KEY ?? ""}`,
rpc: z
.string()
.default("https://mainnet.infura.io/v3/")
.parse(process.env.MAINNET_RPC_URL),
pricesFromTimestamp: Date.UTC(2022, 11, 1, 0, 0, 0),
tokens: [
{
Expand Down Expand Up @@ -85,7 +88,10 @@ export const CHAINS: Chain[] = [
{
id: 5,
name: "goerli",
rpc: `https://goerli.infura.io/v3/${process.env.INFURA_API_KEY ?? ""}`,
rpc: z
.string()
.default("https://goerli.infura.io/v3/")
.parse(process.env.GOERLI_RPC_URL),
pricesFromTimestamp: Date.UTC(2022, 11, 1, 0, 0, 0),
tokens: [
{
Expand Down Expand Up @@ -153,9 +159,10 @@ export const CHAINS: Chain[] = [
{
id: 10,
name: "optimism",
rpc: `https://opt-mainnet.g.alchemy.com/v2/${
process.env.ALCHEMY_API_KEY ?? ""
}`,
rpc: z
.string()
.default("https://opt-mainnet.g.alchemy.com/v2/")
.parse(process.env.OPTIMISM_RPC_URL),
pricesFromTimestamp: Date.UTC(2022, 11, 1, 0, 0, 0),
tokens: [
{
Expand Down Expand Up @@ -206,7 +213,10 @@ export const CHAINS: Chain[] = [
{
id: 250,
name: "fantom",
rpc: "https://rpcapi.fantom.network",
rpc: z
.string()
.default("https://rpcapi.fantom.network")
.parse(process.env.FANTOM_RPC_URL),
pricesFromTimestamp: Date.UTC(2022, 11, 1, 0, 0, 0),
tokens: [
{
Expand Down Expand Up @@ -267,7 +277,10 @@ export const CHAINS: Chain[] = [
{
id: 58008,
name: "pgn-testnet",
rpc: "https://sepolia.publicgoods.network",
rpc: z
.string()
.default("https://sepolia.publicgoods.network")
.parse(process.env.PGN_TESTNET_RPC_URL),
pricesFromTimestamp: Date.UTC(2023, 6, 12, 0, 0, 0),
tokens: [
{
Expand Down Expand Up @@ -309,7 +322,10 @@ export const CHAINS: Chain[] = [
{
id: 424,
name: "pgn-mainnet",
rpc: "https://rpc.publicgoods.network",
rpc: z
.string()
.default("https://rpc.publicgoods.network")
.parse(process.env.PGN_RPC_URL),
pricesFromTimestamp: Date.UTC(2023, 6, 12, 0, 0, 0),
tokens: [
{
Expand Down Expand Up @@ -339,6 +355,128 @@ export const CHAINS: Chain[] = [
},
],
},
{
id: 42161,
name: "arbitrum",
rpc: z
.string()
.default("https://arb-mainnet.g.alchemy.com/v2/")
.parse(process.env.ARBITRUM_RPC_URL),
pricesFromTimestamp: Date.UTC(2023, 8, 1, 0, 0, 0),
tokens: [
{
code: "USDC",
address: "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
decimals: 6,
priceSource: {
chainId: 42161,
address: "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
},
},
{
code: "ARB",
address: "0x912ce59144191c1204e64559fe8253a0e49e6548",
decimals: 18,
priceSource: {
chainId: 42161,
address: "0x912ce59144191c1204e64559fe8253a0e49e6548",
},
},
{
code: "DAI",
address: "0xDA10009cBd5D07dd0CeCc66161FC93D7c9000da1",
decimals: 18,
priceSource: {
chainId: 42161,
address: "0xDA10009cBd5D07dd0CeCc66161FC93D7c9000da1",
},
},
{
code: "ETH",
address: "0x0000000000000000000000000000000000000000",
decimals: 18,
priceSource: {
chainId: 42161,
address: "0x0000000000000000000000000000000000000000",
},
},
],
subscriptions: [
{
address: "0x73AB205af1476Dc22104A6B8b3d4c273B58C6E27",
abi: "#abis/v2/ProjectRegistry.json",
},
{
address: "0xF2a07728107B04266015E67b1468cA0a536956C8",
abi: "#abis/v2/RoundFactory.json",
},
{
address: "0xC3A195EEa198e74D67671732E1B8F8A23781D735",
abi: "#abis/v2/QuadraticFundingVotingStrategyFactory.json",
},
],
},
{
id: 421613,
name: "arbitrum-goerli",
rpc: z
.string()
.default("https://arb-goerli.g.alchemy.com/v2/")
.parse(process.env.ARBITRUM_GOERLI_RPC_URL),
pricesFromTimestamp: Date.UTC(2023, 8, 1, 0, 0, 0),
tokens: [
{
code: "USDC",
address: "0xfd064A18f3BF249cf1f87FC203E90D8f650f2d63",
decimals: 6,
priceSource: {
chainId: 42161,
address: "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
},
},
{
code: "ARB",
address: "0xB50721BCf8d664c30412Cfbc6cf7a15145234ad1",
decimals: 18,
priceSource: {
chainId: 42161,
address: "0x912ce59144191c1204e64559fe8253a0e49e6548",
},
},
{
code: "DAI",
address: "0x02668f5a60D637D21e39689B68B675ed4A7B696d",
decimals: 18,
priceSource: {
chainId: 42161,
address: "0xDA10009cBd5D07dd0CeCc66161FC93D7c9000da1",
},
},
{
code: "ETH",
address: "0x0000000000000000000000000000000000000000",
decimals: 18,
priceSource: {
chainId: 42161,
address: "0x0000000000000000000000000000000000000000",
},
},
],
subscriptions: [
{
address: "0x0CD135777dEaB6D0Bb150bDB0592aC9Baa4d0871",
abi: "#abis/v2/ProjectRegistry.json",
},
{
address: "0xdf25423c9ec15347197Aa5D3a41c2ebE27587D59",
abi: "#abis/v2/RoundFactory.json",
},
{
address: "0x0BFA0AAF5f2D81f859e85C8E82A3fc5b624fc6E8",
abi: "#abis/v2/QuadraticFundingVotingStrategyFactory.json",
},
],
},
];

// mapping of chain id => token address => decimals
Expand Down
10 changes: 9 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,15 @@ async function main(): Promise<void> {
msg: "starting",
buildTag: config.buildTag,
deploymentEnvironment: config.deploymentEnvironment,
chains: config.chains.map((c) => c.name),
chains: config.chains.map(
(c) =>
c.name +
" (rpc: " +
c.rpc.slice(0, 25) +
"..." +
c.rpc.slice(-5, -1) +
")"
),
});

// Promise will be resolved once the catchup is done. Afterwards, services
Expand Down
2 changes: 2 additions & 0 deletions src/prices/coinGecko.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ const platforms: { [key: number]: string } = {
1: "ethereum",
250: "fantom",
10: "optimistic-ethereum",
42161: "arbitrum-one",
};

const nativeTokens: { [key: number]: string } = {
1: "ethereum",
250: "fantom",
10: "ethereum",
42161: "ethereum",
};

type Timestamp = number;
Expand Down

0 comments on commit 16e99ae

Please sign in to comment.