Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: prevent blockchain api calls on testnets #3494

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion packages/appkit/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1584,6 +1584,10 @@ export class AppKit {
return
}

if (caipNetwork.testnet) {
return
}

const balance = await adapter?.getBalance({
address: params.address,
chainId: params.chainId,
Expand Down Expand Up @@ -1645,7 +1649,8 @@ export class AppKit {
}: Pick<AdapterBlueprint.ConnectResult, 'address' | 'chainId'> & {
chainNamespace: ChainNamespace
}) {
if (chainNamespace !== 'eip155') {
const chain = this.caipNetworks?.find(n => n.caipNetworkId === `${chainNamespace}:${chainId}`)
if (chainNamespace !== 'eip155' || chain?.testnet) {
return
}
try {
Expand Down
76 changes: 76 additions & 0 deletions packages/appkit/src/tests/appkit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,82 @@ describe('Base', () => {
expect(BlockchainApiController.fetchIdentity).toHaveBeenCalledOnce()
})

it('should not sync identity on non-evm network', async () => {
const mockAccountData = {
address: '0x123',
chainId: '1',
chainNamespace: 'solana' as const
}

await appKit['syncAccount'](mockAccountData)

expect(BlockchainApiController.fetchIdentity).not.toHaveBeenCalled()
})

it('should not sync identity on a test network', async () => {
const mockAccountData = {
address: '0x123',
chainId: '1',
chainNamespace: 'eip155' as const
}

await appKit['syncAccount'](mockAccountData)

expect(BlockchainApiController.fetchIdentity).not.toHaveBeenCalled()
})

it('should sync balance correctly', async () => {
const mockAccountData = {
address: '0x123',
chainId: '1',
chainNamespace: 'eip155' as const
}

const mockAdapter = {
getAccounts: vi.fn().mockResolvedValue([]),
syncConnection: vi.fn(),
getBalance: vi.fn().mockResolvedValue({ balance: '0', symbol: 'ETH' }),
getProfile: vi.fn().mockResolvedValue({}),
on: vi.fn(),
off: vi.fn(),
emit: vi.fn()
}

vi.spyOn(appKit as any, 'getAdapter').mockReturnValue(mockAdapter)

vi.spyOn(AccountController, 'state', 'get').mockReturnValue(mockAccountData as any)

await appKit['syncAccount'](mockAccountData)

expect(mockAdapter.getBalance).toHaveBeenCalledWith('0x123', 'eip155')
})

it('should not sync balance on testnets', async () => {
const mockAccountData = {
address: '0x123',
chainId: '11155111',
chainNamespace: 'eip155' as const
}

const mockAdapter = {
getAccounts: vi.fn().mockResolvedValue([]),
syncConnection: vi.fn(),
getBalance: vi.fn().mockResolvedValue({ balance: '0', symbol: 'ETH' }),
getProfile: vi.fn().mockResolvedValue({}),
on: vi.fn(),
off: vi.fn(),
emit: vi.fn()
}

vi.spyOn(appKit as any, 'getAdapter').mockReturnValue(mockAdapter)

vi.spyOn(AccountController, 'state', 'get').mockReturnValue(mockAccountData as any)

await appKit['syncAccount'](mockAccountData)

expect(mockAdapter.getBalance).not.toHaveBeenCalled()
})

it('should disconnect correctly', async () => {
vi.mocked(ChainController).state = {
chains: new Map([['eip155', { namespace: 'eip155' }]]),
Expand Down
Loading