-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'release/3.0.0' into 1977-setup-electron-builder-actions
- Loading branch information
Showing
31 changed files
with
536 additions
and
104 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export const selectCurrentAccount = (state) => state.account.current; | ||
export const selectAccounts = (state) => state.account.list || []; | ||
export const selectAccountNonce = (state) => state.account.localNonce || {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { useEffect, useState, useCallback } from 'react'; | ||
import { useQueryClient } from '@tanstack/react-query'; | ||
import { useCurrentApplication } from '@blockchainApplication/manage/hooks'; | ||
import { useCurrentAccount, useAccounts } from '@account/hooks'; | ||
import useSettings from '@settings/hooks/useSettings'; | ||
import { AUTH } from 'src/const/queries'; | ||
import { useAuthConfig } from './queries'; | ||
|
||
// eslint-disable-next-line max-statements | ||
const useNonceSync = () => { | ||
const queryClient = useQueryClient(); | ||
const [currentApplication] = useCurrentApplication(); | ||
const [currentAccount] = useCurrentAccount(); | ||
const { setNonceByAccount, getNonceByAccount, resetNonceByAccount } = useAccounts(); | ||
const currentAccountAddress = currentAccount.metadata.address; | ||
const { mainChainNetwork } = useSettings('mainChainNetwork'); | ||
const chainID = currentApplication.chainID; | ||
const customConfig = { | ||
params: { | ||
address: currentAccountAddress, | ||
}, | ||
}; | ||
const serviceUrl = mainChainNetwork?.serviceUrl; | ||
const config = useAuthConfig(customConfig); | ||
const authData = queryClient.getQueryData([AUTH, chainID, config, serviceUrl]); | ||
const onChainNonce = authData?.data?.nonce ? BigInt(authData?.data.nonce) : BigInt('0'); | ||
const authNonce = typeof onChainNonce === 'bigint' ? onChainNonce.toString() : onChainNonce; | ||
|
||
const [accountNonce, setAccountNonce] = useState(onChainNonce.toString()); | ||
const currentAccountNonce = getNonceByAccount(currentAccountAddress); | ||
|
||
// Store nonce by address in accounts store | ||
const handleLocalNonce = (currentNonce) => { | ||
const storedNonce = BigInt(currentAccountNonce || 0); | ||
const localNonce = storedNonce < currentNonce ? currentNonce : storedNonce; | ||
const localNonceStr = localNonce.toString(); | ||
setNonceByAccount(currentAccountAddress, localNonceStr, 'defaultNonce'); | ||
|
||
setAccountNonce(localNonceStr); | ||
}; | ||
|
||
useEffect(() => { | ||
handleLocalNonce(onChainNonce); | ||
}, [onChainNonce]); | ||
|
||
// Increment nonce after transaction signing | ||
const incrementNonce = useCallback((transactionHex) => { | ||
const localNonce = BigInt(Math.max(currentAccountNonce, Number(accountNonce))) + BigInt(1); | ||
setNonceByAccount(currentAccountAddress, localNonce.toString(), transactionHex); | ||
}, []); | ||
|
||
const resetNonce = () => { | ||
resetNonceByAccount(currentAccountAddress, authNonce); | ||
}; | ||
|
||
return { accountNonce, onChainNonce: authNonce, incrementNonce, resetNonce }; | ||
}; | ||
|
||
export default useNonceSync; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import * as ReactQuery from '@tanstack/react-query'; | ||
import { queryWrapper as wrapper } from 'src/utils/test/queryWrapper'; | ||
import mockSavedAccounts from '@tests/fixtures/accounts'; | ||
import { useAccounts } from 'src/modules/account/hooks'; | ||
import { mockAuth } from '@auth/__fixtures__'; | ||
import useNonceSync from './useNonceSync'; | ||
|
||
const mockedCurrentAccount = mockSavedAccounts[0]; | ||
const mockSetNonceByAccount = jest.fn(); | ||
const mockModifiedMockAuth = { ...mockAuth, data: { ...mockAuth.data, nonce: '2' } }; | ||
|
||
jest.mock('@account/hooks/useCurrentAccount', () => ({ | ||
useCurrentAccount: jest.fn(() => [mockedCurrentAccount, jest.fn()]), | ||
})); | ||
jest.mock('@account/hooks/useAccounts'); | ||
jest.mock('@auth/hooks/queries/useAuth'); | ||
|
||
describe('useNonceSync', () => { | ||
it('renders properly', async () => { | ||
jest | ||
.spyOn(ReactQuery, 'useQueryClient') | ||
.mockReturnValue({ getQueryData: () => mockModifiedMockAuth }); | ||
useAccounts.mockReturnValue({ | ||
accounts: mockedCurrentAccount, | ||
setNonceByAccount: mockSetNonceByAccount, | ||
getNonceByAccount: jest.fn().mockReturnValue(3), | ||
}); | ||
const { result } = renderHook(() => useNonceSync(), { wrapper }); | ||
expect(result.current.accountNonce).toEqual('3'); | ||
result.current.incrementNonce(); | ||
expect(mockSetNonceByAccount).toHaveBeenCalled(); | ||
}); | ||
|
||
it('renders properly if auth nonce is undefined and no local nonce has been previously stored', () => { | ||
jest.spyOn(ReactQuery, 'useQueryClient').mockReturnValue({ getQueryData: () => {} }); | ||
useAccounts.mockReturnValue({ | ||
accounts: mockedCurrentAccount, | ||
setNonceByAccount: mockSetNonceByAccount, | ||
getNonceByAccount: jest.fn().mockReturnValue(undefined), | ||
}); | ||
const { result } = renderHook(() => useNonceSync(), { wrapper }); | ||
expect(result.current.accountNonce).toEqual('0'); | ||
}); | ||
|
||
it("updates local nonce if it's less than on-chain nonce", () => { | ||
jest | ||
.spyOn(ReactQuery, 'useQueryClient') | ||
.mockReturnValue({ getQueryData: () => mockModifiedMockAuth }); | ||
useAccounts.mockReturnValue({ | ||
accounts: mockedCurrentAccount, | ||
setNonceByAccount: mockSetNonceByAccount, | ||
getNonceByAccount: jest.fn().mockReturnValue(1), | ||
}); | ||
const { result } = renderHook(() => useNonceSync(), { wrapper }); | ||
expect(result.current.accountNonce).toEqual('2'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.