Skip to content

Commit

Permalink
fixup! feat: save escrow address as well
Browse files Browse the repository at this point in the history
fix: update method of getting escrow address
  • Loading branch information
frazarshad committed May 1, 2024
1 parent 648c6e2 commit 4797b46
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
10 changes: 7 additions & 3 deletions src/mappings/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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");
44 changes: 33 additions & 11 deletions src/mappings/mappingHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 4797b46

Please sign in to comment.