2024-01-16 07:47:10 -08:00
|
|
|
import { EvmBlock, EvmTransaction, ReceiptTransaction } from "../../entities";
|
2023-12-22 12:50:38 -08:00
|
|
|
import { EvmBlockRepository } from "../../repositories";
|
|
|
|
import { GetEvmOpts } from "./GetEvmLogs";
|
|
|
|
import winston from "winston";
|
|
|
|
|
|
|
|
export class GetEvmTransactions {
|
|
|
|
private readonly blockRepo: EvmBlockRepository;
|
|
|
|
protected readonly logger: winston.Logger;
|
|
|
|
|
|
|
|
constructor(blockRepo: EvmBlockRepository) {
|
|
|
|
this.logger = winston.child({ module: "GetEvmTransactions" });
|
|
|
|
this.blockRepo = blockRepo;
|
|
|
|
}
|
|
|
|
|
|
|
|
async execute(range: Range, opts: GetEvmOpts): Promise<EvmTransaction[]> {
|
|
|
|
const fromBlock = range.fromBlock;
|
|
|
|
const toBlock = range.toBlock;
|
|
|
|
|
|
|
|
if (fromBlock > toBlock) {
|
|
|
|
this.logger.info(`[exec] Invalid range [fromBlock: ${fromBlock} - toBlock: ${toBlock}]`);
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
let populateTransactions: EvmTransaction[] = [];
|
|
|
|
const isTransactionsPresent = true;
|
|
|
|
const chain = opts.chain;
|
|
|
|
|
|
|
|
for (let block = fromBlock; block <= toBlock; block++) {
|
|
|
|
const evmBlock = await this.blockRepo.getBlock(chain, block, isTransactionsPresent);
|
|
|
|
const transactions = evmBlock.transactions ?? [];
|
|
|
|
|
2024-01-16 07:47:10 -08:00
|
|
|
// Only process transactions to the contract address configured
|
|
|
|
const transactionsByAddressConfigured = transactions.filter(
|
2023-12-22 12:50:38 -08:00
|
|
|
(transaction) =>
|
|
|
|
opts.addresses?.includes(String(transaction.to).toLowerCase()) ||
|
|
|
|
opts.addresses?.includes(String(transaction.from).toLowerCase())
|
|
|
|
);
|
|
|
|
|
2024-01-16 07:47:10 -08:00
|
|
|
if (transactionsByAddressConfigured.length > 0) {
|
|
|
|
const hashNumbers = new Set(
|
|
|
|
transactionsByAddressConfigured.map((transaction) => transaction.hash)
|
|
|
|
);
|
|
|
|
const receiptTransaction = await this.blockRepo.getTransactionReceipt(chain, hashNumbers);
|
|
|
|
|
|
|
|
const filterTransactions = this.filterTransactions(
|
|
|
|
opts,
|
|
|
|
transactionsByAddressConfigured,
|
|
|
|
receiptTransaction
|
|
|
|
);
|
|
|
|
|
2023-12-22 12:50:38 -08:00
|
|
|
populateTransactions = await this.populateTransaction(
|
2024-01-16 07:47:10 -08:00
|
|
|
opts,
|
2023-12-22 12:50:38 -08:00
|
|
|
evmBlock,
|
2024-01-16 07:47:10 -08:00
|
|
|
receiptTransaction,
|
|
|
|
filterTransactions
|
2023-12-22 12:50:38 -08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.logger.info(
|
|
|
|
`[${chain}][exec] Got ${
|
|
|
|
populateTransactions?.length
|
|
|
|
} transactions to process for ${this.populateLog(opts, fromBlock, toBlock)}`
|
|
|
|
);
|
|
|
|
return populateTransactions;
|
|
|
|
}
|
|
|
|
|
|
|
|
private async populateTransaction(
|
2024-01-16 07:47:10 -08:00
|
|
|
opts: GetEvmOpts,
|
2023-12-22 12:50:38 -08:00
|
|
|
evmBlock: EvmBlock,
|
2024-01-16 07:47:10 -08:00
|
|
|
receiptTransaction: Record<string, ReceiptTransaction>,
|
|
|
|
filterTransactions: EvmTransaction[]
|
2023-12-22 12:50:38 -08:00
|
|
|
): Promise<EvmTransaction[]> {
|
2024-01-16 07:47:10 -08:00
|
|
|
filterTransactions.forEach((transaction) => {
|
|
|
|
// TODO: Move this logic inside evm mappers
|
|
|
|
const redeemedTopic = opts.topics?.[1];
|
|
|
|
const logs = receiptTransaction[transaction.hash].logs;
|
|
|
|
|
|
|
|
logs
|
|
|
|
.filter((log) => redeemedTopic && log.topics.includes(redeemedTopic))
|
|
|
|
.map((log) => {
|
|
|
|
transaction.emitterChain = Number(log.topics[1]);
|
|
|
|
transaction.emitterAddress = BigInt(log.topics[2])
|
|
|
|
.toString(16)
|
|
|
|
.toUpperCase()
|
|
|
|
.padStart(64, "0");
|
|
|
|
transaction.sequence = Number(log.topics[3]);
|
|
|
|
});
|
2023-12-22 12:50:38 -08:00
|
|
|
|
|
|
|
transaction.status = receiptTransaction[transaction.hash].status;
|
2024-01-16 07:47:10 -08:00
|
|
|
transaction.timestamp = evmBlock.timestamp;
|
|
|
|
transaction.environment = opts.environment;
|
|
|
|
transaction.chainId = opts.chainId;
|
|
|
|
transaction.chain = opts.chain;
|
|
|
|
transaction.logs = logs;
|
|
|
|
|
|
|
|
this.logger.info(
|
|
|
|
`[${opts.chain}][exec] Transaction populated:[hash:${transaction.hash}][VAA:${transaction.emitterChain}/${transaction.emitterAddress}/${transaction.sequence}]`
|
|
|
|
);
|
2023-12-22 12:50:38 -08:00
|
|
|
});
|
|
|
|
|
2024-01-16 07:47:10 -08:00
|
|
|
return filterTransactions;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method filter the transactions in base your logs with the topic and address configured in the job
|
|
|
|
* For example: Redeemed or MintAndWithdraw transactions
|
|
|
|
*/
|
|
|
|
private filterTransactions(
|
|
|
|
opts: GetEvmOpts,
|
|
|
|
transactionsByAddressConfigured: EvmTransaction[],
|
|
|
|
receiptTransaction: Record<string, ReceiptTransaction>
|
|
|
|
): EvmTransaction[] {
|
|
|
|
return transactionsByAddressConfigured.filter((transaction) => {
|
|
|
|
const optsTopics = opts.topics;
|
|
|
|
const logs = receiptTransaction[transaction.hash]?.logs || [];
|
|
|
|
|
|
|
|
return logs.some((log) => {
|
|
|
|
return optsTopics?.find((topic) => log.topics?.includes(topic));
|
|
|
|
});
|
|
|
|
});
|
2023-12-22 12:50:38 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
private populateLog(opts: GetEvmOpts, fromBlock: bigint, toBlock: bigint): string {
|
|
|
|
return `[addresses:${opts.addresses}][topics:${opts.topics}][blocks:${fromBlock} - ${toBlock}]`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Range = {
|
|
|
|
fromBlock: bigint;
|
|
|
|
toBlock: bigint;
|
|
|
|
};
|