Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* since AccessManager doesn't provide enumeration functions on-chain.
*/

import type { Address, Hex, PublicClient } from 'viem';
import type { Address, Chain, Hex, PublicClient } from 'viem';

import type { ExecutionConfig, OperationResult } from '@openzeppelin/ui-types';

Expand Down Expand Up @@ -73,6 +73,17 @@ export class EvmAccessManagerService implements AccessManagerService {
this.transactionExecutor = executor;
}

private getFallbackChain(): Chain | undefined {
if (!this.chainId) return undefined;

return {
id: this.chainId,
name: `Chain ${this.chainId}`,
nativeCurrency: { name: 'ETH', symbol: 'ETH', decimals: 18 },
rpcUrls: { default: { http: [] } },
};
}

/**
* Get the deployment block of a contract.
* Strategy: Sourcify (free, no rate limit) → Etherscan (with retry).
Expand Down Expand Up @@ -823,7 +834,7 @@ export class EvmAccessManagerService implements AccessManagerService {
}

// Strategy 2: fallback to injected provider (window.ethereum)
const { createWalletClient, custom, defineChain } = await import('viem');
const { createWalletClient, custom } = await import('viem');
const provider = (window as unknown as { ethereum?: unknown }).ethereum;
if (!provider) throw new Error('No wallet detected. Please connect a wallet.');

Expand Down Expand Up @@ -880,14 +891,7 @@ export class EvmAccessManagerService implements AccessManagerService {
}
}

const chain = this.chainId
? defineChain({
id: this.chainId,
name: `Chain ${this.chainId}`,
nativeCurrency: { name: 'ETH', symbol: 'ETH', decimals: 18 },
rpcUrls: { default: { http: [] } },
})
: undefined;
const chain = this.getFallbackChain();

const client = createWalletClient({
chain,
Expand Down Expand Up @@ -960,15 +964,8 @@ export class EvmAccessManagerService implements AccessManagerService {
};

// Include chain so wagmi/viem writeContract doesn't fail with "No chain provided"
if (this.chainId) {
const { defineChain } = await import('viem');
transactionData.chain = defineChain({
id: this.chainId,
name: `Chain ${this.chainId}`,
nativeCurrency: { name: 'ETH', symbol: 'ETH', decimals: 18 },
rpcUrls: { default: { http: [] } },
});
}
const fallbackChain = this.getFallbackChain();
if (fallbackChain) transactionData.chain = fallbackChain;

if (this.transactionExecutor) {
try {
Expand Down Expand Up @@ -996,7 +993,7 @@ export class EvmAccessManagerService implements AccessManagerService {
const hash = await walletClient.sendTransaction({
to: managerAddress as Address,
data,
chain: walletClient.chain ?? undefined,
chain: walletClient.chain ?? fallbackChain,
account: walletClient.account,
});
return { id: hash };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ describe('EvmAccessManagerService', () => {
expect(result).toEqual({ id: '0xsubmittedhash' });
expect(onStatus).toHaveBeenCalledWith('pendingSignature', {});
expect(walletClient.sendTransaction).toHaveBeenCalledTimes(1);
expect(walletClient.sendTransaction).toHaveBeenCalledWith(
expect.objectContaining({
chain: expect.objectContaining({ id: 1 }),
})
);
expect(publicClient.waitForTransactionReceipt).not.toHaveBeenCalled();
});

Expand Down
Loading