Skip to content

Commit

Permalink
new decode pack func for bridgeV2 bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
parodime committed Feb 23, 2025
1 parent 5e2fdc6 commit e1b5265
Showing 1 changed file with 43 additions and 20 deletions.
63 changes: 43 additions & 20 deletions packages/rfq-indexer/indexer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,47 @@ import {
} from '@/ponder.config'
import { decodeAbiParameters } from 'viem/utils'

Check failure on line 10 in packages/rfq-indexer/indexer/src/index.ts

View workflow job for this annotation

GitHub Actions / lint

`viem/utils` import should occur before import of `@/generated`


Check warning on line 12 in packages/rfq-indexer/indexer/src/index.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `⏎`
// v2 uses packed data for request bytes & so cannot be decoded via same viem func
// that can be used for v1... This func is somewhat dynamic but still has some assumptions
// & generally will only work with a v2 request bytes payload. future changes to fastbridge
// payloads will probably not work w/ this function as-is & would require tweaks
function decodePackedV2BridgeTx(types: Array<{type: string, name: string}>, data: string) {

Check warning on line 17 in packages/rfq-indexer/indexer/src/index.ts

View workflow job for this annotation

GitHub Actions / lint

Use const or class constructors instead of named functions

Check warning on line 17 in packages/rfq-indexer/indexer/src/index.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `types:·Array<{type:·string,·name:·string}>,·data:·string` with `⏎··types:·Array<{·type:·string;·name:·string·}>,⏎··data:·string⏎`
let offset = 0;

Check warning on line 18 in packages/rfq-indexer/indexer/src/index.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `;`
const results:any = {};

Check warning on line 19 in packages/rfq-indexer/indexer/src/index.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `any·=·{};` with `·any·=·{}`

if (data.startsWith('0x')) {
data = data.slice(2);

Check warning on line 22 in packages/rfq-indexer/indexer/src/index.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `;`
}

types.forEach(({ type, name }) => {
if (type.startsWith('uint')) {
const bits = parseInt(type.slice(4)) || 256; // Default to 256 bits if unspecified

Check failure on line 27 in packages/rfq-indexer/indexer/src/index.ts

View workflow job for this annotation

GitHub Actions / lint

Missing radix parameter

Check warning on line 27 in packages/rfq-indexer/indexer/src/index.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `;`
const length = bits / 8 * 2;

Check warning on line 28 in packages/rfq-indexer/indexer/src/index.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `bits·/·8·*·2;·` with `(bits·/·8)·*·2`
const value = '0x' + data.substring(offset, offset + length);

Check warning on line 29 in packages/rfq-indexer/indexer/src/index.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `;`
results[name] = BigInt(value);

Check warning on line 30 in packages/rfq-indexer/indexer/src/index.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `;`
offset += length;
} else if (type.startsWith('bytes')) {
const value = '0x' + data.substring(offset); // assume rest of the string must be part of the bytes data
results[name] = value;
offset = data.length;
} else if (type === 'address') {
const length = 40;
const value = '0x' + data.substring(offset, offset + length);
results[name] = value;
offset += length;
} else if (type === 'bool') {
const value = data.substring(offset, offset + 2);
results[name] = value === '01';
offset += 2;
} else {
throw new Error(`Unsupported type ${type}`);
}
});

return results;
}

// ponder doesnt seem to handle a situation where two contracts on the same chain share the same topic.
// it seems instead to process the topic under *both* contracts, so this extra step is necessary to ensure
// it only performs indexing functions on the handler respective to the contract. duplicate errors will occur otherwise
Expand Down Expand Up @@ -85,7 +126,7 @@ ponder.on('v2:BridgeRequested', async ({ event, context }) => {
log: { blockNumber },
} = event

const decodedRequestArray = decodeAbiParameters(
const decodedRequest = decodePackedV2BridgeTx(
[
{ type: 'uint32', name: 'originChainId' },
{ type: 'uint32', name: 'destChainId' },
Expand All @@ -106,24 +147,6 @@ ponder.on('v2:BridgeRequested', async ({ event, context }) => {
request
);

const decodedRequest = {
originChainId: decodedRequestArray[0],
destChainId: decodedRequestArray[1],
originSender: decodedRequestArray[2],
destRecipient: decodedRequestArray[3],
originToken: decodedRequestArray[4],
destToken: decodedRequestArray[5],
originAmount: decodedRequestArray[6],
destAmount: decodedRequestArray[7],
originFeeAmount: decodedRequestArray[8],
deadline: decodedRequestArray[9],
nonce: decodedRequestArray[10],
exclusivityRelayer: decodedRequestArray[11],
exclusivityEndTime: decodedRequestArray[12],
zapNative: decodedRequestArray[13],
zapData: decodedRequestArray[14]
};

await BridgeRequestEvents.create({
id: transactionId,
data: {
Expand All @@ -141,7 +164,7 @@ ponder.on('v2:BridgeRequested', async ({ event, context }) => {
destAmount,
destAmountFormatted: formatAmount(destAmount, destToken),
sendChainGas,
exclusivityRelayer: trim(decodedRequest.exclusivityRelayer),
exclusivityRelayer: decodedRequest.exclusivityRelayer,
exclusivityEndTime: decodedRequest.exclusivityEndTime,
zapNative: decodedRequest.zapNative,
zapData: decodedRequest.zapData,
Expand Down

0 comments on commit e1b5265

Please sign in to comment.