Skip to content

Commit

Permalink
Fix hyperlane-xyz#78: Decode warp route messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
spilehchiha committed Nov 27, 2024
1 parent 4103c31 commit ef05c9d
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/features/messages/cards/ContentDetailsCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export function ContentDetailsCard({
recipient,
body,
decodedBody,
warpRouteDetails,
},
blur,
}: Props) {
Expand Down Expand Up @@ -105,6 +106,39 @@ export function ContentDetailsCard({
blurValue={blur}
/>
</div>
{warpRouteDetails && (
<>
<div className="mt-2 border-t border-gray-200 pt-4">
<h4 className="mb-3 text-sm font-medium text-gray-500">Warp Route Details:</h4>
<div className="flex flex-wrap gap-x-6 gap-y-4">
<KeyValueRow
label="Token:"
labelWidth="w-16"
display={warpRouteDetails.token}
displayWidth="w-64 sm:w-80"
showCopy={true}
blurValue={blur}
/>
<KeyValueRow
label="Amount:"
labelWidth="w-16"
display={warpRouteDetails.amount}
displayWidth="w-64 sm:w-80"
showCopy={true}
blurValue={blur}
/>
<KeyValueRow
label="Fee Paid:"
labelWidth="w-16"
display={warpRouteDetails.totalPayment}
displayWidth="w-64 sm:w-80"
showCopy={true}
blurValue={blur}
/>
</div>
</div>
</>
)}
<div>
<div className="flex items-center">
<label className="text-sm text-gray-500">Message Content:</label>
Expand Down
9 changes: 9 additions & 0 deletions src/features/messages/queries/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
MessagesStubQueryResult,
} from './fragments';

import { parseWarpRouteDetails } from '../../../utils/token';

/**
* ========================
* RESULT PARSING UTILITIES
Expand Down Expand Up @@ -111,6 +113,12 @@ function parseMessage(

const body = postgresByteaToString(m.message_body ?? '');
const decodedBody = tryUtf8DecodeBytes(body);
const warpRouteDetails = parseWarpRouteDetails(
body,
{ to: m.origin_tx_recipient, from: m.origin_tx_sender },
{ totalPayment: m.total_payment },
originMetadata
);

return {
...stub,
Expand Down Expand Up @@ -151,6 +159,7 @@ function parseMessage(
totalGasAmount: m.total_gas_amount.toString(),
totalPayment: m.total_payment.toString(),
numPayments: m.num_payments,
warpRouteDetails: warpRouteDetails ?? undefined,
};
} catch (error) {
logger.error('Error parsing message', error);
Expand Down
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,17 @@ export interface Message extends MessageStub {
totalGasAmount?: string;
totalPayment?: string;
numPayments?: number;
warpRouteDetails?: WarpRouteDetails;
}

export interface ExtendedLog extends providers.Log {
timestamp?: number;
from?: Address;
to?: Address;
}

export interface WarpRouteDetails {
token: string;
amount: string;
totalPayment: string;
}
26 changes: 26 additions & 0 deletions src/utils/token.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { parseWarpRouteMessage } from '@hyperlane-xyz/utils';
import { utils } from 'ethers';
import { postgresByteaToAddress } from '../features/messages/queries/encoding';
import { WarpRouteDetails } from '../types';

export function parseWarpRouteDetails(
messageBody: string,
originTx: { to?: string; from?: string } = {},
gasInfo: { totalPayment: any },
metadata?: any
): WarpRouteDetails | undefined {
try {
if (!messageBody?.trim()) throw new Error('Invalid message body');

const parsedMessage = parseWarpRouteMessage(messageBody);

return {
token: originTx?.to ? postgresByteaToAddress(originTx.to, metadata) : 'Unknown Token',
amount: utils.formatEther(parsedMessage.amount.toString()),
totalPayment: utils.formatEther(gasInfo.totalPayment.toString())
};
} catch (error) {
console.error('Failed to parse token details:', error, 'Message body:', messageBody);
return undefined;
}
}

0 comments on commit ef05c9d

Please sign in to comment.