Skip to content

Commit

Permalink
fix: fallback providers
Browse files Browse the repository at this point in the history
  • Loading branch information
Argeare5 committed Jan 7, 2024
1 parent 01caddb commit 61695df
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 17 deletions.
35 changes: 25 additions & 10 deletions src/rpcSwitcher/store/rpcSwitcherSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Chain, createPublicClient, fallback, http } from 'viem';

import { TransactionsSlice } from '../../transactions/store/transactionsSlice';
import { appConfig } from '../../utils/appConfig';
import { initialRpcUrls, setChain } from '../../utils/chains';
import { fallBackConfig, initialRpcUrls, setChain } from '../../utils/chains';
import { chainInfoHelper } from '../../utils/configs';
import {
getLocalStorageRpcUrls,
Expand Down Expand Up @@ -105,11 +105,16 @@ export const createRpcSwitcherSlice: StoreSlice<
chain,
parsedRpcUrlsFromStorage[chainIdNumber].rpcUrl,
) as Draft<Chain>,
transport: fallback([
http(parsedRpcUrlsFromStorage[chainIdNumber].rpcUrl),
...initialRpcUrls[chainIdNumber].map((url) => http(url)),
]),
}) as Draft<PublicClient>,
transport: fallback(
[
http(parsedRpcUrlsFromStorage[chainIdNumber].rpcUrl),
...initialRpcUrls[chainIdNumber].map((url) =>
http(url),
),
],
fallBackConfig,
),
}),
};
}
});
Expand Down Expand Up @@ -153,13 +158,20 @@ export const createRpcSwitcherSlice: StoreSlice<
set((state) =>
produce(state, (draft) => {
draft.appClients[chainId].rpcUrl = rpcUrl;
// @ts-ignore
draft.appClients[chainId].instance = createPublicClient({
batch: {
multicall: true,
},
chain: chainInfoHelper.getChainParameters(chainId),
transport: http(rpcUrl),
}) as Draft<PublicClient>;
transport: fallback(
[
http(rpcUrl),
...initialRpcUrls[chainId].map((url) => http(url)),
],
fallBackConfig,
),
});
}),
);
});
Expand Down Expand Up @@ -259,8 +271,11 @@ export const createRpcSwitcherSlice: StoreSlice<
multicall: true,
},
chain: chainInfoHelper.getChainParameters(chainId),
transport: http(rpcUrl),
}) as PublicClient;
transport: fallback(
[http(rpcUrl), ...initialRpcUrls[chainId].map((url) => http(url))],
fallBackConfig,
),
});

const contractAddresses =
appConfig.payloadsControllerConfig[chainId].contractAddresses;
Expand Down
16 changes: 15 additions & 1 deletion src/utils/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,27 @@ import {
sepolia,
} from 'viem/chains';

export const fallBackConfig = {
rank: {
interval: 100_000,
sampleCount: 2,
timeout: 1000,
weights: {
latency: 0.2,
stability: 0.8,
},
},
retryDelay: 100,
retryCount: 5,
};

// chains RPC urls
export const initialRpcUrls: Record<number, string[]> = {
[mainnet.id]: [
'https://eth.llamarpc.com',
'https://ethereum.publicnode.com',
'https://cloudflare-eth.com',
'https://rpc.mevblocker.io',
'https://blissful-purple-sky.quiknode.pro/9b017a577737f118a5b3ea41790e2dec4aabf393',
],
[polygon.id]: [
'https://polygon.blockpi.network/v1/rpc/public',
Expand Down
7 changes: 5 additions & 2 deletions src/utils/initialClients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { PublicClient } from '@wagmi/core';
import { createPublicClient, fallback, http } from 'viem';

import { appUsedNetworks } from './appConfig';
import { CHAINS, initialRpcUrls } from './chains';
import { CHAINS, fallBackConfig, initialRpcUrls } from './chains';

export const initialClients: Record<number, PublicClient> = {};
appUsedNetworks.forEach((chain) => {
Expand All @@ -11,6 +11,9 @@ appUsedNetworks.forEach((chain) => {
multicall: true,
},
chain: CHAINS[chain],
transport: fallback(initialRpcUrls[chain].map((url) => http(url))),
transport: fallback(
initialRpcUrls[chain].map((url) => http(url)),
fallBackConfig,
),
});
});
11 changes: 7 additions & 4 deletions src/web3/services/delegationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ export class DelegationService {
}

async getUserPowers(userAddress: Hex, underlyingAssets: Hex[]) {
const blockNumber = await this.clients[appConfig.govCoreChainId].getBlock();
const blockNumber = await this.clients[appConfig.govCoreChainId].getBlock({
blockTag: 'safe',
});

const contracts = underlyingAssets.map((asset) => {
return {
Expand Down Expand Up @@ -121,9 +123,10 @@ export class DelegationService {
};
};

const totalPowers = await contract.contract.read.getPowersCurrent([
userAddress,
]);
const totalPowers = await contract.contract.read.getPowersCurrent(
[userAddress],
{ blockNumber: blockNumber.number },
);

const proposition = getPower(
totalPowers[1],
Expand Down

1 comment on commit 61695df

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit was deployed on ipfs

Please sign in to comment.