Skip to content

Commit

Permalink
fix: use a different rpc url for each chain
Browse files Browse the repository at this point in the history
  • Loading branch information
genaroibc committed Oct 26, 2023
1 parent 083ff3a commit 3e3bc48
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
10 changes: 9 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -771,10 +771,18 @@ export class Squid {
}): Promise<TokenBalance[]> {
// remove invalid and duplicate chains and convert to number
const filteredChains = new Set(chains.map(Number).filter(c => !isNaN(c)));
const chainRpcUrls = this.chains.reduce(
(acc, chain) => ({
...acc,
[chain.chainId]: chain.rpc
}),
{}
);

return getAllEvmTokensBalance(
this.tokens.filter(t => filteredChains.has(Number(t.chainId))),
userAddress
userAddress,
chainRpcUrls
);
}

Expand Down
40 changes: 34 additions & 6 deletions src/services/getEvmBalances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ const CHAINS_WITHOUT_MULTICALL_RPC_URLS: Record<number, string> = {

const getTokensBalanceSupportingMultiCall = async (
tokens: TokenData[],
chainRpcUrl: string,
userAddress?: ContractAddress
): Promise<TokenBalance[]> => {
if (!userAddress) return [];

const ETHEREUM_RPC_URL = "https://eth.meowrpc.com";
const provider = new ethers.providers.JsonRpcProvider(ETHEREUM_RPC_URL);
const provider = new ethers.providers.JsonRpcProvider(chainRpcUrl);

const contractCallContext: ContractCallContext[] = tokens.map(token => {
const isNativeToken =
Expand Down Expand Up @@ -116,7 +116,10 @@ const getTokensBalanceWithoutMultiCall = async (

export const getAllEvmTokensBalance = async (
evmTokens: TokenData[],
userAddress: string
userAddress: string,
chainRpcUrls: {
[chainId: string]: string;
}
): Promise<TokenBalance[]> => {
try {
// Some tokens don't support multicall, so we need to fetch them with Promise.all
Expand All @@ -136,11 +139,36 @@ export const getAllEvmTokensBalance = async (
const tokensNotSupportingMulticall = splittedTokensByMultiCallSupport[0];
const tokensSupportingMulticall = splittedTokensByMultiCallSupport[1];

const tokensMulticall = await getTokensBalanceSupportingMultiCall(
tokensSupportingMulticall,
userAddress as ContractAddress
const tokensByChainId = tokensSupportingMulticall.reduce(
(groupedTokens, token) => {
if (!groupedTokens[token.chainId]) {
groupedTokens[token.chainId] = [];
}

groupedTokens[token.chainId].push(token);

return groupedTokens;
},
{} as Record<string, TokenData[]>
);

const tokensMulticall: TokenBalance[] = [];

for (const chainId in tokensByChainId) {
const tokens = tokensByChainId[chainId];
const rpcUrl = chainRpcUrls[chainId];

if (!rpcUrl) continue;

const tokensBalances = await getTokensBalanceSupportingMultiCall(
tokens,
rpcUrl,
userAddress as ContractAddress
);

tokensMulticall.push(...tokensBalances);
}

const tokensNotMultiCall = await getTokensBalanceWithoutMultiCall(
tokensNotSupportingMulticall,
userAddress as ContractAddress
Expand Down

0 comments on commit 3e3bc48

Please sign in to comment.