Skip to content

Commit

Permalink
override wallet connect rpcs to avoid been using the default rpcs pro…
Browse files Browse the repository at this point in the history
…vided on wallet connect (#2998)

* override wallet connect rpcs to avoid been using the default rpcs provided on wallet connect

* add undefined / null / empty string check

* lower case name to avoid comprehensive network list

* add non-null assertion to avoid compilation fail
  • Loading branch information
sebastianscatularo authored Nov 22, 2024
1 parent e0ac4a2 commit f4e09a8
Showing 1 changed file with 43 additions and 26 deletions.
69 changes: 43 additions & 26 deletions wormhole-connect/src/utils/wallet/evm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,60 @@ import config from 'config';
const isChainResourceKey = (key: string): key is keyof ChainResourceMap =>
Object.keys(config.rpcs).includes(key);

const CHAINS_CONFIG = Object.entries(DEFAULT_CHAINS).map(
([wagmiChainName, wagmiConfig]) => {
if (isChainResourceKey(wagmiChainName)) {
const rpc = config.rpcs[wagmiChainName];
if (rpc) {
return {
...wagmiConfig,
rpcUrls: {
...wagmiConfig.rpcUrls,
[wagmiChainName]: {
http: [rpc],
},
default: {
http: [rpc],
},
public: {
http: [rpc],
},
},
};
}
}
return wagmiConfig;
type ChainRpcUrls = (typeof DEFAULT_CHAINS)[0]['rpcUrls']['default'];

const getRpcForChain = (
wormholeChainName: string,
defaultRpc: ChainRpcUrls,
): ChainRpcUrls =>
wormholeChainName in config.rpcs &&
isChainResourceKey(wormholeChainName) &&
config.rpcs[wormholeChainName] !== null &&
config.rpcs[wormholeChainName] !== undefined &&
config.rpcs[wormholeChainName] !== ''
? { ...defaultRpc, http: [config.rpcs[wormholeChainName]!] }
: defaultRpc;

/**
* Should be used to coalesce a wagmi chain name to a wormhole chain name.
* This is necessary because the wormhole chain names are different from the wagmi chain names.
*
* @param name a wagmi chain name
* @returns a wormhole chain name
*
* NOTE: mapping could be incomplete
*/
const coalesceWormholeChainName = (name: string) =>
({
'bnb smart chain': 'bsc',
}[name] || name);

const WAGMI_CONFIG_FOR_CHAINS = DEFAULT_CHAINS.map((wagmiConfig) => ({
...wagmiConfig,
rpcUrls: {
...wagmiConfig.rpcUrls,
default: getRpcForChain(
coalesceWormholeChainName(wagmiConfig.name.toLowerCase()),
wagmiConfig.rpcUrls.default,
),
public: getRpcForChain(
coalesceWormholeChainName(wagmiConfig.name.toLowerCase()),
wagmiConfig.rpcUrls.public,
),
},
);
}));

export const wallets = {
injected: new InjectedWallet({
chains: CHAINS_CONFIG,
chains: WAGMI_CONFIG_FOR_CHAINS,
}),
binance: new BinanceWallet({
options: {},
}),
...(config.walletConnectProjectId
? {
walletConnect: new WalletConnectWallet({
chains: CHAINS_CONFIG,
chains: WAGMI_CONFIG_FOR_CHAINS,
connectorOptions: {
projectId: config.walletConnectProjectId,
},
Expand Down

0 comments on commit f4e09a8

Please sign in to comment.