Skip to content

Commit

Permalink
fix: order erc20 activities
Browse files Browse the repository at this point in the history
  • Loading branch information
phamphong9981 committed Jul 24, 2024
1 parent d32da37 commit 939a449
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 7 deletions.
17 changes: 15 additions & 2 deletions src/services/evm/erc20_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ export class Erc20Handler {
.whereIn(knex.raw('lower("value")'), addresses);
}
})
.withGraphFetched('[transaction, attributes]');
.withGraphFetched('[transaction, attributes]')
.orderBy('event.id', 'asc');
}
erc20Events.forEach((e) => {
if (e.topic0 === ERC20_EVENT_TOPIC0.TRANSFER) {
Expand All @@ -206,7 +207,15 @@ export class Erc20Handler {
logger
);
if (activity) {
erc20Activities.push(activity);
const index = erc20Activities.findIndex(
(e) => e.height > activity.height
);
// sort activity
if (index === -1) {
erc20Activities.splice(erc20Activities.length, 0, activity);
} else {
erc20Activities.splice(index, 0, activity);
}
}
});
return erc20Activities;
Expand Down Expand Up @@ -311,6 +320,7 @@ export class Erc20Handler {
height: e.block_height,
tx_hash: e.tx_hash,
evm_tx_id: e.evm_tx_id,
cosmos_tx_id: e.tx_id,
});
} catch (e) {
logger.error(e);
Expand Down Expand Up @@ -411,6 +421,7 @@ export class Erc20Handler {
height: e.block_height,
tx_hash: e.tx_hash,
evm_tx_id: e.evm_tx_id,
cosmos_tx_id: e.tx_id,
});
} catch (e) {
logger.error(e);
Expand Down Expand Up @@ -453,6 +464,7 @@ export class Erc20Handler {
height: e.block_height,
tx_hash: e.tx_hash,
evm_tx_id: e.evm_tx_id,
cosmos_tx_id: e.tx_id,
});
} catch (e) {
logger.error(e);
Expand Down Expand Up @@ -480,6 +492,7 @@ export class Erc20Handler {
height: e.block_height,
tx_hash: e.tx_hash,
evm_tx_id: e.evm_tx_id,
cosmos_tx_id: e.tx_id,
});
} catch (e) {
logger.error(e);
Expand Down
78 changes: 73 additions & 5 deletions test/unit/services/evm/erc20_handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ export default class Erc20HandlerTest {
timestamp: '2024-07-15T17:08:43.386+07:00',
events: [
{
id: 2,
id: 3,
tx_msg_index: 0,
type: Event.EVENT_TYPE.CONVERT_COIN,
source: 'TX_EVENT',
Expand Down Expand Up @@ -369,7 +369,7 @@ export default class Erc20HandlerTest {
],
},
{
id: 3,
id: 2,
tx_msg_index: 1,
type: Event.EVENT_TYPE.CONVERT_ERC20,
source: 'TX_EVENT',
Expand Down Expand Up @@ -412,15 +412,62 @@ export default class Erc20HandlerTest {
],
});
await Transaction.query().insertGraph(transaction);
const evmEvents = [
// transfer event
EvmEvent.fromJson({
id: 1,
address: erc20Contract.address,
block_hash:
'0xed6a2d3c3ac9a2868420c4fdd67240d2d96298fc4272cd31455cd0cdaabf9093',
block_height: blockHeight - 1,
data: fromHex(
encodeAbiParameters([ABI_TRANSFER_PARAMS.VALUE], [amount]),
'bytes'
),
evm_tx_id: evmTransaction.id,
topic0:
'0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
topic1: encodeAbiParameters([ABI_TRANSFER_PARAMS.FROM], [from]),
topic2: encodeAbiParameters([ABI_TRANSFER_PARAMS.TO], [to]),
tx_hash:
'0x8a82a0c8848487d716f10a91f0aefb0526d35bd0f489166cc5141718a4d8aa64',
topic3: null,
tx_id: evmTransaction.id,
tx_index: 0,
}),
// transfer event
EvmEvent.fromJson({
id: 2,
address: erc20Contract.address,
block_hash:
'0xed6a2d3c3ac9a2868420c4fdd67240d2d96298fc4272cd31455cd0cdaabf9093',
block_height: blockHeight + 1,
data: fromHex(
encodeAbiParameters([ABI_TRANSFER_PARAMS.VALUE], [amount]),
'bytes'
),
evm_tx_id: evmTransaction.id,
topic0:
'0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
topic1: encodeAbiParameters([ABI_TRANSFER_PARAMS.FROM], [from]),
topic2: encodeAbiParameters([ABI_TRANSFER_PARAMS.TO], [to]),
tx_hash:
'0x8a82a0c8848487d716f10a91f0aefb0526d35bd0f489166cc5141718a4d8aa64',
topic3: null,
tx_id: evmTransaction.id,
tx_index: 0,
}),
];
await EvmEvent.query().insert(evmEvents);
await knex.transaction(async (trx) => {
const erc20Activitites = await Erc20Handler.buildErc20Activities(
blockHeight - 1,
blockHeight,
blockHeight - 2,
blockHeight + 1,
trx,
this.broker.logger
);
// test convert coin activity
const convertCoinActivity = erc20Activitites[0];
const convertCoinActivity = erc20Activitites[2];
expect(convertCoinActivity).toMatchObject({
from: convertBech32AddressToEthAddress(
config.networkPrefixAddress,
Expand All @@ -445,6 +492,27 @@ export default class Erc20HandlerTest {
erc20_contract_address: erc20Contract.address,
cosmos_event_id: transaction.events[1].id,
});
// test sort order
const transferActivity1 = erc20Activitites[0];
expect(transferActivity1).toMatchObject({
action: ERC20_ACTION.TRANSFER,
erc20_contract_address: erc20Contract.address,
from,
to,
amount,
evm_tx_id: evmEvents[0].evm_tx_id,
cosmos_tx_id: evmEvents[0].tx_id,
});
const transferActivity2 = erc20Activitites[3];
expect(transferActivity2).toMatchObject({
action: ERC20_ACTION.TRANSFER,
erc20_contract_address: erc20Contract.address,
from,
to,
amount,
evm_tx_id: evmEvents[1].evm_tx_id,
cosmos_tx_id: evmEvents[1].tx_id,
});
});
}

Expand Down

0 comments on commit 939a449

Please sign in to comment.