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 Mar 30, 2024
1 parent 9c6ed27 commit 06e1df2
Showing 1 changed file with 49 additions and 38 deletions.
87 changes: 49 additions & 38 deletions packages/sdk/src/cross-chain-messenger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1249,13 +1249,13 @@ export class CrossChainMessenger {
const challengePeriod =
oracleVersion === '1.0.0'
? // The ABI in the SDK does not contain FINALIZATION_PERIOD_SECONDS
// in OptimismPortal, so making an explicit call instead.
BigNumber.from(
await this.contracts.l1.OptimismPortal.provider.call({
to: this.contracts.l1.OptimismPortal.address,
data: '0xf4daa291', // FINALIZATION_PERIOD_SECONDS
})
)
// in OptimismPortal, so making an explicit call instead.
BigNumber.from(
await this.contracts.l1.OptimismPortal.provider.call({
to: this.contracts.l1.OptimismPortal.address,
data: '0xf4daa291', // FINALIZATION_PERIOD_SECONDS
})
)
: await this.contracts.l1.L2OutputOracle.FINALIZATION_PERIOD_SECONDS()
return challengePeriod.toNumber()
}
Expand Down Expand Up @@ -1496,7 +1496,7 @@ export class CrossChainMessenger {
// latest games are all invalid and the SDK would be forced to make a bunch of archive calls.
for (let i = matches.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1))
;[matches[i], matches[j]] = [matches[j], matches[i]]
;[matches[i], matches[j]] = [matches[j], matches[i]]
}

// Now we verify the proposals in the matches array.
Expand Down 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 06e1df2

Please sign in to comment.