Skip to content

Commit

Permalink
Merge branch 'evm' of github.com:aura-nw/horoscope-v2 into feat/mater…
Browse files Browse the repository at this point in the history
…ial-view-erc721-holder-statistic
  • Loading branch information
phamphong9981 committed Sep 10, 2024
2 parents cfb8af2 + d5744b0 commit f945145
Show file tree
Hide file tree
Showing 27 changed files with 396 additions and 178 deletions.
3 changes: 2 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"src/services/**/*.service.ts",
"--config",
"src/moleculer.config.ts"
]
],
"console": "integratedTerminal"
},
{
"type": "node",
Expand Down
8 changes: 6 additions & 2 deletions ci/config.json.ci
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,10 @@
},
"dashboardStatistics": {
"millisecondCrawl": 10000,
"queryPageLimit": 100
"queryPageLimit": 100,
"currentInflation": 0.05,
"currentCommunityTax": 0,
"denomSupply": "stake"
},
"graphiqlApi": {
"hasuraRole": "internal_service",
Expand Down Expand Up @@ -470,7 +473,8 @@
},
"transport": {
"batchSize": 10,
"waitMilisecond": 1000
"waitMilisecond": 1000,
"timeout": 10000
}
},
"crawlEvmAccount": {
Expand Down
8 changes: 6 additions & 2 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,10 @@
},
"dashboardStatistics": {
"millisecondCrawl": 10000,
"queryPageLimit": 100
"queryPageLimit": 100,
"currentInflation": 0.05,
"currentCommunityTax": 0,
"denomSupply": "stake"
},
"feegrant": {
"updateFeegrant": {
Expand Down Expand Up @@ -468,7 +471,8 @@
},
"transport": {
"batchSize": 10,
"waitMilisecond": 1000
"waitMilisecond": 1000,
"timeout": 10000
}
},
"crawlEvmAccount": {
Expand Down
15 changes: 15 additions & 0 deletions migrations/20240909092417_create_index_validator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Knex } from 'knex';

export async function up(knex: Knex): Promise<void> {
await knex.schema.alterTable('validator', (table) => {
table.index('tokens');
table.index('status');
});
}

export async function down(knex: Knex): Promise<void> {
await knex.schema.alterTable('validator', (table) => {
table.dropIndex('tokens');
table.dropIndex('status');
});
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Knex } from 'knex';
import { EvmInternalTransaction } from '../src/models/evm_internal_transaction';
import { EvmInternalTransaction } from '../../src/models/evm_internal_transaction';

export async function up(knex: Knex): Promise<void> {
await knex.schema.createTable(EvmInternalTransaction.tableName, (table) => {
Expand Down
61 changes: 61 additions & 0 deletions migrations/evm/20240909025646_optimize_evm_tx_schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { Knex } from 'knex';

export async function up(knex: Knex): Promise<void> {
await knex.raw('SET statement_timeout TO 0');
await knex.schema.alterTable('evm_transaction', (table) => {
table.dropIndex('tx_id');
table.dropIndex('tx_msg_id');
table.dropIndex('from');
table.dropIndex('to');
table.dropIndex('hash');
table.dropIndex('status');
table.dropIndex('contract_address');
});
await knex.raw(`
alter table evm_transaction
alter column "from" set data type bytea USING decode(substring("from",3), 'hex'),
alter column "to" set data type bytea USING decode(substring("to",3), 'hex'),
alter column "data" set data type bytea USING decode("data", 'hex'),
alter column "hash" set data type bytea USING decode(substring("hash",3), 'hex'),
alter column "contract_address" set data type bytea USING decode(substring("contract_address",3), 'hex')`);
await knex.schema.alterTable('evm_transaction', (table) => {
table.index('from', 'evm_transaction_from_index', 'hash');
table.index('to', 'evm_transaction_to_index', 'hash');
table.index(
'contract_address',
'evm_transaction_contract_address_index',
'hash'
);
table.index('hash', 'evm_transaction_hash_index', 'hash');
});
}

export async function down(knex: Knex): Promise<void> {
await knex.raw('SET statement_timeout TO 0');
await knex.schema.alterTable('evm_transaction', (table) => {
table.dropIndex('from');
table.dropIndex('to');
table.dropIndex('hash');
table.dropIndex('contract_address');
});
await knex.raw(`
alter table evm_transaction
alter column "from" set data type character varying(255),
alter column "to" set data type character varying(255),
alter column "hash" set data type character varying(255),
alter column "data" set data type text,
alter column "contract_address" set data type character varying(255)`);
await knex.schema.alterTable('evm_transaction', (table) => {
table.index('tx_id', 'evm_transaction_tx_id_index', 'btree');
table.index('tx_msg_id', 'evm_transaction_tx_msg_id_index', 'btree');
table.index('status', 'evm_transaction_status_index', 'btree');
table.index('from', 'evm_transaction_from_index', 'btree');
table.index('to', 'evm_transaction_to_index', 'btree');
table.index(
'contract_address',
'evm_transaction_contract_address_index',
'btree'
);
table.index('hash', 'evm_transaction_hash_index', 'btree');
});
}
13 changes: 13 additions & 0 deletions migrations/evm/20240909092542_create_index_miner_in_evm_block.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Knex } from 'knex';

export async function up(knex: Knex): Promise<void> {
await knex.schema.alterTable('evm_block', (table) => {
table.index('miner');
});
}

export async function down(knex: Knex): Promise<void> {
await knex.schema.alterTable('evm_block', (table) => {
table.dropIndex('miner');
});
}
1 change: 1 addition & 0 deletions src/common/utils/etherjs_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export function getViemClient(chainId?: string): PublicClient {
batchSize: config.viemConfig.transport.batchSize,
wait: config.viemConfig.transport.waitMilisecond,
},
timeout: config.viemConfig.transport.timeout,
}),
}).extend(publicActionsL1());
viemClientMapping.set(chainId ?? config.chainId, viemClient);
Expand Down
13 changes: 6 additions & 7 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 All @@ -65,9 +65,8 @@ export class EVMTransaction extends BaseModel {
static get jsonSchema() {
return {
type: 'object',
required: ['hash', 'height'],
required: ['height'],
properties: {
hash: { type: 'string' },
height: { type: '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) => {
['hash', '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
24 changes: 15 additions & 9 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 @@ -70,7 +70,7 @@ export default class CrawlEvmAccountService extends BullableService {
BULL_JOB_NAME.CRAWL_EVM_TRANSACTION,
BULL_JOB_NAME.EVM_CRAWL_INTERNAL_TX,
],
config.evmCrawlInternalTx.key
config.crawlEvmAccount.key
);
this.logger.info(
`Crawl evm_account from block ${startBlock} to ${endBlock}`
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
37 changes: 23 additions & 14 deletions src/services/evm/crawl_evm_proxy_history.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,17 @@ export default class CrawlProxyContractEVMService extends BullableService {
.where('block_height', '>', startBlock)
.andWhere('block_height', '<=', endBlock)
.select('address', 'topic0', 'topic1', 'block_height', 'tx_hash');
const distictAddresses = _.uniq(evmEvents.map((e) => e.address));
const proxyContractDb = await EVMSmartContract.query()
.whereIn('type', [
EVMSmartContract.TYPES.PROXY_EIP_1967,
EVMSmartContract.TYPES.PROXY_EIP_1822,
EVMSmartContract.TYPES.PROXY_OPEN_ZEPPELIN_IMPLEMENTATION,
EVMSmartContract.TYPES.PROXY_EIP_1167,
EVMSmartContract.TYPES.PROXY_EIP_1967_BEACON,
])
.andWhere('address', 'in', distictAddresses)
.select('address');
const distictAddresses = _.uniq(evmEvents.map((e) => e.address));
const batchReqs: any[] = [];
distictAddresses.forEach((address) => {
batchReqs.push(
Expand Down Expand Up @@ -114,7 +116,7 @@ export default class CrawlProxyContractEVMService extends BullableService {
}) as EVMSmartContract;
const firstTimeCatchProxyEvent =
proxyContractDb.find((proxy) => proxy.address === evmEvent.address) &&
anyProxyHistoryByAddress[evmEvent.address];
!anyProxyHistoryByAddress[evmEvent.address];
const newJSONProxy: Dictionary<any> = {};

switch (evmEvent.topic0) {
Expand All @@ -139,22 +141,29 @@ export default class CrawlProxyContractEVMService extends BullableService {
// break;
default:
if (firstTimeCatchProxyEvent) {
implementationAddress = await this.contractHelper.isContractProxy(
evmEvent.address,
_.find(
EIPProxyContractSupportByteCode,
(value, __) => value.TYPE === evmEventProxy.type
)?.SLOT,
undefined,
bytecodes[evmEvent.address]
);
implementationAddress = (
await this.contractHelper.isContractProxy(
evmEvent.address,
_.find(
EIPProxyContractSupportByteCode,
(value, __) => value.TYPE === evmEventProxy.type
)?.SLOT,
undefined,
bytecodes[evmEvent.address]
)
)?.logicContractAddress;
}
break;
}

newJSONProxy.proxy_contract = _.toLower(evmEvent.address);
newJSONProxy.implementation_contract =
_.toLower(implementationAddress as string) || null;
if (implementationAddress) {
newJSONProxy.implementation_contract = _.toLower(
implementationAddress as string
);
} else {
newJSONProxy.implementation_contract = null;
}
newJSONProxy.block_height = evmEvent.block_height;
newJSONProxy.tx_hash = evmEvent.tx_hash;
newJSONProxy.last_updated_height = lastUpdatedHeight;
Expand All @@ -179,7 +188,7 @@ export default class CrawlProxyContractEVMService extends BullableService {
) {
newProxyContractsToSave.push(proxyHistory);
} else {
this.logger.warn(
this.logger.debug(
`This contract address ${proxyHistory.proxy_contract} is not proxy, at tx hash ${proxyHistory.tx_hash}`
);
}
Expand Down
Loading

0 comments on commit f945145

Please sign in to comment.