Skip to content

Commit

Permalink
pass connected to hardhat as network object prop
Browse files Browse the repository at this point in the history
  • Loading branch information
estebanmino committed Aug 29, 2024
1 parent e04727d commit 9e591cc
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 11 deletions.
11 changes: 11 additions & 0 deletions android/.project
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,15 @@
<natures>
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
</natures>
<filteredResources>
<filter>
<id>1724950860383</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>
15 changes: 10 additions & 5 deletions src/networks/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChainId } from '@/networks/types';
import { ChainId, NetworkProperties } from '@/networks/types';
import store from '@/redux/store';
import * as ls from '@/storage';
import { getArbitrumNetworkObject } from './arbitrum';
Expand All @@ -12,7 +12,6 @@ import { getGoerliNetworkObject } from './goerli';
import { getMainnetNetworkObject } from './mainnet';
import { getOptimismNetworkObject } from './optimism';
import { getPolygonNetworkObject } from './polygon';
import { NetworkProperties } from './types';
import { getZoraNetworkObject } from './zora';

/**
Expand Down Expand Up @@ -52,11 +51,17 @@ export const RainbowSupportedChainIds = [
/**
* Helper function to get specific Rainbow Network's Object
*/
export function getNetworkObject({ chainId }: { chainId: ChainId }): NetworkProperties {
export function getNetworkObject({
chainId,
connectedToHardhat = false,
}: {
chainId: ChainId;
connectedToHardhat?: boolean;
}): NetworkProperties {
switch (chainId) {
// Mainnet
case ChainId.mainnet:
return getMainnetNetworkObject();
return getMainnetNetworkObject({ connectedToHardhat });

// L2s
case ChainId.arbitrum:
Expand Down Expand Up @@ -85,7 +90,7 @@ export function getNetworkObject({ chainId }: { chainId: ChainId }): NetworkProp

// Fallback
default:
return getMainnetNetworkObject();
return getMainnetNetworkObject({ connectedToHardhat });
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/networks/mainnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import { gasUtils } from '@/utils';
import { mainnet } from '@wagmi/chains';
import { ETH_ADDRESS } from '@/references';
import { getRemoteConfig } from '@/model/remoteConfig';
import { useConnectedToHardhatStore } from '@/state/connectedToHardhat';

export const getMainnetNetworkObject = (): NetworkProperties => {
export const getMainnetNetworkObject = (opts: { connectedToHardhat?: boolean } = {}): NetworkProperties => {
const { mainnet_enabled, mainnet_tx_enabled } = getRemoteConfig();
return {
// wagmi chain data
Expand All @@ -26,7 +25,7 @@ export const getMainnetNetworkObject = (): NetworkProperties => {
},

getProvider: () => getProvider({ chainId: ChainId.mainnet }),
rpc: () => (useConnectedToHardhatStore.getState().connectedToHardhat ? 'http://127.0.0.1:8545' : proxyRpcEndpoint(mainnet.id)),
rpc: () => (opts?.connectedToHardhat ? 'http://127.0.0.1:8545' : proxyRpcEndpoint(mainnet.id)),
balanceCheckerAddress: '0x4dcf4562268dd384fe814c00fad239f06c2a0c2b',

// features
Expand Down
2 changes: 0 additions & 2 deletions src/references/chain-assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"fallback": "#E8EAF5",
"primary": "#808088"
},
"chainId": 5,
"decimals": 18,
"icon_url": "https://s3.amazonaws.com/icons.assets/ETH.png",
"name": "Goerli",
Expand Down Expand Up @@ -39,7 +38,6 @@
"fallback": "#E8EAF5",
"primary": "#808088"
},
"chainId": 1,
"decimals": 18,
"icon_url": "https://s3.amazonaws.com/icons.assets/ETH.png",
"name": "Ethereum",
Expand Down
7 changes: 6 additions & 1 deletion src/resources/assets/hardhatAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import { AddressZero } from '@ethersproject/constants';
import chainAssetsByChainId from '@/references/testnet-assets-by-chain';
import { getNetworkObject } from '@/networks';
import { ChainId, ChainName, Network } from '@/networks/types';
import { useConnectedToHardhatStore } from '@/state/connectedToHardhat';

const fetchHardhatBalancesWithBalanceChecker = async (
tokens: string[],
address: string,
chainId: ChainId = ChainId.mainnet
): Promise<{ [tokenAddress: string]: string } | null> => {
const networkObject = getNetworkObject({ chainId });
const connectedToHardhat = useConnectedToHardhatStore.getState().connectedToHardhat;
const networkObject = getNetworkObject({ chainId, connectedToHardhat });
const balanceCheckerContract = new Contract(networkObject.balanceCheckerAddress, balanceCheckerContractAbi, web3Provider);
try {
const values = await balanceCheckerContract.balances([address], tokens);
Expand Down Expand Up @@ -47,6 +49,8 @@ export const fetchHardhatBalances = async (accountAddress: string, chainId: Chai
chainAssets[`${chainId}` as keyof typeof chainAssets],
({ asset }) => `${asset.asset_code}_${asset.chainId}`
);
console.log('chainAssetsMap', chainAssetsMap);
console.log('chainAssetsMap val', Object.values(chainAssetsMap)[0]);

const tokenAddresses = Object.values(chainAssetsMap).map(({ asset: { asset_code } }) =>
asset_code === ETH_ADDRESS ? AddressZero : asset_code.toLowerCase()
Expand All @@ -60,6 +64,7 @@ export const fetchHardhatBalances = async (accountAddress: string, chainId: Chai
asset: {
...chainAsset.asset,
network: chainAsset.asset.network as Network,
chainId: chainAsset.asset.chainId,
},
quantity: balances[assetCode],
};
Expand Down

0 comments on commit 9e591cc

Please sign in to comment.