-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
l1_oracle.go
69 lines (56 loc) · 2.15 KB
/
l1_oracle.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package rollups
import (
"context"
"fmt"
"math/big"
"slices"
"time"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rpc"
"github.com/smartcontractkit/chainlink-common/pkg/services"
"github.com/smartcontractkit/chainlink-common/pkg/logger"
"github.com/smartcontractkit/chainlink/v2/common/config"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/assets"
)
// L1Oracle provides interface for fetching L1-specific fee components if the chain is an L2.
// For example, on Optimistic Rollups, this oracle can return rollup-specific l1BaseFee
//
//go:generate mockery --quiet --name L1Oracle --output ./mocks/ --case=underscore
type L1Oracle interface {
services.Service
GasPrice(ctx context.Context) (*assets.Wei, error)
GetGasCost(ctx context.Context, tx *types.Transaction, blockNum *big.Int) (*assets.Wei, error)
}
//go:generate mockery --quiet --name l1OracleClient --output ./mocks/ --case=underscore --structname L1OracleClient
type l1OracleClient interface {
CallContract(ctx context.Context, msg ethereum.CallMsg, blockNumber *big.Int) ([]byte, error)
BatchCallContext(ctx context.Context, b []rpc.BatchElem) error
}
type priceEntry struct {
price *assets.Wei
timestamp time.Time
}
const (
// Interval at which to poll for L1BaseFee. A good starting point is the L1 block time.
PollPeriod = 6 * time.Second
)
var supportedChainTypes = []config.ChainType{config.ChainArbitrum, config.ChainOptimismBedrock, config.ChainKroma, config.ChainScroll}
func IsRollupWithL1Support(chainType config.ChainType) bool {
return slices.Contains(supportedChainTypes, chainType)
}
func NewL1GasOracle(lggr logger.Logger, ethClient l1OracleClient, chainType config.ChainType) L1Oracle {
if !IsRollupWithL1Support(chainType) {
return nil
}
var l1Oracle L1Oracle
switch chainType {
case config.ChainOptimismBedrock, config.ChainKroma, config.ChainScroll:
l1Oracle = NewOpStackL1GasOracle(lggr, ethClient, chainType)
case config.ChainArbitrum:
l1Oracle = NewArbitrumL1GasOracle(lggr, ethClient)
default:
panic(fmt.Sprintf("Received unspported chaintype %s", chainType))
}
return l1Oracle
}