Skip to content

Commit

Permalink
Merge branch 'main' into fix/copy-address
Browse files Browse the repository at this point in the history
  • Loading branch information
svenvoskamp authored Dec 31, 2024
2 parents f358067 + 6284eb1 commit 09a6fd6
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 5 deletions.
23 changes: 23 additions & 0 deletions .changeset/great-feet-impress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
'@reown/appkit-scaffold-ui': patch
'@reown/appkit-common': patch
'@reown/appkit-core': patch
'@reown/appkit-adapter-bitcoin': patch
'@reown/appkit-adapter-ethers': patch
'@reown/appkit-adapter-ethers5': patch
'@reown/appkit-adapter-solana': patch
'@reown/appkit-adapter-wagmi': patch
'@reown/appkit': patch
'@reown/appkit-utils': patch
'@reown/appkit-cdn': patch
'@reown/appkit-cli': patch
'@reown/appkit-experimental': patch
'@reown/appkit-polyfills': patch
'@reown/appkit-siwe': patch
'@reown/appkit-siwx': patch
'@reown/appkit-ui': patch
'@reown/appkit-wallet': patch
'@reown/appkit-wallet-button': patch
---

Fixed an issue where the balance endpoint was being called every 30 seconds for unsupported networks.
5 changes: 4 additions & 1 deletion packages/common/src/utils/ConstantsUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,8 @@ export const ConstantsUtil = {
'0x55d398326f99059fF775485246999027B3197955',
// Arbitrum
'0xfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9'
]
],
HTTP_STATUS_CODES: {
SERVICE_UNAVAILABLE: 503
}
} as const
4 changes: 2 additions & 2 deletions packages/core/src/controllers/AccountController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,11 @@ export const AccountController = {
ChainController.setAccountProp('farcasterUrl', farcasterUrl, chain)
},

async fetchTokenBalance() {
async fetchTokenBalance(onError?: (error: unknown) => void) {
const chainId = ChainController.state.activeCaipNetwork?.caipNetworkId
const chain = ChainController.state.activeCaipNetwork?.chainNamespace
const caipAddress = ChainController.state.activeCaipAddress
const address = caipAddress ? CoreHelperUtil.getPlainAddress(caipAddress) : undefined

if (
state.lastRetry &&
!CoreHelperUtil.isAllowedRetry(state.lastRetry, 30 * ConstantsUtil.ONE_SEC_MS)
Expand All @@ -262,6 +261,7 @@ export const AccountController = {
} catch (error) {
state.lastRetry = Date.now()

onError?.(error)
SnackController.showError('Token Balance Unavailable')
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,20 @@ export class W3mAccountWalletFeaturesWidget extends LitElement {
}

private watchSwapValues() {
this.watchTokenBalance = setInterval(() => AccountController.fetchTokenBalance(), 10000)
this.watchTokenBalance = setInterval(
() => AccountController.fetchTokenBalance(error => this.onTokenBalanceError(error)),
10_000
)
}

private onTokenBalanceError(error: unknown) {
if (error instanceof Error && error.cause instanceof Response) {
const statusCode = error.cause.status

if (statusCode === CommonConstantsUtil.HTTP_STATUS_CODES.SERVICE_UNAVAILABLE) {
clearInterval(this.watchTokenBalance)
}
}
}

private listContentTemplate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import { fixture, elementUpdated } from '@open-wc/testing'
import { AccountController, CoreHelperUtil, RouterController } from '@reown/appkit-core'
import { html } from 'lit'
import { HelpersUtil } from '../utils/HelpersUtil'
import { ConstantsUtil as CommonConstantsUtil } from '@reown/appkit-common'

// --- Constants ---------------------------------------------------- //
const WALLET_FEATURE_WIDGET_TEST_ID = 'w3m-account-wallet-features-widget'
const MOCK_ADDRESS = '0xcd2a3d9f938e13cd947ec05abc7fe734df8dd826'
const PROFILE_BUTTON = 'w3m-profile-button'

const SERVICE_UNAVAILABLE_MESSAGE = 'Service Unavailable'

const ACCOUNT = {
namespace: 'eip155',
address: '0x123',
Expand All @@ -22,7 +25,7 @@ describe('W3mAccountWalletFeaturesWidget', () => {
})

afterEach(() => {
vi.clearAllMocks()
vi.resetAllMocks()
})

it('it should not return any components if address is not provided in AccountController', () => {
Expand Down Expand Up @@ -106,4 +109,36 @@ describe('W3mAccountWalletFeaturesWidget', () => {

expect(pushSpy).toHaveBeenCalledWith('Profile')
})

it('should clearInterval when fetchTokenBalance fails after 10 seconds', async () => {
vi.useFakeTimers()
vi.spyOn(global, 'setInterval')
vi.spyOn(global, 'clearInterval')
vi.spyOn(AccountController, 'state', 'get').mockReturnValue({
...AccountController.state,
address: ACCOUNT.address
})

const element: W3mAccountWalletFeaturesWidget = await fixture(
html`<w3m-account-wallet-features-widget></w3m-account-wallet-features-widget>`
)

expect(setInterval).toHaveBeenCalled()

const response = new Response(SERVICE_UNAVAILABLE_MESSAGE, {
status: CommonConstantsUtil.HTTP_STATUS_CODES.SERVICE_UNAVAILABLE
})

const error = new Error(SERVICE_UNAVAILABLE_MESSAGE, { cause: response })

vi.spyOn(AccountController, 'fetchTokenBalance').mockImplementation(async callback => {
callback?.(error)
})

vi.advanceTimersByTime(10_000)

expect(clearInterval).toHaveBeenCalledWith((element as any).watchTokenBalance)

vi.useRealTimers()
})
})

0 comments on commit 09a6fd6

Please sign in to comment.