Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Will Cory authored and Will Cory committed Apr 1, 2024
1 parent adba37d commit 1139b74
Showing 1 changed file with 41 additions and 30 deletions.
71 changes: 41 additions & 30 deletions packages/sdk/src/cross-chain-messenger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2308,12 +2308,6 @@ export class CrossChainMessenger {

if (this.bedrock) {
// get everything we need to finalize
const withdrawal = await this.toLowLevelMessage(resolved, messageIndex)
const xdmWithdrawal =
this.contracts.l1.L1CrossDomainMessenger.interface.decodeFunctionData(
'relayMessage',
withdrawal.message
)
const messageHashV1 = hashCrossDomainMessagev1(
resolved.messageNonce,
resolved.sender,
Expand All @@ -2323,37 +2317,54 @@ export class CrossChainMessenger {
resolved.message
)

const isFailed =
await this.contracts.l1.L1CrossDomainMessenger.failedMessages(
// fetch the following
// 1. Whether it needs to be replayed because it failed
// 2. The withdrawal as a low level message
const [isFailed, withdrawal] = await Promise.allSettled([
this.contracts.l1.L1CrossDomainMessenger.failedMessages(
messageHashV1
)
),
this.toLowLevelMessage(resolved, messageIndex),
])

// if failed we need to replay the message rather than finalizing it
if (isFailed === true) {
const tx =
await this.contracts.l1.L1CrossDomainMessenger.populateTransaction.relayMessage(
xdmWithdrawal._nonce,
xdmWithdrawal._sender,
xdmWithdrawal._target,
xdmWithdrawal._value,
xdmWithdrawal._minGasLimit,
xdmWithdrawal._message,
opts?.overrides || {}
)
return tx
// handle errors
if (
isFailed.status === 'rejected' ||
withdrawal.status === 'rejected'
) {
const rejections = [isFailed, withdrawal]
.filter((p) => p.status === 'rejected')
.map((p: PromiseRejectedResult) => p.reason)
throw rejections.length > 1
? new AggregateError(rejections)
: rejections[0]
}
if ('todo remove me') {
throw new Error('should not be trying to finalize')

if (isFailed.value === true) {
const xdmWithdrawal =
this.contracts.l1.L1CrossDomainMessenger.interface.decodeFunctionData(
'relayMessage',
withdrawal.value.message
)
return this.contracts.l1.L1CrossDomainMessenger.populateTransaction.relayMessage(
xdmWithdrawal._nonce,
xdmWithdrawal._sender,
xdmWithdrawal._target,
xdmWithdrawal._value,
xdmWithdrawal._minGasLimit,
xdmWithdrawal._message,
opts?.overrides || {}
)
}

return this.contracts.l1.OptimismPortal.populateTransaction.finalizeWithdrawalTransaction(
[
withdrawal.messageNonce,
withdrawal.sender,
withdrawal.target,
withdrawal.value,
withdrawal.minGasLimit,
withdrawal.message,
withdrawal.value.messageNonce,
withdrawal.value.sender,
withdrawal.value.target,
withdrawal.value.value,
withdrawal.value.minGasLimit,
withdrawal.value.message,
],
opts?.overrides || {}
)
Expand Down

0 comments on commit 1139b74

Please sign in to comment.