Skip to content

Instantly share code, notes, and snippets.

@gigamesh
Created June 24, 2021 01:20
Show Gist options
  • Save gigamesh/ad7e12eeb6a42538b7fbd9c28b41acc9 to your computer and use it in GitHub Desktop.
Save gigamesh/ad7e12eeb6a42538b7fbd9c28b41acc9 to your computer and use it in GitHub Desktop.
const processSentMessage = (
sentTx: Transaction,
layer: number,
relayedTxs: { msgHash: string; txHash: string; timestamp: number }[]
) => {
const tx = { ...sentTx };
const xDomainInterface = new ethers.utils.Interface(abis.xDomainMessenger);
const sentMsgHash = ethers.utils.solidityKeccak256(['bytes'], [tx.message]);
const relayedTx = relayedTxs.find(msg => msg.msgHash === sentMsgHash);
const [_, toAddress, message] = xDomainInterface.decodeFunctionData('relayMessage', tx.message as ethers.BytesLike);
const tokenData = tokenList.find(token => token.extensions?.optimismBridgeAddress === toAddress);
if (tokenData) {
// TODO: remove these checks when Synthetix updates their bridge to the standard interface
const bridgeAbiKey = tokenData.symbol === 'SNX' ? 'snxBridge' : 'standardBridge';
const bridgeInterface = new ethers.utils.Interface(abis[layer === 1 ? 'l2' : 'l1'][bridgeAbiKey]);
const functionName =
layer === 2
? tokenData.symbol === 'SNX'
? 'completeWithdrawal'
: 'finalizeWithdrawal'
: tokenData.symbol === 'SNX'
? 'completeDeposit'
: 'finalizeDeposit';
try {
const [fromAddress, amount] = bridgeInterface.decodeFunctionData(functionName, message as ethers.BytesLike);
tx.symbol = tokenData.symbol;
tx.tokenId = tokens[tokenData.symbol].id;
tx.iconURL = tokens[tokenData.symbol].iconURL;
tx.amount = amount;
} catch (err) {
if (err.message.includes('does not match function')) {
console.error(`Unknown function called on ${tokenData.name} contract. ${toAddress}`);
}
}
}
tx.from = sentTx.from;
tx.to = decodeSentMessage(tx.message as string)[1];
tx.timestamp = tx.timestamp * 1000;
if (layer === 1) {
tx.layer1Hash = tx.txHash;
tx.layer2Hash = relayedTx?.txHash;
} else {
tx.layer1Hash = relayedTx?.txHash;
tx.layer2Hash = tx.txHash;
tx.awaitingRelay =
!tx.layer1Hash &&
DateTime.fromMillis(tx.timestamp)
.plus({ days: 7 })
.toMillis() < Date.now();
}
tx.relayedTxTimestamp = relayedTx && relayedTx.timestamp * 1000;
return tx;
};
const getFilteredRelayedTxs = async ({
sentMsgTxs,
relayedMsgTxs,
}: {
sentMsgTxs: Transaction[];
relayedMsgTxs: QueryResult<any, Record<string, any>>;
}) => {
const sentMsgHashes = sentMsgTxs.map((msgTx: Transaction) => {
return ethers.utils.solidityKeccak256(['bytes'], [msgTx.message]);
});
const relayedTxs = (
await relayedMsgTxs.fetchMore({
query: GET_RELAYED_MSGS_BY_HASH_LIST,
variables: { searchHashes: sentMsgHashes },
})
).data.relayedMessages;
return relayedTxs;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment