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(mobile): use local transactions for graph #13184

Closed
wants to merge 2 commits into from
Closed
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
3 changes: 2 additions & 1 deletion suite-common/graph/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
},
"dependencies": {
"@mobily/ts-belt": "^3.13.1",
"@reduxjs/toolkit": "1.9.5",
"@suite-common/fiat-services": "workspace:*",
"@suite-common/suite-config": "workspace:*",
"@suite-common/suite-utils": "workspace:*",
"@suite-common/transaction-cache-engine": "workspace:*",
"@suite-common/wallet-config": "workspace:*",
"@suite-common/wallet-core": "workspace:*",
"@suite-common/wallet-types": "workspace:*",
"@suite-common/wallet-utils": "workspace:*",
"@trezor/blockchain-link": "workspace:*",
"@trezor/connect": "workspace:*",
Expand Down
7 changes: 7 additions & 0 deletions suite-common/graph/redux.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { AsyncThunkAction } from '@reduxjs/toolkit';

declare module 'redux' {
export interface Dispatch {
<TThunk extends AsyncThunkAction<any, any, any>>(thunk: TThunk): ReturnType<TThunk>;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import { BigNumber } from '@trezor/utils/src/bigNumber';
import { AccountTransaction } from '@trezor/connect';
import { NetworkSymbol, getNetworkType } from '@suite-common/wallet-config';

import { AccountBalanceHistory } from '../types';
export type AccountBalanceHistory = {
time: number;
txs: number;
received: BigNumber;
sent: BigNumber;
sentToSelf: BigNumber;
};

const getAccountBalanceHistoryBTC = ({
transactions,
Expand Down Expand Up @@ -97,8 +103,8 @@ const getAccountBalanceHistoryRipple = ({
if (summaryMap.has(blockTime)) {
const summary = summaryMap.get(blockTime)!;
summary.txs += 1;
summary.received = amount.plus(receivedDrops);
summary.sent = amount.plus(sentDrops);
summary.received = summary.received.plus(receivedDrops);
summary.sent = summary.sent.plus(sentDrops);
} else {
summaryMap.set(blockTime, {
time: blockTime,
Expand All @@ -113,7 +119,7 @@ const getAccountBalanceHistoryRipple = ({
return Array.from(summaryMap.values()).sort((a, b) => a.time - b.time);
};

export const getAccountBalanceHistory = ({
export const getAccountBalanceHistoryFromTransactions = ({
transactions,
coin,
}: {
Expand Down
23 changes: 17 additions & 6 deletions suite-common/graph/src/graphBalanceEvents.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { useDispatch } from 'react-redux';

import { A, pipe } from '@mobily/ts-belt';
import { fromUnixTime, getUnixTime } from 'date-fns';

import { NetworkSymbol, getNetworkType } from '@suite-common/wallet-config';
import TrezorConnect from '@trezor/connect';
import { AccountBalanceHistory as AccountMovementHistory } from '@trezor/blockchain-link';
import {
AccountBalanceHistory,
TransactionCacheEngine,
} from '@suite-common/transaction-cache-engine';
import { fetchAllTransactionsForAccountThunk } from '@suite-common/wallet-core';

import { BalanceMovementEvent, GroupedBalanceMovementEvent, AccountItem } from './types';
import {
AccountBalanceHistory,
getAccountBalanceHistoryFromTransactions,
} from './balanceHistoryUtils';

/**
* Calculates received and sent values of each balance movement point.
Expand Down Expand Up @@ -107,18 +110,26 @@ export const getAccountMovementEvents = async ({
account,
startOfTimeFrameDate,
endOfTimeFrameDate,
dispatch,
}: {
account: AccountItem;
startOfTimeFrameDate: Date | null;
endOfTimeFrameDate: Date;
dispatch: ReturnType<typeof useDispatch>;
}) => {
const { coin, identity, descriptor } = account;

const getBalanceHistory = async () => {
if (getNetworkType(coin) === 'ripple') {
return TransactionCacheEngine.getAccountBalanceHistory({
const allTransactions = await dispatch(
matejkriz marked this conversation as resolved.
Show resolved Hide resolved
fetchAllTransactionsForAccountThunk({
accountKey: account.accountKey,
}),
).unwrap();

return getAccountBalanceHistoryFromTransactions({
transactions: allTransactions,
coin,
descriptor,
});
}
const connectBalanceHistory = await TrezorConnect.blockchainGetAccountBalanceHistory({
Expand Down
36 changes: 27 additions & 9 deletions suite-common/graph/src/graphDataFetching.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { useDispatch } from 'react-redux';

import { A, D, G, pipe } from '@mobily/ts-belt';
import { fromUnixTime, getUnixTime } from 'date-fns';

import { BigNumber } from '@trezor/utils/src/bigNumber';
import { getFiatRatesForTimestamps } from '@suite-common/fiat-services';
import { FiatCurrencyCode } from '@suite-common/suite-config';
import { NetworkSymbol, getNetworkType } from '@suite-common/wallet-config';
import { formatNetworkAmount } from '@suite-common/wallet-utils';
import { AccountBalanceHistory as AccountMovementHistory } from '@trezor/blockchain-link';
import TrezorConnect from '@trezor/connect';
import { getFiatRatesForTimestamps } from '@suite-common/fiat-services';
import {
AccountBalanceHistory,
TransactionCacheEngine,
} from '@suite-common/transaction-cache-engine';
import { BigNumber } from '@trezor/utils/src/bigNumber';
import { fetchAllTransactionsForAccountThunk } from '@suite-common/wallet-core';
import { AccountKey } from '@suite-common/wallet-types';

import { NUMBER_OF_POINTS } from './constants';
import {
Expand All @@ -22,6 +22,10 @@ import {
mergeMultipleFiatBalanceHistories,
} from './graphUtils';
import { AccountItem, FiatGraphPoint, FiatGraphPointWithCryptoBalance } from './types';
import {
AccountBalanceHistory,
getAccountBalanceHistoryFromTransactions,
} from './balanceHistoryUtils';

export const addBalanceForAccountMovementHistory = (
data: AccountMovementHistory[] | AccountBalanceHistory[],
Expand Down Expand Up @@ -87,12 +91,16 @@ const getAccountBalanceHistory = async ({
descriptor,
endOfTimeFrameDate,
forceRefetch,
dispatch,
accountKey,
}: {
coin: NetworkSymbol;
identity?: string;
descriptor: string;
endOfTimeFrameDate: Date;
forceRefetch?: boolean;
dispatch: ReturnType<typeof useDispatch>;
accountKey: AccountKey;
}): Promise<AccountHistoryBalancePoint[]> => {
const endTimeFrameTimestamp = getUnixTime(endOfTimeFrameDate);
const cacheKey = `${coin}-${descriptor}-${endTimeFrameTimestamp}`;
Expand All @@ -103,9 +111,15 @@ const getAccountBalanceHistory = async ({

const getBalanceHistory = async () => {
if (getNetworkType(coin) === 'ripple') {
return TransactionCacheEngine.getAccountBalanceHistory({
const allTransactions = await dispatch(
fetchAllTransactionsForAccountThunk({
accountKey,
}),
).unwrap();

return getAccountBalanceHistoryFromTransactions({
transactions: allTransactions,
coin,
descriptor,
});
}
const connectBalanceHistory = await TrezorConnect.blockchainGetAccountBalanceHistory({
Expand Down Expand Up @@ -205,6 +219,7 @@ export const getMultipleAccountBalanceHistoryWithFiat = async ({
fiatCurrency,
forceRefetch,
isElectrumBackend,
dispatch,
}: {
accounts: AccountItem[];
startOfTimeFrameDate: Date | null;
Expand All @@ -213,15 +228,18 @@ export const getMultipleAccountBalanceHistoryWithFiat = async ({
fiatCurrency: FiatCurrencyCode;
forceRefetch?: boolean;
isElectrumBackend: boolean;
dispatch: ReturnType<typeof useDispatch>;
}): Promise<FiatGraphPoint[] | FiatGraphPointWithCryptoBalance[]> => {
const accountsWithBalanceHistory = await Promise.all(
accounts.map(({ coin, descriptor, identity }) =>
accounts.map(({ coin, descriptor, identity, accountKey }) =>
getAccountBalanceHistory({
coin,
descriptor,
identity,
endOfTimeFrameDate,
forceRefetch,
dispatch,
accountKey,
})
.then(balanceHistory => ({
coin,
Expand Down
6 changes: 5 additions & 1 deletion suite-common/graph/src/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { useSelector } from 'react-redux';
import { useDispatch, useSelector } from 'react-redux';

import { roundToNearestMinutes, subHours } from 'date-fns';
import { A } from '@mobily/ts-belt';
Expand Down Expand Up @@ -93,6 +93,7 @@ export function useGraphForAccounts(params: useGraphForAccountsParams): {
const [error, setError] = useState<string | null>(null);
const isDiscoveryActive = useSelector(selectIsDeviceDiscoveryActive);
const isDeviceAuthorized = useSelector(selectIsDeviceAuthorized);
const dispatch = useDispatch();

const lastFetchTimestamp = useRef<number | null>(null);

Expand All @@ -118,6 +119,7 @@ export function useGraphForAccounts(params: useGraphForAccountsParams): {
endOfTimeFrameDate,
forceRefetch,
isElectrumBackend,
dispatch,
});

let events;
Expand All @@ -128,6 +130,7 @@ export function useGraphForAccounts(params: useGraphForAccountsParams): {
account: accounts[0],
startOfTimeFrameDate,
endOfTimeFrameDate,
dispatch,
});

normalizeExtremeGraphEvents(
Expand Down Expand Up @@ -159,6 +162,7 @@ export function useGraphForAccounts(params: useGraphForAccountsParams): {
isPortfolioGraph,
isDiscoveryActive,
isElectrumBackend,
dispatch,
],
);

Expand Down
Loading
Loading