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

TW-1587: [research] Activity History on Alchemy #1228

Draft
wants to merge 3 commits into
base: TW-1479-evm-transactions-history
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
"@types/webextension-polyfill": "^0.9.1",
"@types/webpack": "^5",
"@types/ws": "^7.4.6",
"alchemy-sdk": "^3.4.8",
"assert": "1.5.0",
"async-mutex": "^0.4.0",
"async-retry": "1.3.3",
Expand Down
27 changes: 7 additions & 20 deletions src/app/templates/activity/EvmActivityList.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import React, { FC, useMemo } from 'react';

import { AxiosError } from 'axios';

import { DeadEndBoundaryError } from 'app/ErrorBoundary';
import { useLoadPartnersPromo } from 'app/hooks/use-load-partners-promo';
import { EvmActivity } from 'lib/activity';
import { getEvmAssetTransactions } from 'lib/activity/evm';
import { useSafeState } from 'lib/ui/hooks';
import { getEvmActivities } from 'lib/activity/evm/fetch';
import { useAccountAddressForEvm } from 'temple/front';
import { useEvmChainByChainId } from 'temple/front/chains';

Expand All @@ -30,43 +27,33 @@ export const EvmActivityList: FC<Props> = ({ chainId, assetSlug, filterKind }) =

useLoadPartnersPromo();

const [nextPage, setNextPage] = useSafeState<number | nullish>(undefined);

const { activities, isLoading, reachedTheEnd, setActivities, setIsLoading, setReachedTheEnd, loadNext } =
useActivitiesLoadingLogic<EvmActivity>(
async (initial, signal) => {
const page = initial ? undefined : nextPage;
if (page === null) return;
if (reachedTheEnd) return;

setIsLoading(true);

const currActivities = initial ? [] : activities;

try {
const { activities: newActivities, nextPage: newNextPage } = await getEvmAssetTransactions(
accountAddress,
chainId,
assetSlug,
page,
signal
);
const olderThanBlockHeight = activities.at(activities.length - 1)?.blockHeight;

const newActivities = await getEvmActivities(chainId, accountAddress, olderThanBlockHeight, signal);

if (signal.aborted) return;

setActivities(currActivities.concat(newActivities));
setNextPage(newNextPage);
if (newNextPage === null || newActivities.length === 0) setReachedTheEnd(true);
if (newActivities.length === 0) setReachedTheEnd(true);
} catch (error) {
if (signal.aborted) return;

console.error(error);
if (error instanceof AxiosError && error.status === 501) setReachedTheEnd(true);
}

setIsLoading(false);
},
[chainId, accountAddress, assetSlug],
() => setNextPage(undefined)
[chainId, accountAddress, assetSlug]
);

const displayActivities = useMemo(
Expand Down
30 changes: 9 additions & 21 deletions src/app/templates/activity/MultichainList.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import React, { memo, useMemo } from 'react';

import { AxiosError } from 'axios';

import { useLoadPartnersPromo } from 'app/hooks/use-load-partners-promo';
import { Activity, EvmActivity, TezosActivity } from 'lib/activity';
import { getEvmAssetTransactions } from 'lib/activity/evm';
import { getEvmActivities } from 'lib/activity/evm/fetch';
import { parseTezosOperationsGroup } from 'lib/activity/tezos';
import fetchTezosOperationsGroups from 'lib/activity/tezos/fetch';
import { TzktApiChainId } from 'lib/apis/tzkt';
Expand Down Expand Up @@ -163,48 +161,38 @@ function isTezosActivity(activity: Activity): activity is TezosActivity {

class EvmActivityLoader {
activities: EvmActivity[] = [];
reachedTheEnd = false;
lastError: unknown;
private nextPage: number | nullish;

constructor(readonly chainId: number, readonly accountAddress: string) {}

get reachedTheEnd() {
return this.nextPage === null;
}

async loadNext(edgeDate: string | undefined, signal: AbortSignal, assetSlug?: string) {
if (edgeDate) {
const lastAct = this.activities.at(-1);
if (lastAct && lastAct.addedAt > edgeDate) return;
}

try {
const { accountAddress, chainId, nextPage } = this;
const { accountAddress, chainId } = this;

if (nextPage === null) return;
if (this.reachedTheEnd || this.lastError) return;

const { nextPage: newNextPage, activities: newActivities } = await getEvmAssetTransactions(
accountAddress,
chainId,
assetSlug,
nextPage,
signal
);
const olderThanBlockHeight = this.activities.at(this.activities.length - 1)?.blockHeight;

if (signal.aborted) return;
const newActivities = await getEvmActivities(chainId, accountAddress, olderThanBlockHeight, signal);

this.nextPage = newNextPage;
if (signal.aborted) return;

if (newActivities.length) this.activities = this.activities.concat(newActivities);
else this.reachedTheEnd = true;

delete this.lastError;
} catch (error) {
if (signal.aborted) return;

console.error(error);

if (error instanceof AxiosError && error.status === 501) this.nextPage = null;
else this.lastError = error;
this.lastError = error;
}
}
}
Expand Down
227 changes: 226 additions & 1 deletion src/lib/activity/evm/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
// TODO: Make requests via axios, not SDK
import {
Alchemy,
AssetTransfersCategory,
Network,
AssetTransfersWithMetadataParams,
SortingOrder,
AssetTransfersWithMetadataResult
} from 'alchemy-sdk';
import { groupBy, uniqBy } from 'lodash';

import { getEvmERC20Transfers, getEvmTransactions } from 'lib/apis/temple/endpoints/evm';
import { fromAssetSlug } from 'lib/assets';
import { EVM_TOKEN_SLUG } from 'lib/assets/defaults';
import { TempleChainKind } from 'temple/types';

import { EvmActivity } from '../types';
import { ActivityStatus, EvmActivity } from '../types';

import { parseGoldRushTransaction, parseGoldRushERC20Transfer } from './parse';
import { parseApprovalLog, parseTransfer } from './parse/alchemy';

export async function getEvmAssetTransactions(
walletAddress: string,
Expand Down Expand Up @@ -57,3 +70,215 @@ export async function getEvmAssetTransactions(
nextPage
};
}

export async function getEvmActivities(
chainId: number,
accAddress: string,
olderThanBlockHeight?: `${number}`,
signal?: AbortSignal
) {
const chainName = CHAINS_NAMES[chainId];
if (!chainName) return [];

const accAddressLowercased = accAddress.toLowerCase();

const allTransfers = await fetchTransfers(chainName, accAddress, olderThanBlockHeight, signal);
if (!allTransfers.length) return [];

const allApprovals = await fetchApprovals(
chainName,
accAddress,
olderThanBlockHeight,
// Loading approvals withing the gap of received transfers.
// TODO: Mind the case of reaching response items number limit & not reaching block heights gap.
allTransfers.at(allTransfers.length - 1)?.blockNum
);

const groups = Object.entries(groupBy(allTransfers, 'hash'));

return groups.map<EvmActivity>(([hash, transfers]) => {
const firstTransfer = transfers.at(0)!;

const approvals = allApprovals.filter(a => a.transactionHash === hash).map(approval => parseApprovalLog(approval));

const operations = transfers.map(transfer => parseTransfer(transfer, accAddressLowercased)).concat(approvals);

return {
chain: TempleChainKind.EVM,
chainId,
hash,
status: ActivityStatus.applied, // TODO: Differentiate - how?
addedAt: firstTransfer.metadata.blockTimestamp,
operations,
operationsCount: operations.length,
blockHeight: `${Number(firstTransfer.blockNum)}`
};
});
}

async function fetchTransfers(
chainName: Network,
accAddress: string,
olderThanBlockHeight?: `${number}`,
signal?: AbortSignal
): Promise<AssetTransfersWithMetadataResult[]> {
const alchemy = new Alchemy({
apiKey: process.env._ALCHEMY_API_KEY, // TODO: To EnvVars
network: chainName
});

const [transfersFrom, transfersTo] = await Promise.all([
_fetchTransfers(alchemy, accAddress, false, olderThanBlockHeight),
_fetchTransfers(alchemy, accAddress, true, olderThanBlockHeight)
]);

const allTransfers = transfersFrom
.concat(transfersTo)
.toSorted((a, b) => (a.metadata.blockTimestamp < b.metadata.blockTimestamp ? 1 : -1));

if (!allTransfers.length) return [];

const sameTrailingHashes = calcSameTrailingHashes(allTransfers);

/** Will need to filter those transfers, that r made from & to the same address */
const uniqByKey: keyof (typeof allTransfers)[number] = 'uniqueId';

if (sameTrailingHashes === allTransfers.length) {
// const moreTransfers = []; // TODO: Fetch more transfers until reach another hash ?
// return uniqBy(allTransfers.concat(moreTransfers), uniqByKey);
}

allTransfers.splice(allTransfers.length - sameTrailingHashes, sameTrailingHashes);

return uniqBy(allTransfers, uniqByKey);
}

async function _fetchTransfers(
alchemy: Alchemy,
accAddress: string,
toAcc = false,
olderThanBlockHeight?: `${number}`
) {
const categories = new Set(Object.values(AssetTransfersCategory));
const excludedCategory = EXCLUDED_CATEGORIES[alchemy.config.network];
if (excludedCategory) categories.delete(excludedCategory);

const reqOptions: AssetTransfersWithMetadataParams = {
order: SortingOrder.DESCENDING,
category: Array.from(categories),
excludeZeroValue: true,
withMetadata: true,
toBlock: olderThanBlockToToBlockValue(olderThanBlockHeight),
maxCount: 50
};

if (toAcc) reqOptions.toAddress = accAddress;
else reqOptions.fromAddress = accAddress;

// TODO: Seems like Alchemy SDK processes Error 429 itself
return alchemy.core.getAssetTransfers(reqOptions).then(r => r.transfers);
}

function calcSameTrailingHashes(transfers: AssetTransfersWithMetadataResult[]) {
const trailingHash = transfers.at(transfers.length - 1)!.hash;
if (transfers.at(0)!.hash === trailingHash) return transfers.length; // All are same, saving runtime

if (transfers.length === 2) return 1; // Preposition for further math

const sameTrailingHashes = transfers.length - 1 - transfers.findLastIndex(tr => tr.hash !== trailingHash);

return sameTrailingHashes;
}

function fetchApprovals(
chainName: Network,
accAddress: string,
olderThanBlockHeight?: `${number}`,
/** Hex string. Including said block. */
fromBlock?: string
) {
const alchemy = new Alchemy({
apiKey: process.env._ALCHEMY_API_KEY, // TODO: To EnvVars
network: chainName
});

return alchemy.core.getLogs({
topics: [
[
'0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925', // Approval
'0x17307eab39ab6107e8899845ad3d59bd9653f200f220920489ca2b5937696c31' // ApprovalForAll
],
`0x000000000000000000000000${accAddress.slice(2)}`
],
toBlock: olderThanBlockToToBlockValue(olderThanBlockHeight),
fromBlock
});
}

function olderThanBlockToToBlockValue(olderThanBlockHeight: `${number}` | undefined) {
return olderThanBlockHeight ? '0x' + (BigInt(olderThanBlockHeight) - BigInt(1)).toString(16) : undefined;
}

/** E.g. 'opt_mainnet' does not support 'internal' category */
const EXCLUDED_CATEGORIES: Partial<Record<Network, AssetTransfersCategory>> = {
[Network.OPT_MAINNET]: AssetTransfersCategory.INTERNAL,
[Network.OPT_SEPOLIA]: AssetTransfersCategory.INTERNAL,
[Network.MATIC_AMOY]: AssetTransfersCategory.INTERNAL
};

/** TODO: Verify this mapping */
const CHAINS_NAMES: Record<number, Network> = {
1: Network.ETH_MAINNET,
5: Network.ETH_GOERLI,
10: Network.OPT_MAINNET,
30: Network.ROOTSTOCK_MAINNET,
31: Network.ROOTSTOCK_TESTNET,
56: Network.BNB_MAINNET,
97: Network.BNB_TESTNET,
100: Network.GNOSIS_MAINNET,
137: Network.MATIC_MAINNET,
204: Network.OPBNB_MAINNET,
250: Network.FANTOM_MAINNET,
300: Network.ZKSYNC_SEPOLIA,
324: Network.ZKSYNC_MAINNET,
360: Network.SHAPE_MAINNET,
420: Network.OPT_GOERLI,
480: Network.WORLDCHAIN_MAINNET,
592: Network.ASTAR_MAINNET,
1088: Network.METIS_MAINNET,
1101: Network.POLYGONZKEVM_MAINNET,
1442: Network.POLYGONZKEVM_TESTNET,
1946: Network.SONEIUM_MINATO,
2442: Network.POLYGONZKEVM_CARDONA,
4002: Network.FANTOM_TESTNET,
4801: Network.WORLDCHAIN_SEPOLIA,
5000: Network.MANTLE_MAINNET,
5003: Network.MANTLE_SEPOLIA,
5611: Network.OPBNB_TESTNET,
7000: Network.ZETACHAIN_MAINNET,
7001: Network.ZETACHAIN_TESTNET,
8453: Network.BASE_MAINNET,
10200: Network.GNOSIS_CHIADO,
11011: Network.SHAPE_SEPOLIA,
42161: Network.ARB_MAINNET,
42220: Network.CELO_MAINNET,
43113: Network.AVAX_FUJI,
43114: Network.AVAX_MAINNET,
42170: Network.ARBNOVA_MAINNET,
44787: Network.CELO_ALFAJORES,
59141: Network.LINEA_SEPOLIA,
59144: Network.LINEA_MAINNET,
80001: Network.MATIC_MUMBAI,
80002: Network.MATIC_AMOY,
80084: Network.BERACHAIN_BARTIO,
81457: Network.BLAST_MAINNET,
84531: Network.BASE_GOERLI,
84532: Network.BASE_SEPOLIA,
421613: Network.ARB_GOERLI,
421614: Network.ARB_SEPOLIA,
534351: Network.SCROLL_SEPOLIA,
534352: Network.SCROLL_MAINNET,
11155111: Network.ETH_SEPOLIA,
11155420: Network.OPT_SEPOLIA,
168587773: Network.BLAST_SEPOLIA
};
Loading