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

feat: optimize transaction history request #1572

Merged
merged 4 commits into from
Oct 10, 2024
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
5 changes: 5 additions & 0 deletions .changeset/silent-hairs-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"fuels-wallet": minor
---

Reduce transaction history loading time by optimizing the complexity of the page info query.
18 changes: 18 additions & 0 deletions packages/app/src/systems/Core/utils/databaseVersioning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,22 @@ export const applyDbVersioning = (db: Dexie) => {
abis: '&contractId',
errors: '&id',
});

// DB VERSION 24
// Add transactionCursors page size column
db.version(24)
.stores({
vaults: 'key',
accounts: '&address, &name',
networks: '&id, &url, &name, chainId',
connections: 'origin',
transactionsCursors: '++id, address, size, providerUrl, endCursor',
assets: '&name, &symbol',
abis: '&contractId',
errors: '&id',
})
.upgrade(async (tx) => {
const transactionsCursors = tx.table('transactionsCursors');
await transactionsCursors.clear();
});
};
26 changes: 26 additions & 0 deletions packages/app/src/systems/Core/utils/graphql.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export const graphqlRequest = async <R, T = Record<string, unknown>>(
url: string,
operationName: string,
query: string,
variables: T
): Promise<R> => {
const res = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
operationName,
query,
variables,
}),
});

if (res.ok) {
const response = await res.json();
return response.data as R;
}

const error = await res.json();
return Promise.reject(error);
};
33 changes: 33 additions & 0 deletions packages/app/src/systems/Transaction/services/queries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export type GetPageInfoQuery = {
transactionsByOwner: {
pageInfo: {
hasPreviousPage: boolean;
hasNextPage: boolean;
startCursor: string;
endCursor: string;
};
};
};

export const getPageInfoQuery = `
query getTransactionsByOwner($owner: Address!, $after: String, $before: String, $first: Int, $last: Int) {
transactionsByOwner(
owner: $owner
after: $after
before: $before
first: $first
last: $last
) {
pageInfo {
...pageInfoFragment
}
}
}

fragment pageInfoFragment on PageInfo {
hasPreviousPage
hasNextPage
startCursor
endCursor
}
`;
21 changes: 14 additions & 7 deletions packages/app/src/systems/Transaction/services/transaction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { WalletLockedCustom, db } from '~/systems/Core';

import { createProvider } from '@fuel-wallet/connections';
import { AccountService } from '~/systems/Account/services/account';
import { graphqlRequest } from '~/systems/Core/utils/graphql';
import { NetworkService } from '~/systems/Network/services/network';
import type { TransactionCursor } from '../types';
import {
Expand All @@ -29,6 +30,7 @@ import {
setGasLimitToTxRequest,
} from '../utils';
import { getCurrentTips } from '../utils/fee';
import { type GetPageInfoQuery, getPageInfoQuery } from './queries';

export type TxInputs = {
getTxCursors: {
Expand Down Expand Up @@ -124,6 +126,9 @@ export class TxService {
.and((cursor) => {
return cursor.address.toLowerCase() === input.address.toLowerCase();
})
.and((cursor) => {
return cursor.size === TXS_PER_PAGE;
})
.sortBy('id');
});
}
Expand All @@ -134,6 +139,7 @@ export class TxService {
const transactionsCursors: TransactionCursor[] = cursors.map(
(endCursor) => ({
address,
size: TXS_PER_PAGE,
providerUrl,
endCursor,
})
Expand Down Expand Up @@ -338,21 +344,22 @@ export class TxService {
providerUrl = '',
initialEndCursor,
}: TxInputs['getAllCursors']) {
const provider = await createProvider(providerUrl);

let hasNextPage = true;
const cursors: string[] = [];
let endCursor: string | null | undefined = initialEndCursor;

while (hasNextPage) {
const { pageInfo } = await getTransactionsSummaries({
provider,
filters: {
const result: GetPageInfoQuery = await graphqlRequest(
providerUrl,
'getTransactionsByOwner',
getPageInfoQuery,
{
owner: address,
first: TXS_PER_PAGE,
after: endCursor,
},
});
}
);
const pageInfo = result.transactionsByOwner.pageInfo;

hasNextPage = pageInfo.hasNextPage;
endCursor = pageInfo.endCursor;
Expand Down
5 changes: 1 addition & 4 deletions packages/app/src/systems/Transaction/types.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { AssetAmount } from '@fuel-wallet/types';
import type {
AddressType,
CallResult,
Expand All @@ -9,9 +8,6 @@ import type {
OutputContractCreated,
TransactionRequestInput,
TransactionRequestLike,
TransactionResponse,
TransactionStatus,
TransactionType,
} from 'fuels';

export enum TxCategory {
Expand All @@ -38,6 +34,7 @@ export type TxOutputContractCreated = OutputContractCreated;

export type TransactionCursor = {
address: string;
size: number;
providerUrl: string;
endCursor: string;
};
Expand Down
Loading