diff --git a/src/mappings/constants.ts b/src/mappings/constants.ts index d6b24a39..e9067d62 100644 --- a/src/mappings/constants.ts +++ b/src/mappings/constants.ts @@ -19,6 +19,7 @@ export const EVENT_TYPES = { TRANSFER: "transfer", SEND_PACKET: "send_packet", RECEIVE_PACKET: "recv_packet", + IBC_TRANSFER: "ibc_transfer", }; export const VAULT_STATES = { @@ -35,6 +36,9 @@ export const SUBKEY_KEY = b64encode("store_subkey"); export const UNPROVED_VALUE_KEY = b64encode("unproved_value"); export const PACKET_DATA_KEY = "packet_data"; export const PACKET_SRC_CHANNEL_KEY = "packet_src_channel"; -export const ACTION_KEY = b64encode('action'); -export const IBC_MESSAGE_TRANSFER_VALUE = b64encode('/ibc.applications.transfer.v1.MsgTransfer'); -export const RECEPIENT_KEY = b64encode('recipient'); +export const ACTION_KEY = b64encode("action"); +export const IBC_MESSAGE_TRANSFER_VALUE = b64encode("/ibc.applications.transfer.v1.MsgTransfer"); +export const RECEPIENT_KEY = b64encode("recipient"); +export const SENDER_KEY = b64encode("sender"); +export const RECEIVER_KEY = b64encode("receiver"); +export const AMOUNT_KEY = b64encode("amount"); diff --git a/src/mappings/mappingHandlers.ts b/src/mappings/mappingHandlers.ts index 90996ab7..af6d4eb0 100644 --- a/src/mappings/mappingHandlers.ts +++ b/src/mappings/mappingHandlers.ts @@ -43,6 +43,9 @@ import { ACTION_KEY, IBC_MESSAGE_TRANSFER_VALUE, RECEPIENT_KEY, + RECEIVER_KEY, + SENDER_KEY, + AMOUNT_KEY, } from "./constants"; import { psmEventKit } from "./events/psm"; import { boardAuxEventKit } from "./events/boardAux"; @@ -73,26 +76,45 @@ export async function handleIbcSendPacketEvent(cosmosEvent: CosmosEvent): Promis logger.warn("No packet source channel found"); return; } + const { amount, denom, receiver, sender } = JSON.parse(packetDataAttr.value); const txns = block.txs; - const ibcTransaction = txns.find(txn => - txn.events.find(event => - event.type === EVENT_TYPES.MESSAGE && event.attributes.find(attribute => attribute.key === ACTION_KEY && attribute.value === IBC_MESSAGE_TRANSFER_VALUE) - ) - ) - const transferEvent = ibcTransaction?.events.find(event => event.type === EVENT_TYPES.TRANSFER) - const encodedAddress = transferEvent?.attributes.find(attribute => attribute.key === RECEPIENT_KEY)?.value; - const escrowAddress = encodedAddress ? b64decode(encodedAddress) : null; + const ibcTransaction = txns.find( + (txn) => + txn.events.find( + (event) => + event.type === EVENT_TYPES.MESSAGE && + event.attributes.find( + (attribute) => attribute.key === ACTION_KEY && attribute.value === IBC_MESSAGE_TRANSFER_VALUE, + ), + ) && + txn.events.find( + (event) => + event.type === EVENT_TYPES.IBC_TRANSFER && + event.attributes.find((attribute) => attribute.key === SENDER_KEY)?.value === b64encode(sender) && + event.attributes.find((attribute) => attribute.key === RECEIVER_KEY)?.value === b64encode(receiver), + ), + ); + const transferEvents = ibcTransaction?.events.filter((event) => event.type === EVENT_TYPES.TRANSFER); + const escrowTransaction = transferEvents + ?.reverse() + .find( + (event) => + event.attributes.find((attribute) => attribute.key === SENDER_KEY)?.value === b64encode(sender) && + event.attributes.find((attribute) => attribute.key === AMOUNT_KEY)?.value === b64encode(amount + denom), + ); + const encodedEscrowAddress = escrowTransaction?.attributes.find( + (attribute) => attribute.key === RECEPIENT_KEY, + )?.value; + const escrowAddress = encodedEscrowAddress ? b64decode(encodedEscrowAddress) : null; if (!escrowAddress) { logger.error(`No escrow address found for ${packetSrcChannelAttr.value} at block height ${block.header.height}`); return; } - const { amount, denom, receiver, sender } = JSON.parse(packetDataAttr.value); - const record = new IBCChannel( - packetSrcChannelAttr.value, + `${block.block.id}-${packetSrcChannelAttr.value}`, block.header.time as any, BigInt(block.header.height), packetSrcChannelAttr.value,