Skip to content

Commit

Permalink
feat: update job query evm_transaction (use hash as binary)
Browse files Browse the repository at this point in the history
  • Loading branch information
fibonacci998 committed Sep 9, 2024
1 parent 35bd353 commit 069307b
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 46 deletions.
10 changes: 5 additions & 5 deletions src/models/evm_transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ export class EVMTransaction extends BaseModel {

id!: number;

hash!: string;
hash!: Buffer;

height!: number;

from!: string;
from!: Buffer;

to!: string;
to!: Buffer;

size!: string;

Expand All @@ -30,15 +30,15 @@ export class EVMTransaction extends BaseModel {

gas_tip_cap!: bigint;

data!: string;
data!: Buffer;

nonce!: bigint;

tx_msg_id!: number;

tx_id!: number;

contract_address!: string;
contract_address!: Buffer;

index!: number;

Expand Down
2 changes: 1 addition & 1 deletion src/services/evm/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const ABI_CHECK_INTERFACE_ERC_1155 = [

export const EVM_CONTRACT_METHOD_HEX_PREFIX = {
// https://ethereum.stackexchange.com/questions/124906/how-to-tell-if-a-transaction-is-contract-creation
CREATE_CONTRACT: '60806040',
CREATE_CONTRACT: '0x60806040',
ABI_INTERFACE_ERC20: ABI_CHECK_INTERFACE_ERC_20.map((method) =>
keccak256(toBytes(method)).slice(2, 10)
),
Expand Down
13 changes: 12 additions & 1 deletion src/services/evm/crawl_contract_evm.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { Service } from '@ourparentcenter/moleculer-decorators-extended';
import { whatsabi } from '@shazow/whatsabi';
import _, { Dictionary } from 'lodash';
import { ServiceBroker } from 'moleculer';
import { GetBytecodeReturnType, PublicClient, keccak256 } from 'viem';
import {
GetBytecodeReturnType,
PublicClient,
bytesToHex,
keccak256,
} from 'viem';
import config from '../../../config.json' assert { type: 'json' };
import BullableService, { QueueHandler } from '../../base/bullable.service';
import knex from '../../common/utils/db_connection';
Expand Down Expand Up @@ -112,6 +117,12 @@ export default class CrawlSmartContractEVMService extends BullableService {
let addresses: string[] = [];
const txCreationWithAdressses: Dictionary<EVMTransaction> = {};
evmTxs.forEach((evmTx: any) => {
['from', 'to', 'data', 'contractAddress'].forEach((key) => {
if (evmTx[key]) {
// eslint-disable-next-line no-param-reassign
evmTx[key] = bytesToHex(evmTx[key]);
}
});
const { data, contractAddress } = evmTx;
let currentAddresses: string[] = [];
['from', 'to', 'contractAddress'].forEach((key) => {
Expand Down
22 changes: 14 additions & 8 deletions src/services/evm/crawl_evm_account.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
import { Knex } from 'knex';
import _, { Dictionary } from 'lodash';
import { Context } from 'moleculer';
import { PublicClient } from 'viem';
import { bytesToHex, PublicClient } from 'viem';
import config from '../../../config.json' assert { type: 'json' };
import '../../../fetch-polyfill.js';
import BullableService, { QueueHandler } from '../../base/bullable.service';
Expand Down Expand Up @@ -106,15 +106,21 @@ export default class CrawlEvmAccountService extends BullableService {
.andWhere('evm_tx_id', '<=', toTx.id)
.andWhere('evm_internal_transaction.error', null)
.select('evm_internal_transaction.from', 'evm_internal_transaction.to');
participants.forEach((partcicipant) => {
if (partcicipant.from !== ZERO_ADDRESS) {
accountsAddress.add(partcicipant.from);
participants.forEach((participant) => {
['from', 'to', 'contractAddress'].forEach((key) => {
if (participant[key]) {
// eslint-disable-next-line no-param-reassign
participant[key] = bytesToHex(participant[key]);
}
});
if (String(participant.from) !== ZERO_ADDRESS) {
accountsAddress.add(String(participant.from));
}
if (partcicipant.to && partcicipant.to !== ZERO_ADDRESS) {
accountsAddress.add(partcicipant.to);
if (participant.to && String(participant.to) !== ZERO_ADDRESS) {
accountsAddress.add(String(participant.to));
}
if (partcicipant.contractAddress) {
accountsAddress.add(partcicipant.contractAddress);
if (participant.contractAddress) {
accountsAddress.add(participant.contractAddress);
}
});
participantsFromInternal.forEach((participant) => {
Expand Down
30 changes: 4 additions & 26 deletions src/services/evm/evm_crawl_internal_tx.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Service } from '@ourparentcenter/moleculer-decorators-extended';
import { ServiceBroker } from 'moleculer';
import { PublicClient } from 'viem';
import { bytesToHex, PublicClient } from 'viem';
import _ from 'lodash';
import { getViemClient } from '../../common/utils/etherjs_client';
import {
BlockCheckpoint,
Expand Down Expand Up @@ -62,7 +63,7 @@ export default class EvmCrawlInternalTxService extends BullableService {
.where('height', '>', startBlock)
.andWhere('height', '<=', endBlock),
]);

const evmTxsByHash = _.keyBy(evmTxs, (e) => bytesToHex(e.hash));
if (evmBlocks.length === 0) {
this.logger.info(`No evm block found from ${startBlock} to ${endBlock}`);
blockCheckpoint.height = endBlock;
Expand All @@ -85,29 +86,6 @@ export default class EvmCrawlInternalTxService extends BullableService {
],
})
);

// const requests = evmBlocks.map((evmBlock) =>
// axios({
// url: 'https://testnet.storyrpc.io',
// method: 'POST',
// headers: {
// Accept: 'application/json',
// 'Content-Type': 'application/json',
// },
// data: {
// jsonrpc: '2.0',
// id: evmBlock.height,
// method: 'debug_traceBlockByNumber',
// params: [
// `0x${evmBlock.height.toString(16)}`,
// {
// tracer: 'callTracer',
// timeout: config.evmCrawlInternalTx.timeoutJSONRPC,
// },
// ],
// },
// })
// );
const responseBlocks = await Promise.all(requests);
const internalTxSave: EvmInternalTransaction[] = [];

Expand All @@ -116,7 +94,7 @@ export default class EvmCrawlInternalTxService extends BullableService {
responseBlock.forEach((responseTx: any) => {
if (responseTx.result?.calls) {
const { txHash } = responseTx;
const evmTxDB = evmTxs.find((tx) => tx.hash === txHash);
const evmTxDB = evmTxsByHash[txHash];
if (!evmTxDB) {
throw Error('Cannot found this evm_tx_id');
}
Expand Down
21 changes: 16 additions & 5 deletions src/services/evm/statistic/account_statistics.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import BigNumber from 'bignumber.js';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import _ from 'lodash';
import { bytesToHex } from 'viem';
import {
AccountStatistics,
EVMBlock,
Expand Down Expand Up @@ -197,13 +198,23 @@ export default class EVMAccountStatisticsService extends BullableService {
.andWhere('evm_tx_id', '<=', toTx.id);
});
dailyTxs.forEach((tx) => {
if (!accountStats[tx.from]) {
accountStats[tx.from] = AccountStatistics.newAccountStat(tx.from, date);
['from'].forEach((key) => {
if (tx[key]) {
// eslint-disable-next-line no-param-reassign
tx[key] = bytesToHex(tx[key]);
}
});

if (!accountStats[String(tx.from)]) {
accountStats[String(tx.from)] = AccountStatistics.newAccountStat(
String(tx.from),
date
);
}
accountStats[tx.from].tx_sent += 1;
accountStats[String(tx.from)].tx_sent += 1;

accountStats[tx.from].gas_used = (
BigInt(accountStats[tx.from].gas_used) + BigInt(tx.gas_used)
accountStats[String(tx.from)].gas_used = (
BigInt(accountStats[String(tx.from)].gas_used) + BigInt(tx.gas_used)
).toString();

tx.evm_internal_transactions.forEach(
Expand Down

0 comments on commit 069307b

Please sign in to comment.