Skip to content

Commit

Permalink
feat: polls for new transactions when also fetching updated account i…
Browse files Browse the repository at this point in the history
…nformation (#53)
  • Loading branch information
kieranroneill authored Dec 14, 2023
1 parent 9bea549 commit 3279431
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 38 deletions.
4 changes: 2 additions & 2 deletions src/extension/apps/main/Root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import WalletConnectModal from '@extension/components/WalletConnectModal';
// features
import {
fetchAccountsFromStorageThunk,
startPollingForAccountInformationThunk,
startPollingForAccountsThunk,
} from '@extension/features/accounts';
import {
fetchAssetsThunk,
Expand Down Expand Up @@ -88,7 +88,7 @@ const Root: FC = () => {
dispatch(fetchSessionsThunk());
dispatch(fetchAssetsThunk());
dispatch(initializeWalletConnectThunk());
dispatch(startPollingForAccountInformationThunk());
dispatch(startPollingForAccountsThunk());
dispatch(startPollingForTransactionsParamsThunk());
}, []);
// 2. when the selected network has been fetched from storage
Expand Down
6 changes: 2 additions & 4 deletions src/extension/enums/AccountsThunkEnum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ enum AccountsThunkEnum {
FetchAccountsFromStorage = 'accounts/fetchAccountsFromStorage',
RemoveAccountById = 'accounts/removeAccountById',
SaveNewAccount = 'accounts/saveNewAccount',
StartPollingForAccountInformation = 'accounts/startPollingForAccountInformation',
StopPollingForAccountInformation = 'accounts/stopPollingForAccountInformation',
UpdateAccountInformation = 'accounts/updateAccountInformation',
StartPollingForAccounts = 'accounts/startPollingForAccounts',
StopPollingForAccounts = 'accounts/stopPollingForAccounts',
UpdateAccounts = 'accounts/updateAccounts',
UpdateAccountTransactions = 'accounts/updateAccountTransactions',
}

export default AccountsThunkEnum;
8 changes: 4 additions & 4 deletions src/extension/features/accounts/slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {
fetchAccountsFromStorageThunk,
removeAccountByIdThunk,
saveNewAccountThunk,
startPollingForAccountInformationThunk,
stopPollingForAccountInformationThunk,
startPollingForAccountsThunk,
stopPollingForAccountsThunk,
updateAccountsThunk,
} from './thunks';

Expand Down Expand Up @@ -92,14 +92,14 @@ const slice = createSlice({
});
/** start polling for account information **/
builder.addCase(
startPollingForAccountInformationThunk.fulfilled,
startPollingForAccountsThunk.fulfilled,
(state: IAccountsState, action: PayloadAction<number>) => {
state.pollingId = action.payload;
}
);
/** stop polling for account information **/
builder.addCase(
stopPollingForAccountInformationThunk.fulfilled,
stopPollingForAccountsThunk.fulfilled,
(state: IAccountsState) => {
state.pollingId = null;
}
Expand Down
4 changes: 2 additions & 2 deletions src/extension/features/accounts/thunks/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export { default as fetchAccountsFromStorageThunk } from './fetchAccountsFromStorageThunk';
export { default as removeAccountByIdThunk } from './removeAccountByIdThunk';
export { default as saveNewAccountThunk } from './saveNewAccountThunk';
export { default as startPollingForAccountInformationThunk } from './startPollingForAccountInformationThunk';
export { default as stopPollingForAccountInformationThunk } from './stopPollingForAccountInformationThunk';
export { default as startPollingForAccountsThunk } from './startPollingForAccountsThunk';
export { default as stopPollingForAccountsThunk } from './stopPollingForAccountsThunk';
export { default as updateAccountsThunk } from './updateAccountsThunk';
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,30 @@ import updateAccountsThunk from './updateAccountsThunk';
import { ILogger } from '@common/types';
import { IMainRootState } from '@extension/types';

const startPollingForAccountInformationThunk: AsyncThunk<
const startPollingForAccountsThunk: AsyncThunk<
number, // return
undefined, // args
Record<string, never>
> = createAsyncThunk<number, undefined, { state: IMainRootState }>(
AccountsThunkEnum.StartPollingForAccountInformation,
AccountsThunkEnum.StartPollingForAccounts,
(_, { dispatch, getState }) => {
const logger: ILogger = getState().system.logger;

logger.debug(
`${AccountsThunkEnum.StartPollingForAccountInformation}: started polling for account information`
`${AccountsThunkEnum.StartPollingForAccounts}: started polling for account information and recent transactions`
);

return window.setInterval(
() =>
dispatch(
updateAccountsThunk({
informationOnly: true, // only update account information
informationOnly: false, // get account information
refreshTransactions: true, // get any new transactions
})
),
ACCOUNT_INFORMATION_REFRESH_INTERVAL
); // update every 2 minutes
}
);

export default startPollingForAccountInformationThunk;
export default startPollingForAccountsThunk;
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@ import { AccountsThunkEnum } from '@extension/enums';
import { ILogger } from '@common/types';
import { IMainRootState } from '@extension/types';

const stopPollingForAccountInformationThunk: AsyncThunk<
const stopPollingForAccountsThunk: AsyncThunk<
void, // return
undefined, // args
Record<string, never>
> = createAsyncThunk<void, undefined, { state: IMainRootState }>(
AccountsThunkEnum.StopPollingForAccountInformation,
AccountsThunkEnum.StopPollingForAccounts,
(_, { getState }) => {
const logger: ILogger = getState().system.logger;
const pollingId: number | null = getState().accounts.pollingId;

if (pollingId) {
logger.debug(
`${AccountsThunkEnum.StopPollingForAccountInformation}: stopped polling for account information`
`${AccountsThunkEnum.StopPollingForAccounts}: stopped polling for account information and recent transactions`
);

window.clearInterval(pollingId);
}
}
);

export default stopPollingForAccountInformationThunk;
export default stopPollingForAccountsThunk;
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface IOptions {
* @param {IOptions} options - options needed to send the request.
* @returns {IAlgorandAccountInformation} account information from the node.
*/
export default async function fetchAlgorandAccountInformationWithDelay({
export default async function algorandAccountInformationWithDelay({
address,
client,
delay,
Expand Down
4 changes: 2 additions & 2 deletions src/extension/features/accounts/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export { default as fetchAccountInformationWithDelay } from './fetchAlgorandAccountInformationWithDelay';
export { default as fetchAlgorandAccountTransactionsWithDelay } from './fetchAlgorandAccountTransactionsWithDelay';
export { default as algorandAccountInformationWithDelay } from './algorandAccountInformationWithDelay';
export { default as getInitialState } from './getInitialState';
export { default as lookupAlgorandAccountTransactionsWithDelay } from './lookupAlgorandAccountTransactionsWithDelay';
export { default as refreshTransactions } from './refreshTransactions';
export { default as updateAccountInformation } from './updateAccountInformation';
export { default as updateAccountTransactions } from './updateAccountTransactions';
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ interface IOptions {
}

/**
* Fetches account transactions from the node with a delay.
* Looks up the account transactions for a given address with a delay.
* @param {IOptions} options - options needed to send the request.
* @returns {IAlgorandAccountTransaction} account transactions from the node.
*/
export default async function fetchAlgorandAccountTransactionsWithDelay({
export default async function lookupAlgorandAccountTransactionsWithDelay({
address,
afterTime,
client,
Expand Down
4 changes: 2 additions & 2 deletions src/extension/features/accounts/utils/refreshTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {

// utils
import { mapAlgorandTransactionToTransaction } from '@extension/utils';
import fetchAlgorandAccountTransactionsWithDelay from './fetchAlgorandAccountTransactionsWithDelay';
import lookupAlgorandAccountTransactionsWithDelay from './lookupAlgorandAccountTransactionsWithDelay';

interface IOptions extends IBaseOptions {
address: string;
Expand Down Expand Up @@ -47,7 +47,7 @@ export default async function refreshTransactions({

try {
algorandAccountTransactions =
await fetchAlgorandAccountTransactionsWithDelay({
await lookupAlgorandAccountTransactionsWithDelay({
address,
afterTime,
client,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
// utils
import { getAlgodClient } from '@common/utils';
import { mapAlgorandAccountInformationToAccount } from '@extension/utils';
import fetchAlgorandAccountInformationWithDelay from './fetchAlgorandAccountInformationWithDelay';
import algorandAccountInformationWithDelay from './algorandAccountInformationWithDelay';

interface IOptions extends IBaseOptions {
address: string;
Expand Down Expand Up @@ -69,13 +69,11 @@ export default async function updateAccountInformation({
);

try {
algorandAccountInformation = await fetchAlgorandAccountInformationWithDelay(
{
address,
client,
delay,
}
);
algorandAccountInformation = await algorandAccountInformationWithDelay({
address,
client,
delay,
});
updatedAt = new Date();

logger &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
// utils
import { getIndexerClient } from '@common/utils';
import { mapAlgorandTransactionToTransaction } from '@extension/utils';
import fetchAlgorandAccountTransactionsWithDelay from './fetchAlgorandAccountTransactionsWithDelay';
import lookupAlgorandAccountTransactionsWithDelay from './lookupAlgorandAccountTransactionsWithDelay';
import refreshTransactions from './refreshTransactions';

interface IOptions extends IBaseOptions {
Expand Down Expand Up @@ -86,7 +86,7 @@ export default async function updateAccountTransactions({

try {
algorandAccountTransaction =
await fetchAlgorandAccountTransactionsWithDelay({
await lookupAlgorandAccountTransactionsWithDelay({
address,
afterTime: null,
client,
Expand Down

0 comments on commit 3279431

Please sign in to comment.