Skip to content

Commit

Permalink
fix: remove calldata parser to improve sync times
Browse files Browse the repository at this point in the history
Signed-off-by: Tomás Migone <[email protected]>
  • Loading branch information
tmigone authored and juanmardefago committed Feb 16, 2023
1 parent 9261844 commit 4d83c38
Showing 1 changed file with 3 additions and 48 deletions.
51 changes: 3 additions & 48 deletions src/mappings/helpers/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,21 @@ import {
Bytes,
BigInt,
ethereum,
log,
crypto,
ByteArray,
} from "@graphprotocol/graph-ts";
import { DepositInitiated } from "../../types/L1GraphTokenGateway/L1GraphTokenGateway";
import { bigIntToBytes, padZeros, strip0xPrefix } from "./byte";
import { bigIntToBytes, padZeros } from "./byte";
import { RLPEncodeArray } from "./rlp";
import { getOutBoxTransactionExecutedData } from "./events/OutBoxTransactionExecuted";
import { getMessageDeliveredData } from "./events/MessageDelivered";
import { getInboxMessageDeliveredData } from "./events/InboxMessageDelivered";
import { getTxToL2Data } from "./events/TxToL2";

// Gets transactionIndex
// Returns null if the withdrawal call is made under the following conditions:
// - Call is made from another contract (i.e: a multicall call) and
// - Call contains multiple 'OutBoxTransactionExecuted' events (i.e: multiple withdrawals on same multicall call)
// Returns null if the withdrawal call contains multiple 'OutBoxTransactionExecuted' events (i.e: multiple withdrawals on same multicall call)
export function getTransactionIndex(event: ethereum.Event): BigInt | null {
return (
getTransactionIndexFromCalldata(event) || getTransactionIndexFromLogs(event)
);
}

// Gets transactionIndex from the calldata
// Returns null if the call is made from another contract (i.e: a multicall call)
export function getTransactionIndexFromCalldata(
event: ethereum.Event
): BigInt | null {
let stringCallData = event.transaction.input.toHexString();
let strippedCallData = strip0xPrefix(stringCallData);

// Validate selector
// Method signature: executeTransaction(bytes32[],uint256,address,address,uint256,uint256,uint256,uint256,bytes)
// MethodID: 0x08635a95
let selector = strippedCallData.slice(0, 8);
if (selector != "08635a95") {
log.error("Invalid function selector", [selector]);
return null;
}

// Decode ABI using a modified types mapping since the decode method fails with dynamic types
// - Original types mapping: (bytes32[],uint256,address,address,uint256,uint256,uint256,uint256,bytes)
// - Modified types mapping: (uint256,uint256,address,address,uint256,uint256,uint256,uint256)
//
// The bytes32[] is replaced with uint256, this is ok since it's a dynamic type so the actual value stored
// is the length of the bytes array which is a uint256 and the array contents are appended at the end of the calldata
//
// The bytes at the end can easily be dropped since we don't use it
let types =
"(uint256,uint256,address,address,uint256,uint256,uint256,uint256)";

let decoded = ethereum.decode(
types,
Bytes.fromHexString(strippedCallData.substring(8))
);
if (decoded !== null) {
return decoded.toTuple()[1].toBigInt(); // transactionIndex
} else {
log.error("Could not decode call data!", []);
return null;
}
return getTransactionIndexFromLogs(event);
}

// Get transactionIndex from the 'OutBoxTransactionExecuted' event, emitted by Arbitrum's Outbox contract on the same tx
Expand Down

0 comments on commit 4d83c38

Please sign in to comment.