Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Dynamic gateway generation #45

Draft
wants to merge 37 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
4cde3d1
add dynamic bridge generation
Jason-W123 Nov 16, 2022
a2396b5
fix
Jason-W123 Nov 16, 2022
380ce44
add axios check
Jason-W123 Nov 16, 2022
0343984
fix
Jason-W123 Nov 16, 2022
9a0a7b3
add default gateway searcher
Jason-W123 Nov 17, 2022
86d51ae
add comments
Jason-W123 Nov 17, 2022
c03dfad
sdk
Jason-W123 Nov 17, 2022
f81b7ed
Update src/lib/utils.ts
Jason-Wanxt Nov 21, 2022
041b19c
delete file
Jason-W123 Nov 21, 2022
b3fcc02
fix
Jason-W123 Nov 21, 2022
3a44447
add checkMapResultByL2Gateway to avoid edge case
Jason-W123 Nov 23, 2022
43b55d9
format
Jason-W123 Nov 23, 2022
24853f5
Update token_list_gen.ts
Jason-Wanxt Dec 12, 2022
4560de6
use subgraph to pull gatewaySet events
Jason-W123 Dec 20, 2022
82847d3
delete file
Jason-W123 Dec 20, 2022
28cc632
add subgraph list
Dec 20, 2022
fe82e46
support grt
Dec 20, 2022
0b6087e
rename sort method
Dec 20, 2022
acf5f78
conflicts
Dec 21, 2022
1615178
remove lines
Dec 21, 2022
e94dbad
add timeComparableEvent interface
Dec 23, 2022
c585adf
remove lpg line
Dec 23, 2022
ce0b130
add number cast
Dec 23, 2022
57ff9d0
conflict
Jason-W123 Jan 31, 2023
4c41280
resolve conflicts
Jason-W123 Jan 31, 2023
790eb41
resolve conflicts
Jason-W123 Feb 24, 2023
aa27214
add subgraph changes
Jason-W123 Feb 24, 2023
c3a4613
delete subgrahp and move functions to graph
Jason-W123 Feb 27, 2023
a6af8a3
update yarn.lock
Jason-W123 Feb 27, 2023
ce84306
format
Jason-W123 Feb 27, 2023
b6d0408
fix graph
Jason-W123 Feb 27, 2023
dff4af1
add goerli grt and resolve unset gateway
Jason-W123 Feb 28, 2023
6faf00c
solve endpoint mess
Jason-W123 Feb 28, 2023
c3d2396
add network check for grt gateway
Jason-W123 Mar 2, 2023
430ec1c
add network check for grt gateway
Jason-W123 Mar 2, 2023
56e29bb
curly brace and reset casting
Jason-W123 Mar 3, 2023
a079d9a
remove file
Jason-W123 Mar 7, 2023
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"@uniswap/token-lists": "^1.0.0-beta.31",
"ajv": "^8.12.0",
"ajv-formats": "^2.1.1",
"axios": "^0.23.0",
"axios": "^1.1.3",
"better-ajv-errors": "^1.1.2",
"dotenv": "^16.0.3",
"dotenv-expand": "^10.0.0",
Expand Down
1 change: 0 additions & 1 deletion src/ArbTokenLists/arbed_arb_whitelist_era.json

This file was deleted.

21 changes: 21 additions & 0 deletions src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,33 @@ export interface L2ToL1GatewayAddresses {
[contractAddress: string]: string;
}

export interface GraphEndpoints {
[networkId: number]: string;
}

const objKeyAndValToLowerCase = (obj: { [key: string]: string }) =>
Object.keys(obj).reduce((acc: { [key: string]: string }, key) => {
acc[key.toLowerCase()] = obj[key].toLowerCase();
return acc;
}, {});

export const bridgeGraphEndpoints: GraphEndpoints = {
42161:
'https://api.thegraph.com/subgraphs/name/gvladika/arb-bridge-eth-nitro',
42170: 'https://api.thegraph.com/subgraphs/name/gvladika/arb-bridge-eth-nova',
421613:
'https://api.thegraph.com/subgraphs/name/gvladika/arb-bridge-eth-goerli',
};

export const tokenGatewayGraphEndpoints: GraphEndpoints = {
42161:
'https://api.thegraph.com/subgraphs/name/fredlacs/layer2-token-gateway',
421611:
'https://api.thegraph.com/subgraphs/name/fredlacs/layer2-token-gateway-rinkeby',
421613:
'https://api.thegraph.com/subgraphs/name/fredlacs/layer2-token-gateway-nitro-goerli',
};

// TODO: read these values from the gateway or a subgraph
export const l2ToL1GatewayAddresses: L2ToL1GatewayAddresses =
objKeyAndValToLowerCase({
Expand Down
76 changes: 52 additions & 24 deletions src/lib/graph.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
import { request, gql } from 'graphql-request';
import { isNetwork } from './utils';
import { GraphTokenResult, GraphTokensResult } from './types';
import { excludeList } from './constants';
import {
GraphTokenResult,
GraphTokensResult,
GatewaySetsResult,
GatewaySetInfo,
} from './types';
import {
excludeList,
tokenGatewayGraphEndpoints,
bridgeGraphEndpoints,
} from './constants';
import { getArgvs } from './options';

const apolloL2GatewaysRinkebyClient =
'https://api.thegraph.com/subgraphs/name/fredlacs/layer2-token-gateway-rinkeby';
const apolloL2GatewaysClient =
'https://api.thegraph.com/subgraphs/name/fredlacs/layer2-token-gateway';

const appoloL2GatewaysGoerliRollupClient =
'https://api.thegraph.com/subgraphs/name/fredlacs/layer2-token-gateway-nitro-goerli';

const chaidIdToGraphClientUrl = (chainID: string) => {
switch (chainID) {
case '42161':
return apolloL2GatewaysClient;
case '421611':
return apolloL2GatewaysRinkebyClient;
case '421613':
return appoloL2GatewaysGoerliRollupClient;
default:
throw new Error('Unsupported chain');
const sortByTime = (a: GatewaySetInfo, b: GatewaySetInfo): number => {
if (Number(a.blockNumber) === Number(b.blockNumber)) {
return a.logIndex - b.logIndex;
}
return Number(a.blockNumber) - Number(b.blockNumber);
};

const isGraphTokenResult = (obj: GraphTokenResult) => {
Expand Down Expand Up @@ -59,7 +55,7 @@ export const getTokens = async (
}
const networkID =
typeof _networkID === 'number' ? _networkID.toString() : _networkID;
const clientUrl = chaidIdToGraphClientUrl(networkID);
const clientUrl = tokenGatewayGraphEndpoints[Number(networkID)];
// lazy solution for big lists for now; we'll have to paginate once we have > 500 tokens registed
if (tokenList.length > 500) {
const allTokens = await getAllTokens(networkID);
Expand Down Expand Up @@ -100,7 +96,7 @@ export const getTokens = async (
}
`;

const { tokens } = (await request(clientUrl, query)) as GraphTokensResult;
const { tokens }: GraphTokensResult = await request(clientUrl, query);
tokens.map((token) => isGraphTokenResult(token));

return tokens.filter(
Expand All @@ -113,7 +109,7 @@ export const getAllTokens = async (
): Promise<Array<GraphTokenResult>> => {
const networkID =
typeof _networkID === 'number' ? _networkID.toString() : _networkID;
const clientUrl = chaidIdToGraphClientUrl(networkID);
const clientUrl = tokenGatewayGraphEndpoints[Number(networkID)];
const blockNumber = graphGatewayBlockNumField(_networkID);
const query = gql`
{
Expand All @@ -137,7 +133,7 @@ export const getAllTokens = async (
}
`;

const { tokens } = (await request(clientUrl, query)) as GraphTokensResult;
const { tokens }: GraphTokensResult = await request(clientUrl, query);
const res = tokens.map((token) => {
isGraphTokenResult(token);
return { ...token };
Expand All @@ -147,3 +143,35 @@ export const getAllTokens = async (
(token) => !excludeList.includes(token.l1TokenAddr.toLowerCase()),
);
};

export async function getGatewaysets(): Promise<any[]> {
const eventResult = [];
let currentResult: GatewaySetInfo[];
let skip = 0;
const clientUrl = bridgeGraphEndpoints[getArgvs().l2NetworkID];
do {
currentResult = [];
const query = gql`query EventQuery {
gatewaySets(first: 100, orderBy: id, skip: ${skip}) {
id
l1Token
gateway
blockNumber
}
}`;
const { gatewaySets }: GatewaySetsResult = await request(clientUrl, query);

//get logIndex only
for (let i = 0; i < gatewaySets.length; i++) {
currentResult[i] = {
...gatewaySets[i],
tx: gatewaySets[i].id.substring(0, 66), //tx length 64 but there is 0x in front so it is 66
logIndex: 1,
};
}
eventResult.push(...currentResult);
skip += 100;
} while (currentResult.length == 100);
eventResult.sort(sortByTime);
return eventResult;
}
10 changes: 8 additions & 2 deletions src/lib/token_list_gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
getChunks,
promiseErrorMultiplier,
getL1GatewayAddress,
generateGatewayMap,
} from './utils';
import { validateTokenListWithErrorThrowing } from './validateTokenList';
import { constants as arbConstants } from '@arbitrum/sdk';
Expand Down Expand Up @@ -184,7 +185,11 @@ export const generateTokenList = async (
}

const tokenData = intermediateTokenData.flat(1);

const l2ToL1GatewayAddresses = await generateGatewayMap(
l2.multiCaller,
l2.network,
l1.provider,
);
const _arbifiedTokenList = tokens
.map((t, i) => ({
token: t,
Expand All @@ -201,7 +206,8 @@ export const generateTokenList = async (
const l2GatewayAddress =
token.token.joinTableEntry[0].gateway.gatewayAddr;
const l1GatewayAddress =
(await getL1GatewayAddress(l2GatewayAddress)) ?? 'N/A';
(await getL1GatewayAddress(l2GatewayAddress, l2ToL1GatewayAddresses)) ??
'N/A';

let { name: _name, decimals, symbol: _symbol } = token.tokenDatum;

Expand Down
16 changes: 16 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,19 @@ export interface GraphTokenResult {
export interface GraphTokensResult {
tokens: GraphTokenResult[];
}

export interface GatewaySetResult {
id: string;
l1Token: string;
gateway: string;
blockNumber: string;
}

export interface GatewaySetsResult {
gatewaySets: GatewaySetResult[];
}

export interface GatewaySetInfo extends GatewaySetResult {
tx: string;
logIndex: number;
}
Loading