Skip to content

Commit

Permalink
fix: erc20 module account logic
Browse files Browse the repository at this point in the history
  • Loading branch information
phamphong9981 committed Jul 26, 2024
1 parent 14b9ba9 commit d06b063
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 64 deletions.
16 changes: 1 addition & 15 deletions src/services/evm/erc20.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { QueryModuleAccountByNameResponseSDKType } from '@aura-nw/aurajs/types/codegen/cosmos/auth/v1beta1/query';
import {
Action,
Service,
Expand All @@ -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';
Expand All @@ -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);
}
Expand Down Expand Up @@ -352,17 +349,6 @@ export default class Erc20Service extends BullableService {
public async _start(): Promise<void> {
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,
Expand Down
33 changes: 4 additions & 29 deletions src/services/evm/erc20_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ export class Erc20Handler {

erc20Contracts: Dictionary<Erc20Contract>;

factoryAccounts: string[];

static erc20ModuleAccount: any;

constructor(
accountBalances: Dictionary<AccountBalance>,
erc20Activities: Erc20Activity[],
Expand All @@ -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() {
Expand All @@ -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];
Expand Down Expand Up @@ -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}`;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -257,7 +240,6 @@ export class Erc20Handler {
erc20CosmosEvents.forEach((event) => {
const activity = Erc20Handler.buildTransferActivityByCosmos(
event,
this.erc20ModuleAccount,
logger
);
if (activity) {
Expand Down Expand Up @@ -396,7 +378,6 @@ export class Erc20Handler {

static buildTransferActivityByCosmos(
e: Event,
erc20ModuleAccount: string,
logger: Moleculer.LoggerInstance
): Erc20Activity | undefined {
try {
Expand Down Expand Up @@ -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
Expand Down
17 changes: 3 additions & 14 deletions test/unit/services/evm/erc20_handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -97,7 +96,6 @@ export default class Erc20HandlerTest {
wrapSmartContract,
]);
await Erc20Contract.query().insert([erc20Contract, erc20WrapContract]);
Erc20Handler.erc20ModuleAccount = erc20ModuleAccount;
}

@AfterAll()
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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',
Expand Down
7 changes: 1 addition & 6 deletions test/unit/services/evm/erc20_reindex.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit d06b063

Please sign in to comment.