From d06b063ab616035c5e23f28803a1d28ef434eae2 Mon Sep 17 00:00:00 2001 From: phamphong9981 Date: Fri, 26 Jul 2024 08:59:10 +0700 Subject: [PATCH] fix: erc20 module account logic --- src/services/evm/erc20.service.ts | 16 +--------- src/services/evm/erc20_handler.ts | 33 +++----------------- test/unit/services/evm/erc20_handler.spec.ts | 17 ++-------- test/unit/services/evm/erc20_reindex.spec.ts | 7 +---- 4 files changed, 9 insertions(+), 64 deletions(-) diff --git a/src/services/evm/erc20.service.ts b/src/services/evm/erc20.service.ts index 2ca5dcb7b..a50edbd8e 100644 --- a/src/services/evm/erc20.service.ts +++ b/src/services/evm/erc20.service.ts @@ -1,4 +1,3 @@ -import { QueryModuleAccountByNameResponseSDKType } from '@aura-nw/aurajs/types/codegen/cosmos/auth/v1beta1/query'; import { Action, Service, @@ -9,7 +8,7 @@ import { Context, ServiceBroker } from 'moleculer'; import { PublicClient, getContract } from 'viem'; import config from '../../../config.json' assert { type: 'json' }; import BullableService, { QueueHandler } from '../../base/bullable.service'; -import { SERVICE as COSMOS_SERVICE, Config, getLcdClient } from '../../common'; +import { SERVICE as COSMOS_SERVICE, Config } from '../../common'; import knex from '../../common/utils/db_connection'; import { getViemClient } from '../../common/utils/etherjs_client'; import { BlockCheckpoint, EVMSmartContract } from '../../models'; @@ -28,8 +27,6 @@ const { NODE_ENV } = Config; export default class Erc20Service extends BullableService { viemClient!: PublicClient; - erc20ModuleAccount!: string; - public constructor(public broker: ServiceBroker) { super(broker); } @@ -352,17 +349,6 @@ export default class Erc20Service extends BullableService { public async _start(): Promise { this.viemClient = getViemClient(); if (NODE_ENV !== 'test') { - if (config.evmOnly === false) { - const lcdClient = await getLcdClient(); - const erc20Account: QueryModuleAccountByNameResponseSDKType = - await lcdClient.provider.cosmos.auth.v1beta1.moduleAccountByName({ - name: 'erc20', - }); - Erc20Handler.erc20ModuleAccount = - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - erc20Account.account.base_account.address; - } await this.createJob( BULL_JOB_NAME.HANDLE_ERC20_CONTRACT, BULL_JOB_NAME.HANDLE_ERC20_CONTRACT, diff --git a/src/services/evm/erc20_handler.ts b/src/services/evm/erc20_handler.ts index 9cae1799f..6058a0a49 100644 --- a/src/services/evm/erc20_handler.ts +++ b/src/services/evm/erc20_handler.ts @@ -64,10 +64,6 @@ export class Erc20Handler { erc20Contracts: Dictionary; - factoryAccounts: string[]; - - static erc20ModuleAccount: any; - constructor( accountBalances: Dictionary, erc20Activities: Erc20Activity[], @@ -76,16 +72,6 @@ export class Erc20Handler { this.accountBalances = accountBalances; this.erc20Activities = erc20Activities; this.erc20Contracts = erc20Contracts; - this.factoryAccounts = [ZERO_ADDRESS]; - if (Erc20Handler.erc20ModuleAccount) { - this.factoryAccounts.push( - // convert to evm address - convertBech32AddressToEthAddress( - config.networkPrefixAddress, - Erc20Handler.erc20ModuleAccount - ).toLowerCase() - ); - } } process() { @@ -104,7 +90,7 @@ export class Erc20Handler { handlerErc20Transfer(erc20Activity: Erc20Activity) { // update from account balance if from != ZERO_ADDRESS - if (!this.factoryAccounts.includes(erc20Activity.from)) { + if (erc20Activity.from !== ZERO_ADDRESS) { const fromAccountId = erc20Activity.from_account_id; const key = `${fromAccountId}_${erc20Activity.erc20_contract_address}`; const fromAccountBalance = this.accountBalances[key]; @@ -142,7 +128,7 @@ export class Erc20Handler { } } // update from account balance if to != ZERO_ADDRESS - if (!this.factoryAccounts.includes(erc20Activity.to)) { + if (erc20Activity.to !== ZERO_ADDRESS) { // update to account balance const toAccountId = erc20Activity.to_account_id; const key = `${toAccountId}_${erc20Activity.erc20_contract_address}`; @@ -214,9 +200,6 @@ export class Erc20Handler { ); let erc20CosmosEvents: Event[] = []; if (config.evmOnly === false) { - if (!this.erc20ModuleAccount) { - throw new Error('erc20 module account undefined'); - } erc20CosmosEvents = await Event.query() .transacting(trx) .where('event.block_height', '>', startBlock) @@ -257,7 +240,6 @@ export class Erc20Handler { erc20CosmosEvents.forEach((event) => { const activity = Erc20Handler.buildTransferActivityByCosmos( event, - this.erc20ModuleAccount, logger ); if (activity) { @@ -396,7 +378,6 @@ export class Erc20Handler { static buildTransferActivityByCosmos( e: Event, - erc20ModuleAccount: string, logger: Moleculer.LoggerInstance ): Erc20Activity | undefined { try { @@ -428,15 +409,9 @@ export class Erc20Handler { ); const sender = from; if (e.type === Event.EVENT_TYPE.CONVERT_COIN) { - from = convertBech32AddressToEthAddress( - config.networkPrefixAddress, - erc20ModuleAccount - ).toLowerCase(); + from = ZERO_ADDRESS; } else if (e.type === Event.EVENT_TYPE.CONVERT_ERC20) { - to = convertBech32AddressToEthAddress( - config.networkPrefixAddress, - erc20ModuleAccount - ).toLowerCase(); + to = ZERO_ADDRESS; } const amount = e.attributes.find( (attr) => attr.key === EventAttribute.ATTRIBUTE_KEY.AMOUNT diff --git a/test/unit/services/evm/erc20_handler.spec.ts b/test/unit/services/evm/erc20_handler.spec.ts index ff779fafb..e70900d51 100644 --- a/test/unit/services/evm/erc20_handler.spec.ts +++ b/test/unit/services/evm/erc20_handler.spec.ts @@ -29,7 +29,6 @@ import { ERC20_ACTION, Erc20Handler, } from '../../../../src/services/evm/erc20_handler'; -import { convertBech32AddressToEthAddress } from '../../../../src/services/evm/utils'; const evmTransaction = EVMTransaction.fromJson({ id: 2931, @@ -97,7 +96,6 @@ export default class Erc20HandlerTest { wrapSmartContract, ]); await Erc20Contract.query().insert([erc20Contract, erc20WrapContract]); - Erc20Handler.erc20ModuleAccount = erc20ModuleAccount; } @AfterAll() @@ -422,10 +420,7 @@ export default class Erc20HandlerTest { // test convert coin activity const convertCoinActivity = erc20Activitites[0]; expect(convertCoinActivity).toMatchObject({ - from: convertBech32AddressToEthAddress( - config.networkPrefixAddress, - erc20ModuleAccount - ).toLowerCase(), + from: ZERO_ADDRESS, to, amount, action: ERC20_ACTION.TRANSFER, @@ -436,10 +431,7 @@ export default class Erc20HandlerTest { const convertErc20Activity = erc20Activitites[1]; expect(convertErc20Activity).toMatchObject({ from, - to: convertBech32AddressToEthAddress( - config.networkPrefixAddress, - erc20ModuleAccount - ).toLowerCase(), + to: ZERO_ADDRESS, amount, action: ERC20_ACTION.TRANSFER, erc20_contract_address: erc20Contract.address, @@ -801,10 +793,7 @@ export default class Erc20HandlerTest { erc20_contract_address: '0x98605ae21dd3be686337a6d7a8f156d0d8baee92', amount: '12345222', from: '0xD83E708D7FE0E769Af80d990f9241458734808Ac', - to: convertBech32AddressToEthAddress( - config.networkPrefixAddress, - erc20ModuleAccount - ).toLowerCase(), + to: ZERO_ADDRESS, height: 10000, tx_hash: '0xb97228e533e3af1323d873c9c3e4c0a9b85d95ecd8e98110c8890c9453d2f077', diff --git a/test/unit/services/evm/erc20_reindex.spec.ts b/test/unit/services/evm/erc20_reindex.spec.ts index 14023a0fe..ef92dbf97 100644 --- a/test/unit/services/evm/erc20_reindex.spec.ts +++ b/test/unit/services/evm/erc20_reindex.spec.ts @@ -13,10 +13,7 @@ import { EVMSmartContract, EVMTransaction, } from '../../../../src/models'; -import { - ABI_TRANSFER_PARAMS, - Erc20Handler, -} from '../../../../src/services/evm/erc20_handler'; +import { ABI_TRANSFER_PARAMS } from '../../../../src/services/evm/erc20_handler'; import { Erc20Reindexer } from '../../../../src/services/evm/erc20_reindex'; const accounts = [ @@ -148,8 +145,6 @@ export default class Erc20ReindexTest { @Test('test reindex') async testReindex() { const viemClient = getViemClient(); - Erc20Handler.erc20ModuleAccount = - 'aura16st9fmex8xjdahwzwxldhjm2ssu6utw8rnxlkj'; jest.spyOn(viemClient, 'getBlockNumber').mockResolvedValue(BigInt(123456)); // Instantiate Erc20Reindexer with the mock const reindexer = new Erc20Reindexer(viemClient, this.broker.logger);