diff --git a/migrations/evm/20240805101025_remove_foreign_key_contract_in_proxy_history.ts b/migrations/evm/20240805101025_remove_foreign_key_contract_in_proxy_history.ts deleted file mode 100644 index 911eb9eb5..000000000 --- a/migrations/evm/20240805101025_remove_foreign_key_contract_in_proxy_history.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { Knex } from 'knex'; -import { EvmProxyHistory } from '../../src/models/evm_proxy_history'; - -export async function up(knex: Knex): Promise { - await knex.schema.alterTable(EvmProxyHistory.tableName, (table) => { - table.dropForeign('proxy_contract'); - }); -} - -export async function down(knex: Knex): Promise { - await knex.schema.alterTable(EvmProxyHistory.tableName, (table) => { - table - .foreign('proxy_contract') - .references('address') - .inTable('evm_smart_contract'); - }); -} diff --git a/src/services/evm/crawl_evm_proxy_history.service.ts b/src/services/evm/crawl_evm_proxy_history.service.ts index cdebeb57f..336550eee 100644 --- a/src/services/evm/crawl_evm_proxy_history.service.ts +++ b/src/services/evm/crawl_evm_proxy_history.service.ts @@ -139,10 +139,27 @@ export default class CrawlProxyContractEVMService extends BullableService { newProxyHistories.push(EvmProxyHistory.fromJson(newJSONProxy)); } - const newProxyContractsToSave = _.filter( - newProxyHistories, - (proxyContract) => proxyContract.implementation_contract !== null + // check evm_smart_contract if proxy_contract is existed + const foundContractsInDB = _.keyBy( + await EVMSmartContract.query().whereIn( + 'address', + newProxyHistories.map((e) => e.proxy_contract) + ), + 'address' ); + const newProxyContractsToSave: EvmProxyHistory[] = []; + newProxyHistories.forEach((proxyHistory) => { + if ( + proxyHistory.implementation_contract !== null && + foundContractsInDB[proxyHistory.implementation_contract] !== null + ) { + newProxyContractsToSave.push(proxyHistory); + } else { + this.logger.warn( + `This contract address ${proxyHistory.implementation_contract} is not proxy, at tx hash ${proxyHistory.tx_hash}` + ); + } + }); const newProxyContracts: EvmProxyHistory[] = []; await knex.transaction(async (trx) => { if (newProxyContractsToSave.length > 0) {