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

fix: new recursive refresh txns mechanism and account info and txns are pooled together #47

Merged
merged 2 commits into from
Dec 12, 2023
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
4 changes: 2 additions & 2 deletions src/extension/apps/main/Root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ const Root: FC = () => {
if (accounts.length < 1) {
dispatch(
fetchAccountsFromStorageThunk({
updateAccountInformation: true,
updateAccountTransactions: true,
updateInformation: true,
updateTransactions: true,
})
);
}
Expand Down
18 changes: 5 additions & 13 deletions src/extension/components/SendAssetModal/SendAssetModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ import { ErrorCodeEnum } from '@extension/enums';
import { BaseExtensionError } from '@extension/errors';

// features
import {
updateAccountInformationThunk,
updateAccountTransactionsThunk,
} from '@extension/features/accounts';
import { updateAccountsThunk } from '@extension/features/accounts';
import { create as createNotification } from '@extension/features/notifications';
import {
setAmount,
Expand Down Expand Up @@ -403,17 +400,12 @@ const SendAssetModal: FC<IProps> = ({ onClose }: IProps) => {

// refresh the account transactions
if (fromAccount) {
// force update the account information as we spent fees
dispatch(
updateAccountInformationThunk({
accountIds: [fromAccount.id],
forceUpdate: true,
})
);
// force update the account information as we spent fees and refresh all the new transactions
dispatch(
updateAccountTransactionsThunk({
updateAccountsThunk({
accountIds: [fromAccount.id],
refresh: true,
forceInformationUpdate: true,
refreshTransactions: true,
})
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ const AssetFreezeTransactionContent: FC<IProps> = ({
(async () => {
let account: IAccount | null;
let accountInformation: IAccountInformation;
let encodedGenesisHash: string;

if (!freezeAddress) {
return;
Expand All @@ -186,11 +187,18 @@ const AssetFreezeTransactionContent: FC<IProps> = ({

setFetchingFreezeAccountInformation(true);

encodedGenesisHash = convertGenesisHashToHex(
network.genesisHash
).toUpperCase();
account = AccountService.initializeDefaultAccount({
publicKey:
AccountService.convertAlgorandAddressToPublicKey(freezeAddress),
});
accountInformation = await updateAccountInformation(account, {
accountInformation = await updateAccountInformation({
address: freezeAddress,
currentAccountInformation:
account.networkInformation[encodedGenesisHash] ||
AccountService.initializeDefaultAccountInformation(),
logger,
network,
});
Expand All @@ -199,8 +207,7 @@ const AssetFreezeTransactionContent: FC<IProps> = ({
...account,
networkInformation: {
...account.networkInformation,
[convertGenesisHashToHex(network.genesisHash).toUpperCase()]:
accountInformation,
[encodedGenesisHash]: accountInformation,
},
});
setFetchingFreezeAccountInformation(false);
Expand Down
13 changes: 12 additions & 1 deletion src/extension/components/SignTxnsModal/SignTxnsModalContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ const SignTxnsModalContent: FC<IProps> = ({
value.publicKey.toUpperCase() === encodedPublicKey.toUpperCase()
) || null;
let accountInformation: IAccountInformation;
let encodedGenesisHash: string;

// if we have this account, just return it
if (account) {
Expand All @@ -125,7 +126,17 @@ const SignTxnsModalContent: FC<IProps> = ({
account = AccountService.initializeDefaultAccount({
publicKey: encodedPublicKey,
});
accountInformation = await updateAccountInformation(account, {
encodedGenesisHash = convertGenesisHashToHex(
network.genesisHash
).toUpperCase();
accountInformation = await updateAccountInformation({
address:
AccountService.convertPublicKeyToAlgorandAddress(
encodedPublicKey
),
currentAccountInformation:
account.networkInformation[encodedGenesisHash] ||
AccountService.initializeDefaultAccountInformation(),
delay: index * NODE_REQUEST_DELAY,
logger,
network,
Expand Down
2 changes: 2 additions & 0 deletions src/extension/constants/Limits.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const DEFAULT_TRANSACTION_INDEXER_LIMIT: number = 20;
export const UPPER_TRANSACTION_INDEXER_LIMIT: number = 100;
1 change: 1 addition & 0 deletions src/extension/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from './Dimensions';
export * from './Durations';
export * from './Keys';
export * from './Links';
export * from './Limits';
export * from './Routes';
export * from './Urls';
export * from './Validation';
1 change: 1 addition & 0 deletions src/extension/enums/AccountsThunkEnum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ enum AccountsThunkEnum {
StartPollingForAccountInformation = 'accounts/startPollingForAccountInformation',
StopPollingForAccountInformation = 'accounts/stopPollingForAccountInformation',
UpdateAccountInformation = 'accounts/updateAccountInformation',
UpdateAccounts = 'accounts/updateAccounts',
UpdateAccountTransactions = 'accounts/updateAccountTransactions',
}

Expand Down
104 changes: 69 additions & 35 deletions src/extension/features/accounts/slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,25 @@ import {
saveNewAccountThunk,
startPollingForAccountInformationThunk,
stopPollingForAccountInformationThunk,
updateAccountInformationThunk,
updateAccountTransactionsThunk,
updateAccountsThunk,
} from './thunks';

// types
import { IAccount } from '@extension/types';
import { IAccountsState } from './types';
import {
IAccount,
IPendingActionMeta,
IRejectedActionMeta,
} from '@extension/types';
import {
IAccountsState,
IAccountUpdate,
IUpdateAccountsPayload,
} from './types';

// utils
import { upsertItemsById } from '@extension/utils';
import { getInitialState } from './utils';
import { stat } from 'copy-webpack-plugin/types/utils';

const slice = createSlice({
extraReducers: (builder) => {
Expand Down Expand Up @@ -96,50 +104,76 @@ const slice = createSlice({
state.pollingId = null;
}
);
/** update account information **/
/** update accounts **/
builder.addCase(
updateAccountInformationThunk.fulfilled,
updateAccountsThunk.fulfilled,
(state: IAccountsState, action: PayloadAction<IAccount[]>) => {
state.items = state.items.map(
(account) =>
action.payload.find((value) => value.id === account.id) || account
);
state.updatingInformation = false;
}
);
builder.addCase(
updateAccountInformationThunk.pending,
(state: IAccountsState) => {
state.updatingInformation = true;
}
);
builder.addCase(
updateAccountInformationThunk.rejected,
(state: IAccountsState) => {
state.updatingInformation = false;
}
);
/** update account transactions **/
builder.addCase(
updateAccountTransactionsThunk.fulfilled,
(state: IAccountsState, action: PayloadAction<IAccount[]>) => {
state.items = state.items.map(
(account) =>
action.payload.find((value) => value.id === account.id) || account

// remove all the updated accounts from the account update list
state.updatingAccounts = state.updatingAccounts.filter(
(accountUpdate) =>
!action.payload.find((value) => value.id === accountUpdate.id)
);
state.updatingTransactions = false;
}
);
builder.addCase(
updateAccountTransactionsThunk.pending,
(state: IAccountsState) => {
state.updatingTransactions = true;
updateAccountsThunk.pending,
(state: IAccountsState, action) => {
// if no account ids, all accounts are being updated
if (!action.meta.arg?.accountIds) {
state.updatingAccounts = state.items.map((value) => ({
id: value.id,
information: true,
transactions: !action.meta?.arg?.informationOnly,
}));

return;
}

// filter the accounts by the supplied ids
state.updatingAccounts = [
...(state.updatingAccounts = state.updatingAccounts.filter(
(accountUpdate) =>
!action.meta.arg?.accountIds?.find(
(value) => value === accountUpdate.id
)
)),
...(action.meta.arg?.accountIds?.map((value) => ({
id: value,
information: true,
transactions: !action.meta?.arg?.informationOnly,
})) || []),
];
}
);
builder.addCase(
updateAccountTransactionsThunk.rejected,
(state: IAccountsState) => {
state.updatingTransactions = false;
updateAccountsThunk.rejected,
(state: IAccountsState, action) => {
// if no account ids, no accounts are being updated
if (!action.meta.arg?.accountIds) {
state.updatingAccounts = [];

return;
}

// filter the accounts by the supplied ids
state.updatingAccounts = [
...(state.updatingAccounts = state.updatingAccounts.filter(
(accountUpdate) =>
!action.meta.arg?.accountIds?.find(
(value) => value === accountUpdate.id
)
)),
...(action.meta.arg?.accountIds?.map((value) => ({
id: value,
information: false,
transactions: false,
})) || []),
];
}
);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const fetchAccountsFromStorageThunk: AsyncThunk<
).toUpperCase();

// update the account information for selected network
if (options?.updateAccountInformation) {
if (options?.updateInformation) {
logger.debug(
`${AccountsThunkEnum.FetchAccountsFromStorage}: updating account information for "${selectedNetwork.genesisId}"`
);
Expand All @@ -67,7 +67,13 @@ const fetchAccountsFromStorageThunk: AsyncThunk<
...account,
networkInformation: {
...account.networkInformation,
[encodedGenesisHash]: await updateAccountInformation(account, {
[encodedGenesisHash]: await updateAccountInformation({
address: AccountService.convertPublicKeyToAlgorandAddress(
account.publicKey
),
currentAccountInformation:
account.networkInformation[encodedGenesisHash] ||
AccountService.initializeDefaultAccountInformation(),
delay: index * NODE_REQUEST_DELAY, // delay each request by 100ms from the last one, see https://algonode.io/api/#limits
logger,
network: selectedNetwork,
Expand All @@ -81,7 +87,7 @@ const fetchAccountsFromStorageThunk: AsyncThunk<
}

// update the accounts transactions for selected network
if (options?.updateAccountTransactions) {
if (options?.updateTransactions) {
logger.debug(
`${AccountsThunkEnum.FetchAccountsFromStorage}: updating account transactions for "${selectedNetwork.genesisId}"`
);
Expand All @@ -91,7 +97,14 @@ const fetchAccountsFromStorageThunk: AsyncThunk<
...account,
networkTransactions: {
...account.networkTransactions,
[encodedGenesisHash]: await updateAccountTransactions(account, {
[encodedGenesisHash]: await updateAccountTransactions({
address: AccountService.convertPublicKeyToAlgorandAddress(
account.publicKey
),
currentAccountTransactions:
account.networkTransactions[encodedGenesisHash] ||
AccountService.initializeDefaultAccountTransactions(),
delay: index * NODE_REQUEST_DELAY, // delay each request by 100ms from the last one, see https://algonode.io/api/#limits
logger,
network: selectedNetwork,
}),
Expand Down
3 changes: 1 addition & 2 deletions src/extension/features/accounts/thunks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ 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 updateAccountInformationThunk } from './updateAccountInformationThunk';
export { default as updateAccountTransactionsThunk } from './updateAccountTransactionsThunk';
export { default as updateAccountsThunk } from './updateAccountsThunk';
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ACCOUNT_INFORMATION_REFRESH_INTERVAL } from '@extension/constants';
import { AccountsThunkEnum } from '@extension/enums';

// thunks
import updateAccountInformationThunk from './updateAccountInformationThunk';
import updateAccountsThunk from './updateAccountsThunk';

// types
import { ILogger } from '@common/types';
Expand All @@ -27,7 +27,12 @@ const startPollingForAccountInformationThunk: AsyncThunk<
);

return window.setInterval(
() => dispatch(updateAccountInformationThunk()),
() =>
dispatch(
updateAccountsThunk({
informationOnly: true, // only update account information
})
),
ACCOUNT_INFORMATION_REFRESH_INTERVAL
); // update every 2 minutes
}
Expand Down
Loading
Loading