Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
nebula
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
exchain
nebula
Commits
de14541a
Unverified
Commit
de14541a
authored
Jan 24, 2022
by
Kelvin Fichter
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(sdk): implement L2 message gas estimation
parent
4c55210f
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
116 additions
and
4 deletions
+116
-4
cross-chain-provider.ts
packages/sdk/src/cross-chain-provider.ts
+20
-2
cross-chain-provider.ts
packages/sdk/src/interfaces/cross-chain-provider.ts
+8
-1
cross-chain-provider.spec.ts
packages/sdk/test/cross-chain-provider.spec.ts
+88
-1
No files found.
packages/sdk/src/cross-chain-provider.ts
View file @
de14541a
...
@@ -421,9 +421,27 @@ export class CrossChainProvider implements ICrossChainProvider {
...
@@ -421,9 +421,27 @@ export class CrossChainProvider implements ICrossChainProvider {
}
}
public
async
estimateL2MessageGasLimit
(
public
async
estimateL2MessageGasLimit
(
message
:
MessageLike
message
:
MessageLike
,
opts
?:
{
bufferPercent
?:
number
}
):
Promise
<
BigNumber
>
{
):
Promise
<
BigNumber
>
{
throw
new
Error
(
'
Not implemented
'
)
const
resolved
=
await
this
.
toCrossChainMessage
(
message
)
// L2 message gas estimation is only used for L1 => L2 messages.
if
(
resolved
.
direction
===
MessageDirection
.
L2_TO_L1
)
{
throw
new
Error
(
`cannot estimate gas limit for L2 => L1 message`
)
}
const
estimate
=
await
this
.
l2Provider
.
estimateGas
({
from
:
resolved
.
sender
,
to
:
resolved
.
target
,
data
:
resolved
.
message
,
})
// Return the estimate plus a buffer of 20% just in case.
const
bufferPercent
=
opts
?.
bufferPercent
||
20
return
estimate
.
mul
(
100
+
bufferPercent
).
div
(
100
)
}
}
public
async
estimateMessageWaitTimeSeconds
(
public
async
estimateMessageWaitTimeSeconds
(
...
...
packages/sdk/src/interfaces/cross-chain-provider.ts
View file @
de14541a
...
@@ -201,9 +201,16 @@ export interface ICrossChainProvider {
...
@@ -201,9 +201,16 @@ export interface ICrossChainProvider {
* L1 => L2 messages. You would supply this gas limit when sending the message to L2.
* L1 => L2 messages. You would supply this gas limit when sending the message to L2.
*
*
* @param message Message get a gas estimate for.
* @param message Message get a gas estimate for.
* @param opts Options object.
* @param opts.bufferPercent Percentage of gas to add to the estimate. Defaults to 20.
* @returns Estimates L2 gas limit.
* @returns Estimates L2 gas limit.
*/
*/
estimateL2MessageGasLimit
(
message
:
MessageLike
):
Promise
<
BigNumber
>
estimateL2MessageGasLimit
(
message
:
MessageLike
,
opts
?:
{
bufferPercent
?:
number
}
):
Promise
<
BigNumber
>
/**
/**
* Returns the estimated amount of time before the message can be executed. When this is a
* Returns the estimated amount of time before the message can be executed. When this is a
...
...
packages/sdk/test/cross-chain-provider.spec.ts
View file @
de14541a
import
{
Provider
}
from
'
@ethersproject/abstract-provider
'
import
{
Provider
}
from
'
@ethersproject/abstract-provider
'
import
{
expectApprox
}
from
'
@eth-optimism/core-utils
'
import
{
Contract
}
from
'
ethers
'
import
{
Contract
}
from
'
ethers
'
import
{
ethers
}
from
'
hardhat
'
import
{
ethers
}
from
'
hardhat
'
...
@@ -1091,7 +1092,93 @@ describe('CrossChainProvider', () => {
...
@@ -1091,7 +1092,93 @@ describe('CrossChainProvider', () => {
})
})
describe
(
'
estimateL2MessageGasLimit
'
,
()
=>
{
describe
(
'
estimateL2MessageGasLimit
'
,
()
=>
{
it
(
'
should perform a gas estimation of the L2 action
'
)
let
provider
:
CrossChainProvider
beforeEach
(
async
()
=>
{
provider
=
new
CrossChainProvider
({
l1Provider
:
ethers
.
provider
,
l2Provider
:
ethers
.
provider
,
l1ChainId
:
31337
,
})
})
describe
(
'
when the message is an L1 to L2 message
'
,
()
=>
{
it
(
'
should return an accurate gas estimate plus a ~20% buffer
'
,
async
()
=>
{
const
message
=
{
direction
:
MessageDirection
.
L1_TO_L2
,
target
:
'
0x
'
+
'
11
'
.
repeat
(
20
),
sender
:
'
0x
'
+
'
22
'
.
repeat
(
20
),
message
:
'
0x
'
+
'
33
'
.
repeat
(
64
),
messageNonce
:
1234
,
logIndex
:
0
,
blockNumber
:
1234
,
transactionHash
:
'
0x
'
+
'
44
'
.
repeat
(
32
),
}
const
estimate
=
await
ethers
.
provider
.
estimateGas
({
to
:
message
.
target
,
from
:
message
.
sender
,
data
:
message
.
message
,
})
// Approximately 20% greater than the estimate, +/- 1%.
expectApprox
(
await
provider
.
estimateL2MessageGasLimit
(
message
),
estimate
.
mul
(
120
).
div
(
100
),
{
percentUpperDeviation
:
1
,
percentLowerDeviation
:
1
,
}
)
})
it
(
'
should return an accurate gas estimate when a custom buffer is provided
'
,
async
()
=>
{
const
message
=
{
direction
:
MessageDirection
.
L1_TO_L2
,
target
:
'
0x
'
+
'
11
'
.
repeat
(
20
),
sender
:
'
0x
'
+
'
22
'
.
repeat
(
20
),
message
:
'
0x
'
+
'
33
'
.
repeat
(
64
),
messageNonce
:
1234
,
logIndex
:
0
,
blockNumber
:
1234
,
transactionHash
:
'
0x
'
+
'
44
'
.
repeat
(
32
),
}
const
estimate
=
await
ethers
.
provider
.
estimateGas
({
to
:
message
.
target
,
from
:
message
.
sender
,
data
:
message
.
message
,
})
// Approximately 30% greater than the estimate, +/- 1%.
expectApprox
(
await
provider
.
estimateL2MessageGasLimit
(
message
,
{
bufferPercent
:
30
,
}),
estimate
.
mul
(
130
).
div
(
100
),
{
percentUpperDeviation
:
1
,
percentLowerDeviation
:
1
,
}
)
})
})
describe
(
'
when the message is an L2 to L1 message
'
,
()
=>
{
it
(
'
should throw an error
'
,
async
()
=>
{
const
message
=
{
direction
:
MessageDirection
.
L2_TO_L1
,
target
:
'
0x
'
+
'
11
'
.
repeat
(
20
),
sender
:
'
0x
'
+
'
22
'
.
repeat
(
20
),
message
:
'
0x
'
+
'
33
'
.
repeat
(
64
),
messageNonce
:
1234
,
logIndex
:
0
,
blockNumber
:
1234
,
transactionHash
:
'
0x
'
+
'
44
'
.
repeat
(
32
),
}
await
expect
(
provider
.
estimateL2MessageGasLimit
(
message
)).
to
.
be
.
rejected
})
})
})
})
describe
(
'
estimateMessageWaitTimeBlocks
'
,
()
=>
{
describe
(
'
estimateMessageWaitTimeBlocks
'
,
()
=>
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment