Skip to content

Commit

Permalink
feat: return custom rpc when transports is used (#3421)
Browse files Browse the repository at this point in the history
Co-authored-by: tomiir <[email protected]>
  • Loading branch information
svenvoskamp and tomiir authored Dec 10, 2024
1 parent 8147db9 commit 50c619a
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 13 deletions.
21 changes: 21 additions & 0 deletions .changeset/soft-toes-promise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
'@reown/appkit-adapter-wagmi': patch
'@reown/appkit-utils': patch
'@reown/appkit-adapter-ethers': patch
'@reown/appkit-adapter-ethers5': patch
'@reown/appkit-adapter-solana': patch
'@reown/appkit': patch
'@reown/appkit-cdn': patch
'appkit-cli': patch
'@reown/appkit-common': patch
'@reown/appkit-core': patch
'@reown/appkit-experimental': patch
'@reown/appkit-polyfills': patch
'@reown/appkit-scaffold-ui': patch
'@reown/appkit-siwe': patch
'@reown/appkit-siwx': patch
'@reown/appkit-ui': patch
'@reown/appkit-wallet': patch
---

Return custom RPC when configured in wagmi config
19 changes: 12 additions & 7 deletions packages/adapters/wagmi/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,27 @@ export class WagmiAdapter extends AdapterBlueprint {
projectId: configParams.projectId,
networks: CaipNetworksUtil.extendCaipNetworks(configParams.networks, {
projectId: configParams.projectId,
customNetworkImageUrls: {}
customNetworkImageUrls: {},
customRpcChainIds: configParams.transports
? Object.keys(configParams.transports).map(Number)
: []
}) as [CaipNetwork, ...CaipNetwork[]]
})

this.namespace = CommonConstantsUtil.CHAIN.EVM

this.createConfig({
...configParams,
networks: CaipNetworksUtil.extendCaipNetworks(configParams.networks, {
projectId: configParams.projectId,
customNetworkImageUrls: {}
customNetworkImageUrls: {},
customRpcChainIds: configParams.transports
? Object.keys(configParams.transports).map(Number)
: []
}) as [CaipNetwork, ...CaipNetwork[]],
projectId: configParams.projectId
})

this.setupWatchers()
}

Expand Down Expand Up @@ -121,11 +130,7 @@ export class WagmiAdapter extends AdapterBlueprint {
projectId: string
}
) {
this.caipNetworks = CaipNetworksUtil.extendCaipNetworks(configParams.networks, {
projectId: configParams.projectId,
customNetworkImageUrls: {}
}) as [CaipNetwork, ...CaipNetwork[]]

this.caipNetworks = configParams.networks
this.wagmiChains = this.caipNetworks.filter(
caipNetwork => caipNetwork.chainNamespace === CommonConstantsUtil.CHAIN.EVM
) as unknown as [BaseNetwork, ...BaseNetwork[]]
Expand Down
22 changes: 21 additions & 1 deletion packages/adapters/wagmi/src/tests/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {
writeContract as wagmiWriteContract,
waitForTransactionReceipt,
getAccount,
watchPendingTransactions
watchPendingTransactions,
http
} from '@wagmi/core'
import { mainnet } from '@wagmi/core/chains'
import { CaipNetworksUtil } from '@reown/appkit-utils'
Expand Down Expand Up @@ -112,6 +113,25 @@ describe('WagmiAdapter', () => {

expect(injectedConnector?.info).toBeUndefined()
})

it('should return reown RPC by default', () => {
expect(adapter.wagmiChains?.[0].rpcUrls.default.http[0]).toBe(
`https://rpc.walletconnect.org/v1/?chainId=eip155%3A1&projectId=${mockProjectId}`
)
})
it('should return custom RPC if transports is provided', () => {
const adapterWithCustomRpc = new WagmiAdapter({
networks: mockNetworks,
projectId: mockProjectId,
transports: {
[mainnet.id]: http('https://cloudflare-eth.com')
}
})

expect(adapterWithCustomRpc.wagmiChains?.[0].rpcUrls.default.http[0]).toBe(
`https://cloudflare-eth.com`
)
})
})

describe('WagmiAdapter - signMessage', () => {
Expand Down
26 changes: 21 additions & 5 deletions packages/appkit-utils/src/CaipNetworkUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ const WC_HTTP_RPC_SUPPORTED_CHAINS = [
type ExtendCaipNetworkParams = {
customNetworkImageUrls: Record<number | string, string> | undefined
projectId: string
customRpc?: boolean
}

export const CaipNetworksUtil = {
Expand Down Expand Up @@ -109,7 +110,8 @@ export const CaipNetworksUtil = {
return `${ConstantsUtil.CHAIN.EVM}:${network.id}` as CaipNetworkId
},

getRpcUrl(caipNetwork: AppKitNetwork, caipNetworkId: CaipNetworkId, projectId: string) {
// eslint-disable-next-line max-params
getDefaultRpcUrl(caipNetwork: AppKitNetwork, caipNetworkId: CaipNetworkId, projectId: string) {
const defaultRpcUrl = caipNetwork.rpcUrls?.default?.http?.[0]

if (WC_HTTP_RPC_SUPPORTED_CHAINS.includes(caipNetworkId)) {
Expand All @@ -126,15 +128,24 @@ export const CaipNetworksUtil = {
* @param params.networkImageIds - The network image IDs
* @param params.customNetworkImageUrls - The custom network image URLs
* @param params.projectId - The project ID
* @param params.customRpc - Boolean to indicate if the custom RPC URL should be used
* @returns The extended array of CaipNetwork objects
*/
extendCaipNetwork(
caipNetwork: AppKitNetwork,
{ customNetworkImageUrls, projectId }: ExtendCaipNetworkParams
{ customNetworkImageUrls, projectId, customRpc }: ExtendCaipNetworkParams
): CaipNetwork {
const caipNetworkId = this.getCaipNetworkId(caipNetwork)
const chainNamespace = this.getChainNamespace(caipNetwork)
const rpcUrl = this.getRpcUrl(caipNetwork, caipNetworkId, projectId)

let rpcUrl = ''
if (customRpc) {
// If custom RPC is enabled, use the original RPC URL
rpcUrl = caipNetwork.rpcUrls.default.http?.[0] || ''
} else {
// If custom RPC is not enabled, get the default Reown RPC URL
rpcUrl = this.getDefaultRpcUrl(caipNetwork, caipNetworkId, projectId)
}

return {
...caipNetwork,
Expand Down Expand Up @@ -168,12 +179,17 @@ export const CaipNetworksUtil = {
*/
extendCaipNetworks(
caipNetworks: AppKitNetwork[],
{ customNetworkImageUrls, projectId }: ExtendCaipNetworkParams
{
customNetworkImageUrls,
projectId,
customRpcChainIds
}: ExtendCaipNetworkParams & { customRpcChainIds?: number[] }
) {
return caipNetworks.map(caipNetwork =>
CaipNetworksUtil.extendCaipNetwork(caipNetwork, {
customNetworkImageUrls,
projectId
projectId,
customRpc: customRpcChainIds?.includes(caipNetwork.id as number)
})
) as [CaipNetwork, ...CaipNetwork[]]
},
Expand Down

0 comments on commit 50c619a

Please sign in to comment.