mirror of
https://github.com/scroll-tech/scroll.git
synced 2026-01-11 23:18:07 -05:00
Compare commits
40 Commits
v4.0.30
...
store_proo
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
117b451bd4 | ||
|
|
6850150e5d | ||
|
|
f4c0b075b0 | ||
|
|
477d13212e | ||
|
|
5ebc6ae531 | ||
|
|
3caeab8f7d | ||
|
|
d4c552a751 | ||
|
|
664af271d2 | ||
|
|
3980399599 | ||
|
|
92f41b08d7 | ||
|
|
3d983c3053 | ||
|
|
a1422a7ed6 | ||
|
|
e15d7d8fc3 | ||
|
|
d0979a821c | ||
|
|
4057425746 | ||
|
|
e84f8c359f | ||
|
|
090ae5c29c | ||
|
|
1af1541a09 | ||
|
|
cd2b758b97 | ||
|
|
ee05ff1be5 | ||
|
|
9979cec2a2 | ||
|
|
6b149ce3e4 | ||
|
|
a2ca7ce9a1 | ||
|
|
38f48744c2 | ||
|
|
5849862967 | ||
|
|
904453cb87 | ||
|
|
2b1de651b3 | ||
|
|
503a856ced | ||
|
|
ed67808672 | ||
|
|
9d243960cf | ||
|
|
1026d4883d | ||
|
|
43266d2885 | ||
|
|
453c645bea | ||
|
|
0eaea3985e | ||
|
|
e68dabb035 | ||
|
|
6df380f15e | ||
|
|
181e547c22 | ||
|
|
eb87ad3da0 | ||
|
|
deaf766a81 | ||
|
|
3b2fa02cd7 |
1
.github/workflows/bridge_history_api.yml
vendored
1
.github/workflows/bridge_history_api.yml
vendored
@@ -51,6 +51,7 @@ jobs:
|
||||
uses: actions/checkout@v2
|
||||
- name: Test
|
||||
run: |
|
||||
go get ./...
|
||||
make test
|
||||
- name: Upload coverage reports to Codecov
|
||||
uses: codecov/codecov-action@v3
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
@@ -10,22 +9,20 @@ import (
|
||||
"github.com/kataras/iris/v12"
|
||||
"github.com/kataras/iris/v12/mvc"
|
||||
"github.com/urfave/cli/v2"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"bridge-history-api/config"
|
||||
"bridge-history-api/controller"
|
||||
"bridge-history-api/db"
|
||||
"bridge-history-api/service"
|
||||
cutils "bridge-history-api/utils"
|
||||
|
||||
"scroll-tech/common/database"
|
||||
)
|
||||
|
||||
var (
|
||||
app *cli.App
|
||||
db *gorm.DB
|
||||
subCtx context.Context
|
||||
app *cli.App
|
||||
)
|
||||
|
||||
var database db.OrmFactory
|
||||
|
||||
func pong(ctx iris.Context) {
|
||||
_, err := ctx.WriteString("pong")
|
||||
if err != nil {
|
||||
@@ -36,8 +33,7 @@ func pong(ctx iris.Context) {
|
||||
func setupQueryByAddressHandler(backendApp *mvc.Application) {
|
||||
// Register Dependencies.
|
||||
backendApp.Register(
|
||||
subCtx,
|
||||
db,
|
||||
database,
|
||||
service.NewHistoryService,
|
||||
)
|
||||
|
||||
@@ -48,8 +44,7 @@ func setupQueryByAddressHandler(backendApp *mvc.Application) {
|
||||
func setupQueryClaimableHandler(backendApp *mvc.Application) {
|
||||
// Register Dependencies.
|
||||
backendApp.Register(
|
||||
subCtx,
|
||||
db,
|
||||
database,
|
||||
service.NewHistoryService,
|
||||
)
|
||||
|
||||
@@ -59,8 +54,7 @@ func setupQueryClaimableHandler(backendApp *mvc.Application) {
|
||||
|
||||
func setupQueryByHashHandler(backendApp *mvc.Application) {
|
||||
backendApp.Register(
|
||||
subCtx,
|
||||
db,
|
||||
database,
|
||||
service.NewHistoryService,
|
||||
)
|
||||
backendApp.Handle(new(controller.QueryHashController))
|
||||
@@ -92,22 +86,15 @@ func action(ctx *cli.Context) error {
|
||||
if err != nil {
|
||||
log.Crit("failed to load config file", "config file", cfgFile, "error", err)
|
||||
}
|
||||
dbCfg := &database.Config{
|
||||
DriverName: cfg.DB.DriverName,
|
||||
DSN: cfg.DB.DSN,
|
||||
MaxOpenNum: cfg.DB.MaxOpenNum,
|
||||
MaxIdleNum: cfg.DB.MaxIdleNum,
|
||||
}
|
||||
db, err = database.InitDB(dbCfg)
|
||||
database, err = db.NewOrmFactory(cfg)
|
||||
if err != nil {
|
||||
log.Crit("failed to init db", "err", err)
|
||||
log.Crit("can not connect to database", "err", err)
|
||||
}
|
||||
defer func() {
|
||||
if deferErr := database.CloseDB(db); deferErr != nil {
|
||||
log.Error("failed to close db", "err", err)
|
||||
if err = database.Close(); err != nil {
|
||||
log.Error("failed to close database", "err", err)
|
||||
}
|
||||
}()
|
||||
subCtx = ctx.Context
|
||||
bridgeApp := iris.New()
|
||||
bridgeApp.UseRouter(corsOptions)
|
||||
bridgeApp.Get("/ping", pong).Describe("healthcheck")
|
||||
|
||||
@@ -14,10 +14,8 @@ import (
|
||||
"bridge-history-api/config"
|
||||
"bridge-history-api/crossmsg"
|
||||
"bridge-history-api/crossmsg/messageproof"
|
||||
"bridge-history-api/orm"
|
||||
"bridge-history-api/db"
|
||||
cutils "bridge-history-api/utils"
|
||||
|
||||
"scroll-tech/common/database"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -57,18 +55,9 @@ func action(ctx *cli.Context) error {
|
||||
log.Crit("failed to connect l2 geth", "config file", cfgFile, "error", err)
|
||||
}
|
||||
|
||||
dbCfg := &database.Config{
|
||||
DriverName: cfg.DB.DriverName,
|
||||
DSN: cfg.DB.DSN,
|
||||
MaxOpenNum: cfg.DB.MaxOpenNum,
|
||||
MaxIdleNum: cfg.DB.MaxIdleNum,
|
||||
}
|
||||
db, err := database.InitDB(dbCfg)
|
||||
if err != nil {
|
||||
log.Crit("failed to init db", "err", err)
|
||||
}
|
||||
db, err := db.NewOrmFactory(cfg)
|
||||
defer func() {
|
||||
if deferErr := database.CloseDB(db); deferErr != nil {
|
||||
if deferErr := db.Close(); deferErr != nil {
|
||||
log.Error("failed to close db", "err", err)
|
||||
}
|
||||
}()
|
||||
@@ -116,14 +105,12 @@ func action(ctx *cli.Context) error {
|
||||
go l2crossMsgFetcher.Start()
|
||||
defer l2crossMsgFetcher.Stop()
|
||||
|
||||
CrossMsgOrm := orm.NewCrossMsg(db)
|
||||
|
||||
// BlockTimestamp fetcher for l1 and l2
|
||||
l1BlockTimeFetcher := crossmsg.NewBlockTimestampFetcher(subCtx, cfg.L1.Confirmation, int(cfg.L1.BlockTime), l1client, CrossMsgOrm.UpdateL1BlockTimestamp, CrossMsgOrm.GetL1EarliestNoBlockTimestampHeight)
|
||||
l1BlockTimeFetcher := crossmsg.NewBlockTimestampFetcher(subCtx, cfg.L1.Confirmation, int(cfg.L1.BlockTime), l1client, db.UpdateL1BlockTimestamp, db.GetL1EarliestNoBlockTimestampHeight)
|
||||
go l1BlockTimeFetcher.Start()
|
||||
defer l1BlockTimeFetcher.Stop()
|
||||
|
||||
l2BlockTimeFetcher := crossmsg.NewBlockTimestampFetcher(subCtx, cfg.L2.Confirmation, int(cfg.L2.BlockTime), l2client, CrossMsgOrm.UpdateL2BlockTimestamp, CrossMsgOrm.GetL2EarliestNoBlockTimestampHeight)
|
||||
l2BlockTimeFetcher := crossmsg.NewBlockTimestampFetcher(subCtx, cfg.L2.Confirmation, int(cfg.L2.BlockTime), l2client, db.UpdateL2BlockTimestamp, db.GetL2EarliestNoBlockTimestampHeight)
|
||||
go l2BlockTimeFetcher.Start()
|
||||
defer l2BlockTimeFetcher.Stop()
|
||||
|
||||
|
||||
@@ -2,14 +2,13 @@ package app
|
||||
|
||||
import (
|
||||
"github.com/ethereum/go-ethereum/log"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/urfave/cli/v2"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"bridge-history-api/config"
|
||||
"bridge-history-api/orm/migrate"
|
||||
"bridge-history-api/db"
|
||||
"bridge-history-api/db/migrate"
|
||||
"bridge-history-api/utils"
|
||||
|
||||
"scroll-tech/common/database"
|
||||
)
|
||||
|
||||
func getConfig(ctx *cli.Context) (*config.Config, error) {
|
||||
@@ -21,14 +20,14 @@ func getConfig(ctx *cli.Context) (*config.Config, error) {
|
||||
return dbCfg, nil
|
||||
}
|
||||
|
||||
func initDB(dbCfg *config.DBConfig) (*gorm.DB, error) {
|
||||
cfg := &database.Config{
|
||||
DriverName: dbCfg.DriverName,
|
||||
DSN: dbCfg.DSN,
|
||||
MaxOpenNum: dbCfg.MaxOpenNum,
|
||||
MaxIdleNum: dbCfg.MaxIdleNum,
|
||||
func initDB(dbCfg *config.Config) (*sqlx.DB, error) {
|
||||
factory, err := db.NewOrmFactory(dbCfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return database.InitDB(cfg)
|
||||
log.Debug("Got db config from env", "driver name", dbCfg.DB.DriverName, "dsn", dbCfg.DB.DSN)
|
||||
|
||||
return factory.GetDB(), nil
|
||||
}
|
||||
|
||||
// resetDB clean or reset database.
|
||||
@@ -37,15 +36,11 @@ func resetDB(ctx *cli.Context) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
gormDB, err := initDB(cfg.DB)
|
||||
db, err := initDB(cfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
db, err := gormDB.DB()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = migrate.ResetDB(db)
|
||||
err = migrate.ResetDB(db.DB)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -59,15 +54,12 @@ func checkDBStatus(ctx *cli.Context) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
gormDB, err := initDB(cfg.DB)
|
||||
db, err := initDB(cfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
db, err := gormDB.DB()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return migrate.Status(db)
|
||||
|
||||
return migrate.Status(db.DB)
|
||||
}
|
||||
|
||||
// dbVersion return the latest version
|
||||
@@ -76,15 +68,12 @@ func dbVersion(ctx *cli.Context) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
gormDB, err := initDB(cfg.DB)
|
||||
db, err := initDB(cfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
db, err := gormDB.DB()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
version, err := migrate.Current(db)
|
||||
|
||||
version, err := migrate.Current(db.DB)
|
||||
log.Info("show database version", "db version", version)
|
||||
|
||||
return err
|
||||
@@ -96,15 +85,12 @@ func migrateDB(ctx *cli.Context) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
gormDB, err := initDB(cfg.DB)
|
||||
db, err := initDB(cfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
db, err := gormDB.DB()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return migrate.Migrate(db)
|
||||
|
||||
return migrate.Migrate(db.DB)
|
||||
}
|
||||
|
||||
// rollbackDB rollback db by version
|
||||
@@ -113,14 +99,10 @@ func rollbackDB(ctx *cli.Context) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
gormDB, err := initDB(cfg.DB)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
db, err := gormDB.DB()
|
||||
db, err := initDB(cfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
version := ctx.Int64("version")
|
||||
return migrate.Rollback(db, &version)
|
||||
return migrate.Rollback(db.DB, &version)
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ type QueryClaimableController struct {
|
||||
|
||||
// Get defines the http get method behavior for QueryClaimableController
|
||||
func (c *QueryClaimableController) Get(req model.QueryByAddressRequest) (*model.QueryByAddressResponse, error) {
|
||||
txs, total, err := c.Service.GetClaimableTxsByAddress(common.HexToAddress(req.Address), req.Offset, req.Limit)
|
||||
txs, total, err := c.Service.GetClaimableTxsByAddress(common.HexToAddress(req.Address), int64(req.Offset), int64(req.Limit))
|
||||
if err != nil {
|
||||
return &model.QueryByAddressResponse{Message: "500", Data: &model.Data{}}, err
|
||||
}
|
||||
@@ -38,7 +38,7 @@ func (c *QueryClaimableController) Get(req model.QueryByAddressRequest) (*model.
|
||||
|
||||
// Get defines the http get method behavior for QueryAddressController
|
||||
func (c *QueryAddressController) Get(req model.QueryByAddressRequest) (*model.QueryByAddressResponse, error) {
|
||||
message, total, err := c.Service.GetTxsByAddress(common.HexToAddress(req.Address), req.Offset, req.Limit)
|
||||
message, total, err := c.Service.GetTxsByAddress(common.HexToAddress(req.Address), int64(req.Offset), int64(req.Limit))
|
||||
if err != nil {
|
||||
return &model.QueryByAddressResponse{Message: "500", Data: &model.Data{}}, err
|
||||
}
|
||||
|
||||
@@ -7,10 +7,9 @@ import (
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/ethclient"
|
||||
"github.com/ethereum/go-ethereum/log"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"bridge-history-api/crossmsg/messageproof"
|
||||
"bridge-history-api/orm"
|
||||
"bridge-history-api/db"
|
||||
"bridge-history-api/utils"
|
||||
)
|
||||
|
||||
@@ -22,13 +21,12 @@ type BatchInfoFetcher struct {
|
||||
confirmation uint64
|
||||
blockTimeInSec int
|
||||
client *ethclient.Client
|
||||
db *gorm.DB
|
||||
rollupOrm *orm.RollupBatch
|
||||
db db.OrmFactory
|
||||
msgProofUpdater *messageproof.MsgProofUpdater
|
||||
}
|
||||
|
||||
// NewBatchInfoFetcher creates a new BatchInfoFetcher instance
|
||||
func NewBatchInfoFetcher(ctx context.Context, scrollChainAddr common.Address, batchInfoStartNumber uint64, confirmation uint64, blockTimeInSec int, client *ethclient.Client, db *gorm.DB, msgProofUpdater *messageproof.MsgProofUpdater) *BatchInfoFetcher {
|
||||
func NewBatchInfoFetcher(ctx context.Context, scrollChainAddr common.Address, batchInfoStartNumber uint64, confirmation uint64, blockTimeInSec int, client *ethclient.Client, db db.OrmFactory, msgProofUpdater *messageproof.MsgProofUpdater) *BatchInfoFetcher {
|
||||
return &BatchInfoFetcher{
|
||||
ctx: ctx,
|
||||
scrollChainAddr: scrollChainAddr,
|
||||
@@ -37,7 +35,6 @@ func NewBatchInfoFetcher(ctx context.Context, scrollChainAddr common.Address, ba
|
||||
blockTimeInSec: blockTimeInSec,
|
||||
client: client,
|
||||
db: db,
|
||||
rollupOrm: orm.NewRollupBatch(db),
|
||||
msgProofUpdater: msgProofUpdater,
|
||||
}
|
||||
}
|
||||
@@ -83,20 +80,19 @@ func (b *BatchInfoFetcher) fetchBatchInfo() error {
|
||||
log.Error("Can not get latest block number: ", "err", err)
|
||||
return err
|
||||
}
|
||||
latestBatchHeight, err := b.rollupOrm.GetLatestRollupBatchProcessedHeight(b.ctx)
|
||||
latestBatch, err := b.db.GetLatestRollupBatch()
|
||||
if err != nil {
|
||||
log.Error("Can not get latest BatchInfo: ", "err", err)
|
||||
return err
|
||||
}
|
||||
var startHeight uint64
|
||||
if latestBatchHeight == 0 {
|
||||
log.Info("no batch record in database, start from batchInfoStartNumber", "batchInfoStartNumber", b.batchInfoStartNumber)
|
||||
if latestBatch == nil {
|
||||
startHeight = b.batchInfoStartNumber
|
||||
} else {
|
||||
startHeight = latestBatchHeight + 1
|
||||
startHeight = latestBatch.CommitHeight + 1
|
||||
}
|
||||
for from := startHeight; number >= from; from += fetchLimit {
|
||||
to := from + fetchLimit - 1
|
||||
for from := startHeight; number >= from; from += uint64(fetchLimit) {
|
||||
to := from + uint64(fetchLimit) - 1
|
||||
// number - confirmation can never less than 0 since the for loop condition
|
||||
// but watch out the overflow
|
||||
if to > number {
|
||||
|
||||
@@ -10,10 +10,10 @@ import (
|
||||
)
|
||||
|
||||
// GetEarliestNoBlockTimestampHeightFunc is a function type that gets the earliest record without block timestamp from database
|
||||
type GetEarliestNoBlockTimestampHeightFunc func(ctx context.Context) (uint64, error)
|
||||
type GetEarliestNoBlockTimestampHeightFunc func() (uint64, error)
|
||||
|
||||
// UpdateBlockTimestampFunc is a function type that updates block timestamp into database
|
||||
type UpdateBlockTimestampFunc func(ctx context.Context, height uint64, timestamp time.Time) error
|
||||
type UpdateBlockTimestampFunc func(height uint64, timestamp time.Time) error
|
||||
|
||||
// BlockTimestampFetcher fetches block timestamp from blockchain and saves them to database
|
||||
type BlockTimestampFetcher struct {
|
||||
@@ -52,7 +52,7 @@ func (b *BlockTimestampFetcher) Start() {
|
||||
log.Error("Can not get latest block number", "err", err)
|
||||
continue
|
||||
}
|
||||
startHeight, err := b.getEarliestNoBlockTimestampHeightFunc(b.ctx)
|
||||
startHeight, err := b.getEarliestNoBlockTimestampHeightFunc()
|
||||
if err != nil {
|
||||
log.Error("Can not get latest record without block timestamp", "err", err)
|
||||
continue
|
||||
@@ -63,12 +63,12 @@ func (b *BlockTimestampFetcher) Start() {
|
||||
log.Error("Can not get block by number", "err", err)
|
||||
break
|
||||
}
|
||||
err = b.updateBlockTimestampFunc(b.ctx, height, time.Unix(int64(block.Time), 0))
|
||||
err = b.updateBlockTimestampFunc(height, time.Unix(int64(block.Time), 0))
|
||||
if err != nil {
|
||||
log.Error("Can not update blockTimestamp into DB ", "err", err)
|
||||
break
|
||||
}
|
||||
height, err = b.getEarliestNoBlockTimestampHeightFunc(b.ctx)
|
||||
height, err = b.getEarliestNoBlockTimestampHeightFunc()
|
||||
if err != nil {
|
||||
log.Error("Can not get latest record without block timestamp", "err", err)
|
||||
break
|
||||
|
||||
@@ -12,9 +12,9 @@ import (
|
||||
"github.com/ethereum/go-ethereum/ethclient"
|
||||
"github.com/ethereum/go-ethereum/log"
|
||||
"github.com/modern-go/reflect2"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"bridge-history-api/config"
|
||||
"bridge-history-api/db"
|
||||
"bridge-history-api/utils"
|
||||
)
|
||||
|
||||
@@ -22,7 +22,7 @@ import (
|
||||
type MsgFetcher struct {
|
||||
ctx context.Context
|
||||
config *config.LayerConfig
|
||||
db *gorm.DB
|
||||
db db.OrmFactory
|
||||
client *ethclient.Client
|
||||
worker *FetchEventWorker
|
||||
reorgHandling ReorgHandling
|
||||
@@ -34,7 +34,7 @@ type MsgFetcher struct {
|
||||
}
|
||||
|
||||
// NewMsgFetcher creates a new MsgFetcher instance
|
||||
func NewMsgFetcher(ctx context.Context, config *config.LayerConfig, db *gorm.DB, client *ethclient.Client, worker *FetchEventWorker, addressList []common.Address, reorg ReorgHandling) (*MsgFetcher, error) {
|
||||
func NewMsgFetcher(ctx context.Context, config *config.LayerConfig, db db.OrmFactory, client *ethclient.Client, worker *FetchEventWorker, addressList []common.Address, reorg ReorgHandling) (*MsgFetcher, error) {
|
||||
msgFetcher := &MsgFetcher{
|
||||
ctx: ctx,
|
||||
config: config,
|
||||
@@ -120,23 +120,22 @@ func (c *MsgFetcher) forwardFetchAndSaveMissingEvents(confirmation uint64) {
|
||||
log.Error(fmt.Sprintf("%s: invalid get/fetch function", c.worker.Name))
|
||||
return
|
||||
}
|
||||
processedHeight, err := c.worker.G(c.ctx, c.db)
|
||||
processedHeight, err := c.worker.G(c.db)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("%s: can not get latest processed block height", c.worker.Name))
|
||||
}
|
||||
log.Info(fmt.Sprintf("%s: ", c.worker.Name), "height", processedHeight)
|
||||
if processedHeight <= 0 || processedHeight < c.config.StartHeight {
|
||||
processedHeight = c.config.StartHeight
|
||||
if processedHeight <= 0 || processedHeight < int64(c.config.StartHeight) {
|
||||
processedHeight = int64(c.config.StartHeight)
|
||||
} else {
|
||||
processedHeight++
|
||||
}
|
||||
for from := processedHeight; from <= number; from += fetchLimit {
|
||||
for from := processedHeight; from <= int64(number); from += fetchLimit {
|
||||
to := from + fetchLimit - 1
|
||||
if to > number {
|
||||
to = number
|
||||
if to > int64(number) {
|
||||
to = int64(number)
|
||||
}
|
||||
// watch for overflow here, tho its unlikely to happen
|
||||
err := c.worker.F(c.ctx, c.client, c.db, int64(from), int64(to), c.addressList)
|
||||
err := c.worker.F(c.ctx, c.client, c.db, from, to, c.addressList)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("%s: failed!", c.worker.Name), "err", err)
|
||||
break
|
||||
@@ -192,7 +191,7 @@ func (c *MsgFetcher) fetchMissingLatestHeaders() {
|
||||
log.Crit("Can not get safe number during reorg, quit the process", "err", err)
|
||||
}
|
||||
// clear all our saved data, because no data is safe now
|
||||
err = c.reorgHandling(c.ctx, num, c.db)
|
||||
err = c.reorgHandling(c.ctx, int64(num), c.db)
|
||||
// if handling success then we can update the cachedHeaders
|
||||
if err == nil {
|
||||
c.cachedHeaders = c.cachedHeaders[:0]
|
||||
@@ -201,7 +200,7 @@ func (c *MsgFetcher) fetchMissingLatestHeaders() {
|
||||
c.reorgEndCh <- struct{}{}
|
||||
return
|
||||
}
|
||||
err = c.reorgHandling(c.ctx, c.cachedHeaders[index].Number.Uint64(), c.db)
|
||||
err = c.reorgHandling(c.ctx, c.cachedHeaders[index].Number.Int64(), c.db)
|
||||
// if handling success then we can update the cachedHeaders
|
||||
if err == nil {
|
||||
c.cachedHeaders = c.cachedHeaders[:index+1]
|
||||
|
||||
@@ -8,24 +8,23 @@ import (
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/ethclient"
|
||||
"github.com/ethereum/go-ethereum/log"
|
||||
"gorm.io/gorm"
|
||||
|
||||
backendabi "bridge-history-api/abi"
|
||||
"bridge-history-api/orm"
|
||||
"bridge-history-api/db"
|
||||
"bridge-history-api/utils"
|
||||
)
|
||||
|
||||
// Todo : read from config
|
||||
var (
|
||||
// the number of blocks fetch per round
|
||||
fetchLimit = uint64(3000)
|
||||
fetchLimit = int64(3000)
|
||||
)
|
||||
|
||||
// FetchAndSave is a function type that fetches events from blockchain and saves them to database
|
||||
type FetchAndSave func(ctx context.Context, client *ethclient.Client, database *gorm.DB, from int64, to int64, addressList []common.Address) error
|
||||
type FetchAndSave func(ctx context.Context, client *ethclient.Client, database db.OrmFactory, from int64, to int64, addressList []common.Address) error
|
||||
|
||||
// GetLatestProcessed is a function type that gets the latest processed block height from database
|
||||
type GetLatestProcessed func(ctx context.Context, db *gorm.DB) (uint64, error)
|
||||
type GetLatestProcessed func(db db.OrmFactory) (int64, error)
|
||||
|
||||
// FetchEventWorker defines worker with fetch and save function, processed number getter, and name
|
||||
type FetchEventWorker struct {
|
||||
@@ -35,15 +34,13 @@ type FetchEventWorker struct {
|
||||
}
|
||||
|
||||
// GetLatestL1ProcessedHeight get L1 the latest processed height
|
||||
func GetLatestL1ProcessedHeight(ctx context.Context, db *gorm.DB) (uint64, error) {
|
||||
l1CrossMsgOrm := orm.NewCrossMsg(db)
|
||||
relayedOrm := orm.NewRelayedMsg(db)
|
||||
crossHeight, err := l1CrossMsgOrm.GetLatestL1ProcessedHeight(ctx)
|
||||
func GetLatestL1ProcessedHeight(db db.OrmFactory) (int64, error) {
|
||||
crossHeight, err := db.GetLatestL1ProcessedHeight()
|
||||
if err != nil {
|
||||
log.Error("failed to get L1 cross message processed height: ", "err", err)
|
||||
return 0, err
|
||||
}
|
||||
relayedHeight, err := relayedOrm.GetLatestRelayedHeightOnL1(ctx)
|
||||
relayedHeight, err := db.GetLatestRelayedHeightOnL1()
|
||||
if err != nil {
|
||||
log.Error("failed to get L1 relayed message processed height: ", "err", err)
|
||||
return 0, err
|
||||
@@ -55,21 +52,18 @@ func GetLatestL1ProcessedHeight(ctx context.Context, db *gorm.DB) (uint64, error
|
||||
}
|
||||
|
||||
// GetLatestL2ProcessedHeight get L2 latest processed height
|
||||
func GetLatestL2ProcessedHeight(ctx context.Context, db *gorm.DB) (uint64, error) {
|
||||
l2CrossMsgOrm := orm.NewCrossMsg(db)
|
||||
relayedOrm := orm.NewRelayedMsg(db)
|
||||
l2SentMsgOrm := orm.NewL2SentMsg(db)
|
||||
crossHeight, err := l2CrossMsgOrm.GetLatestL2ProcessedHeight(ctx)
|
||||
func GetLatestL2ProcessedHeight(db db.OrmFactory) (int64, error) {
|
||||
crossHeight, err := db.GetLatestL2ProcessedHeight()
|
||||
if err != nil {
|
||||
log.Error("failed to get L2 cross message processed height", "err", err)
|
||||
return 0, err
|
||||
}
|
||||
relayedHeight, err := relayedOrm.GetLatestRelayedHeightOnL2(ctx)
|
||||
relayedHeight, err := db.GetLatestRelayedHeightOnL2()
|
||||
if err != nil {
|
||||
log.Error("failed to get L2 relayed message processed height", "err", err)
|
||||
return 0, err
|
||||
}
|
||||
l2SentHeight, err := l2SentMsgOrm.GetLatestSentMsgHeightOnL2(ctx)
|
||||
l2SentHeight, err := db.GetLatestSentMsgHeightOnL2()
|
||||
if err != nil {
|
||||
log.Error("failed to get L2 sent message processed height", "err", err)
|
||||
return 0, err
|
||||
@@ -85,9 +79,7 @@ func GetLatestL2ProcessedHeight(ctx context.Context, db *gorm.DB) (uint64, error
|
||||
}
|
||||
|
||||
// L1FetchAndSaveEvents fetch and save events on L1
|
||||
func L1FetchAndSaveEvents(ctx context.Context, client *ethclient.Client, db *gorm.DB, from int64, to int64, addrList []common.Address) error {
|
||||
l1CrossMsgOrm := orm.NewCrossMsg(db)
|
||||
relayedOrm := orm.NewRelayedMsg(db)
|
||||
func L1FetchAndSaveEvents(ctx context.Context, client *ethclient.Client, database db.OrmFactory, from int64, to int64, addrList []common.Address) error {
|
||||
query := geth.FilterQuery{
|
||||
FromBlock: big.NewInt(from), // inclusive
|
||||
ToBlock: big.NewInt(to), // inclusive
|
||||
@@ -113,28 +105,41 @@ func L1FetchAndSaveEvents(ctx context.Context, client *ethclient.Client, db *gor
|
||||
log.Error("l1FetchAndSaveEvents: Failed to parse cross msg event logs", "err", err)
|
||||
return err
|
||||
}
|
||||
err = db.Transaction(func(tx *gorm.DB) error {
|
||||
if txErr := l1CrossMsgOrm.InsertL1CrossMsg(ctx, depositL1CrossMsgs, tx); txErr != nil {
|
||||
log.Error("l1FetchAndSaveEvents: Failed to insert cross msg event logs", "err", txErr)
|
||||
return txErr
|
||||
}
|
||||
if txErr := relayedOrm.InsertRelayedMsg(ctx, relayedMsg, tx); txErr != nil {
|
||||
log.Error("l1FetchAndSaveEvents: Failed to insert relayed msg event logs", "err", txErr)
|
||||
return txErr
|
||||
}
|
||||
return nil
|
||||
})
|
||||
dbTx, err := database.Beginx()
|
||||
if err != nil {
|
||||
log.Crit("l2FetchAndSaveEvents: Failed to finish transaction", "err", err)
|
||||
log.Error("l2FetchAndSaveEvents: Failed to begin db transaction", "err", err)
|
||||
return err
|
||||
}
|
||||
return err
|
||||
err = database.BatchInsertL1CrossMsgDBTx(dbTx, depositL1CrossMsgs)
|
||||
if err != nil {
|
||||
if rollBackErr := dbTx.Rollback(); rollBackErr != nil {
|
||||
log.Error("dbTx Rollback failed", "err", rollBackErr)
|
||||
}
|
||||
log.Crit("l1FetchAndSaveEvents: Failed to insert cross msg event logs", "err", err)
|
||||
}
|
||||
|
||||
err = database.BatchInsertRelayedMsgDBTx(dbTx, relayedMsg)
|
||||
if err != nil {
|
||||
if rollBackErr := dbTx.Rollback(); rollBackErr != nil {
|
||||
log.Error("dbTx Rollback failed", "err", rollBackErr)
|
||||
}
|
||||
log.Crit("l1FetchAndSaveEvents: Failed to insert relayed message event logs", "err", err)
|
||||
}
|
||||
err = dbTx.Commit()
|
||||
if err != nil {
|
||||
// if we can not insert into DB, there must something wrong, need a on-call member handle the dababase manually
|
||||
if rollBackErr := dbTx.Rollback(); rollBackErr != nil {
|
||||
log.Error("dbTx Rollback failed", "err", rollBackErr)
|
||||
}
|
||||
log.Error("l1FetchAndSaveEvents: Failed to commit db transaction", "err", err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// L2FetchAndSaveEvents fetche and save events on L2
|
||||
func L2FetchAndSaveEvents(ctx context.Context, client *ethclient.Client, db *gorm.DB, from int64, to int64, addrList []common.Address) error {
|
||||
l2CrossMsgOrm := orm.NewCrossMsg(db)
|
||||
relayedOrm := orm.NewRelayedMsg(db)
|
||||
l2SentMsgOrm := orm.NewL2SentMsg(db)
|
||||
func L2FetchAndSaveEvents(ctx context.Context, client *ethclient.Client, database db.OrmFactory, from int64, to int64, addrList []common.Address) error {
|
||||
query := geth.FilterQuery{
|
||||
FromBlock: big.NewInt(from), // inclusive
|
||||
ToBlock: big.NewInt(to), // inclusive
|
||||
@@ -161,32 +166,50 @@ func L2FetchAndSaveEvents(ctx context.Context, client *ethclient.Client, db *gor
|
||||
return err
|
||||
}
|
||||
|
||||
err = db.Transaction(func(tx *gorm.DB) error {
|
||||
if txErr := l2CrossMsgOrm.InsertL2CrossMsg(ctx, depositL2CrossMsgs, tx); txErr != nil {
|
||||
log.Error("l2FetchAndSaveEvents: Failed to insert cross msg event logs", "err", txErr)
|
||||
return txErr
|
||||
}
|
||||
|
||||
if txErr := relayedOrm.InsertRelayedMsg(ctx, relayedMsg, tx); txErr != nil {
|
||||
log.Error("l2FetchAndSaveEvents: Failed to insert relayed message event logs", "err", txErr)
|
||||
return txErr
|
||||
}
|
||||
|
||||
if txErr := l2SentMsgOrm.InsertL2SentMsg(ctx, l2SentMsgs, tx); txErr != nil {
|
||||
log.Error("l2FetchAndSaveEvents: Failed to insert l2 sent message", "err", txErr)
|
||||
return txErr
|
||||
}
|
||||
return nil
|
||||
})
|
||||
dbTx, err := database.Beginx()
|
||||
if err != nil {
|
||||
log.Crit("l2FetchAndSaveEvents: Failed to begin db transaction", "err", err)
|
||||
log.Error("l2FetchAndSaveEvents: Failed to begin db transaction", "err", err)
|
||||
return err
|
||||
}
|
||||
return err
|
||||
err = database.BatchInsertL2CrossMsgDBTx(dbTx, depositL2CrossMsgs)
|
||||
if err != nil {
|
||||
if rollBackErr := dbTx.Rollback(); rollBackErr != nil {
|
||||
log.Error("dbTx Rollback failed", "err", rollBackErr)
|
||||
}
|
||||
log.Crit("l2FetchAndSaveEvents: Failed to insert cross msg event logs", "err", err)
|
||||
}
|
||||
|
||||
err = database.BatchInsertRelayedMsgDBTx(dbTx, relayedMsg)
|
||||
if err != nil {
|
||||
if rollBackErr := dbTx.Rollback(); rollBackErr != nil {
|
||||
log.Error("dbTx Rollback failed", "err", rollBackErr)
|
||||
}
|
||||
log.Crit("l2FetchAndSaveEvents: Failed to insert relayed message event logs", "err", err)
|
||||
}
|
||||
|
||||
err = database.BatchInsertL2SentMsgDBTx(dbTx, l2SentMsgs)
|
||||
if err != nil {
|
||||
if rollBackErr := dbTx.Rollback(); rollBackErr != nil {
|
||||
log.Error("dbTx Rollback failed", "err", rollBackErr)
|
||||
}
|
||||
log.Crit("l2FetchAndSaveEvents: Failed to insert l2 sent message", "err", err)
|
||||
}
|
||||
|
||||
err = dbTx.Commit()
|
||||
if err != nil {
|
||||
// if we can not insert into DB, there must something wrong, need a on-call member handle the dababase manually
|
||||
if rollBackErr := dbTx.Rollback(); rollBackErr != nil {
|
||||
log.Error("dbTx Rollback failed", "err", rollBackErr)
|
||||
}
|
||||
log.Error("l2FetchAndSaveEvents: Failed to commit db transaction", "err", err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// FetchAndSaveBatchIndex fetche and save batch index
|
||||
func FetchAndSaveBatchIndex(ctx context.Context, client *ethclient.Client, db *gorm.DB, from int64, to int64, scrollChainAddr common.Address) error {
|
||||
rollupBatchOrm := orm.NewRollupBatch(db)
|
||||
func FetchAndSaveBatchIndex(ctx context.Context, client *ethclient.Client, database db.OrmFactory, from int64, to int64, scrollChainAddr common.Address) error {
|
||||
query := geth.FilterQuery{
|
||||
FromBlock: big.NewInt(from), // inclusive
|
||||
ToBlock: big.NewInt(to), // inclusive
|
||||
@@ -205,9 +228,26 @@ func FetchAndSaveBatchIndex(ctx context.Context, client *ethclient.Client, db *g
|
||||
log.Error("FetchAndSaveBatchIndex: Failed to parse batch commit msg event logs", "err", err)
|
||||
return err
|
||||
}
|
||||
if txErr := rollupBatchOrm.InsertRollupBatch(ctx, rollupBatches); txErr != nil {
|
||||
log.Crit("FetchAndSaveBatchIndex: Failed to insert batch commit msg event logs", "err", txErr)
|
||||
return txErr
|
||||
dbTx, err := database.Beginx()
|
||||
if err != nil {
|
||||
log.Error("FetchAndSaveBatchIndex: Failed to begin db transaction", "err", err)
|
||||
return err
|
||||
}
|
||||
err = database.BatchInsertRollupBatchDBTx(dbTx, rollupBatches)
|
||||
if err != nil {
|
||||
if rollBackErr := dbTx.Rollback(); rollBackErr != nil {
|
||||
log.Error("dbTx Rollback failed", "err", rollBackErr)
|
||||
}
|
||||
log.Crit("FetchAndSaveBatchIndex: Failed to insert batch commit msg event logs", "err", err)
|
||||
}
|
||||
err = dbTx.Commit()
|
||||
if err != nil {
|
||||
// if we can not insert into DB, there must something wrong, need a on-call member handle the dababase manually
|
||||
if rollBackErr := dbTx.Rollback(); rollBackErr != nil {
|
||||
log.Error("dbTx Rollback failed", "err", rollBackErr)
|
||||
}
|
||||
log.Error("FetchAndSaveBatchIndex: Failed to commit db transaction", "err", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -2,32 +2,30 @@ package messageproof
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/log"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"bridge-history-api/orm"
|
||||
"bridge-history-api/db"
|
||||
"bridge-history-api/db/orm"
|
||||
)
|
||||
|
||||
// MsgProofUpdater is used to update message proof in db
|
||||
type MsgProofUpdater struct {
|
||||
ctx context.Context
|
||||
db *gorm.DB
|
||||
l2SentMsgOrm *orm.L2SentMsg
|
||||
rollupOrm *orm.RollupBatch
|
||||
db db.OrmFactory
|
||||
withdrawTrie *WithdrawTrie
|
||||
}
|
||||
|
||||
// NewMsgProofUpdater new MsgProofUpdater instance
|
||||
func NewMsgProofUpdater(ctx context.Context, confirmations uint64, startBlock uint64, db *gorm.DB) *MsgProofUpdater {
|
||||
func NewMsgProofUpdater(ctx context.Context, confirmations uint64, startBlock uint64, db db.OrmFactory) *MsgProofUpdater {
|
||||
return &MsgProofUpdater{
|
||||
ctx: ctx,
|
||||
db: db,
|
||||
l2SentMsgOrm: orm.NewL2SentMsg(db),
|
||||
rollupOrm: orm.NewRollupBatch(db),
|
||||
withdrawTrie: NewWithdrawTrie(),
|
||||
}
|
||||
}
|
||||
@@ -44,7 +42,7 @@ func (m *MsgProofUpdater) Start() {
|
||||
tick.Stop()
|
||||
return
|
||||
case <-tick.C:
|
||||
latestBatch, err := m.rollupOrm.GetLatestRollupBatch(m.ctx)
|
||||
latestBatch, err := m.db.GetLatestRollupBatch()
|
||||
if err != nil {
|
||||
log.Warn("MsgProofUpdater: Can not get latest RollupBatch: ", "err", err)
|
||||
continue
|
||||
@@ -52,7 +50,7 @@ func (m *MsgProofUpdater) Start() {
|
||||
if latestBatch == nil {
|
||||
continue
|
||||
}
|
||||
latestBatchIndexWithProof, err := m.l2SentMsgOrm.GetLatestL2SentMsgBatchIndex(m.ctx)
|
||||
latestBatchIndexWithProof, err := m.db.GetLatestL2SentMsgBatchIndex()
|
||||
if err != nil {
|
||||
log.Error("MsgProofUpdater: Can not get latest L2SentMsgBatchIndex: ", "err", err)
|
||||
continue
|
||||
@@ -64,7 +62,7 @@ func (m *MsgProofUpdater) Start() {
|
||||
start = uint64(latestBatchIndexWithProof) + 1
|
||||
}
|
||||
for i := start; i <= latestBatch.BatchIndex; i++ {
|
||||
batch, err := m.rollupOrm.GetRollupBatchByIndex(m.ctx, i)
|
||||
batch, err := m.db.GetRollupBatchByIndex(i)
|
||||
if err != nil {
|
||||
log.Error("MsgProofUpdater: Can not get RollupBatch: ", "err", err, "index", i)
|
||||
break
|
||||
@@ -113,8 +111,8 @@ func (m *MsgProofUpdater) initialize(ctx context.Context) {
|
||||
|
||||
func (m *MsgProofUpdater) initializeWithdrawTrie() error {
|
||||
var batch *orm.RollupBatch
|
||||
firstMsg, err := m.l2SentMsgOrm.GetL2SentMessageByNonce(m.ctx, 0)
|
||||
if err != nil {
|
||||
firstMsg, err := m.db.GetL2SentMessageByNonce(0)
|
||||
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
return fmt.Errorf("failed to get first l2 message: %v", err)
|
||||
}
|
||||
// no l2 message
|
||||
@@ -125,7 +123,7 @@ func (m *MsgProofUpdater) initializeWithdrawTrie() error {
|
||||
}
|
||||
|
||||
// if no batch, return and wait for next try round
|
||||
batch, err = m.rollupOrm.GetLatestRollupBatch(m.ctx)
|
||||
batch, err = m.db.GetLatestRollupBatch()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get latest batch: %v", err)
|
||||
}
|
||||
@@ -137,7 +135,7 @@ func (m *MsgProofUpdater) initializeWithdrawTrie() error {
|
||||
batchIndex := batch.BatchIndex
|
||||
for {
|
||||
var msg *orm.L2SentMsg
|
||||
msg, err = m.l2SentMsgOrm.GetLatestL2SentMsgLEHeight(m.ctx, batch.EndBlockNumber)
|
||||
msg, err = m.db.GetLatestL2SentMsgLEHeight(batch.EndBlockNumber)
|
||||
if err != nil {
|
||||
log.Warn("failed to get l2 sent message less than height", "endBlocknum", batch.EndBlockNumber, "err", err)
|
||||
}
|
||||
@@ -161,8 +159,8 @@ func (m *MsgProofUpdater) initializeWithdrawTrie() error {
|
||||
// iterate for next batch
|
||||
batchIndex--
|
||||
|
||||
batch, err = m.rollupOrm.GetRollupBatchByIndex(m.ctx, batchIndex)
|
||||
if err != nil || batch == nil {
|
||||
batch, err = m.db.GetRollupBatchByIndex(batchIndex)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get block batch %v: %v", batchIndex, err)
|
||||
}
|
||||
}
|
||||
@@ -193,26 +191,36 @@ func (m *MsgProofUpdater) updateMsgProof(msgs []*orm.L2SentMsg, proofs [][]byte,
|
||||
if len(msgs) != len(proofs) {
|
||||
return fmt.Errorf("illegal state: len(msgs) != len(proofs)")
|
||||
}
|
||||
err := m.db.Transaction(func(tx *gorm.DB) error {
|
||||
for i, msg := range msgs {
|
||||
proofHex := common.Bytes2Hex(proofs[i])
|
||||
log.Debug("updateMsgProof", "msgHash", msg.MsgHash, "batchIndex", batchIndex, "proof", proofHex)
|
||||
if err := m.l2SentMsgOrm.UpdateL2MessageProof(m.ctx, msg.MsgHash, proofHex, batchIndex, tx); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
dbTx, err := m.db.Beginx()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for i, msg := range msgs {
|
||||
proofHex := common.Bytes2Hex(proofs[i])
|
||||
log.Debug("updateMsgProof", "msgHash", msg.MsgHash, "batchIndex", batchIndex, "proof", proofHex)
|
||||
if dbTxErr := m.db.UpdateL2MessageProofInDBTx(m.ctx, dbTx, msg.MsgHash, proofHex, batchIndex); dbTxErr != nil {
|
||||
if err := dbTx.Rollback(); err != nil {
|
||||
log.Error("dbTx.Rollback()", "err", err)
|
||||
}
|
||||
return dbTxErr
|
||||
}
|
||||
}
|
||||
|
||||
if dbTxErr := dbTx.Commit(); dbTxErr != nil {
|
||||
if err := dbTx.Rollback(); err != nil {
|
||||
log.Error("dbTx.Rollback()", "err", err)
|
||||
}
|
||||
return dbTxErr
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// appendL2Messages will append all messages between firstBlock and lastBlock (both inclusive) to withdrawTrie and compute corresponding merkle proof of each message.
|
||||
func (m *MsgProofUpdater) appendL2Messages(firstBlock, lastBlock uint64) ([]*orm.L2SentMsg, [][]byte, error) {
|
||||
var msgProofs [][]byte
|
||||
messages, err := m.l2SentMsgOrm.GetL2SentMsgMsgHashByHeightRange(m.ctx, firstBlock, lastBlock)
|
||||
messages, err := m.db.GetL2SentMsgMsgHashByHeightRange(firstBlock, lastBlock)
|
||||
if err != nil {
|
||||
log.Error("GetL2SentMsgMsgHashByHeightRange failed", "error", err, "firstBlock", firstBlock, "lastBlock", lastBlock)
|
||||
return messages, msgProofs, err
|
||||
|
||||
@@ -6,13 +6,12 @@ import (
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/ethereum/go-ethereum/ethclient"
|
||||
"github.com/ethereum/go-ethereum/log"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"bridge-history-api/orm"
|
||||
"bridge-history-api/db"
|
||||
)
|
||||
|
||||
// ReorgHandling handles reorg function type
|
||||
type ReorgHandling func(ctx context.Context, reorgHeight uint64, db *gorm.DB) error
|
||||
type ReorgHandling func(ctx context.Context, reorgHeight int64, db db.OrmFactory) error
|
||||
|
||||
func reverseArray(arr []*types.Header) []*types.Header {
|
||||
for i := 0; i < len(arr)/2; i++ {
|
||||
@@ -61,48 +60,73 @@ func BackwardFindReorgBlock(ctx context.Context, headers []*types.Header, client
|
||||
}
|
||||
|
||||
// L1ReorgHandling handles l1 reorg
|
||||
func L1ReorgHandling(ctx context.Context, reorgHeight uint64, db *gorm.DB) error {
|
||||
l1CrossMsgOrm := orm.NewCrossMsg(db)
|
||||
relayedOrm := orm.NewRelayedMsg(db)
|
||||
err := db.Transaction(func(tx *gorm.DB) error {
|
||||
if err := l1CrossMsgOrm.DeleteL1CrossMsgAfterHeight(ctx, reorgHeight, tx); err != nil {
|
||||
log.Error("delete l1 cross msg from height", "height", reorgHeight, "err", err)
|
||||
return err
|
||||
}
|
||||
if err := relayedOrm.DeleteL1RelayedHashAfterHeight(ctx, reorgHeight, tx); err != nil {
|
||||
log.Error("delete l1 relayed msg from height", "height", reorgHeight, "err", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
func L1ReorgHandling(ctx context.Context, reorgHeight int64, db db.OrmFactory) error {
|
||||
dbTx, err := db.Beginx()
|
||||
if err != nil {
|
||||
log.Crit("l1 reorg handling failed", "err", err)
|
||||
log.Crit("begin db tx failed", "err", err)
|
||||
}
|
||||
return err
|
||||
err = db.DeleteL1CrossMsgAfterHeightDBTx(dbTx, reorgHeight)
|
||||
if err != nil {
|
||||
if rollBackErr := dbTx.Rollback(); rollBackErr != nil {
|
||||
log.Error("dbTx Rollback failed", "err", rollBackErr)
|
||||
}
|
||||
log.Crit("delete l1 cross msg from height", "height", reorgHeight, "err", err)
|
||||
}
|
||||
err = db.DeleteL1RelayedHashAfterHeightDBTx(dbTx, reorgHeight)
|
||||
if err != nil {
|
||||
if rollBackErr := dbTx.Rollback(); rollBackErr != nil {
|
||||
log.Error("dbTx Rollback failed", "err", rollBackErr)
|
||||
}
|
||||
log.Crit("delete l1 relayed hash from height", "height", reorgHeight, "err", err)
|
||||
}
|
||||
err = dbTx.Commit()
|
||||
if err != nil {
|
||||
if rollBackErr := dbTx.Rollback(); rollBackErr != nil {
|
||||
log.Error("dbTx Rollback failed", "err", rollBackErr)
|
||||
}
|
||||
log.Error("commit tx failed", "err", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// L2ReorgHandling handles l2 reorg
|
||||
func L2ReorgHandling(ctx context.Context, reorgHeight uint64, db *gorm.DB) error {
|
||||
l2CrossMsgOrm := orm.NewCrossMsg(db)
|
||||
relayedOrm := orm.NewRelayedMsg(db)
|
||||
l2SentMsgOrm := orm.NewL2SentMsg(db)
|
||||
err := db.Transaction(func(tx *gorm.DB) error {
|
||||
if err := l2CrossMsgOrm.DeleteL2CrossMsgFromHeight(ctx, reorgHeight, tx); err != nil {
|
||||
log.Error("delete l2 cross msg from height", "height", reorgHeight, "err", err)
|
||||
return err
|
||||
}
|
||||
if err := relayedOrm.DeleteL2RelayedHashAfterHeight(ctx, reorgHeight, tx); err != nil {
|
||||
log.Error("delete l2 relayed msg from height", "height", reorgHeight, "err", err)
|
||||
return err
|
||||
}
|
||||
if err := l2SentMsgOrm.DeleteL2SentMsgAfterHeight(ctx, reorgHeight, tx); err != nil {
|
||||
log.Error("delete l2 sent msg from height", "height", reorgHeight, "err", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
func L2ReorgHandling(ctx context.Context, reorgHeight int64, db db.OrmFactory) error {
|
||||
dbTx, err := db.Beginx()
|
||||
if err != nil {
|
||||
log.Crit("l2 reorg handling failed", "err", err)
|
||||
if rollBackErr := dbTx.Rollback(); rollBackErr != nil {
|
||||
log.Error("dbTx Rollback failed", "err", rollBackErr)
|
||||
}
|
||||
log.Crit("begin db tx failed", "err", err)
|
||||
}
|
||||
return err
|
||||
err = db.DeleteL2CrossMsgFromHeightDBTx(dbTx, reorgHeight)
|
||||
if err != nil {
|
||||
if rollBackErr := dbTx.Rollback(); rollBackErr != nil {
|
||||
log.Error("dbTx Rollback failed", "err", rollBackErr)
|
||||
}
|
||||
log.Crit("delete l2 cross msg from height", "height", reorgHeight, "err", err)
|
||||
}
|
||||
err = db.DeleteL2RelayedHashAfterHeightDBTx(dbTx, reorgHeight)
|
||||
if err != nil {
|
||||
if rollBackErr := dbTx.Rollback(); rollBackErr != nil {
|
||||
log.Error("dbTx Rollback failed", "err", rollBackErr)
|
||||
}
|
||||
log.Crit("delete l2 relayed hash from height", "height", reorgHeight, "err", err)
|
||||
}
|
||||
err = db.DeleteL2SentMsgAfterHeightDBTx(dbTx, reorgHeight)
|
||||
if err != nil {
|
||||
if rollBackErr := dbTx.Rollback(); rollBackErr != nil {
|
||||
log.Error("dbTx Rollback failed", "err", rollBackErr)
|
||||
}
|
||||
log.Crit("delete l2 sent msg from height", "height", reorgHeight, "err", err)
|
||||
}
|
||||
err = dbTx.Commit()
|
||||
if err != nil {
|
||||
if rollBackErr := dbTx.Rollback(); rollBackErr != nil {
|
||||
log.Error("dbTx Rollback failed", "err", rollBackErr)
|
||||
}
|
||||
log.Error("commit tx failed", "err", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
71
bridge-history-api/db/orm/batch.go
Normal file
71
bridge-history-api/db/orm/batch.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package orm
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
"github.com/ethereum/go-ethereum/log"
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
type rollupBatchOrm struct {
|
||||
db *sqlx.DB
|
||||
}
|
||||
|
||||
// RollupBatch is the struct for rollup_batch table
|
||||
type RollupBatch struct {
|
||||
ID uint64 `json:"id" db:"id"`
|
||||
BatchIndex uint64 `json:"batch_index" db:"batch_index"`
|
||||
BatchHash string `json:"batch_hash" db:"batch_hash"`
|
||||
CommitHeight uint64 `json:"commit_height" db:"commit_height"`
|
||||
StartBlockNumber uint64 `json:"start_block_number" db:"start_block_number"`
|
||||
EndBlockNumber uint64 `json:"end_block_number" db:"end_block_number"`
|
||||
}
|
||||
|
||||
// NewRollupBatchOrm create an NewRollupBatchOrm instance
|
||||
func NewRollupBatchOrm(db *sqlx.DB) RollupBatchOrm {
|
||||
return &rollupBatchOrm{db: db}
|
||||
}
|
||||
|
||||
func (b *rollupBatchOrm) BatchInsertRollupBatchDBTx(dbTx *sqlx.Tx, batches []*RollupBatch) error {
|
||||
if len(batches) == 0 {
|
||||
return nil
|
||||
}
|
||||
var err error
|
||||
batchMaps := make([]map[string]interface{}, len(batches))
|
||||
for i, batch := range batches {
|
||||
batchMaps[i] = map[string]interface{}{
|
||||
"commit_height": batch.CommitHeight,
|
||||
"batch_index": batch.BatchIndex,
|
||||
"batch_hash": batch.BatchHash,
|
||||
"start_block_number": batch.StartBlockNumber,
|
||||
"end_block_number": batch.EndBlockNumber,
|
||||
}
|
||||
}
|
||||
_, err = dbTx.NamedExec(`insert into rollup_batch(commit_height, batch_index, batch_hash, start_block_number, end_block_number) values(:commit_height, :batch_index, :batch_hash, :start_block_number, :end_block_number);`, batchMaps)
|
||||
if err != nil {
|
||||
log.Error("BatchInsertRollupBatchDBTx: failed to insert batch event msgs", "err", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *rollupBatchOrm) GetLatestRollupBatch() (*RollupBatch, error) {
|
||||
result := &RollupBatch{}
|
||||
row := b.db.QueryRowx(`SELECT id, batch_index, commit_height, batch_hash, start_block_number, end_block_number FROM rollup_batch ORDER BY batch_index DESC LIMIT 1;`)
|
||||
if err := row.StructScan(result); err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (b *rollupBatchOrm) GetRollupBatchByIndex(index uint64) (*RollupBatch, error) {
|
||||
result := &RollupBatch{}
|
||||
row := b.db.QueryRowx(`SELECT id, batch_index, batch_hash, commit_height, start_block_number, end_block_number FROM rollup_batch WHERE batch_index = $1;`, index)
|
||||
if err := row.StructScan(result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
132
bridge-history-api/db/orm/interface.go
Normal file
132
bridge-history-api/db/orm/interface.go
Normal file
@@ -0,0 +1,132 @@
|
||||
package orm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
// AssetType can be ETH/ERC20/ERC1155/ERC721
|
||||
type AssetType int
|
||||
|
||||
// MsgType can be layer1/layer2 msg
|
||||
type MsgType int
|
||||
|
||||
func (a AssetType) String() string {
|
||||
switch a {
|
||||
case ETH:
|
||||
return "ETH"
|
||||
case ERC20:
|
||||
return "ERC20"
|
||||
case ERC1155:
|
||||
return "ERC1155"
|
||||
case ERC721:
|
||||
return "ERC721"
|
||||
}
|
||||
return "Unknown Asset Type"
|
||||
}
|
||||
|
||||
const (
|
||||
// ETH = 0
|
||||
ETH AssetType = iota
|
||||
// ERC20 = 1
|
||||
ERC20
|
||||
// ERC721 = 2
|
||||
ERC721
|
||||
// ERC1155 = 3
|
||||
ERC1155
|
||||
)
|
||||
|
||||
const (
|
||||
// UnknownMsg = 0
|
||||
UnknownMsg MsgType = iota
|
||||
// Layer1Msg = 1
|
||||
Layer1Msg
|
||||
// Layer2Msg = 2
|
||||
Layer2Msg
|
||||
)
|
||||
|
||||
// CrossMsg represents a cross message from layer 1 to layer 2
|
||||
type CrossMsg struct {
|
||||
ID uint64 `json:"id" db:"id"`
|
||||
MsgHash string `json:"msg_hash" db:"msg_hash"`
|
||||
Height uint64 `json:"height" db:"height"`
|
||||
Sender string `json:"sender" db:"sender"`
|
||||
Target string `json:"target" db:"target"`
|
||||
Amount string `json:"amount" db:"amount"`
|
||||
Layer1Hash string `json:"layer1_hash" db:"layer1_hash"`
|
||||
Layer2Hash string `json:"layer2_hash" db:"layer2_hash"`
|
||||
Layer1Token string `json:"layer1_token" db:"layer1_token"`
|
||||
Layer2Token string `json:"layer2_token" db:"layer2_token"`
|
||||
TokenIDs string `json:"token_ids" db:"token_ids"`
|
||||
TokenAmounts string `json:"token_amounts" db:"token_amounts"`
|
||||
Asset int `json:"asset" db:"asset"`
|
||||
MsgType int `json:"msg_type" db:"msg_type"`
|
||||
Timestamp *time.Time `json:"timestamp" db:"block_timestamp"`
|
||||
CreatedAt *time.Time `json:"created_at" db:"created_at"`
|
||||
UpdatedAt *time.Time `json:"updated_at" db:"updated_at"`
|
||||
DeletedAt *time.Time `json:"deleted_at" db:"deleted_at"`
|
||||
}
|
||||
|
||||
// L1CrossMsgOrm provides operations on l1_cross_message table
|
||||
type L1CrossMsgOrm interface {
|
||||
GetL1CrossMsgByHash(l1Hash common.Hash) (*CrossMsg, error)
|
||||
GetL1CrossMsgsByAddress(sender common.Address) ([]*CrossMsg, error)
|
||||
BatchInsertL1CrossMsgDBTx(dbTx *sqlx.Tx, messages []*CrossMsg) error
|
||||
// UpdateL1CrossMsgHashDBTx invoked when SentMessage event is received
|
||||
UpdateL1CrossMsgHashDBTx(ctx context.Context, dbTx *sqlx.Tx, l1Hash, msgHash common.Hash) error
|
||||
UpdateL1CrossMsgHash(ctx context.Context, l1Hash, msgHash common.Hash) error
|
||||
GetLatestL1ProcessedHeight() (int64, error)
|
||||
DeleteL1CrossMsgAfterHeightDBTx(dbTx *sqlx.Tx, height int64) error
|
||||
UpdateL1BlockTimestamp(height uint64, timestamp time.Time) error
|
||||
GetL1EarliestNoBlockTimestampHeight() (uint64, error)
|
||||
}
|
||||
|
||||
// L2CrossMsgOrm provides operations on cross_message table
|
||||
type L2CrossMsgOrm interface {
|
||||
GetL2CrossMsgByHash(l2Hash common.Hash) (*CrossMsg, error)
|
||||
GetL2CrossMsgByAddress(sender common.Address) ([]*CrossMsg, error)
|
||||
BatchInsertL2CrossMsgDBTx(dbTx *sqlx.Tx, messages []*CrossMsg) error
|
||||
// UpdateL2CrossMsgHashDBTx invoked when SentMessage event is received
|
||||
UpdateL2CrossMsgHashDBTx(ctx context.Context, dbTx *sqlx.Tx, l2Hash, msgHash common.Hash) error
|
||||
UpdateL2CrossMsgHash(ctx context.Context, l2Hash, msgHash common.Hash) error
|
||||
GetLatestL2ProcessedHeight() (int64, error)
|
||||
DeleteL2CrossMsgFromHeightDBTx(dbTx *sqlx.Tx, height int64) error
|
||||
UpdateL2BlockTimestamp(height uint64, timestamp time.Time) error
|
||||
GetL2EarliestNoBlockTimestampHeight() (uint64, error)
|
||||
GetL2CrossMsgByMsgHashList(msgHashList []string) ([]*CrossMsg, error)
|
||||
}
|
||||
|
||||
// RelayedMsgOrm provides operations on relayed_msg table
|
||||
type RelayedMsgOrm interface {
|
||||
BatchInsertRelayedMsgDBTx(dbTx *sqlx.Tx, messages []*RelayedMsg) error
|
||||
GetRelayedMsgByHash(msgHash string) (*RelayedMsg, error)
|
||||
GetLatestRelayedHeightOnL1() (int64, error)
|
||||
GetLatestRelayedHeightOnL2() (int64, error)
|
||||
DeleteL1RelayedHashAfterHeightDBTx(dbTx *sqlx.Tx, height int64) error
|
||||
DeleteL2RelayedHashAfterHeightDBTx(dbTx *sqlx.Tx, height int64) error
|
||||
}
|
||||
|
||||
// L2SentMsgOrm provides operations on l2_sent_msg table
|
||||
type L2SentMsgOrm interface {
|
||||
BatchInsertL2SentMsgDBTx(dbTx *sqlx.Tx, messages []*L2SentMsg) error
|
||||
GetL2SentMsgByHash(l2Hash string) (*L2SentMsg, error)
|
||||
GetLatestSentMsgHeightOnL2() (int64, error)
|
||||
GetL2SentMessageByNonce(nonce uint64) (*L2SentMsg, error)
|
||||
GetLatestL2SentMsgLEHeight(endBlockNumber uint64) (*L2SentMsg, error)
|
||||
GetL2SentMsgMsgHashByHeightRange(startHeight, endHeight uint64) ([]*L2SentMsg, error)
|
||||
UpdateL2MessageProofInDBTx(ctx context.Context, dbTx *sqlx.Tx, msgHash string, proof string, batchIndex uint64) error
|
||||
GetLatestL2SentMsgBatchIndex() (int64, error)
|
||||
GetClaimableL2SentMsgByAddressWithOffset(address string, offset int64, limit int64) ([]*L2SentMsg, error)
|
||||
GetClaimableL2SentMsgByAddressTotalNum(address string) (uint64, error)
|
||||
DeleteL2SentMsgAfterHeightDBTx(dbTx *sqlx.Tx, height int64) error
|
||||
}
|
||||
|
||||
// RollupBatchOrm provides operations on rollup_batch table
|
||||
type RollupBatchOrm interface {
|
||||
GetLatestRollupBatch() (*RollupBatch, error)
|
||||
GetRollupBatchByIndex(index uint64) (*RollupBatch, error)
|
||||
BatchInsertRollupBatchDBTx(dbTx *sqlx.Tx, messages []*RollupBatch) error
|
||||
}
|
||||
148
bridge-history-api/db/orm/l1_cross_msg.go
Normal file
148
bridge-history-api/db/orm/l1_cross_msg.go
Normal file
@@ -0,0 +1,148 @@
|
||||
package orm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/log"
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
type l1CrossMsgOrm struct {
|
||||
db *sqlx.DB
|
||||
}
|
||||
|
||||
// NewL1CrossMsgOrm create an NewL1CrossMsgOrm instance
|
||||
func NewL1CrossMsgOrm(db *sqlx.DB) L1CrossMsgOrm {
|
||||
return &l1CrossMsgOrm{db: db}
|
||||
}
|
||||
|
||||
func (l *l1CrossMsgOrm) GetL1CrossMsgByHash(l1Hash common.Hash) (*CrossMsg, error) {
|
||||
result := &CrossMsg{}
|
||||
row := l.db.QueryRowx(`SELECT * FROM cross_message WHERE layer1_hash = $1 AND msg_type = $2 AND deleted_at IS NULL;`, l1Hash.String(), Layer1Msg)
|
||||
if err := row.StructScan(result); err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// GetL1CrossMsgsByAddress returns all layer1 cross messages under given address
|
||||
// Warning: return empty slice if no data found
|
||||
func (l *l1CrossMsgOrm) GetL1CrossMsgsByAddress(sender common.Address) ([]*CrossMsg, error) {
|
||||
var results []*CrossMsg
|
||||
rows, err := l.db.Queryx(`SELECT * FROM cross_message WHERE sender = $1 AND msg_type = 1 AND deleted_at IS NULL;`, sender.String(), Layer1Msg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() {
|
||||
if err = rows.Close(); err != nil {
|
||||
log.Error("failed to close rows", "err", err)
|
||||
}
|
||||
}()
|
||||
for rows.Next() {
|
||||
msg := &CrossMsg{}
|
||||
if err = rows.StructScan(msg); err != nil {
|
||||
break
|
||||
}
|
||||
results = append(results, msg)
|
||||
}
|
||||
if len(results) == 0 && errors.Is(err, sql.ErrNoRows) {
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return results, nil
|
||||
}
|
||||
|
||||
func (l *l1CrossMsgOrm) BatchInsertL1CrossMsgDBTx(dbTx *sqlx.Tx, messages []*CrossMsg) error {
|
||||
if len(messages) == 0 {
|
||||
return nil
|
||||
}
|
||||
var err error
|
||||
messageMaps := make([]map[string]interface{}, len(messages))
|
||||
for i, msg := range messages {
|
||||
messageMaps[i] = map[string]interface{}{
|
||||
"height": msg.Height,
|
||||
"sender": msg.Sender,
|
||||
"target": msg.Target,
|
||||
"amount": msg.Amount,
|
||||
"asset": msg.Asset,
|
||||
"msg_hash": msg.MsgHash,
|
||||
"layer1_hash": msg.Layer1Hash,
|
||||
"layer1_token": msg.Layer1Token,
|
||||
"layer2_token": msg.Layer2Token,
|
||||
"token_ids": msg.TokenIDs,
|
||||
"msg_type": Layer1Msg,
|
||||
}
|
||||
}
|
||||
_, err = dbTx.NamedExec(`insert into cross_message(height, sender, target, amount, asset, msg_hash, layer1_hash, layer1_token, layer2_token, token_ids, msg_type) values(:height, :sender, :target, :amount, :asset, :msg_hash, :layer1_hash, :layer1_token, :layer2_token, :token_ids, :msg_type);`, messageMaps)
|
||||
if err != nil {
|
||||
log.Error("BatchInsertL1CrossMsgDBTx: failed to insert l1 cross msgs", "err", err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateL1CrossMsgHashDBTx update l1 cross msg hash in db, no need to check msg_type since layer1_hash wont be empty if its layer1 msg
|
||||
func (l *l1CrossMsgOrm) UpdateL1CrossMsgHashDBTx(ctx context.Context, dbTx *sqlx.Tx, l1Hash, msgHash common.Hash) error {
|
||||
if _, err := dbTx.ExecContext(ctx, l.db.Rebind("update public.cross_message set msg_hash = ? where layer1_hash = ? AND deleted_at IS NULL;"), msgHash.String(), l1Hash.String()); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func (l *l1CrossMsgOrm) UpdateL1CrossMsgHash(ctx context.Context, l1Hash, msgHash common.Hash) error {
|
||||
if _, err := l.db.ExecContext(ctx, l.db.Rebind("update public.l1_cross_message set msg_hash = ? where layer1_hash = ? AND deleted_at IS NULL;"), msgHash.String(), l1Hash.String()); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func (l *l1CrossMsgOrm) GetLatestL1ProcessedHeight() (int64, error) {
|
||||
row := l.db.QueryRowx(`SELECT height FROM cross_message WHERE msg_type = $1 AND deleted_at IS NULL ORDER BY id DESC LIMIT 1;`, Layer1Msg)
|
||||
var result sql.NullInt64
|
||||
if err := row.Scan(&result); err != nil {
|
||||
if err == sql.ErrNoRows || !result.Valid {
|
||||
return -1, nil
|
||||
}
|
||||
return 0, err
|
||||
}
|
||||
if result.Valid {
|
||||
return result.Int64, nil
|
||||
}
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func (l *l1CrossMsgOrm) DeleteL1CrossMsgAfterHeightDBTx(dbTx *sqlx.Tx, height int64) error {
|
||||
if _, err := l.db.Exec(`UPDATE cross_message SET deleted_at = current_timestamp WHERE height > $1 AND msg_type = $2;`, height, Layer1Msg); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *l1CrossMsgOrm) UpdateL1BlockTimestamp(height uint64, timestamp time.Time) error {
|
||||
if _, err := l.db.Exec(`UPDATE cross_message SET block_timestamp = $1 where height = $2 AND msg_type = $3 AND deleted_at IS NULL`, timestamp, height, Layer1Msg); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *l1CrossMsgOrm) GetL1EarliestNoBlockTimestampHeight() (uint64, error) {
|
||||
row := l.db.QueryRowx(`SELECT height FROM cross_message WHERE block_timestamp IS NULL AND msg_type = $1 AND deleted_at IS NULL ORDER BY height ASC LIMIT 1;`, Layer1Msg)
|
||||
var result uint64
|
||||
if err := row.Scan(&result); err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return 0, nil
|
||||
}
|
||||
return 0, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
177
bridge-history-api/db/orm/l2_cross_msg.go
Normal file
177
bridge-history-api/db/orm/l2_cross_msg.go
Normal file
@@ -0,0 +1,177 @@
|
||||
package orm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/log"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/lib/pq"
|
||||
)
|
||||
|
||||
type l2CrossMsgOrm struct {
|
||||
db *sqlx.DB
|
||||
}
|
||||
|
||||
// NewL2CrossMsgOrm create an NewL2CrossMsgOrm instance
|
||||
func NewL2CrossMsgOrm(db *sqlx.DB) L2CrossMsgOrm {
|
||||
return &l2CrossMsgOrm{db: db}
|
||||
}
|
||||
|
||||
func (l *l2CrossMsgOrm) GetL2CrossMsgByHash(l2Hash common.Hash) (*CrossMsg, error) {
|
||||
result := &CrossMsg{}
|
||||
row := l.db.QueryRowx(`SELECT * FROM cross_message WHERE layer2_hash = $1 AND deleted_at IS NULL;`, l2Hash.String())
|
||||
if err := row.StructScan(result); err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// GetL2CrossMsgByAddress returns all layer2 cross messages under given address
|
||||
// Warning: return empty slice if no data found
|
||||
func (l *l2CrossMsgOrm) GetL2CrossMsgByAddress(sender common.Address) ([]*CrossMsg, error) {
|
||||
var results []*CrossMsg
|
||||
rows, err := l.db.Queryx(`SELECT * FROM cross_message WHERE sender = $1 AND msg_type = $2 AND deleted_at IS NULL;`, sender.String(), Layer2Msg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() {
|
||||
if err = rows.Close(); err != nil {
|
||||
log.Error("failed to close rows", "err", err)
|
||||
}
|
||||
}()
|
||||
for rows.Next() {
|
||||
msg := &CrossMsg{}
|
||||
if err = rows.StructScan(msg); err != nil {
|
||||
break
|
||||
}
|
||||
results = append(results, msg)
|
||||
}
|
||||
if len(results) == 0 && errors.Is(err, sql.ErrNoRows) {
|
||||
// log.Warn("no unprocessed layer1 messages in db", "err", err)
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return results, nil
|
||||
|
||||
}
|
||||
|
||||
func (l *l2CrossMsgOrm) DeleteL2CrossMsgFromHeightDBTx(dbTx *sqlx.Tx, height int64) error {
|
||||
_, err := dbTx.Exec(`UPDATE cross_message SET deleted_at = current_timestamp where height > $1 AND msg_type = $2 ;`, height, Layer2Msg)
|
||||
if err != nil {
|
||||
log.Error("DeleteL1CrossMsgAfterHeightDBTx: failed to delete", "height", height, "err", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *l2CrossMsgOrm) BatchInsertL2CrossMsgDBTx(dbTx *sqlx.Tx, messages []*CrossMsg) error {
|
||||
if len(messages) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
var err error
|
||||
messageMaps := make([]map[string]interface{}, len(messages))
|
||||
for i, msg := range messages {
|
||||
messageMaps[i] = map[string]interface{}{
|
||||
"height": msg.Height,
|
||||
"sender": msg.Sender,
|
||||
"target": msg.Target,
|
||||
"asset": msg.Asset,
|
||||
"msg_hash": msg.MsgHash,
|
||||
"layer2_hash": msg.Layer2Hash,
|
||||
"layer1_token": msg.Layer1Token,
|
||||
"layer2_token": msg.Layer2Token,
|
||||
"token_ids": msg.TokenIDs,
|
||||
"amount": msg.Amount,
|
||||
"msg_type": Layer2Msg,
|
||||
}
|
||||
}
|
||||
_, err = dbTx.NamedExec(`insert into cross_message(height, sender, target, asset, msg_hash, layer2_hash, layer1_token, layer2_token, token_ids, amount, msg_type) values(:height, :sender, :target, :asset, :msg_hash, :layer2_hash, :layer1_token, :layer2_token, :token_ids, :amount, :msg_type);`, messageMaps)
|
||||
if err != nil {
|
||||
log.Error("BatchInsertL2CrossMsgDBTx: failed to insert l2 cross msgs", "err", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *l2CrossMsgOrm) UpdateL2CrossMsgHashDBTx(ctx context.Context, dbTx *sqlx.Tx, l2Hash, msgHash common.Hash) error {
|
||||
if _, err := dbTx.ExecContext(ctx, l.db.Rebind("update cross_message set msg_hash = ? where layer2_hash = ? AND deleted_at IS NULL;"), msgHash.String(), l2Hash.String()); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *l2CrossMsgOrm) UpdateL2CrossMsgHash(ctx context.Context, l2Hash, msgHash common.Hash) error {
|
||||
if _, err := l.db.ExecContext(ctx, l.db.Rebind("update cross_message set msg_hash = ? where layer2_hash = ? AND deleted_at IS NULL;"), msgHash.String(), l2Hash.String()); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *l2CrossMsgOrm) GetLatestL2ProcessedHeight() (int64, error) {
|
||||
row := l.db.QueryRowx(`SELECT height FROM cross_message WHERE msg_type = $1 AND deleted_at IS NULL ORDER BY id DESC LIMIT 1;`, Layer2Msg)
|
||||
var result sql.NullInt64
|
||||
if err := row.Scan(&result); err != nil {
|
||||
if err == sql.ErrNoRows || !result.Valid {
|
||||
return -1, nil
|
||||
}
|
||||
return 0, err
|
||||
}
|
||||
if result.Valid {
|
||||
return result.Int64, nil
|
||||
}
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func (l *l2CrossMsgOrm) UpdateL2BlockTimestamp(height uint64, timestamp time.Time) error {
|
||||
if _, err := l.db.Exec(`UPDATE cross_message SET block_timestamp = $1 where height = $2 AND msg_type = $3 AND deleted_at IS NULL`, timestamp, height, Layer2Msg); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *l2CrossMsgOrm) GetL2EarliestNoBlockTimestampHeight() (uint64, error) {
|
||||
row := l.db.QueryRowx(`SELECT height FROM cross_message WHERE block_timestamp IS NULL AND msg_type = $1 AND deleted_at IS NULL ORDER BY height ASC LIMIT 1;`, Layer2Msg)
|
||||
var result uint64
|
||||
if err := row.Scan(&result); err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return 0, nil
|
||||
}
|
||||
return 0, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (l *l2CrossMsgOrm) GetL2CrossMsgByMsgHashList(msgHashList []string) ([]*CrossMsg, error) {
|
||||
var results []*CrossMsg
|
||||
rows, err := l.db.Queryx(`SELECT * FROM cross_message WHERE msg_hash = ANY($1) AND msg_type = $2 AND deleted_at IS NULL;`, pq.Array(msgHashList), Layer2Msg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() {
|
||||
if err = rows.Close(); err != nil {
|
||||
log.Error("failed to close rows", "err", err)
|
||||
}
|
||||
}()
|
||||
for rows.Next() {
|
||||
msg := &CrossMsg{}
|
||||
if err = rows.StructScan(msg); err != nil {
|
||||
break
|
||||
}
|
||||
results = append(results, msg)
|
||||
}
|
||||
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, err
|
||||
}
|
||||
if len(results) == 0 {
|
||||
log.Debug("no L2CrossMsg under given msg hashes", "msg hash list", msgHashList)
|
||||
}
|
||||
return results, nil
|
||||
}
|
||||
189
bridge-history-api/db/orm/l2_sent_msg.go
Normal file
189
bridge-history-api/db/orm/l2_sent_msg.go
Normal file
@@ -0,0 +1,189 @@
|
||||
package orm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/log"
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
// L2SentMsg defines the struct for l2_sent_msg table record
|
||||
type L2SentMsg struct {
|
||||
ID uint64 `json:"id" db:"id"`
|
||||
OriginalSender string `json:"original_sender" db:"original_sender"`
|
||||
TxHash string `json:"tx_hash" db:"tx_hash"`
|
||||
MsgHash string `json:"msg_hash" db:"msg_hash"`
|
||||
Sender string `json:"sender" db:"sender"`
|
||||
Target string `json:"target" db:"target"`
|
||||
Value string `json:"value" db:"value"`
|
||||
Height uint64 `json:"height" db:"height"`
|
||||
Nonce uint64 `json:"nonce" db:"nonce"`
|
||||
BatchIndex uint64 `json:"batch_index" db:"batch_index"`
|
||||
MsgProof string `json:"msg_proof" db:"msg_proof"`
|
||||
MsgData string `json:"msg_data" db:"msg_data"`
|
||||
CreatedAt *time.Time `json:"created_at" db:"created_at"`
|
||||
UpdatedAt *time.Time `json:"updated_at" db:"updated_at"`
|
||||
DeletedAt *time.Time `json:"deleted_at" db:"deleted_at"`
|
||||
}
|
||||
|
||||
type l2SentMsgOrm struct {
|
||||
db *sqlx.DB
|
||||
}
|
||||
|
||||
// NewL2SentMsgOrm create an NewRollupBatchOrm instance
|
||||
func NewL2SentMsgOrm(db *sqlx.DB) L2SentMsgOrm {
|
||||
return &l2SentMsgOrm{db: db}
|
||||
}
|
||||
|
||||
func (l *l2SentMsgOrm) GetL2SentMsgByHash(msgHash string) (*L2SentMsg, error) {
|
||||
result := &L2SentMsg{}
|
||||
row := l.db.QueryRowx(`SELECT * FROM l2_sent_msg WHERE msg_hash = $1 AND deleted_at IS NULL;`, msgHash)
|
||||
if err := row.StructScan(result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (l *l2SentMsgOrm) BatchInsertL2SentMsgDBTx(dbTx *sqlx.Tx, messages []*L2SentMsg) error {
|
||||
if len(messages) == 0 {
|
||||
return nil
|
||||
}
|
||||
var err error
|
||||
messageMaps := make([]map[string]interface{}, len(messages))
|
||||
for i, msg := range messages {
|
||||
messageMaps[i] = map[string]interface{}{
|
||||
"original_sender": msg.OriginalSender,
|
||||
"tx_hash": msg.TxHash,
|
||||
"sender": msg.Sender,
|
||||
"target": msg.Target,
|
||||
"value": msg.Value,
|
||||
"msg_hash": msg.MsgHash,
|
||||
"height": msg.Height,
|
||||
"nonce": msg.Nonce,
|
||||
"batch_index": msg.BatchIndex,
|
||||
"msg_proof": msg.MsgProof,
|
||||
"msg_data": msg.MsgData,
|
||||
}
|
||||
}
|
||||
_, err = dbTx.NamedExec(`insert into l2_sent_msg(original_sender, tx_hash, sender, target, value, msg_hash, height, nonce, batch_index, msg_proof, msg_data) values(:original_sender, :tx_hash, :sender, :target, :value, :msg_hash, :height, :nonce, :batch_index, :msg_proof, :msg_data);`, messageMaps)
|
||||
if err != nil {
|
||||
log.Error("BatchInsertL2SentMsgDBTx: failed to insert l2 sent msgs", "err", err)
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (l *l2SentMsgOrm) GetLatestSentMsgHeightOnL2() (int64, error) {
|
||||
row := l.db.QueryRow(`SELECT height FROM l2_sent_msg WHERE deleted_at IS NULL ORDER BY nonce DESC LIMIT 1;`)
|
||||
var result sql.NullInt64
|
||||
if err := row.Scan(&result); err != nil {
|
||||
if err == sql.ErrNoRows || !result.Valid {
|
||||
return -1, nil
|
||||
}
|
||||
return 0, err
|
||||
}
|
||||
if result.Valid {
|
||||
return result.Int64, nil
|
||||
}
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func (l *l2SentMsgOrm) UpdateL2MessageProofInDBTx(ctx context.Context, dbTx *sqlx.Tx, msgHash string, proof string, batchIndex uint64) error {
|
||||
if _, err := dbTx.ExecContext(ctx, l.db.Rebind("update l2_sent_msg set msg_proof = ?, batch_index = ? where msg_hash = ? AND deleted_at IS NULL;"), proof, batchIndex, msgHash); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *l2SentMsgOrm) GetLatestL2SentMsgBatchIndex() (int64, error) {
|
||||
row := l.db.QueryRow(`SELECT batch_index FROM l2_sent_msg WHERE batch_index != 0 AND deleted_at IS NULL ORDER BY batch_index DESC LIMIT 1;`)
|
||||
var result sql.NullInt64
|
||||
if err := row.Scan(&result); err != nil {
|
||||
if err == sql.ErrNoRows || !result.Valid {
|
||||
return -1, nil
|
||||
}
|
||||
return -1, err
|
||||
}
|
||||
if result.Valid {
|
||||
return result.Int64, nil
|
||||
}
|
||||
return -1, nil
|
||||
}
|
||||
|
||||
func (l *l2SentMsgOrm) GetL2SentMsgMsgHashByHeightRange(startHeight, endHeight uint64) ([]*L2SentMsg, error) {
|
||||
var results []*L2SentMsg
|
||||
rows, err := l.db.Queryx(`SELECT * FROM l2_sent_msg WHERE height >= $1 AND height <= $2 AND deleted_at IS NULL ORDER BY nonce ASC;`, startHeight, endHeight)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() {
|
||||
if err = rows.Close(); err != nil {
|
||||
log.Error("failed to close rows", "err", err)
|
||||
}
|
||||
}()
|
||||
for rows.Next() {
|
||||
msg := &L2SentMsg{}
|
||||
if err = rows.StructScan(msg); err != nil {
|
||||
break
|
||||
}
|
||||
results = append(results, msg)
|
||||
}
|
||||
return results, err
|
||||
}
|
||||
|
||||
func (l *l2SentMsgOrm) GetL2SentMessageByNonce(nonce uint64) (*L2SentMsg, error) {
|
||||
result := &L2SentMsg{}
|
||||
row := l.db.QueryRowx(`SELECT * FROM l2_sent_msg WHERE nonce = $1 AND deleted_at IS NULL;`, nonce)
|
||||
err := row.StructScan(result)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (l *l2SentMsgOrm) GetLatestL2SentMsgLEHeight(endBlockNumber uint64) (*L2SentMsg, error) {
|
||||
result := &L2SentMsg{}
|
||||
row := l.db.QueryRowx(`select * from l2_sent_msg where height <= $1 AND deleted_at IS NULL order by nonce desc limit 1`, endBlockNumber)
|
||||
err := row.StructScan(result)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (l *l2SentMsgOrm) DeleteL2SentMsgAfterHeightDBTx(dbTx *sqlx.Tx, height int64) error {
|
||||
_, err := dbTx.Exec(`UPDATE l2_sent_msg SET deleted_at = current_timestamp WHERE height > $1;`, height)
|
||||
return err
|
||||
}
|
||||
|
||||
func (l *l2SentMsgOrm) GetClaimableL2SentMsgByAddressWithOffset(address string, offset int64, limit int64) ([]*L2SentMsg, error) {
|
||||
var results []*L2SentMsg
|
||||
rows, err := l.db.Queryx(`SELECT * FROM l2_sent_msg WHERE id NOT IN (SELECT l2_sent_msg.id FROM l2_sent_msg INNER JOIN relayed_msg ON l2_sent_msg.msg_hash = relayed_msg.msg_hash WHERE l2_sent_msg.deleted_at IS NULL AND relayed_msg.deleted_at IS NULL) AND (original_sender=$1 OR sender = $1) ORDER BY id DESC LIMIT $2 OFFSET $3;`, address, limit, offset)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() {
|
||||
if err = rows.Close(); err != nil {
|
||||
log.Error("failed to close rows", "err", err)
|
||||
}
|
||||
}()
|
||||
for rows.Next() {
|
||||
msg := &L2SentMsg{}
|
||||
if err = rows.StructScan(msg); err != nil {
|
||||
break
|
||||
}
|
||||
results = append(results, msg)
|
||||
}
|
||||
return results, err
|
||||
}
|
||||
|
||||
func (l *l2SentMsgOrm) GetClaimableL2SentMsgByAddressTotalNum(address string) (uint64, error) {
|
||||
var count uint64
|
||||
row := l.db.QueryRowx(`SELECT COUNT(*) FROM l2_sent_msg WHERE id NOT IN (SELECT l2_sent_msg.id FROM l2_sent_msg INNER JOIN relayed_msg ON l2_sent_msg.msg_hash = relayed_msg.msg_hash WHERE l2_sent_msg.deleted_at IS NULL AND relayed_msg.deleted_at IS NULL) AND (original_sender=$1 OR sender = $1);`, address)
|
||||
if err := row.Scan(&count); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
100
bridge-history-api/db/orm/relayed_msg.go
Normal file
100
bridge-history-api/db/orm/relayed_msg.go
Normal file
@@ -0,0 +1,100 @@
|
||||
package orm
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
|
||||
"github.com/ethereum/go-ethereum/log"
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
// RelayedMsg is the struct for relayed_msg table
|
||||
type RelayedMsg struct {
|
||||
MsgHash string `json:"msg_hash" db:"msg_hash"`
|
||||
Height uint64 `json:"height" db:"height"`
|
||||
Layer1Hash string `json:"layer1_hash" db:"layer1_hash"`
|
||||
Layer2Hash string `json:"layer2_hash" db:"layer2_hash"`
|
||||
}
|
||||
|
||||
type relayedMsgOrm struct {
|
||||
db *sqlx.DB
|
||||
}
|
||||
|
||||
// NewRelayedMsgOrm create an NewRelayedMsgOrm instance
|
||||
func NewRelayedMsgOrm(db *sqlx.DB) RelayedMsgOrm {
|
||||
return &relayedMsgOrm{db: db}
|
||||
}
|
||||
|
||||
func (l *relayedMsgOrm) BatchInsertRelayedMsgDBTx(dbTx *sqlx.Tx, messages []*RelayedMsg) error {
|
||||
if len(messages) == 0 {
|
||||
return nil
|
||||
}
|
||||
var err error
|
||||
messageMaps := make([]map[string]interface{}, len(messages))
|
||||
for i, msg := range messages {
|
||||
messageMaps[i] = map[string]interface{}{
|
||||
"msg_hash": msg.MsgHash,
|
||||
"height": msg.Height,
|
||||
"layer1_hash": msg.Layer1Hash,
|
||||
"layer2_hash": msg.Layer2Hash,
|
||||
}
|
||||
}
|
||||
_, err = dbTx.NamedExec(`insert into relayed_msg(msg_hash, height, layer1_hash, layer2_hash) values(:msg_hash, :height, :layer1_hash, :layer2_hash);`, messageMaps)
|
||||
if err != nil {
|
||||
log.Error("BatchInsertRelayedMsgDBTx: failed to insert relayed msgs", "err", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *relayedMsgOrm) GetRelayedMsgByHash(msgHash string) (*RelayedMsg, error) {
|
||||
result := &RelayedMsg{}
|
||||
row := l.db.QueryRowx(`SELECT msg_hash, height, layer1_hash, layer2_hash FROM relayed_msg WHERE msg_hash = $1 AND deleted_at IS NULL;`, msgHash)
|
||||
if err := row.StructScan(result); err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (l *relayedMsgOrm) GetLatestRelayedHeightOnL1() (int64, error) {
|
||||
row := l.db.QueryRow(`SELECT height FROM relayed_msg WHERE layer1_hash != '' AND deleted_at IS NULL ORDER BY height DESC LIMIT 1;`)
|
||||
var result sql.NullInt64
|
||||
if err := row.Scan(&result); err != nil {
|
||||
if err == sql.ErrNoRows || !result.Valid {
|
||||
return -1, nil
|
||||
}
|
||||
return 0, err
|
||||
}
|
||||
if result.Valid {
|
||||
return result.Int64, nil
|
||||
}
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func (l *relayedMsgOrm) GetLatestRelayedHeightOnL2() (int64, error) {
|
||||
row := l.db.QueryRow(`SELECT height FROM relayed_msg WHERE layer2_hash != '' AND deleted_at IS NULL ORDER BY height DESC LIMIT 1;`)
|
||||
var result sql.NullInt64
|
||||
if err := row.Scan(&result); err != nil {
|
||||
if err == sql.ErrNoRows || !result.Valid {
|
||||
return -1, nil
|
||||
}
|
||||
return 0, err
|
||||
}
|
||||
if result.Valid {
|
||||
return result.Int64, nil
|
||||
}
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func (l *relayedMsgOrm) DeleteL1RelayedHashAfterHeightDBTx(dbTx *sqlx.Tx, height int64) error {
|
||||
_, err := dbTx.Exec(`UPDATE relayed_msg SET deleted_at = current_timestamp WHERE height > $1 AND layer1_hash != '';`, height)
|
||||
return err
|
||||
}
|
||||
|
||||
func (l *relayedMsgOrm) DeleteL2RelayedHashAfterHeightDBTx(dbTx *sqlx.Tx, height int64) error {
|
||||
_, err := dbTx.Exec(`UPDATE relayed_msg SET deleted_at = current_timestamp WHERE height > $1 AND layer2_hash != '';`, height)
|
||||
return err
|
||||
}
|
||||
103
bridge-history-api/db/orm_factory.go
Normal file
103
bridge-history-api/db/orm_factory.go
Normal file
@@ -0,0 +1,103 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
|
||||
"github.com/ethereum/go-ethereum/log"
|
||||
"github.com/jmoiron/sqlx"
|
||||
_ "github.com/lib/pq" //nolint:golint
|
||||
|
||||
"bridge-history-api/config"
|
||||
"bridge-history-api/db/orm"
|
||||
)
|
||||
|
||||
// OrmFactory include all ormFactory interface
|
||||
type OrmFactory interface {
|
||||
orm.L1CrossMsgOrm
|
||||
orm.L2CrossMsgOrm
|
||||
orm.RelayedMsgOrm
|
||||
orm.L2SentMsgOrm
|
||||
orm.RollupBatchOrm
|
||||
GetTotalCrossMsgCountByAddress(sender string) (uint64, error)
|
||||
GetCrossMsgsByAddressWithOffset(sender string, offset int64, limit int64) ([]*orm.CrossMsg, error)
|
||||
GetDB() *sqlx.DB
|
||||
Beginx() (*sqlx.Tx, error)
|
||||
Close() error
|
||||
}
|
||||
|
||||
type ormFactory struct {
|
||||
orm.L1CrossMsgOrm
|
||||
orm.L2CrossMsgOrm
|
||||
orm.RelayedMsgOrm
|
||||
orm.L2SentMsgOrm
|
||||
orm.RollupBatchOrm
|
||||
*sqlx.DB
|
||||
}
|
||||
|
||||
// NewOrmFactory create an ormFactory factory include all ormFactory interface
|
||||
func NewOrmFactory(cfg *config.Config) (OrmFactory, error) {
|
||||
// Initialize sql/sqlx
|
||||
db, err := sqlx.Open(cfg.DB.DriverName, cfg.DB.DSN)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
db.SetMaxOpenConns(cfg.DB.MaxOpenNum)
|
||||
db.SetMaxIdleConns(cfg.DB.MaxIdleNum)
|
||||
if err = db.Ping(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &ormFactory{
|
||||
L1CrossMsgOrm: orm.NewL1CrossMsgOrm(db),
|
||||
L2CrossMsgOrm: orm.NewL2CrossMsgOrm(db),
|
||||
RelayedMsgOrm: orm.NewRelayedMsgOrm(db),
|
||||
L2SentMsgOrm: orm.NewL2SentMsgOrm(db),
|
||||
RollupBatchOrm: orm.NewRollupBatchOrm(db),
|
||||
DB: db,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (o *ormFactory) GetDB() *sqlx.DB {
|
||||
return o.DB
|
||||
}
|
||||
|
||||
func (o *ormFactory) Beginx() (*sqlx.Tx, error) {
|
||||
return o.DB.Beginx()
|
||||
}
|
||||
|
||||
func (o *ormFactory) GetTotalCrossMsgCountByAddress(sender string) (uint64, error) {
|
||||
var count uint64
|
||||
row := o.DB.QueryRowx(`SELECT COUNT(*) FROM cross_message WHERE sender = $1 AND deleted_at IS NULL;`, sender)
|
||||
if err := row.Scan(&count); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
func (o *ormFactory) GetCrossMsgsByAddressWithOffset(sender string, offset int64, limit int64) ([]*orm.CrossMsg, error) {
|
||||
para := sender
|
||||
var results []*orm.CrossMsg
|
||||
rows, err := o.DB.Queryx(`SELECT * FROM cross_message WHERE sender = $1 AND deleted_at IS NULL ORDER BY block_timestamp DESC NULLS FIRST, id DESC LIMIT $2 OFFSET $3;`, para, limit, offset)
|
||||
if err != nil || rows == nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() {
|
||||
if err = rows.Close(); err != nil {
|
||||
log.Error("failed to close rows", "err", err)
|
||||
}
|
||||
}()
|
||||
for rows.Next() {
|
||||
msg := &orm.CrossMsg{}
|
||||
if err = rows.StructScan(msg); err != nil {
|
||||
break
|
||||
}
|
||||
results = append(results, msg)
|
||||
}
|
||||
if len(results) == 0 && errors.Is(err, sql.ErrNoRows) {
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return results, nil
|
||||
}
|
||||
@@ -5,14 +5,15 @@ go 1.19
|
||||
require (
|
||||
github.com/ethereum/go-ethereum v1.12.0
|
||||
github.com/iris-contrib/middleware/cors v0.0.0-20230531125531-980d3a09a458
|
||||
github.com/jmoiron/sqlx v1.3.5
|
||||
github.com/kataras/iris/v12 v12.2.0
|
||||
github.com/lib/pq v1.10.9
|
||||
github.com/mattn/go-colorable v0.1.13
|
||||
github.com/mattn/go-isatty v0.0.19
|
||||
github.com/modern-go/reflect2 v1.0.2
|
||||
github.com/pressly/goose/v3 v3.7.0
|
||||
github.com/stretchr/testify v1.8.3
|
||||
github.com/urfave/cli/v2 v2.25.7
|
||||
gorm.io/gorm v1.25.2
|
||||
)
|
||||
|
||||
require (
|
||||
@@ -53,6 +54,7 @@ require (
|
||||
github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff // indirect
|
||||
github.com/getsentry/sentry-go v0.18.0 // indirect
|
||||
github.com/go-ole/go-ole v1.2.6 // indirect
|
||||
github.com/go-sql-driver/mysql v1.7.1 // indirect
|
||||
github.com/go-stack/stack v1.8.1 // indirect
|
||||
github.com/gobwas/httphead v0.1.0 // indirect
|
||||
github.com/gobwas/pool v0.2.1 // indirect
|
||||
@@ -76,8 +78,6 @@ require (
|
||||
github.com/iris-contrib/go.uuid v2.0.0+incompatible // indirect
|
||||
github.com/iris-contrib/schema v0.0.6 // indirect
|
||||
github.com/jackpal/go-nat-pmp v1.0.2 // indirect
|
||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||
github.com/jinzhu/now v1.1.5 // indirect
|
||||
github.com/josharian/intern v1.0.0 // indirect
|
||||
github.com/kataras/blocks v0.0.7 // indirect
|
||||
github.com/kataras/golog v0.1.8 // indirect
|
||||
@@ -91,9 +91,10 @@ require (
|
||||
github.com/mailgun/raymond/v2 v2.0.48 // indirect
|
||||
github.com/mailru/easyjson v0.7.7 // indirect
|
||||
github.com/mattn/go-runewidth v0.0.14 // indirect
|
||||
github.com/mattn/go-sqlite3 v1.14.16 // indirect
|
||||
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
|
||||
github.com/mediocregopher/radix/v3 v3.8.1 // indirect
|
||||
github.com/microcosm-cc/bluemonday v1.0.25 // indirect
|
||||
github.com/microcosm-cc/bluemonday v1.0.23 // indirect
|
||||
github.com/mitchellh/mapstructure v1.5.0 // indirect
|
||||
github.com/mitchellh/pointerstructure v1.2.0 // indirect
|
||||
github.com/mmcloughlin/addchain v0.4.0 // indirect
|
||||
|
||||
@@ -143,6 +143,9 @@ github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
|
||||
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
|
||||
github.com/go-openapi/jsonpointer v0.19.5/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
|
||||
github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
|
||||
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
|
||||
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
|
||||
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
|
||||
github.com/go-stack/stack v1.8.1 h1:ntEHSVwIt7PNXNpgPmVfMrNhLtgjlmnZha2kOpuRiDw=
|
||||
github.com/go-stack/stack v1.8.1/go.mod h1:dcoOX6HbPZSZptuspn9bctJ+N/CnF5gGygcUP3XYfe4=
|
||||
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE=
|
||||
@@ -248,10 +251,8 @@ github.com/iris-contrib/schema v0.0.6/go.mod h1:iYszG0IOsuIsfzjymw1kMzTL8YQcCWlm
|
||||
github.com/jackpal/go-nat-pmp v1.0.2 h1:KzKSgb7qkJvOUTqYl9/Hg/me3pWgBmERKrTGD7BdWus=
|
||||
github.com/jackpal/go-nat-pmp v1.0.2/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc=
|
||||
github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
|
||||
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
|
||||
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
||||
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
|
||||
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
|
||||
github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g=
|
||||
github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ=
|
||||
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
|
||||
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
|
||||
github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ=
|
||||
@@ -301,6 +302,9 @@ github.com/labstack/echo/v4 v4.2.1/go.mod h1:AA49e0DZ8kk5jTOOCKNuPR6oTnBS0dYiM4F
|
||||
github.com/labstack/echo/v4 v4.5.0/go.mod h1:czIriw4a0C1dFun+ObrXp7ok03xON0N1awStJ6ArI7Y=
|
||||
github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL7NoOu+k=
|
||||
github.com/leanovate/gopter v0.2.9 h1:fQjYxZaynp97ozCzfOyOuAGOU4aU/z37zf/tOujFk7c=
|
||||
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
|
||||
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
|
||||
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
|
||||
github.com/mailgun/raymond/v2 v2.0.48 h1:5dmlB680ZkFG2RN/0lvTAghrSxIESeu9/2aeDqACtjw=
|
||||
github.com/mailgun/raymond/v2 v2.0.48/go.mod h1:lsgvL50kgt1ylcFJYZiULi5fjPBkkhNfj4KA0W54Z18=
|
||||
@@ -328,6 +332,9 @@ github.com/mattn/go-runewidth v0.0.3/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzp
|
||||
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
|
||||
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
|
||||
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
||||
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
|
||||
github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=
|
||||
github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
|
||||
github.com/mattn/goveralls v0.0.2/go.mod h1:8d1ZMHsd7fW6IRPKQh46F2WRpyib5/X4FOpevwGNQEw=
|
||||
github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo=
|
||||
github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
|
||||
@@ -335,8 +342,8 @@ github.com/mediocregopher/radix/v3 v3.4.2/go.mod h1:8FL3F6UQRXHXIBSPUs5h0RybMF8i
|
||||
github.com/mediocregopher/radix/v3 v3.8.1 h1:rOkHflVuulFKlwsLY01/M2cM2tWCjDoETcMqKbAWu1M=
|
||||
github.com/mediocregopher/radix/v3 v3.8.1/go.mod h1:8FL3F6UQRXHXIBSPUs5h0RybMF8i4n7wVopoX3x7Bv8=
|
||||
github.com/microcosm-cc/bluemonday v1.0.2/go.mod h1:iVP4YcDBq+n/5fb23BhYFvIMq/leAFZyRl6bYmGDlGc=
|
||||
github.com/microcosm-cc/bluemonday v1.0.25 h1:4NEwSfiJ+Wva0VxN5B8OwMicaJvD8r9tlJWm9rtloEg=
|
||||
github.com/microcosm-cc/bluemonday v1.0.25/go.mod h1:ZIOjCQp1OrzBBPIJmfX4qDYFuhU02nx4bn030ixfHLE=
|
||||
github.com/microcosm-cc/bluemonday v1.0.23 h1:SMZe2IGa0NuHvnVNAZ+6B38gsTbi5e4sViiWJyDDqFY=
|
||||
github.com/microcosm-cc/bluemonday v1.0.23/go.mod h1:mN70sk7UkkF8TUr2IGBpNN0jAgStuPzlK76QuruE/z4=
|
||||
github.com/minio/highwayhash v1.0.2 h1:Aak5U0nElisjDCfPSG79Tgzkn2gl66NxOMspRrKnA/g=
|
||||
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
|
||||
github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0=
|
||||
@@ -713,8 +720,6 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C
|
||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gorm.io/gorm v1.25.2 h1:gs1o6Vsa+oVKG/a9ElL3XgyGfghFfkKA2SInQaCyMho=
|
||||
gorm.io/gorm v1.25.2/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
|
||||
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
lukechampine.com/uint128 v1.2.0 h1:mBi/5l91vocEN8otkC5bDLhi2KdCticRiwbdB0O+rjI=
|
||||
|
||||
@@ -1,91 +0,0 @@
|
||||
package orm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/log"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// RollupBatch is the struct for rollup_batch table
|
||||
type RollupBatch struct {
|
||||
db *gorm.DB `gorm:"column:-"`
|
||||
|
||||
ID uint64 `json:"id" gorm:"column:id"`
|
||||
BatchIndex uint64 `json:"batch_index" gorm:"column:batch_index"`
|
||||
BatchHash string `json:"batch_hash" gorm:"column:batch_hash"`
|
||||
CommitHeight uint64 `json:"commit_height" gorm:"column:commit_height"`
|
||||
StartBlockNumber uint64 `json:"start_block_number" gorm:"column:start_block_number"`
|
||||
EndBlockNumber uint64 `json:"end_block_number" gorm:"column:end_block_number"`
|
||||
CreatedAt *time.Time `json:"created_at" gorm:"column:created_at"`
|
||||
UpdatedAt *time.Time `json:"updated_at" gorm:"column:updated_at"`
|
||||
DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"column:deleted_at;default:NULL"`
|
||||
}
|
||||
|
||||
// NewRollupBatch create an RollupBatch instance
|
||||
func NewRollupBatch(db *gorm.DB) *RollupBatch {
|
||||
return &RollupBatch{db: db}
|
||||
}
|
||||
|
||||
// TableName returns the table name for the Batch model.
|
||||
func (*RollupBatch) TableName() string {
|
||||
return "rollup_batch"
|
||||
}
|
||||
|
||||
// GetLatestRollupBatchProcessedHeight return latest processed height from rollup_batch table
|
||||
func (r *RollupBatch) GetLatestRollupBatchProcessedHeight(ctx context.Context) (uint64, error) {
|
||||
var result RollupBatch
|
||||
err := r.db.WithContext(ctx).Unscoped().Select("commit_height").Order("id desc").First(&result).Error
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("RollupBatch.GetLatestRollupBatchProcessedHeight error: %w", err)
|
||||
}
|
||||
return result.CommitHeight, nil
|
||||
}
|
||||
|
||||
// GetLatestRollupBatch return the latest rollup batch in db
|
||||
func (r *RollupBatch) GetLatestRollupBatch(ctx context.Context) (*RollupBatch, error) {
|
||||
var result RollupBatch
|
||||
err := r.db.WithContext(ctx).Model(&RollupBatch{}).Where("batch_hash is not NULL").Order("batch_index desc").First(&result).Error
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, fmt.Errorf("RollupBatch.GetLatestRollupBatch error: %w", err)
|
||||
}
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
// GetRollupBatchByIndex return the rollup batch by index
|
||||
func (r *RollupBatch) GetRollupBatchByIndex(ctx context.Context, index uint64) (*RollupBatch, error) {
|
||||
var result RollupBatch
|
||||
err := r.db.WithContext(ctx).Model(&RollupBatch{}).Where("batch_index = ?", index).First(&result).Error
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("RollupBatch.GetRollupBatchByIndex error: %w", err)
|
||||
}
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
// InsertRollupBatch batch insert rollup batch into db and return the transaction
|
||||
func (r *RollupBatch) InsertRollupBatch(ctx context.Context, batches []*RollupBatch, dbTx ...*gorm.DB) error {
|
||||
if len(batches) == 0 {
|
||||
return nil
|
||||
}
|
||||
db := r.db
|
||||
if len(dbTx) > 0 && dbTx[0] != nil {
|
||||
db = dbTx[0]
|
||||
}
|
||||
err := db.WithContext(ctx).Model(&RollupBatch{}).Create(&batches).Error
|
||||
if err != nil {
|
||||
batchIndexes := make([]uint64, 0, len(batches))
|
||||
heights := make([]uint64, 0, len(batches))
|
||||
for _, batch := range batches {
|
||||
batchIndexes = append(batchIndexes, batch.BatchIndex)
|
||||
heights = append(heights, batch.CommitHeight)
|
||||
}
|
||||
log.Error("failed to insert rollup batch", "batchIndexes", batchIndexes, "heights", heights)
|
||||
return fmt.Errorf("RollupBatch.InsertRollupBatch error: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,370 +0,0 @@
|
||||
package orm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/log"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// AssetType can be ETH/ERC20/ERC1155/ERC721
|
||||
type AssetType int
|
||||
|
||||
// MsgType can be layer1/layer2 msg
|
||||
type MsgType int
|
||||
|
||||
func (a AssetType) String() string {
|
||||
switch a {
|
||||
case ETH:
|
||||
return "ETH"
|
||||
case ERC20:
|
||||
return "ERC20"
|
||||
case ERC1155:
|
||||
return "ERC1155"
|
||||
case ERC721:
|
||||
return "ERC721"
|
||||
}
|
||||
return "Unknown Asset Type"
|
||||
}
|
||||
|
||||
const (
|
||||
// ETH = 0
|
||||
ETH AssetType = iota
|
||||
// ERC20 = 1
|
||||
ERC20
|
||||
// ERC721 = 2
|
||||
ERC721
|
||||
// ERC1155 = 3
|
||||
ERC1155
|
||||
)
|
||||
|
||||
const (
|
||||
// UnknownMsg = 0
|
||||
UnknownMsg MsgType = iota
|
||||
// Layer1Msg = 1
|
||||
Layer1Msg
|
||||
// Layer2Msg = 2
|
||||
Layer2Msg
|
||||
)
|
||||
|
||||
// CrossMsg represents a cross message from layer 1 to layer 2
|
||||
type CrossMsg struct {
|
||||
db *gorm.DB `gorm:"column:-"`
|
||||
|
||||
ID uint64 `json:"id" gorm:"column:id"`
|
||||
MsgHash string `json:"msg_hash" gorm:"column:msg_hash"`
|
||||
Height uint64 `json:"height" gorm:"column:height"`
|
||||
Sender string `json:"sender" gorm:"column:sender"`
|
||||
Target string `json:"target" gorm:"column:target"`
|
||||
Amount string `json:"amount" gorm:"column:amount"`
|
||||
Layer1Hash string `json:"layer1_hash" gorm:"column:layer1_hash;default:''"`
|
||||
Layer2Hash string `json:"layer2_hash" gorm:"column:layer2_hash;default:''"`
|
||||
Layer1Token string `json:"layer1_token" gorm:"column:layer1_token;default:''"`
|
||||
Layer2Token string `json:"layer2_token" gorm:"column:layer2_token;default:''"`
|
||||
TokenIDs string `json:"token_ids" gorm:"column:token_ids;default:''"`
|
||||
TokenAmounts string `json:"token_amounts" gorm:"column:token_amounts;default:''"`
|
||||
Asset int `json:"asset" gorm:"column:asset"`
|
||||
MsgType int `json:"msg_type" gorm:"column:msg_type"`
|
||||
Timestamp *time.Time `json:"timestamp" gorm:"column:block_timestamp;default;NULL"`
|
||||
CreatedAt *time.Time `json:"created_at" gorm:"column:created_at"`
|
||||
UpdatedAt *time.Time `json:"updated_at" gorm:"column:updated_at"`
|
||||
DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"column:deleted_at;default:NULL"`
|
||||
}
|
||||
|
||||
// TableName returns the table name for the CrossMsg model.
|
||||
func (*CrossMsg) TableName() string {
|
||||
return "cross_message"
|
||||
}
|
||||
|
||||
// NewCrossMsg returns a new instance of CrossMsg.
|
||||
func NewCrossMsg(db *gorm.DB) *CrossMsg {
|
||||
return &CrossMsg{db: db}
|
||||
}
|
||||
|
||||
// L1 Cross Msgs Operations
|
||||
|
||||
// GetL1CrossMsgByHash returns layer1 cross message by given hash
|
||||
func (c *CrossMsg) GetL1CrossMsgByHash(ctx context.Context, l1Hash common.Hash) (*CrossMsg, error) {
|
||||
var result CrossMsg
|
||||
err := c.db.WithContext(ctx).Model(&CrossMsg{}).Where("layer1_hash = ? AND msg_type = ?", l1Hash.String(), Layer1Msg).First(&result).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, fmt.Errorf("CrossMsg.GetL1CrossMsgByHash error: %w", err)
|
||||
}
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
// GetLatestL1ProcessedHeight returns the latest processed height of layer1 cross messages
|
||||
func (c *CrossMsg) GetLatestL1ProcessedHeight(ctx context.Context) (uint64, error) {
|
||||
var result CrossMsg
|
||||
err := c.db.WithContext(ctx).Model(&CrossMsg{}).Where("msg_type = ?", Layer1Msg).
|
||||
Select("height").
|
||||
Order("id DESC").
|
||||
First(&result).
|
||||
Error
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return 0, nil
|
||||
}
|
||||
return 0, fmt.Errorf("CrossMsg.GetLatestL1ProcessedHeight error: %w", err)
|
||||
}
|
||||
return result.Height, nil
|
||||
}
|
||||
|
||||
// GetL1EarliestNoBlockTimestampHeight returns the earliest layer1 cross message height which has no block timestamp
|
||||
func (c *CrossMsg) GetL1EarliestNoBlockTimestampHeight(ctx context.Context) (uint64, error) {
|
||||
var result CrossMsg
|
||||
err := c.db.WithContext(ctx).Model(&CrossMsg{}).
|
||||
Where("block_timestamp IS NULL AND msg_type = ?", Layer1Msg).
|
||||
Select("height").
|
||||
Order("height ASC").
|
||||
First(&result).
|
||||
Error
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return 0, nil
|
||||
}
|
||||
return 0, fmt.Errorf("CrossMsg.GetL1EarliestNoBlockTimestampHeight error: %w", err)
|
||||
}
|
||||
return result.Height, nil
|
||||
}
|
||||
|
||||
// InsertL1CrossMsg batch insert layer1 cross messages into db
|
||||
func (c *CrossMsg) InsertL1CrossMsg(ctx context.Context, messages []*CrossMsg, dbTx ...*gorm.DB) error {
|
||||
if len(messages) == 0 {
|
||||
return nil
|
||||
}
|
||||
db := c.db
|
||||
if len(dbTx) > 0 && dbTx[0] != nil {
|
||||
db = dbTx[0]
|
||||
}
|
||||
db.WithContext(ctx)
|
||||
err := db.Model(&CrossMsg{}).Create(&messages).Error
|
||||
if err != nil {
|
||||
l1hashes := make([]string, 0, len(messages))
|
||||
heights := make([]uint64, 0, len(messages))
|
||||
for _, msg := range messages {
|
||||
l1hashes = append(l1hashes, msg.Layer1Hash)
|
||||
heights = append(heights, msg.Height)
|
||||
}
|
||||
log.Error("failed to insert l1 cross messages", "l1hashes", l1hashes, "heights", heights, "err", err)
|
||||
return fmt.Errorf("CrossMsg.InsertL1CrossMsg error: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateL1CrossMsgHash update l1 cross msg hash in db, no need to check msg_type since layer1_hash wont be empty if its layer1 msg
|
||||
func (c *CrossMsg) UpdateL1CrossMsgHash(ctx context.Context, l1Hash, msgHash common.Hash, dbTx ...*gorm.DB) error {
|
||||
db := c.db
|
||||
if len(dbTx) > 0 && dbTx[0] != nil {
|
||||
db = dbTx[0]
|
||||
}
|
||||
db.WithContext(ctx)
|
||||
err := c.db.Model(&CrossMsg{}).Where("layer1_hash = ?", l1Hash.Hex()).Update("msg_hash", msgHash.Hex()).Error
|
||||
if err != nil {
|
||||
return fmt.Errorf("CrossMsg.UpdateL1CrossMsgHash error: %w", err)
|
||||
}
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
// UpdateL1BlockTimestamp update layer1 block timestamp
|
||||
func (c *CrossMsg) UpdateL1BlockTimestamp(ctx context.Context, height uint64, timestamp time.Time) error {
|
||||
err := c.db.WithContext(ctx).Model(&CrossMsg{}).
|
||||
Where("height = ? AND msg_type = ?", height, Layer1Msg).
|
||||
Update("block_timestamp", timestamp).Error
|
||||
if err != nil {
|
||||
return fmt.Errorf("CrossMsg.UpdateL1BlockTimestamp error: %w", err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// DeleteL1CrossMsgAfterHeight soft delete layer1 cross messages after given height
|
||||
func (c *CrossMsg) DeleteL1CrossMsgAfterHeight(ctx context.Context, height uint64, dbTx ...*gorm.DB) error {
|
||||
db := c.db
|
||||
if len(dbTx) > 0 && dbTx[0] != nil {
|
||||
db = dbTx[0]
|
||||
}
|
||||
db.WithContext(ctx)
|
||||
err := db.Delete(&CrossMsg{}, "height > ? AND msg_type = ?", height, Layer1Msg).Error
|
||||
if err != nil {
|
||||
return fmt.Errorf("CrossMsg.DeleteL1CrossMsgAfterHeight error: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// L2 Cross Msgs Operations
|
||||
|
||||
// GetL2CrossMsgByHash returns layer2 cross message by given hash
|
||||
func (c *CrossMsg) GetL2CrossMsgByHash(ctx context.Context, l2Hash common.Hash) (*CrossMsg, error) {
|
||||
var result CrossMsg
|
||||
err := c.db.WithContext(ctx).Model(&CrossMsg{}).Where("layer2_hash = ? AND msg_type = ?", l2Hash.String(), Layer1Msg).First(&result).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, fmt.Errorf("CrossMsg.GetL2CrossMsgByHash error: %w", err)
|
||||
}
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
// GetLatestL2ProcessedHeight returns the latest processed height of layer2 cross messages
|
||||
func (c *CrossMsg) GetLatestL2ProcessedHeight(ctx context.Context) (uint64, error) {
|
||||
var result CrossMsg
|
||||
err := c.db.WithContext(ctx).Model(&CrossMsg{}).
|
||||
Select("height").
|
||||
Where("msg_type = ?", Layer2Msg).
|
||||
Order("id DESC").
|
||||
First(&result).
|
||||
Error
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return 0, nil
|
||||
}
|
||||
return 0, fmt.Errorf("CrossMsg.GetLatestL2ProcessedHeight error: %w", err)
|
||||
}
|
||||
return result.Height, nil
|
||||
}
|
||||
|
||||
// GetL2CrossMsgByMsgHashList returns layer2 cross messages under given msg hashes
|
||||
func (c *CrossMsg) GetL2CrossMsgByMsgHashList(ctx context.Context, msgHashList []string) ([]*CrossMsg, error) {
|
||||
var results []*CrossMsg
|
||||
err := c.db.WithContext(ctx).Model(&CrossMsg{}).
|
||||
Where("msg_hash IN (?) AND msg_type = ?", msgHashList, Layer2Msg).
|
||||
Find(&results).
|
||||
Error
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("CrossMsg.GetL2CrossMsgByMsgHashList error: %w", err)
|
||||
}
|
||||
if len(results) == 0 {
|
||||
log.Debug("no CrossMsg under given msg hashes", "msg hash list", msgHashList)
|
||||
}
|
||||
return results, nil
|
||||
}
|
||||
|
||||
// GetL2EarliestNoBlockTimestampHeight returns the earliest layer2 cross message height which has no block timestamp
|
||||
func (c *CrossMsg) GetL2EarliestNoBlockTimestampHeight(ctx context.Context) (uint64, error) {
|
||||
var result CrossMsg
|
||||
err := c.db.WithContext(ctx).Model(&CrossMsg{}).
|
||||
Where("block_timestamp IS NULL AND msg_type = ?", Layer2Msg).
|
||||
Select("height").
|
||||
Order("height ASC").
|
||||
First(&result).
|
||||
Error
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return 0, nil
|
||||
}
|
||||
return 0, fmt.Errorf("CrossMsg.GetL2EarliestNoBlockTimestampHeight error: %w", err)
|
||||
}
|
||||
return result.Height, nil
|
||||
}
|
||||
|
||||
// InsertL2CrossMsg batch insert layer2 cross messages
|
||||
func (c *CrossMsg) InsertL2CrossMsg(ctx context.Context, messages []*CrossMsg, dbTx ...*gorm.DB) error {
|
||||
if len(messages) == 0 {
|
||||
return nil
|
||||
}
|
||||
db := c.db
|
||||
if len(dbTx) > 0 && dbTx[0] != nil {
|
||||
db = dbTx[0]
|
||||
}
|
||||
db.WithContext(ctx)
|
||||
err := db.Model(&CrossMsg{}).Create(&messages).Error
|
||||
if err != nil {
|
||||
l2hashes := make([]string, 0, len(messages))
|
||||
heights := make([]uint64, 0, len(messages))
|
||||
for _, msg := range messages {
|
||||
l2hashes = append(l2hashes, msg.Layer2Hash)
|
||||
heights = append(heights, msg.Height)
|
||||
}
|
||||
log.Error("failed to insert l2 cross messages", "l2hashes", l2hashes, "heights", heights, "err", err)
|
||||
return fmt.Errorf("CrossMsg.InsertL2CrossMsg error: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateL2CrossMsgHash update layer2 cross message hash
|
||||
func (c *CrossMsg) UpdateL2CrossMsgHash(ctx context.Context, l2Hash, msgHash common.Hash, dbTx ...*gorm.DB) error {
|
||||
db := c.db
|
||||
if len(dbTx) > 0 && dbTx[0] != nil {
|
||||
db = dbTx[0]
|
||||
}
|
||||
db.WithContext(ctx)
|
||||
err := db.Model(&CrossMsg{}).
|
||||
Where("layer2_hash = ?", l2Hash.String()).
|
||||
Update("msg_hash", msgHash.String()).
|
||||
Error
|
||||
if err != nil {
|
||||
return fmt.Errorf("CrossMsg.UpdateL2CrossMsgHash error: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateL2BlockTimestamp update layer2 cross message block timestamp
|
||||
func (c *CrossMsg) UpdateL2BlockTimestamp(ctx context.Context, height uint64, timestamp time.Time) error {
|
||||
err := c.db.WithContext(ctx).Model(&CrossMsg{}).
|
||||
Where("height = ? AND msg_type = ?", height, Layer2Msg).
|
||||
Update("block_timestamp", timestamp).Error
|
||||
if err != nil {
|
||||
return fmt.Errorf("CrossMsg.UpdateL2BlockTimestamp error: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteL2CrossMsgFromHeight delete layer2 cross messages from given height
|
||||
func (c *CrossMsg) DeleteL2CrossMsgFromHeight(ctx context.Context, height uint64, dbTx ...*gorm.DB) error {
|
||||
db := c.db
|
||||
if len(dbTx) > 0 && dbTx[0] != nil {
|
||||
db = dbTx[0]
|
||||
}
|
||||
db.WithContext(ctx)
|
||||
err := db.Model(&CrossMsg{}).Delete("height > ? AND msg_type = ?", height, Layer2Msg).Error
|
||||
if err != nil {
|
||||
return fmt.Errorf("CrossMsg.DeleteL2CrossMsgFromHeight error: %w", err)
|
||||
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// General Operations
|
||||
|
||||
// GetTotalCrossMsgCountByAddress get total cross msg count by address
|
||||
func (c *CrossMsg) GetTotalCrossMsgCountByAddress(ctx context.Context, sender string) (uint64, error) {
|
||||
var count int64
|
||||
err := c.db.WithContext(ctx).Model(&CrossMsg{}).
|
||||
Where("sender = ?", sender).
|
||||
Count(&count).
|
||||
Error
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return 0, nil
|
||||
}
|
||||
return 0, fmt.Errorf("CrossMsg.GetTotalCrossMsgCountByAddress error: %w", err)
|
||||
|
||||
}
|
||||
return uint64(count), nil
|
||||
}
|
||||
|
||||
// GetCrossMsgsByAddressWithOffset get cross msgs by address with offset
|
||||
func (c *CrossMsg) GetCrossMsgsByAddressWithOffset(ctx context.Context, sender string, offset int, limit int) ([]CrossMsg, error) {
|
||||
var messages []CrossMsg
|
||||
err := c.db.WithContext(ctx).Model(&CrossMsg{}).
|
||||
Where("sender = ?", sender).
|
||||
Order("block_timestamp DESC NULLS FIRST, id DESC").
|
||||
Limit(limit).
|
||||
Offset(offset).
|
||||
Find(&messages).
|
||||
Error
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("CrossMsg.GetCrossMsgsByAddressWithOffset error: %w", err)
|
||||
}
|
||||
return messages, nil
|
||||
}
|
||||
@@ -1,215 +0,0 @@
|
||||
package orm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/log"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// L2SentMsg defines the struct for l2_sent_msg table record
|
||||
type L2SentMsg struct {
|
||||
db *gorm.DB `gorm:"column:-"`
|
||||
|
||||
ID uint64 `json:"id" gorm:"column:id"`
|
||||
OriginalSender string `json:"original_sender" gorm:"column:original_sender;default:''"`
|
||||
TxHash string `json:"tx_hash" gorm:"column:tx_hash"`
|
||||
MsgHash string `json:"msg_hash" gorm:"column:msg_hash"`
|
||||
Sender string `json:"sender" gorm:"column:sender"`
|
||||
Target string `json:"target" gorm:"column:target"`
|
||||
Value string `json:"value" gorm:"column:value"`
|
||||
Height uint64 `json:"height" gorm:"column:height"`
|
||||
Nonce uint64 `json:"nonce" gorm:"column:nonce"`
|
||||
BatchIndex uint64 `json:"batch_index" gorm:"column:batch_index;default:0"`
|
||||
MsgProof string `json:"msg_proof" gorm:"column:msg_proof;default:''"`
|
||||
MsgData string `json:"msg_data" gorm:"column:msg_data;default:''"`
|
||||
CreatedAt *time.Time `json:"created_at" gorm:"column:created_at"`
|
||||
UpdatedAt *time.Time `json:"updated_at" gorm:"column:updated_at"`
|
||||
DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"column:deleted_at;default:NULL"`
|
||||
}
|
||||
|
||||
// NewL2SentMsg create an NewL2SentMsg instance
|
||||
func NewL2SentMsg(db *gorm.DB) *L2SentMsg {
|
||||
return &L2SentMsg{db: db}
|
||||
}
|
||||
|
||||
// TableName returns the table name for the L2SentMsg model.
|
||||
func (*L2SentMsg) TableName() string {
|
||||
return "l2_sent_msg"
|
||||
}
|
||||
|
||||
// GetL2SentMsgByHash get l2 sent msg by hash
|
||||
func (l *L2SentMsg) GetL2SentMsgByHash(ctx context.Context, msgHash string) (*L2SentMsg, error) {
|
||||
var result L2SentMsg
|
||||
err := l.db.WithContext(ctx).Model(&L2SentMsg{}).
|
||||
Where("msg_hash = ?", msgHash).
|
||||
First(&result).
|
||||
Error
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("L2SentMsg.GetL2SentMsgByHash error: %w", err)
|
||||
}
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
// GetLatestSentMsgHeightOnL2 get latest sent msg height on l2
|
||||
func (l *L2SentMsg) GetLatestSentMsgHeightOnL2(ctx context.Context) (uint64, error) {
|
||||
var result L2SentMsg
|
||||
err := l.db.WithContext(ctx).Model(&L2SentMsg{}).
|
||||
Select("height").
|
||||
Order("nonce DESC").
|
||||
First(&result).Error
|
||||
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return 0, nil
|
||||
}
|
||||
return 0, fmt.Errorf("L2SentMsg.GetLatestSentMsgHeightOnL2 error: %w", err)
|
||||
|
||||
}
|
||||
return result.Height, nil
|
||||
}
|
||||
|
||||
// GetClaimableL2SentMsgByAddressWithOffset get claimable l2 sent msg by address with offset
|
||||
func (l *L2SentMsg) GetClaimableL2SentMsgByAddressWithOffset(ctx context.Context, address string, offset int, limit int) ([]*L2SentMsg, error) {
|
||||
var results []*L2SentMsg
|
||||
err := l.db.WithContext(ctx).Raw(`SELECT * FROM l2_sent_msg WHERE id NOT IN (SELECT l2_sent_msg.id FROM l2_sent_msg INNER JOIN relayed_msg ON l2_sent_msg.msg_hash = relayed_msg.msg_hash WHERE l2_sent_msg.deleted_at IS NULL AND relayed_msg.deleted_at IS NULL) AND (original_sender=$1 OR sender = $1) AND msg_proof !='' ORDER BY id DESC LIMIT $2 OFFSET $3;`, address, limit, offset).
|
||||
Scan(&results).Error
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("L2SentMsg.GetClaimableL2SentMsgByAddressWithOffset error: %w", err)
|
||||
}
|
||||
return results, nil
|
||||
}
|
||||
|
||||
// GetClaimableL2SentMsgByAddressTotalNum get claimable l2 sent msg by address total num
|
||||
func (l *L2SentMsg) GetClaimableL2SentMsgByAddressTotalNum(ctx context.Context, address string) (uint64, error) {
|
||||
var count uint64
|
||||
err := l.db.WithContext(ctx).Raw(`SELECT COUNT(*) FROM l2_sent_msg WHERE id NOT IN (SELECT l2_sent_msg.id FROM l2_sent_msg INNER JOIN relayed_msg ON l2_sent_msg.msg_hash = relayed_msg.msg_hash WHERE l2_sent_msg.deleted_at IS NULL AND relayed_msg.deleted_at IS NULL) AND (original_sender=$1 OR sender = $1) AND msg_proof !='';`, address).
|
||||
Scan(&count).Error
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("L2SentMsg.GetClaimableL2SentMsgByAddressTotalNum error: %w", err)
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// GetLatestL2SentMsgBatchIndex get latest l2 sent msg batch index
|
||||
func (l *L2SentMsg) GetLatestL2SentMsgBatchIndex(ctx context.Context) (int64, error) {
|
||||
var result L2SentMsg
|
||||
err := l.db.WithContext(ctx).Model(&L2SentMsg{}).
|
||||
Where("batch_index != 0").
|
||||
Order("batch_index DESC").
|
||||
Select("batch_index").
|
||||
First(&result).
|
||||
Error
|
||||
if err != nil {
|
||||
return -1, fmt.Errorf("L2SentMsg.GetLatestL2SentMsgBatchIndex error: %w", err)
|
||||
}
|
||||
// Watch for overflow, tho its not likely to happen
|
||||
return int64(result.Height), nil
|
||||
}
|
||||
|
||||
// GetL2SentMsgMsgHashByHeightRange get l2 sent msg msg hash by height range
|
||||
func (l *L2SentMsg) GetL2SentMsgMsgHashByHeightRange(ctx context.Context, startHeight, endHeight uint64) ([]*L2SentMsg, error) {
|
||||
var results []*L2SentMsg
|
||||
err := l.db.WithContext(ctx).Model(&L2SentMsg{}).
|
||||
Where("height >= ? AND height <= ?", startHeight, endHeight).
|
||||
Order("nonce ASC").
|
||||
Find(&results).
|
||||
Error
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("L2SentMsg.GetL2SentMsgMsgHashByHeightRange error: %w", err)
|
||||
}
|
||||
return results, nil
|
||||
}
|
||||
|
||||
// GetL2SentMessageByNonce get l2 sent message by nonce
|
||||
func (l *L2SentMsg) GetL2SentMessageByNonce(ctx context.Context, nonce uint64) (*L2SentMsg, error) {
|
||||
var result L2SentMsg
|
||||
err := l.db.WithContext(ctx).Model(&L2SentMsg{}).
|
||||
Where("nonce = ?", nonce).
|
||||
First(&result).
|
||||
Error
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, fmt.Errorf("L2SentMsg.GetL2SentMessageByNonce error: %w", err)
|
||||
|
||||
}
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
// GetLatestL2SentMsgLEHeight get latest l2 sent msg less than or equal to end block number
|
||||
func (l *L2SentMsg) GetLatestL2SentMsgLEHeight(ctx context.Context, endBlockNumber uint64) (*L2SentMsg, error) {
|
||||
var result L2SentMsg
|
||||
err := l.db.WithContext(ctx).Model(&L2SentMsg{}).
|
||||
Where("height <= ?", endBlockNumber).
|
||||
Order("nonce DESC").
|
||||
First(&result).
|
||||
Error
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, fmt.Errorf("L2SentMsg.GetLatestL2SentMsgLEHeight error: %w", err)
|
||||
|
||||
}
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
// InsertL2SentMsg batch insert l2 sent msg
|
||||
func (l *L2SentMsg) InsertL2SentMsg(ctx context.Context, messages []*L2SentMsg, dbTx ...*gorm.DB) error {
|
||||
if len(messages) == 0 {
|
||||
return nil
|
||||
}
|
||||
db := l.db
|
||||
if len(dbTx) > 0 && dbTx[0] != nil {
|
||||
db = dbTx[0]
|
||||
}
|
||||
db.WithContext(ctx)
|
||||
err := db.Model(&L2SentMsg{}).Create(&messages).Error
|
||||
if err != nil {
|
||||
l2hashes := make([]string, 0, len(messages))
|
||||
heights := make([]uint64, 0, len(messages))
|
||||
for _, msg := range messages {
|
||||
l2hashes = append(l2hashes, msg.TxHash)
|
||||
heights = append(heights, msg.Height)
|
||||
}
|
||||
log.Error("failed to insert l2 sent messages", "l2hashes", l2hashes, "heights", heights, "err", err)
|
||||
return fmt.Errorf("L2SentMsg.InsertL2SentMsg error: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateL2MessageProof update l2 message proof in db tx
|
||||
func (l *L2SentMsg) UpdateL2MessageProof(ctx context.Context, msgHash string, proof string, batchIndex uint64, dbTx ...*gorm.DB) error {
|
||||
db := l.db
|
||||
if len(dbTx) > 0 && dbTx[0] != nil {
|
||||
db = dbTx[0]
|
||||
}
|
||||
db.WithContext(ctx)
|
||||
err := db.Model(&L2SentMsg{}).
|
||||
Where("msg_hash = ?", msgHash).
|
||||
Updates(map[string]interface{}{
|
||||
"msg_proof": proof,
|
||||
"batch_index": batchIndex,
|
||||
}).Error
|
||||
if err != nil {
|
||||
return fmt.Errorf("L2SentMsg.UpdateL2MessageProof error: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteL2SentMsgAfterHeight delete l2 sent msg after height
|
||||
func (l *L2SentMsg) DeleteL2SentMsgAfterHeight(ctx context.Context, height uint64, dbTx ...*gorm.DB) error {
|
||||
db := l.db
|
||||
if len(dbTx) > 0 && dbTx[0] != nil {
|
||||
db = dbTx[0]
|
||||
}
|
||||
err := db.WithContext(ctx).Model(&L2SentMsg{}).Delete("height > ?", height).Error
|
||||
if err != nil {
|
||||
return fmt.Errorf("L2SentMsg.DeleteL2SentMsgAfterHeight error: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,142 +0,0 @@
|
||||
package orm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/log"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// RelayedMsg is the struct for relayed_msg table
|
||||
type RelayedMsg struct {
|
||||
db *gorm.DB `gorm:"column:-"`
|
||||
|
||||
ID uint64 `json:"id" gorm:"column:id"`
|
||||
MsgHash string `json:"msg_hash" gorm:"column:msg_hash"`
|
||||
Height uint64 `json:"height" gorm:"column:height"`
|
||||
Layer1Hash string `json:"layer1_hash" gorm:"column:layer1_hash;default:''"`
|
||||
Layer2Hash string `json:"layer2_hash" gorm:"column:layer2_hash;default:''"`
|
||||
CreatedAt *time.Time `json:"created_at" gorm:"column:created_at"`
|
||||
UpdatedAt *time.Time `json:"updated_at" gorm:"column:updated_at"`
|
||||
DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"column:deleted_at;default:NULL"`
|
||||
}
|
||||
|
||||
// NewRelayedMsg create an NewRelayedMsg instance
|
||||
func NewRelayedMsg(db *gorm.DB) *RelayedMsg {
|
||||
return &RelayedMsg{db: db}
|
||||
}
|
||||
|
||||
// TableName returns the table name for the RelayedMsg model.
|
||||
func (*RelayedMsg) TableName() string {
|
||||
return "relayed_msg"
|
||||
}
|
||||
|
||||
// GetRelayedMsgByHash get relayed msg by hash
|
||||
func (r *RelayedMsg) GetRelayedMsgByHash(ctx context.Context, msgHash string) (*RelayedMsg, error) {
|
||||
var result RelayedMsg
|
||||
err := r.db.WithContext(ctx).Model(&RelayedMsg{}).
|
||||
Where("msg_hash = ?", msgHash).
|
||||
First(&result).
|
||||
Error
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, fmt.Errorf("RelayedMsg.GetRelayedMsgByHash error: %w", err)
|
||||
}
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
// GetLatestRelayedHeightOnL1 get latest relayed height on l1
|
||||
func (r *RelayedMsg) GetLatestRelayedHeightOnL1(ctx context.Context) (uint64, error) {
|
||||
var result RelayedMsg
|
||||
err := r.db.WithContext(ctx).Model(&RelayedMsg{}).
|
||||
Select("height").
|
||||
Where("layer1_hash != ''").
|
||||
Order("height DESC").
|
||||
First(&result).
|
||||
Error
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return 0, nil
|
||||
}
|
||||
return 0, fmt.Errorf("RelayedMsg.GetLatestRelayedHeightOnL1 error: %w", err)
|
||||
}
|
||||
return result.Height, err
|
||||
}
|
||||
|
||||
// GetLatestRelayedHeightOnL2 get latest relayed height on l2
|
||||
func (r *RelayedMsg) GetLatestRelayedHeightOnL2(ctx context.Context) (uint64, error) {
|
||||
var result RelayedMsg
|
||||
err := r.db.WithContext(ctx).Model(&RelayedMsg{}).
|
||||
Select("height").
|
||||
Where("layer2_hash != ''").
|
||||
Order("height DESC").
|
||||
First(&result).
|
||||
Error
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return 0, nil
|
||||
}
|
||||
return 0, fmt.Errorf("RelayedMsg.GetLatestRelayedHeightOnL2 error: %w", err)
|
||||
}
|
||||
return result.Height, nil
|
||||
}
|
||||
|
||||
// InsertRelayedMsg batch insert relayed msg into db and return the transaction
|
||||
func (r *RelayedMsg) InsertRelayedMsg(ctx context.Context, messages []*RelayedMsg, dbTx ...*gorm.DB) error {
|
||||
if len(messages) == 0 {
|
||||
return nil
|
||||
}
|
||||
db := r.db
|
||||
if len(dbTx) > 0 && dbTx[0] != nil {
|
||||
db = dbTx[0]
|
||||
}
|
||||
db.WithContext(ctx)
|
||||
err := db.Model(&RelayedMsg{}).Create(&messages).Error
|
||||
if err != nil {
|
||||
l2hashes := make([]string, 0, len(messages))
|
||||
l1hashes := make([]string, 0, len(messages))
|
||||
heights := make([]uint64, 0, len(messages))
|
||||
for _, msg := range messages {
|
||||
l2hashes = append(l2hashes, msg.Layer2Hash)
|
||||
l1hashes = append(l1hashes, msg.Layer1Hash)
|
||||
heights = append(heights, msg.Height)
|
||||
}
|
||||
log.Error("failed to insert l2 sent messages", "l2hashes", l2hashes, "l1hashes", l1hashes, "heights", heights, "err", err)
|
||||
return fmt.Errorf("RelayedMsg.InsertRelayedMsg error: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteL1RelayedHashAfterHeight delete l1 relayed hash after height
|
||||
func (r *RelayedMsg) DeleteL1RelayedHashAfterHeight(ctx context.Context, height uint64, dbTx ...*gorm.DB) error {
|
||||
db := r.db
|
||||
if len(dbTx) > 0 && dbTx[0] != nil {
|
||||
db = dbTx[0]
|
||||
}
|
||||
db.WithContext(ctx)
|
||||
err := db.Model(&RelayedMsg{}).
|
||||
Delete("height > ? AND layer1_hash != ''", height).Error
|
||||
if err != nil {
|
||||
return fmt.Errorf("RelayedMsg.DeleteL1RelayedHashAfterHeight error: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteL2RelayedHashAfterHeight delete l2 relayed hash after heights
|
||||
func (r *RelayedMsg) DeleteL2RelayedHashAfterHeight(ctx context.Context, height uint64, dbTx ...*gorm.DB) error {
|
||||
db := r.db
|
||||
if len(dbTx) > 0 && dbTx[0] != nil {
|
||||
db = dbTx[0]
|
||||
}
|
||||
db.WithContext(ctx)
|
||||
err := db.Model(&RelayedMsg{}).
|
||||
Delete("height > ? AND layer2_hash != ''", height).Error
|
||||
if err != nil {
|
||||
return fmt.Errorf("RelayedMsg.DeleteL2RelayedHashAfterHeight error: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,15 +1,14 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/log"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"bridge-history-api/orm"
|
||||
"bridge-history-api/db"
|
||||
"bridge-history-api/db/orm"
|
||||
)
|
||||
|
||||
// Finalized the schema of tx finalized infos
|
||||
@@ -49,33 +48,30 @@ type TxHistoryInfo struct {
|
||||
|
||||
// HistoryService example service.
|
||||
type HistoryService interface {
|
||||
GetTxsByAddress(address common.Address, offset int, limit int) ([]*TxHistoryInfo, uint64, error)
|
||||
GetTxsByAddress(address common.Address, offset int64, limit int64) ([]*TxHistoryInfo, uint64, error)
|
||||
GetTxsByHashes(hashes []string) ([]*TxHistoryInfo, error)
|
||||
GetClaimableTxsByAddress(address common.Address, offset int, limit int) ([]*TxHistoryInfo, uint64, error)
|
||||
GetClaimableTxsByAddress(address common.Address, offset int64, limit int64) ([]*TxHistoryInfo, uint64, error)
|
||||
}
|
||||
|
||||
// NewHistoryService returns a service backed with a "db"
|
||||
func NewHistoryService(ctx context.Context, db *gorm.DB) HistoryService {
|
||||
service := &historyBackend{ctx: ctx, db: db, prefix: "Scroll-Bridge-History-Server"}
|
||||
func NewHistoryService(db db.OrmFactory) HistoryService {
|
||||
service := &historyBackend{db: db, prefix: "Scroll-Bridge-History-Server"}
|
||||
return service
|
||||
}
|
||||
|
||||
type historyBackend struct {
|
||||
prefix string
|
||||
ctx context.Context
|
||||
db *gorm.DB
|
||||
db db.OrmFactory
|
||||
}
|
||||
|
||||
// GetCrossTxClaimInfo get UserClaimInfos by address
|
||||
func GetCrossTxClaimInfo(ctx context.Context, msgHash string, db *gorm.DB) *UserClaimInfo {
|
||||
l2SentMsgOrm := orm.NewL2SentMsg(db)
|
||||
rollupOrm := orm.NewRollupBatch(db)
|
||||
l2sentMsg, err := l2SentMsgOrm.GetL2SentMsgByHash(ctx, msgHash)
|
||||
if err != nil || l2sentMsg == nil {
|
||||
func GetCrossTxClaimInfo(msgHash string, db db.OrmFactory) *UserClaimInfo {
|
||||
l2sentMsg, err := db.GetL2SentMsgByHash(msgHash)
|
||||
if err != nil {
|
||||
log.Debug("GetCrossTxClaimInfo failed", "error", err)
|
||||
return &UserClaimInfo{}
|
||||
}
|
||||
batch, err := rollupOrm.GetRollupBatchByIndex(ctx, l2sentMsg.BatchIndex)
|
||||
batch, err := db.GetRollupBatchByIndex(l2sentMsg.BatchIndex)
|
||||
if err != nil {
|
||||
log.Debug("GetCrossTxClaimInfo failed", "error", err)
|
||||
return &UserClaimInfo{}
|
||||
@@ -93,11 +89,10 @@ func GetCrossTxClaimInfo(ctx context.Context, msgHash string, db *gorm.DB) *User
|
||||
|
||||
}
|
||||
|
||||
func updateCrossTxHash(ctx context.Context, msgHash string, txInfo *TxHistoryInfo, db *gorm.DB) {
|
||||
relayed := orm.NewRelayedMsg(db)
|
||||
relayed, err := relayed.GetRelayedMsgByHash(ctx, msgHash)
|
||||
func updateCrossTxHash(msgHash string, txInfo *TxHistoryInfo, db db.OrmFactory) {
|
||||
relayed, err := db.GetRelayedMsgByHash(msgHash)
|
||||
if err != nil {
|
||||
log.Debug("updateCrossTxHash failed", "error", err)
|
||||
log.Error("updateCrossTxHash failed", "error", err)
|
||||
return
|
||||
}
|
||||
if relayed == nil {
|
||||
@@ -117,15 +112,13 @@ func updateCrossTxHash(ctx context.Context, msgHash string, txInfo *TxHistoryInf
|
||||
}
|
||||
|
||||
// GetClaimableTxsByAddress get all claimable txs under given address
|
||||
func (h *historyBackend) GetClaimableTxsByAddress(address common.Address, offset int, limit int) ([]*TxHistoryInfo, uint64, error) {
|
||||
func (h *historyBackend) GetClaimableTxsByAddress(address common.Address, offset int64, limit int64) ([]*TxHistoryInfo, uint64, error) {
|
||||
var txHistories []*TxHistoryInfo
|
||||
l2SentMsgOrm := orm.NewL2SentMsg(h.db)
|
||||
l2CrossMsgOrm := orm.NewCrossMsg(h.db)
|
||||
total, err := l2SentMsgOrm.GetClaimableL2SentMsgByAddressTotalNum(h.ctx, address.Hex())
|
||||
total, err := h.db.GetClaimableL2SentMsgByAddressTotalNum(address.Hex())
|
||||
if err != nil || total == 0 {
|
||||
return txHistories, 0, err
|
||||
}
|
||||
results, err := l2SentMsgOrm.GetClaimableL2SentMsgByAddressWithOffset(h.ctx, address.Hex(), offset, limit)
|
||||
results, err := h.db.GetClaimableL2SentMsgByAddressWithOffset(address.Hex(), offset, limit)
|
||||
if err != nil || len(results) == 0 {
|
||||
return txHistories, 0, err
|
||||
}
|
||||
@@ -133,7 +126,7 @@ func (h *historyBackend) GetClaimableTxsByAddress(address common.Address, offset
|
||||
for _, result := range results {
|
||||
msgHashList = append(msgHashList, result.MsgHash)
|
||||
}
|
||||
crossMsgs, err := l2CrossMsgOrm.GetL2CrossMsgByMsgHashList(h.ctx, msgHashList)
|
||||
crossMsgs, err := h.db.GetL2CrossMsgByMsgHashList(msgHashList)
|
||||
// crossMsgs can be empty, because they can be emitted by user directly call contract
|
||||
if err != nil {
|
||||
return txHistories, 0, err
|
||||
@@ -148,7 +141,7 @@ func (h *historyBackend) GetClaimableTxsByAddress(address common.Address, offset
|
||||
IsL1: false,
|
||||
BlockNumber: result.Height,
|
||||
FinalizeTx: &Finalized{},
|
||||
ClaimInfo: GetCrossTxClaimInfo(h.ctx, result.MsgHash, h.db),
|
||||
ClaimInfo: GetCrossTxClaimInfo(result.MsgHash, h.db),
|
||||
}
|
||||
if crossMsg, exist := crossMsgMap[result.MsgHash]; exist {
|
||||
txInfo.Amount = crossMsg.Amount
|
||||
@@ -162,14 +155,13 @@ func (h *historyBackend) GetClaimableTxsByAddress(address common.Address, offset
|
||||
}
|
||||
|
||||
// GetTxsByAddress get all txs under given address
|
||||
func (h *historyBackend) GetTxsByAddress(address common.Address, offset int, limit int) ([]*TxHistoryInfo, uint64, error) {
|
||||
func (h *historyBackend) GetTxsByAddress(address common.Address, offset int64, limit int64) ([]*TxHistoryInfo, uint64, error) {
|
||||
var txHistories []*TxHistoryInfo
|
||||
utilOrm := orm.NewCrossMsg(h.db)
|
||||
total, err := utilOrm.GetTotalCrossMsgCountByAddress(h.ctx, address.String())
|
||||
total, err := h.db.GetTotalCrossMsgCountByAddress(address.String())
|
||||
if err != nil || total == 0 {
|
||||
return txHistories, 0, err
|
||||
}
|
||||
result, err := utilOrm.GetCrossMsgsByAddressWithOffset(h.ctx, address.String(), offset, limit)
|
||||
result, err := h.db.GetCrossMsgsByAddressWithOffset(address.String(), offset, limit)
|
||||
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
@@ -186,9 +178,9 @@ func (h *historyBackend) GetTxsByAddress(address common.Address, offset int, lim
|
||||
FinalizeTx: &Finalized{
|
||||
Hash: "",
|
||||
},
|
||||
ClaimInfo: GetCrossTxClaimInfo(h.ctx, msg.MsgHash, h.db),
|
||||
ClaimInfo: GetCrossTxClaimInfo(msg.MsgHash, h.db),
|
||||
}
|
||||
updateCrossTxHash(h.ctx, msg.MsgHash, txHistory, h.db)
|
||||
updateCrossTxHash(msg.MsgHash, txHistory, h.db)
|
||||
txHistories = append(txHistories, txHistory)
|
||||
}
|
||||
return txHistories, total, nil
|
||||
@@ -197,9 +189,8 @@ func (h *historyBackend) GetTxsByAddress(address common.Address, offset int, lim
|
||||
// GetTxsByHashes get tx infos under given tx hashes
|
||||
func (h *historyBackend) GetTxsByHashes(hashes []string) ([]*TxHistoryInfo, error) {
|
||||
txHistories := make([]*TxHistoryInfo, 0)
|
||||
CrossMsgOrm := orm.NewCrossMsg(h.db)
|
||||
for _, hash := range hashes {
|
||||
l1result, err := CrossMsgOrm.GetL1CrossMsgByHash(h.ctx, common.HexToHash(hash))
|
||||
l1result, err := h.db.GetL1CrossMsgByHash(common.HexToHash(hash))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -216,11 +207,11 @@ func (h *historyBackend) GetTxsByHashes(hashes []string) ([]*TxHistoryInfo, erro
|
||||
Hash: "",
|
||||
},
|
||||
}
|
||||
updateCrossTxHash(h.ctx, l1result.MsgHash, txHistory, h.db)
|
||||
updateCrossTxHash(l1result.MsgHash, txHistory, h.db)
|
||||
txHistories = append(txHistories, txHistory)
|
||||
continue
|
||||
}
|
||||
l2result, err := CrossMsgOrm.GetL2CrossMsgByHash(h.ctx, common.HexToHash(hash))
|
||||
l2result, err := h.db.GetL2CrossMsgByHash(common.HexToHash(hash))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -236,9 +227,9 @@ func (h *historyBackend) GetTxsByHashes(hashes []string) ([]*TxHistoryInfo, erro
|
||||
FinalizeTx: &Finalized{
|
||||
Hash: "",
|
||||
},
|
||||
ClaimInfo: GetCrossTxClaimInfo(h.ctx, l2result.MsgHash, h.db),
|
||||
ClaimInfo: GetCrossTxClaimInfo(l2result.MsgHash, h.db),
|
||||
}
|
||||
updateCrossTxHash(h.ctx, l2result.MsgHash, txHistory, h.db)
|
||||
updateCrossTxHash(l2result.MsgHash, txHistory, h.db)
|
||||
txHistories = append(txHistories, txHistory)
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
"github.com/ethereum/go-ethereum/log"
|
||||
|
||||
backendabi "bridge-history-api/abi"
|
||||
"bridge-history-api/orm"
|
||||
"bridge-history-api/db/orm"
|
||||
)
|
||||
|
||||
// CachedParsedTxCalldata store parsed batch infos
|
||||
@@ -46,7 +46,6 @@ func ParseBackendL1EventLogs(logs []types.Log) ([]*orm.CrossMsg, []*orm.RelayedM
|
||||
Amount: event.Amount.String(),
|
||||
Asset: int(orm.ETH),
|
||||
Layer1Hash: vlog.TxHash.Hex(),
|
||||
MsgType: int(orm.Layer1Msg),
|
||||
MsgHash: msgHash,
|
||||
})
|
||||
case backendabi.L1DepositERC20Sig:
|
||||
@@ -65,7 +64,6 @@ func ParseBackendL1EventLogs(logs []types.Log) ([]*orm.CrossMsg, []*orm.RelayedM
|
||||
Layer1Hash: vlog.TxHash.Hex(),
|
||||
Layer1Token: event.L1Token.Hex(),
|
||||
Layer2Token: event.L2Token.Hex(),
|
||||
MsgType: int(orm.Layer1Msg),
|
||||
MsgHash: msgHash,
|
||||
})
|
||||
case backendabi.L1DepositERC721Sig:
|
||||
@@ -84,7 +82,6 @@ func ParseBackendL1EventLogs(logs []types.Log) ([]*orm.CrossMsg, []*orm.RelayedM
|
||||
Layer1Token: event.L1Token.Hex(),
|
||||
Layer2Token: event.L2Token.Hex(),
|
||||
TokenIDs: event.TokenID.String(),
|
||||
MsgType: int(orm.Layer1Msg),
|
||||
MsgHash: msgHash,
|
||||
})
|
||||
case backendabi.L1DepositERC1155Sig:
|
||||
@@ -104,7 +101,6 @@ func ParseBackendL1EventLogs(logs []types.Log) ([]*orm.CrossMsg, []*orm.RelayedM
|
||||
Layer2Token: event.L2Token.Hex(),
|
||||
TokenIDs: event.TokenID.String(),
|
||||
Amount: event.Amount.String(),
|
||||
MsgType: int(orm.Layer1Msg),
|
||||
MsgHash: msgHash,
|
||||
})
|
||||
case backendabi.L1SentMessageEventSignature:
|
||||
@@ -132,7 +128,6 @@ func ParseBackendL1EventLogs(logs []types.Log) ([]*orm.CrossMsg, []*orm.RelayedM
|
||||
Layer1Token: event.L1Token.Hex(),
|
||||
Layer2Token: event.L2Token.Hex(),
|
||||
TokenIDs: convertBigIntArrayToString(event.TokenIDs),
|
||||
MsgType: int(orm.Layer1Msg),
|
||||
MsgHash: msgHash,
|
||||
})
|
||||
case backendabi.L1BatchDepositERC1155Sig:
|
||||
@@ -152,7 +147,6 @@ func ParseBackendL1EventLogs(logs []types.Log) ([]*orm.CrossMsg, []*orm.RelayedM
|
||||
Layer2Token: event.L2Token.Hex(),
|
||||
TokenIDs: convertBigIntArrayToString(event.TokenIDs),
|
||||
TokenAmounts: convertBigIntArrayToString(event.TokenAmounts),
|
||||
MsgType: int(orm.Layer1Msg),
|
||||
MsgHash: msgHash,
|
||||
})
|
||||
case backendabi.L1RelayedMessageEventSignature:
|
||||
@@ -200,7 +194,6 @@ func ParseBackendL2EventLogs(logs []types.Log) ([]*orm.CrossMsg, []*orm.RelayedM
|
||||
Amount: event.Amount.String(),
|
||||
Asset: int(orm.ETH),
|
||||
Layer2Hash: vlog.TxHash.Hex(),
|
||||
MsgType: int(orm.Layer2Msg),
|
||||
MsgHash: l2SentMsgs[len(l2SentMsgs)-1].MsgHash,
|
||||
})
|
||||
case backendabi.L2WithdrawERC20Sig:
|
||||
@@ -220,7 +213,6 @@ func ParseBackendL2EventLogs(logs []types.Log) ([]*orm.CrossMsg, []*orm.RelayedM
|
||||
Layer2Hash: vlog.TxHash.Hex(),
|
||||
Layer1Token: event.L1Token.Hex(),
|
||||
Layer2Token: event.L2Token.Hex(),
|
||||
MsgType: int(orm.Layer2Msg),
|
||||
MsgHash: l2SentMsgs[len(l2SentMsgs)-1].MsgHash,
|
||||
})
|
||||
case backendabi.L2WithdrawERC721Sig:
|
||||
@@ -240,7 +232,6 @@ func ParseBackendL2EventLogs(logs []types.Log) ([]*orm.CrossMsg, []*orm.RelayedM
|
||||
Layer1Token: event.L1Token.Hex(),
|
||||
Layer2Token: event.L2Token.Hex(),
|
||||
TokenIDs: event.TokenID.String(),
|
||||
MsgType: int(orm.Layer2Msg),
|
||||
MsgHash: l2SentMsgs[len(l2SentMsgs)-1].MsgHash,
|
||||
})
|
||||
case backendabi.L2WithdrawERC1155Sig:
|
||||
@@ -261,7 +252,6 @@ func ParseBackendL2EventLogs(logs []types.Log) ([]*orm.CrossMsg, []*orm.RelayedM
|
||||
Layer2Token: event.L2Token.Hex(),
|
||||
TokenIDs: event.TokenID.String(),
|
||||
Amount: event.Amount.String(),
|
||||
MsgType: int(orm.Layer2Msg),
|
||||
MsgHash: l2SentMsgs[len(l2SentMsgs)-1].MsgHash,
|
||||
})
|
||||
case backendabi.L2BatchWithdrawERC721Sig:
|
||||
@@ -280,7 +270,6 @@ func ParseBackendL2EventLogs(logs []types.Log) ([]*orm.CrossMsg, []*orm.RelayedM
|
||||
Layer1Hash: vlog.TxHash.Hex(),
|
||||
Layer1Token: event.L1Token.Hex(),
|
||||
Layer2Token: event.L2Token.Hex(),
|
||||
MsgType: int(orm.Layer2Msg),
|
||||
TokenIDs: convertBigIntArrayToString(event.TokenIDs),
|
||||
MsgHash: l2SentMsgs[len(l2SentMsgs)-1].MsgHash,
|
||||
})
|
||||
@@ -300,7 +289,6 @@ func ParseBackendL2EventLogs(logs []types.Log) ([]*orm.CrossMsg, []*orm.RelayedM
|
||||
Layer1Hash: vlog.TxHash.Hex(),
|
||||
Layer1Token: event.L1Token.Hex(),
|
||||
Layer2Token: event.L2Token.Hex(),
|
||||
MsgType: int(orm.Layer2Msg),
|
||||
TokenIDs: convertBigIntArrayToString(event.TokenIDs),
|
||||
TokenAmounts: convertBigIntArrayToString(event.TokenAmounts),
|
||||
MsgHash: l2SentMsgs[len(l2SentMsgs)-1].MsgHash,
|
||||
@@ -346,6 +334,7 @@ func ParseBackendL2EventLogs(logs []types.Log) ([]*orm.CrossMsg, []*orm.RelayedM
|
||||
// ParseBatchInfoFromScrollChain parses ScrollChain events
|
||||
func ParseBatchInfoFromScrollChain(ctx context.Context, client *ethclient.Client, logs []types.Log) ([]*orm.RollupBatch, error) {
|
||||
var rollupBatches []*orm.RollupBatch
|
||||
cache := make(map[string]CachedParsedTxCalldata)
|
||||
for _, vlog := range logs {
|
||||
switch vlog.Topics[0] {
|
||||
case backendabi.L1CommitBatchEventSignature:
|
||||
@@ -355,22 +344,42 @@ func ParseBatchInfoFromScrollChain(ctx context.Context, client *ethclient.Client
|
||||
log.Warn("Failed to unpack CommitBatch event", "err", err)
|
||||
return rollupBatches, err
|
||||
}
|
||||
if _, ok := cache[vlog.TxHash.Hex()]; ok {
|
||||
c := cache[vlog.TxHash.Hex()]
|
||||
c.CallDataIndex++
|
||||
rollupBatches = append(rollupBatches, &orm.RollupBatch{
|
||||
CommitHeight: vlog.BlockNumber,
|
||||
BatchIndex: c.BatchIndices[c.CallDataIndex],
|
||||
BatchHash: event.BatchHash.Hex(),
|
||||
StartBlockNumber: c.StartBlocks[c.CallDataIndex],
|
||||
EndBlockNumber: c.EndBlocks[c.CallDataIndex],
|
||||
})
|
||||
cache[vlog.TxHash.Hex()] = c
|
||||
continue
|
||||
}
|
||||
|
||||
commitTx, isPending, err := client.TransactionByHash(ctx, vlog.TxHash)
|
||||
if err != nil || isPending {
|
||||
log.Warn("Failed to get commit Batch tx receipt or the tx is still pending", "err", err)
|
||||
return rollupBatches, err
|
||||
}
|
||||
index, startBlock, endBlock, err := GetBatchRangeFromCalldataV2(commitTx.Data())
|
||||
indices, startBlocks, endBlocks, err := GetBatchRangeFromCalldataV1(commitTx.Data())
|
||||
if err != nil {
|
||||
log.Warn("Failed to get batch range from calldata", "hash", commitTx.Hash().Hex(), "height", vlog.BlockNumber)
|
||||
return rollupBatches, err
|
||||
}
|
||||
cache[vlog.TxHash.Hex()] = CachedParsedTxCalldata{
|
||||
CallDataIndex: 0,
|
||||
BatchIndices: indices,
|
||||
StartBlocks: startBlocks,
|
||||
EndBlocks: endBlocks,
|
||||
}
|
||||
rollupBatches = append(rollupBatches, &orm.RollupBatch{
|
||||
CommitHeight: vlog.BlockNumber,
|
||||
BatchIndex: index,
|
||||
BatchIndex: indices[0],
|
||||
BatchHash: event.BatchHash.Hex(),
|
||||
StartBlockNumber: startBlock,
|
||||
EndBlockNumber: endBlock,
|
||||
StartBlockNumber: startBlocks[0],
|
||||
EndBlockNumber: endBlocks[0],
|
||||
})
|
||||
|
||||
default:
|
||||
|
||||
@@ -171,9 +171,9 @@ func (r *Layer2Relayer) initializeGenesis() error {
|
||||
|
||||
chunk := &types.Chunk{
|
||||
Blocks: []*types.WrappedBlock{{
|
||||
Header: genesis,
|
||||
Transactions: nil,
|
||||
WithdrawRoot: common.Hash{},
|
||||
Header: genesis,
|
||||
Transactions: nil,
|
||||
WithdrawTrieRoot: common.Hash{},
|
||||
}},
|
||||
}
|
||||
|
||||
|
||||
@@ -169,15 +169,15 @@ func (w *L2WatcherClient) getAndStoreBlockTraces(ctx context.Context, from, to u
|
||||
|
||||
log.Info("retrieved block", "height", block.Header().Number, "hash", block.Header().Hash().String())
|
||||
|
||||
withdrawRoot, err3 := w.StorageAt(ctx, w.messageQueueAddress, w.withdrawTrieRootSlot, big.NewInt(int64(number)))
|
||||
withdrawTrieRoot, err3 := w.StorageAt(ctx, w.messageQueueAddress, w.withdrawTrieRootSlot, big.NewInt(int64(number)))
|
||||
if err3 != nil {
|
||||
return fmt.Errorf("failed to get withdrawRoot: %v. number: %v", err3, number)
|
||||
return fmt.Errorf("failed to get withdrawTrieRoot: %v. number: %v", err3, number)
|
||||
}
|
||||
|
||||
blocks = append(blocks, &types.WrappedBlock{
|
||||
Header: block.Header(),
|
||||
Transactions: txsToTxsData(block.Transactions()),
|
||||
WithdrawRoot: common.BytesToHash(withdrawRoot),
|
||||
Header: block.Header(),
|
||||
Transactions: txsToTxsData(block.Transactions()),
|
||||
WithdrawTrieRoot: common.BytesToHash(withdrawTrieRoot),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -30,7 +30,6 @@ type Batch struct {
|
||||
EndChunkHash string `json:"end_chunk_hash" gorm:"column:end_chunk_hash"`
|
||||
StateRoot string `json:"state_root" gorm:"column:state_root"`
|
||||
WithdrawRoot string `json:"withdraw_root" gorm:"column:withdraw_root"`
|
||||
ParentBatchHash string `json:"parent_batch_hash" gorm:"column:parent_batch_hash"`
|
||||
BatchHeader []byte `json:"batch_header" gorm:"column:batch_header"`
|
||||
|
||||
// proof
|
||||
@@ -259,8 +258,7 @@ func (o *Batch) InsertBatch(ctx context.Context, startChunkIndex, endChunkIndex
|
||||
EndChunkHash: endChunkHash,
|
||||
EndChunkIndex: endChunkIndex,
|
||||
StateRoot: chunks[numChunks-1].Blocks[lastChunkBlockNum-1].Header.Root.Hex(),
|
||||
WithdrawRoot: chunks[numChunks-1].Blocks[lastChunkBlockNum-1].WithdrawRoot.Hex(),
|
||||
ParentBatchHash: parentBatchHash.Hex(),
|
||||
WithdrawRoot: chunks[numChunks-1].Blocks[lastChunkBlockNum-1].WithdrawTrieRoot.Hex(),
|
||||
BatchHeader: batchHeader.Encode(),
|
||||
ChunkProofsStatus: int16(types.ChunkProofsStatusPending),
|
||||
ProvingStatus: int16(types.ProvingTaskUnassigned),
|
||||
|
||||
@@ -26,10 +26,6 @@ type Chunk struct {
|
||||
StartBlockTime uint64 `json:"start_block_time" gorm:"column:start_block_time"`
|
||||
TotalL1MessagesPoppedBefore uint64 `json:"total_l1_messages_popped_before" gorm:"column:total_l1_messages_popped_before"`
|
||||
TotalL1MessagesPoppedInChunk uint32 `json:"total_l1_messages_popped_in_chunk" gorm:"column:total_l1_messages_popped_in_chunk"`
|
||||
ParentChunkHash string `json:"parent_chunk_hash" gorm:"column:parent_chunk_hash"`
|
||||
StateRoot string `json:"state_root" gorm:"column:state_root"`
|
||||
ParentChunkStateRoot string `json:"parent_chunk_state_root" gorm:"column:parent_chunk_state_root"`
|
||||
WithdrawRoot string `json:"withdraw_root" gorm:"column:withdraw_root"`
|
||||
|
||||
// proof
|
||||
ProvingStatus int16 `json:"proving_status" gorm:"column:proving_status;default:1"`
|
||||
@@ -122,8 +118,6 @@ func (o *Chunk) InsertChunk(ctx context.Context, chunk *types.Chunk, dbTX ...*go
|
||||
|
||||
var chunkIndex uint64
|
||||
var totalL1MessagePoppedBefore uint64
|
||||
var parentChunkHash string
|
||||
var parentChunkStateRoot string
|
||||
parentChunk, err := o.GetLatestChunk(ctx)
|
||||
if err != nil && !errors.Is(errors.Unwrap(err), gorm.ErrRecordNotFound) {
|
||||
log.Error("failed to get latest chunk", "err", err)
|
||||
@@ -136,8 +130,6 @@ func (o *Chunk) InsertChunk(ctx context.Context, chunk *types.Chunk, dbTX ...*go
|
||||
if parentChunk != nil {
|
||||
chunkIndex = parentChunk.Index + 1
|
||||
totalL1MessagePoppedBefore = parentChunk.TotalL1MessagesPoppedBefore + uint64(parentChunk.TotalL1MessagesPoppedInChunk)
|
||||
parentChunkHash = parentChunk.Hash
|
||||
parentChunkStateRoot = parentChunk.StateRoot
|
||||
}
|
||||
|
||||
hash, err := chunk.Hash(totalL1MessagePoppedBefore)
|
||||
@@ -172,10 +164,6 @@ func (o *Chunk) InsertChunk(ctx context.Context, chunk *types.Chunk, dbTX ...*go
|
||||
StartBlockTime: chunk.Blocks[0].Header.Time,
|
||||
TotalL1MessagesPoppedBefore: totalL1MessagePoppedBefore,
|
||||
TotalL1MessagesPoppedInChunk: uint32(chunk.NumL1Messages(totalL1MessagePoppedBefore)),
|
||||
ParentChunkHash: parentChunkHash,
|
||||
StateRoot: chunk.Blocks[numBlocks-1].Header.Root.Hex(),
|
||||
ParentChunkStateRoot: parentChunkStateRoot,
|
||||
WithdrawRoot: chunk.Blocks[numBlocks-1].WithdrawRoot.Hex(),
|
||||
ProvingStatus: int16(types.ProvingTaskUnassigned),
|
||||
}
|
||||
|
||||
|
||||
@@ -19,16 +19,15 @@ type L2Block struct {
|
||||
db *gorm.DB `gorm:"column:-"`
|
||||
|
||||
// block
|
||||
Number uint64 `json:"number" gorm:"number"`
|
||||
Hash string `json:"hash" gorm:"hash"`
|
||||
ParentHash string `json:"parent_hash" gorm:"parent_hash"`
|
||||
Header string `json:"header" gorm:"header"`
|
||||
Transactions string `json:"transactions" gorm:"transactions"`
|
||||
WithdrawRoot string `json:"withdraw_root" gorm:"withdraw_root"`
|
||||
StateRoot string `json:"state_root" gorm:"state_root"`
|
||||
TxNum uint32 `json:"tx_num" gorm:"tx_num"`
|
||||
GasUsed uint64 `json:"gas_used" gorm:"gas_used"`
|
||||
BlockTimestamp uint64 `json:"block_timestamp" gorm:"block_timestamp"`
|
||||
Number uint64 `json:"number" gorm:"number"`
|
||||
Hash string `json:"hash" gorm:"hash"`
|
||||
ParentHash string `json:"parent_hash" gorm:"parent_hash"`
|
||||
Header string `json:"header" gorm:"header"`
|
||||
Transactions string `json:"transactions" gorm:"transactions"`
|
||||
WithdrawTrieRoot string `json:"withdraw_trie_root" gorm:"withdraw_trie_root"`
|
||||
TxNum uint32 `json:"tx_num" gorm:"tx_num"`
|
||||
GasUsed uint64 `json:"gas_used" gorm:"gas_used"`
|
||||
BlockTimestamp uint64 `json:"block_timestamp" gorm:"block_timestamp"`
|
||||
|
||||
// chunk
|
||||
ChunkHash string `json:"chunk_hash" gorm:"chunk_hash;default:NULL"`
|
||||
@@ -68,7 +67,7 @@ func (o *L2Block) GetL2BlocksLatestHeight(ctx context.Context) (uint64, error) {
|
||||
func (o *L2Block) GetUnchunkedBlocks(ctx context.Context) ([]*types.WrappedBlock, error) {
|
||||
db := o.db.WithContext(ctx)
|
||||
db = db.Model(&L2Block{})
|
||||
db = db.Select("header, transactions, withdraw_root")
|
||||
db = db.Select("header, transactions, withdraw_trie_root")
|
||||
db = db.Where("chunk_hash IS NULL")
|
||||
db = db.Order("number ASC")
|
||||
|
||||
@@ -90,7 +89,7 @@ func (o *L2Block) GetUnchunkedBlocks(ctx context.Context) ([]*types.WrappedBlock
|
||||
return nil, fmt.Errorf("L2Block.GetUnchunkedBlocks error: %w", err)
|
||||
}
|
||||
|
||||
wrappedBlock.WithdrawRoot = common.HexToHash(v.WithdrawRoot)
|
||||
wrappedBlock.WithdrawTrieRoot = common.HexToHash(v.WithdrawTrieRoot)
|
||||
wrappedBlocks = append(wrappedBlocks, &wrappedBlock)
|
||||
}
|
||||
|
||||
@@ -134,7 +133,7 @@ func (o *L2Block) GetL2BlocksInRange(ctx context.Context, startBlockNumber uint6
|
||||
|
||||
db := o.db.WithContext(ctx)
|
||||
db = db.Model(&L2Block{})
|
||||
db = db.Select("header, transactions, withdraw_root")
|
||||
db = db.Select("header, transactions, withdraw_trie_root")
|
||||
db = db.Where("number >= ? AND number <= ?", startBlockNumber, endBlockNumber)
|
||||
db = db.Order("number ASC")
|
||||
|
||||
@@ -161,7 +160,7 @@ func (o *L2Block) GetL2BlocksInRange(ctx context.Context, startBlockNumber uint6
|
||||
return nil, fmt.Errorf("L2Block.GetL2BlocksInRange error: %w, start block: %v, end block: %v", err, startBlockNumber, endBlockNumber)
|
||||
}
|
||||
|
||||
wrappedBlock.WithdrawRoot = common.HexToHash(v.WithdrawRoot)
|
||||
wrappedBlock.WithdrawTrieRoot = common.HexToHash(v.WithdrawTrieRoot)
|
||||
wrappedBlocks = append(wrappedBlocks, &wrappedBlock)
|
||||
}
|
||||
|
||||
@@ -185,16 +184,15 @@ func (o *L2Block) InsertL2Blocks(ctx context.Context, blocks []*types.WrappedBlo
|
||||
}
|
||||
|
||||
l2Block := L2Block{
|
||||
Number: block.Header.Number.Uint64(),
|
||||
Hash: block.Header.Hash().String(),
|
||||
ParentHash: block.Header.ParentHash.String(),
|
||||
Transactions: string(txs),
|
||||
WithdrawRoot: block.WithdrawRoot.Hex(),
|
||||
StateRoot: block.Header.Root.Hex(),
|
||||
TxNum: uint32(len(block.Transactions)),
|
||||
GasUsed: block.Header.GasUsed,
|
||||
BlockTimestamp: block.Header.Time,
|
||||
Header: string(header),
|
||||
Number: block.Header.Number.Uint64(),
|
||||
Hash: block.Header.Hash().String(),
|
||||
ParentHash: block.Header.ParentHash.String(),
|
||||
Transactions: string(txs),
|
||||
WithdrawTrieRoot: block.WithdrawTrieRoot.Hex(),
|
||||
TxNum: uint32(len(block.Transactions)),
|
||||
GasUsed: block.Header.GasUsed,
|
||||
BlockTimestamp: block.Header.Time,
|
||||
Header: string(header),
|
||||
}
|
||||
l2Blocks = append(l2Blocks, l2Block)
|
||||
}
|
||||
|
||||
@@ -60,8 +60,8 @@ contract MockBridgeL1 {
|
||||
|
||||
/// @notice Emitted when a batch is finalized.
|
||||
/// @param batchHash The hash of the batch
|
||||
/// @param stateRoot The state root on layer 2 after this batch.
|
||||
/// @param withdrawRoot The merkle root on layer2 after this batch.
|
||||
/// @param stateRoot The state root in layer 2 after this batch.
|
||||
/// @param withdrawRoot The merkle root in layer2 after this batch.
|
||||
event FinalizeBatch(bytes32 indexed batchHash, bytes32 stateRoot, bytes32 withdrawRoot);
|
||||
|
||||
/***********
|
||||
|
||||
@@ -80,8 +80,8 @@ func testImportL2GasPrice(t *testing.T) {
|
||||
Difficulty: big.NewInt(0),
|
||||
BaseFee: big.NewInt(0),
|
||||
},
|
||||
Transactions: nil,
|
||||
WithdrawRoot: common.Hash{},
|
||||
Transactions: nil,
|
||||
WithdrawTrieRoot: common.Hash{},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
@@ -45,9 +45,9 @@ func testCommitBatchAndFinalizeBatch(t *testing.T) {
|
||||
BaseFee: big.NewInt(0),
|
||||
}
|
||||
wrappedBlocks = append(wrappedBlocks, &types.WrappedBlock{
|
||||
Header: &header,
|
||||
Transactions: nil,
|
||||
WithdrawRoot: common.Hash{},
|
||||
Header: &header,
|
||||
Transactions: nil,
|
||||
WithdrawTrieRoot: common.Hash{},
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -24,7 +24,6 @@ COPY ./bridge/go.* ./bridge/
|
||||
COPY ./common/go.* ./common/
|
||||
COPY ./coordinator/go.* ./coordinator/
|
||||
COPY ./database/go.* ./database/
|
||||
COPY ./prover-stats-api/go.* ./prover-stats-api/
|
||||
COPY ./roller/go.* ./roller/
|
||||
COPY ./tests/integration-test/go.* ./tests/integration-test/
|
||||
COPY ./bridge-history-api/go.* ./bridge-history-api/
|
||||
@@ -34,17 +33,17 @@ RUN go mod download -x
|
||||
# Build coordinator
|
||||
FROM base as builder
|
||||
COPY . .
|
||||
RUN cp -r ./common/libzkp/interface ./coordinator/internal/logic/verifier/lib
|
||||
COPY --from=zkp-builder /app/target/release/libzkp.so ./coordinator/internal/logic/verifier/lib/
|
||||
COPY --from=zkp-builder /app/target/release/libzktrie.so ./coordinator/internal/logic/verifier/lib/
|
||||
RUN cd ./coordinator && go build -v -p 4 -o /bin/coordinator ./cmd && mv internal/logic/verifier/lib /bin/
|
||||
RUN cp -r ./common/libzkp/interface ./coordinator/verifier/lib
|
||||
COPY --from=zkp-builder /app/target/release/libzkp.so ./coordinator/verifier/lib/
|
||||
COPY --from=zkp-builder /app/target/release/libzktrie.so ./coordinator/verifier/lib/
|
||||
RUN cd ./coordinator && go build -v -p 4 -o /bin/coordinator ./cmd && mv verifier/lib /bin/
|
||||
|
||||
# Pull coordinator into a second stage deploy alpine container
|
||||
FROM ubuntu:20.04
|
||||
ENV LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/src/coordinator/internal/logic/verifier/lib
|
||||
ENV LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/src/coordinator/verifier/lib
|
||||
# ENV CHAIN_ID=534353
|
||||
RUN mkdir -p /src/coordinator/internal/logic/verifier/lib
|
||||
COPY --from=builder /bin/lib /src/coordinator/internal/logic/verifier/lib
|
||||
RUN mkdir -p /src/coordinator/verifier/lib
|
||||
COPY --from=builder /bin/lib /src/coordinator/verifier/lib
|
||||
COPY --from=builder /bin/coordinator /bin/
|
||||
|
||||
|
||||
|
||||
@@ -7,7 +7,6 @@ COPY ./bridge/go.* ./bridge/
|
||||
COPY ./common/go.* ./common/
|
||||
COPY ./coordinator/go.* ./coordinator/
|
||||
COPY ./database/go.* ./database/
|
||||
COPY ./prover-stats-api/go.* ./prover-stats-api/
|
||||
COPY ./roller/go.* ./roller/
|
||||
COPY ./tests/integration-test/go.* ./tests/integration-test/
|
||||
COPY ./bridge-history-api/go.* ./bridge-history-api/
|
||||
|
||||
@@ -7,7 +7,6 @@ COPY ./bridge/go.* ./bridge/
|
||||
COPY ./common/go.* ./common/
|
||||
COPY ./coordinator/go.* ./coordinator/
|
||||
COPY ./database/go.* ./database/
|
||||
COPY ./prover-stats-api/go.* ./prover-stats-api/
|
||||
COPY ./roller/go.* ./roller/
|
||||
COPY ./tests/integration-test/go.* ./tests/integration-test/
|
||||
COPY ./bridge-history-api/go.* ./bridge-history-api/
|
||||
|
||||
@@ -7,7 +7,6 @@ COPY ./bridge/go.* ./bridge/
|
||||
COPY ./common/go.* ./common/
|
||||
COPY ./coordinator/go.* ./coordinator/
|
||||
COPY ./database/go.* ./database/
|
||||
COPY ./prover-stats-api/go.* ./prover-stats-api/
|
||||
COPY ./roller/go.* ./roller/
|
||||
COPY ./tests/integration-test/go.* ./tests/integration-test/
|
||||
COPY ./bridge-history-api/go.* ./bridge-history-api/
|
||||
|
||||
@@ -7,7 +7,6 @@ COPY ./bridge/go.* ./bridge/
|
||||
COPY ./common/go.* ./common/
|
||||
COPY ./coordinator/go.* ./coordinator/
|
||||
COPY ./database/go.* ./database/
|
||||
COPY ./prover-stats-api/go.* ./prover-stats-api/
|
||||
COPY ./roller/go.* ./roller/
|
||||
COPY ./tests/integration-test/go.* ./tests/integration-test/
|
||||
COPY ./bridge-history-api/go.* ./bridge-history-api/
|
||||
|
||||
@@ -7,7 +7,6 @@ COPY ./bridge/go.* ./bridge/
|
||||
COPY ./common/go.* ./common/
|
||||
COPY ./coordinator/go.* ./coordinator/
|
||||
COPY ./database/go.* ./database/
|
||||
COPY ./prover-stats-api/go.* ./prover-stats-api/
|
||||
COPY ./roller/go.* ./roller/
|
||||
COPY ./tests/integration-test/go.* ./tests/integration-test/
|
||||
COPY ./bridge-history-api/go.* ./bridge-history-api/
|
||||
|
||||
@@ -3,7 +3,7 @@ set -uex
|
||||
|
||||
profile_name=$1
|
||||
|
||||
exclude_dirs=("scroll-tech/bridge/cmd" "scroll-tech/bridge/tests" "scroll-tech/bridge/mock_bridge" "scroll-tech/coordinator/cmd" "scroll-tech/coordinator/internal/logic/verifier")
|
||||
exclude_dirs=("scroll-tech/bridge/cmd" "scroll-tech/bridge/tests" "scroll-tech/bridge/mock_bridge" "scroll-tech/coordinator/cmd" "scroll-tech/coordinator/verifier")
|
||||
|
||||
all_packages=$(go list ./... | grep -v "^scroll-tech/${profile_name}$")
|
||||
coverpkg="scroll-tech/${profile_name}"
|
||||
|
||||
@@ -10,8 +10,6 @@ import (
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
"gorm.io/gorm/utils"
|
||||
|
||||
cutils "scroll-tech/common/utils"
|
||||
)
|
||||
|
||||
type gormLogger struct {
|
||||
@@ -52,12 +50,8 @@ func InitDB(config *Config) (*gorm.DB, error) {
|
||||
db, err := gorm.Open(postgres.Open(config.DSN), &gorm.Config{
|
||||
Logger: &tmpGormLogger,
|
||||
NowFunc: func() time.Time {
|
||||
// why set time to UTC.
|
||||
// if now set this, the inserted data time will use local timezone. like 2023-07-18 18:24:00 CST+8
|
||||
// but when inserted, store to postgres is 2023-07-18 18:24:00 UTC+0 the timezone is incorrect.
|
||||
// As mysql dsn user:pass@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local, we cant set
|
||||
// the timezone by loc=Local. but postgres's dsn don't have loc option to set timezone, so just need set the gorm option like that.
|
||||
return cutils.NowUTC()
|
||||
utc, _ := time.LoadLocation("")
|
||||
return time.Now().In(utc)
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
@@ -13,12 +13,12 @@ import (
|
||||
const nonZeroByteGas uint64 = 16
|
||||
const zeroByteGas uint64 = 4
|
||||
|
||||
// WrappedBlock contains the block's Header, Transactions and WithdrawRoot hash.
|
||||
// WrappedBlock contains the block's Header, Transactions and WithdrawTrieRoot hash.
|
||||
type WrappedBlock struct {
|
||||
Header *types.Header `json:"header"`
|
||||
// Transactions is only used for recover types.Transactions, the from of types.TransactionData field is missing.
|
||||
Transactions []*types.TransactionData `json:"transactions"`
|
||||
WithdrawRoot common.Hash `json:"withdraw_trie_root,omitempty"`
|
||||
Transactions []*types.TransactionData `json:"transactions"`
|
||||
WithdrawTrieRoot common.Hash `json:"withdraw_trie_root,omitempty"`
|
||||
}
|
||||
|
||||
// NumL1Messages returns the number of L1 messages in this block.
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
package utils
|
||||
|
||||
import "time"
|
||||
|
||||
// NowUTC get the utc time.Now
|
||||
func NowUTC() time.Time {
|
||||
utc, _ := time.LoadLocation("")
|
||||
return time.Now().In(utc)
|
||||
}
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"runtime/debug"
|
||||
)
|
||||
|
||||
var tag = "v4.0.30"
|
||||
var tag = "v4.0.25"
|
||||
|
||||
var commit = func() string {
|
||||
if info, ok := debug.ReadBuildInfo(); ok {
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
> L1ERC1155Gateway
|
||||
|
||||
The `L1ERC1155Gateway` is used to deposit ERC1155 compatible NFT on layer 1 and finalize withdraw the NFTs from layer 2.
|
||||
The `L1ERC1155Gateway` is used to deposit ERC1155 compatible NFT in layer 1 and finalize withdraw the NFTs from layer 2.
|
||||
|
||||
*The deposited NFTs are held in this gateway. On finalizing withdraw, the corresponding NFT will be transfer to the recipient directly. This will be changed if we have more specific scenarios.*
|
||||
|
||||
@@ -24,7 +24,7 @@ Deposit a list of some ERC1155 NFT to caller's account on layer 2.
|
||||
|
||||
| Name | Type | Description |
|
||||
|---|---|---|
|
||||
| _token | address | The address of ERC1155 NFT on layer 1. |
|
||||
| _token | address | The address of ERC1155 NFT in layer 1. |
|
||||
| _tokenIds | uint256[] | The list of token ids to deposit. |
|
||||
| _amounts | uint256[] | The list of corresponding number of token to deposit. |
|
||||
| _gasLimit | uint256 | Estimated gas limit required to complete the deposit on layer 2. |
|
||||
@@ -43,8 +43,8 @@ Deposit a list of some ERC1155 NFT to a recipient's account on layer 2.
|
||||
|
||||
| Name | Type | Description |
|
||||
|---|---|---|
|
||||
| _token | address | The address of ERC1155 NFT on layer 1. |
|
||||
| _to | address | The address of recipient on layer 2. |
|
||||
| _token | address | The address of ERC1155 NFT in layer 1. |
|
||||
| _to | address | The address of recipient in layer 2. |
|
||||
| _tokenIds | uint256[] | The list of token ids to deposit. |
|
||||
| _amounts | uint256[] | The list of corresponding number of token to deposit. |
|
||||
| _gasLimit | uint256 | Estimated gas limit required to complete the deposit on layer 2. |
|
||||
@@ -80,8 +80,8 @@ Deposit some ERC1155 NFT to a recipient's account on layer 2.
|
||||
|
||||
| Name | Type | Description |
|
||||
|---|---|---|
|
||||
| _token | address | The address of ERC1155 NFT on layer 1. |
|
||||
| _to | address | The address of recipient on layer 2. |
|
||||
| _token | address | The address of ERC1155 NFT in layer 1. |
|
||||
| _to | address | The address of recipient in layer 2. |
|
||||
| _tokenId | uint256 | The token id to deposit. |
|
||||
| _amount | uint256 | The amount of token to deposit. |
|
||||
| _gasLimit | uint256 | Estimated gas limit required to complete the deposit on layer 2. |
|
||||
@@ -100,7 +100,7 @@ Deposit some ERC1155 NFT to caller's account on layer 2.
|
||||
|
||||
| Name | Type | Description |
|
||||
|---|---|---|
|
||||
| _token | address | The address of ERC1155 NFT on layer 1. |
|
||||
| _token | address | The address of ERC1155 NFT in layer 1. |
|
||||
| _tokenId | uint256 | The token id to deposit. |
|
||||
| _amount | uint256 | The amount of token to deposit. |
|
||||
| _gasLimit | uint256 | Estimated gas limit required to complete the deposit on layer 2. |
|
||||
@@ -111,7 +111,7 @@ Deposit some ERC1155 NFT to caller's account on layer 2.
|
||||
function finalizeBatchWithdrawERC1155(address _l1Token, address _l2Token, address _from, address _to, uint256[] _tokenIds, uint256[] _amounts) external nonpayable
|
||||
```
|
||||
|
||||
Complete ERC1155 batch withdraw from layer 2 to layer 1 and send fund to recipient's account on layer 1. The function should only be called by L1ScrollMessenger. The function should also only be called by L2ERC1155Gateway on layer 2.
|
||||
Complete ERC1155 batch withdraw from layer 2 to layer 1 and send fund to recipient's account in layer 1. The function should only be called by L1ScrollMessenger. The function should also only be called by L2ERC1155Gateway in layer 2.
|
||||
|
||||
|
||||
|
||||
@@ -121,8 +121,8 @@ Complete ERC1155 batch withdraw from layer 2 to layer 1 and send fund to recipie
|
||||
|---|---|---|
|
||||
| _l1Token | address | The address of corresponding layer 1 token. |
|
||||
| _l2Token | address | The address of corresponding layer 2 token. |
|
||||
| _from | address | The address of account who withdraw the token on layer 2. |
|
||||
| _to | address | The address of recipient on layer 1 to receive the token. |
|
||||
| _from | address | The address of account who withdraw the token in layer 2. |
|
||||
| _to | address | The address of recipient in layer 1 to receive the token. |
|
||||
| _tokenIds | uint256[] | The list of token ids to withdraw. |
|
||||
| _amounts | uint256[] | The list of corresponding number of token to withdraw. |
|
||||
|
||||
@@ -132,7 +132,7 @@ Complete ERC1155 batch withdraw from layer 2 to layer 1 and send fund to recipie
|
||||
function finalizeWithdrawERC1155(address _l1Token, address _l2Token, address _from, address _to, uint256 _tokenId, uint256 _amount) external nonpayable
|
||||
```
|
||||
|
||||
Complete ERC1155 withdraw from layer 2 to layer 1 and send fund to recipient's account on layer 1. The function should only be called by L1ScrollMessenger. The function should also only be called by L2ERC1155Gateway on layer 2.
|
||||
Complete ERC1155 withdraw from layer 2 to layer 1 and send fund to recipient's account in layer 1. The function should only be called by L1ScrollMessenger. The function should also only be called by L2ERC1155Gateway in layer 2.
|
||||
|
||||
|
||||
|
||||
@@ -142,8 +142,8 @@ Complete ERC1155 withdraw from layer 2 to layer 1 and send fund to recipient'
|
||||
|---|---|---|
|
||||
| _l1Token | address | The address of corresponding layer 1 token. |
|
||||
| _l2Token | address | The address of corresponding layer 2 token. |
|
||||
| _from | address | The address of account who withdraw the token on layer 2. |
|
||||
| _to | address | The address of recipient on layer 1 to receive the token. |
|
||||
| _from | address | The address of account who withdraw the token in layer 2. |
|
||||
| _to | address | The address of recipient in layer 1 to receive the token. |
|
||||
| _tokenId | uint256 | The token id to withdraw. |
|
||||
| _amount | uint256 | The amount of token to withdraw. |
|
||||
|
||||
@@ -368,8 +368,8 @@ Update layer 2 to layer 2 token mapping.
|
||||
|
||||
| Name | Type | Description |
|
||||
|---|---|---|
|
||||
| _l1Token | address | The address of ERC1155 token on layer 1. |
|
||||
| _l2Token | address | The address of corresponding ERC1155 token on layer 2. |
|
||||
| _l1Token | address | The address of corresponding ERC1155 token in layer 2. |
|
||||
| _l2Token | address | undefined |
|
||||
|
||||
|
||||
|
||||
@@ -381,7 +381,7 @@ Update layer 2 to layer 2 token mapping.
|
||||
event BatchDepositERC1155(address indexed _l1Token, address indexed _l2Token, address indexed _from, address _to, uint256[] _tokenIds, uint256[] _amounts)
|
||||
```
|
||||
|
||||
Emitted when the ERC1155 NFT is batch deposited to gateway on layer 1.
|
||||
Emitted when the ERC1155 NFT is batch deposited to gateway in layer 1.
|
||||
|
||||
|
||||
|
||||
@@ -421,7 +421,7 @@ Emitted when some ERC1155 token is refunded.
|
||||
event DepositERC1155(address indexed _l1Token, address indexed _l2Token, address indexed _from, address _to, uint256 _tokenId, uint256 _amount)
|
||||
```
|
||||
|
||||
Emitted when the ERC1155 NFT is deposited to gateway on layer 1.
|
||||
Emitted when the ERC1155 NFT is deposited to gateway in layer 1.
|
||||
|
||||
|
||||
|
||||
@@ -442,7 +442,7 @@ Emitted when the ERC1155 NFT is deposited to gateway on layer 1.
|
||||
event FinalizeBatchWithdrawERC1155(address indexed _l1Token, address indexed _l2Token, address indexed _from, address _to, uint256[] _tokenIds, uint256[] _amounts)
|
||||
```
|
||||
|
||||
Emitted when the ERC1155 NFT is batch transfered to recipient on layer 1.
|
||||
Emitted when the ERC1155 NFT is batch transfered to recipient in layer 1.
|
||||
|
||||
|
||||
|
||||
@@ -463,7 +463,7 @@ Emitted when the ERC1155 NFT is batch transfered to recipient on layer 1.
|
||||
event FinalizeWithdrawERC1155(address indexed _l1Token, address indexed _l2Token, address indexed _from, address _to, uint256 _tokenId, uint256 _amount)
|
||||
```
|
||||
|
||||
Emitted when the ERC1155 NFT is transfered to recipient on layer 1.
|
||||
Emitted when the ERC1155 NFT is transfered to recipient in layer 1.
|
||||
|
||||
|
||||
|
||||
@@ -544,8 +544,8 @@ Emitted when token mapping for ERC1155 token is updated.
|
||||
|
||||
| Name | Type | Description |
|
||||
|---|---|---|
|
||||
| _l1Token | address | The address of ERC1155 token on layer 1. |
|
||||
| _l2Token | address | The address of corresponding ERC1155 token on layer 2. |
|
||||
| _l1Token | address | The address of corresponding ERC1155 token in layer 2. |
|
||||
| _l2Token | address | undefined |
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
> L1ERC721Gateway
|
||||
|
||||
The `L1ERC721Gateway` is used to deposit ERC721 compatible NFT on layer 1 and finalize withdraw the NFTs from layer 2.
|
||||
The `L1ERC721Gateway` is used to deposit ERC721 compatible NFT in layer 1 and finalize withdraw the NFTs from layer 2.
|
||||
|
||||
*The deposited NFTs are held in this gateway. On finalizing withdraw, the corresponding NFT will be transfer to the recipient directly. This will be changed if we have more specific scenarios.*
|
||||
|
||||
@@ -24,8 +24,8 @@ Deposit a list of some ERC721 NFT to a recipient's account on layer 2.
|
||||
|
||||
| Name | Type | Description |
|
||||
|---|---|---|
|
||||
| _token | address | The address of ERC721 NFT on layer 1. |
|
||||
| _to | address | The address of recipient on layer 2. |
|
||||
| _token | address | The address of ERC721 NFT in layer 1. |
|
||||
| _to | address | The address of recipient in layer 2. |
|
||||
| _tokenIds | uint256[] | The list of token ids to deposit. |
|
||||
| _gasLimit | uint256 | Estimated gas limit required to complete the deposit on layer 2. |
|
||||
|
||||
@@ -43,7 +43,7 @@ Deposit a list of some ERC721 NFT to caller's account on layer 2.
|
||||
|
||||
| Name | Type | Description |
|
||||
|---|---|---|
|
||||
| _token | address | The address of ERC721 NFT on layer 1. |
|
||||
| _token | address | The address of ERC721 NFT in layer 1. |
|
||||
| _tokenIds | uint256[] | The list of token ids to deposit. |
|
||||
| _gasLimit | uint256 | Estimated gas limit required to complete the deposit on layer 2. |
|
||||
|
||||
@@ -78,8 +78,8 @@ Deposit some ERC721 NFT to a recipient's account on layer 2.
|
||||
|
||||
| Name | Type | Description |
|
||||
|---|---|---|
|
||||
| _token | address | The address of ERC721 NFT on layer 1. |
|
||||
| _to | address | The address of recipient on layer 2. |
|
||||
| _token | address | The address of ERC721 NFT in layer 1. |
|
||||
| _to | address | The address of recipient in layer 2. |
|
||||
| _tokenId | uint256 | The token id to deposit. |
|
||||
| _gasLimit | uint256 | Estimated gas limit required to complete the deposit on layer 2. |
|
||||
|
||||
@@ -97,7 +97,7 @@ Deposit some ERC721 NFT to caller's account on layer 2.
|
||||
|
||||
| Name | Type | Description |
|
||||
|---|---|---|
|
||||
| _token | address | The address of ERC721 NFT on layer 1. |
|
||||
| _token | address | The address of ERC721 NFT in layer 1. |
|
||||
| _tokenId | uint256 | The token id to deposit. |
|
||||
| _gasLimit | uint256 | Estimated gas limit required to complete the deposit on layer 2. |
|
||||
|
||||
@@ -107,9 +107,9 @@ Deposit some ERC721 NFT to caller's account on layer 2.
|
||||
function finalizeBatchWithdrawERC721(address _l1Token, address _l2Token, address _from, address _to, uint256[] _tokenIds) external nonpayable
|
||||
```
|
||||
|
||||
Complete ERC721 batch withdraw from layer 2 to layer 1 and send NFT to recipient's account on layer 1.
|
||||
Complete ERC721 batch withdraw from layer 2 to layer 1 and send NFT to recipient's account in layer 1.
|
||||
|
||||
*Requirements: - The function should only be called by L1ScrollMessenger. - The function should also only be called by L2ERC721Gateway on layer 2.*
|
||||
*Requirements: - The function should only be called by L1ScrollMessenger. - The function should also only be called by L2ERC721Gateway in layer 2.*
|
||||
|
||||
#### Parameters
|
||||
|
||||
@@ -117,8 +117,8 @@ Complete ERC721 batch withdraw from layer 2 to layer 1 and send NFT to recipient
|
||||
|---|---|---|
|
||||
| _l1Token | address | The address of corresponding layer 1 token. |
|
||||
| _l2Token | address | The address of corresponding layer 2 token. |
|
||||
| _from | address | The address of account who withdraw the token on layer 2. |
|
||||
| _to | address | The address of recipient on layer 1 to receive the token. |
|
||||
| _from | address | The address of account who withdraw the token in layer 2. |
|
||||
| _to | address | The address of recipient in layer 1 to receive the token. |
|
||||
| _tokenIds | uint256[] | The list of token ids to withdraw. |
|
||||
|
||||
### finalizeWithdrawERC721
|
||||
@@ -127,9 +127,9 @@ Complete ERC721 batch withdraw from layer 2 to layer 1 and send NFT to recipient
|
||||
function finalizeWithdrawERC721(address _l1Token, address _l2Token, address _from, address _to, uint256 _tokenId) external nonpayable
|
||||
```
|
||||
|
||||
Complete ERC721 withdraw from layer 2 to layer 1 and send NFT to recipient's account on layer 1.
|
||||
Complete ERC721 withdraw from layer 2 to layer 1 and send NFT to recipient's account in layer 1.
|
||||
|
||||
*Requirements: - The function should only be called by L1ScrollMessenger. - The function should also only be called by L2ERC721Gateway on layer 2.*
|
||||
*Requirements: - The function should only be called by L1ScrollMessenger. - The function should also only be called by L2ERC721Gateway in layer 2.*
|
||||
|
||||
#### Parameters
|
||||
|
||||
@@ -137,8 +137,8 @@ Complete ERC721 withdraw from layer 2 to layer 1 and send NFT to recipient's
|
||||
|---|---|---|
|
||||
| _l1Token | address | The address of corresponding layer 1 token. |
|
||||
| _l2Token | address | The address of corresponding layer 2 token. |
|
||||
| _from | address | The address of account who withdraw the token on layer 2. |
|
||||
| _to | address | The address of recipient on layer 1 to receive the token. |
|
||||
| _from | address | The address of account who withdraw the token in layer 2. |
|
||||
| _to | address | The address of recipient in layer 1 to receive the token. |
|
||||
| _tokenId | uint256 | The token id to withdraw. |
|
||||
|
||||
### initialize
|
||||
@@ -313,8 +313,8 @@ Update layer 2 to layer 2 token mapping.
|
||||
|
||||
| Name | Type | Description |
|
||||
|---|---|---|
|
||||
| _l1Token | address | The address of ERC721 token on layer 1. |
|
||||
| _l2Token | address | The address of corresponding ERC721 token on layer 2. |
|
||||
| _l1Token | address | The address of corresponding ERC721 token in layer 2. |
|
||||
| _l2Token | address | undefined |
|
||||
|
||||
|
||||
|
||||
@@ -326,7 +326,7 @@ Update layer 2 to layer 2 token mapping.
|
||||
event BatchDepositERC721(address indexed _l1Token, address indexed _l2Token, address indexed _from, address _to, uint256[] _tokenIds)
|
||||
```
|
||||
|
||||
Emitted when the ERC721 NFT is batch deposited to gateway on layer 1.
|
||||
Emitted when the ERC721 NFT is batch deposited to gateway in layer 1.
|
||||
|
||||
|
||||
|
||||
@@ -364,7 +364,7 @@ Emitted when a batch of ERC721 tokens are refunded.
|
||||
event DepositERC721(address indexed _l1Token, address indexed _l2Token, address indexed _from, address _to, uint256 _tokenId)
|
||||
```
|
||||
|
||||
Emitted when the ERC721 NFT is deposited to gateway on layer 1.
|
||||
Emitted when the ERC721 NFT is deposited to gateway in layer 1.
|
||||
|
||||
|
||||
|
||||
@@ -384,7 +384,7 @@ Emitted when the ERC721 NFT is deposited to gateway on layer 1.
|
||||
event FinalizeBatchWithdrawERC721(address indexed _l1Token, address indexed _l2Token, address indexed _from, address _to, uint256[] _tokenIds)
|
||||
```
|
||||
|
||||
Emitted when the ERC721 NFT is batch transfered to recipient on layer 1.
|
||||
Emitted when the ERC721 NFT is batch transfered to recipient in layer 1.
|
||||
|
||||
|
||||
|
||||
@@ -404,7 +404,7 @@ Emitted when the ERC721 NFT is batch transfered to recipient on layer 1.
|
||||
event FinalizeWithdrawERC721(address indexed _l1Token, address indexed _l2Token, address indexed _from, address _to, uint256 _tokenId)
|
||||
```
|
||||
|
||||
Emitted when the ERC721 NFT is transfered to recipient on layer 1.
|
||||
Emitted when the ERC721 NFT is transfered to recipient in layer 1.
|
||||
|
||||
|
||||
|
||||
@@ -483,8 +483,8 @@ Emitted when token mapping for ERC721 token is updated.
|
||||
|
||||
| Name | Type | Description |
|
||||
|---|---|---|
|
||||
| _l1Token | address | The address of ERC721 token on layer 1. |
|
||||
| _l2Token | address | The address of corresponding ERC721 token on layer 2. |
|
||||
| _l1Token | address | The address of corresponding ERC721 token in layer 2. |
|
||||
| _l2Token | address | undefined |
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
> L1StandardERC20Gateway
|
||||
|
||||
The `L1StandardERC20Gateway` is used to deposit standard ERC20 tokens on layer 1 and finalize withdraw the tokens from layer 2.
|
||||
The `L1StandardERC20Gateway` is used to deposit standard ERC20 tokens in layer 1 and finalize withdraw the tokens from layer 2.
|
||||
|
||||
*The deposited ERC20 tokens are held in this gateway. On finalizing withdraw, the corresponding token will be transfer to the recipient directly. Any ERC20 that requires non-standard functionality should use a separate gateway.*
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
> L1WETHGateway
|
||||
|
||||
The `L1WETHGateway` contract is used to deposit `WETH` token on layer 1 and finalize withdraw `WETH` from layer 2.
|
||||
The `L1WETHGateway` contract is used to deposit `WETH` token in layer 1 and finalize withdraw `WETH` from layer 2.
|
||||
|
||||
*The deposited WETH tokens are not held in the gateway. It will first be unwrapped as Ether and then the Ether will be sent to the `L1ScrollMessenger` contract. On finalizing withdraw, the Ether will be transfered from `L1ScrollMessenger`, then wrapped as WETH and finally transfer to recipient.*
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
> L2ERC1155Gateway
|
||||
|
||||
The `L2ERC1155Gateway` is used to withdraw ERC1155 compatible NFTs on layer 2 and finalize deposit the NFTs from layer 1.
|
||||
The `L2ERC1155Gateway` is used to withdraw ERC1155 compatible NFTs in layer 2 and finalize deposit the NFTs from layer 1.
|
||||
|
||||
*The withdrawn NFTs tokens will be burned directly. On finalizing deposit, the corresponding NFT will be minted and transfered to the recipient. This will be changed if we have more specific scenarios.*
|
||||
|
||||
@@ -72,9 +72,9 @@ The address of corresponding L1/L2 Gateway contract.
|
||||
function finalizeBatchDepositERC1155(address _l1Token, address _l2Token, address _from, address _to, uint256[] _tokenIds, uint256[] _amounts) external nonpayable
|
||||
```
|
||||
|
||||
Complete ERC1155 deposit from layer 1 to layer 2 and send NFT to recipient's account on layer 2.
|
||||
Complete ERC1155 deposit from layer 1 to layer 2 and send NFT to recipient's account in layer 2.
|
||||
|
||||
*Requirements: - The function should only be called by L2ScrollMessenger. - The function should also only be called by L1ERC1155Gateway on layer 1.*
|
||||
*Requirements: - The function should only be called by L2ScrollMessenger. - The function should also only be called by L1ERC1155Gateway in layer 1.*
|
||||
|
||||
#### Parameters
|
||||
|
||||
@@ -93,9 +93,9 @@ Complete ERC1155 deposit from layer 1 to layer 2 and send NFT to recipient's
|
||||
function finalizeDepositERC1155(address _l1Token, address _l2Token, address _from, address _to, uint256 _tokenId, uint256 _amount) external nonpayable
|
||||
```
|
||||
|
||||
Complete ERC1155 deposit from layer 1 to layer 2 and send NFT to recipient's account on layer 2.
|
||||
Complete ERC1155 deposit from layer 1 to layer 2 and send NFT to recipient's account in layer 2.
|
||||
|
||||
*Requirements: - The function should only be called by L2ScrollMessenger. - The function should also only be called by L1ERC1155Gateway on layer 1.*
|
||||
*Requirements: - The function should only be called by L2ScrollMessenger. - The function should also only be called by L1ERC1155Gateway in layer 1.*
|
||||
|
||||
#### Parameters
|
||||
|
||||
@@ -313,8 +313,8 @@ Update layer 2 to layer 1 token mapping.
|
||||
|
||||
| Name | Type | Description |
|
||||
|---|---|---|
|
||||
| _l2Token | address | The address of corresponding ERC1155 token on layer 2. |
|
||||
| _l1Token | address | The address of ERC1155 token on layer 1. |
|
||||
| _l2Token | address | undefined |
|
||||
| _l1Token | address | The address of ERC1155 token in layer 1. |
|
||||
|
||||
### withdrawERC1155
|
||||
|
||||
@@ -365,7 +365,7 @@ Withdraw some ERC1155 NFT to caller's account on layer 1.
|
||||
event BatchWithdrawERC1155(address indexed l1Token, address indexed l2Token, address indexed from, address to, uint256[] tokenIds, uint256[] amounts)
|
||||
```
|
||||
|
||||
Emitted when the ERC1155 NFT is batch transfered to gateway on layer 2.
|
||||
Emitted when the ERC1155 NFT is batch transfered to gateway in layer 2.
|
||||
|
||||
|
||||
|
||||
@@ -386,7 +386,7 @@ Emitted when the ERC1155 NFT is batch transfered to gateway on layer 2.
|
||||
event FinalizeBatchDepositERC1155(address indexed l1Token, address indexed l2Token, address indexed from, address to, uint256[] tokenIds, uint256[] amounts)
|
||||
```
|
||||
|
||||
Emitted when the ERC1155 NFT is batch transfered to recipient on layer 2.
|
||||
Emitted when the ERC1155 NFT is batch transfered to recipient in layer 2.
|
||||
|
||||
|
||||
|
||||
@@ -407,7 +407,7 @@ Emitted when the ERC1155 NFT is batch transfered to recipient on layer 2.
|
||||
event FinalizeDepositERC1155(address indexed l1Token, address indexed l2Token, address indexed from, address to, uint256 tokenId, uint256 amount)
|
||||
```
|
||||
|
||||
Emitted when the ERC1155 NFT is transfered to recipient on layer 2.
|
||||
Emitted when the ERC1155 NFT is transfered to recipient in layer 2.
|
||||
|
||||
|
||||
|
||||
@@ -469,8 +469,8 @@ Emitted when token mapping for ERC1155 token is updated.
|
||||
|
||||
| Name | Type | Description |
|
||||
|---|---|---|
|
||||
| _l2Token | address | The address of corresponding ERC1155 token on layer 2. |
|
||||
| _l1Token | address | The address of ERC1155 token on layer 1. |
|
||||
| _l2Token | address | undefined |
|
||||
| _l1Token | address | The address of ERC1155 token in layer 1. |
|
||||
|
||||
### WithdrawERC1155
|
||||
|
||||
@@ -478,7 +478,7 @@ Emitted when token mapping for ERC1155 token is updated.
|
||||
event WithdrawERC1155(address indexed l1Token, address indexed l2Token, address indexed from, address to, uint256 tokenId, uint256 amount)
|
||||
```
|
||||
|
||||
Emitted when the ERC1155 NFT is transfered to gateway on layer 2.
|
||||
Emitted when the ERC1155 NFT is transfered to gateway in layer 2.
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
> L2ERC721Gateway
|
||||
|
||||
The `L2ERC721Gateway` is used to withdraw ERC721 compatible NFTs on layer 2 and finalize deposit the NFTs from layer 1.
|
||||
The `L2ERC721Gateway` is used to withdraw ERC721 compatible NFTs in layer 2 and finalize deposit the NFTs from layer 1.
|
||||
|
||||
*The withdrawn NFTs tokens will be burned directly. On finalizing deposit, the corresponding NFT will be minted and transfered to the recipient. This will be changed if we have more specific scenarios.*
|
||||
|
||||
@@ -70,9 +70,9 @@ The address of corresponding L1/L2 Gateway contract.
|
||||
function finalizeBatchDepositERC721(address _l1Token, address _l2Token, address _from, address _to, uint256[] _tokenIds) external nonpayable
|
||||
```
|
||||
|
||||
Complete ERC721 deposit from layer 1 to layer 2 and send NFT to recipient's account on layer 2.
|
||||
Complete ERC721 deposit from layer 1 to layer 2 and send NFT to recipient's account in layer 2.
|
||||
|
||||
*Requirements: - The function should only be called by L2ScrollMessenger. - The function should also only be called by L1ERC721Gateway on layer 1.*
|
||||
*Requirements: - The function should only be called by L2ScrollMessenger. - The function should also only be called by L1ERC721Gateway in layer 1.*
|
||||
|
||||
#### Parameters
|
||||
|
||||
@@ -90,9 +90,9 @@ Complete ERC721 deposit from layer 1 to layer 2 and send NFT to recipient's
|
||||
function finalizeDepositERC721(address _l1Token, address _l2Token, address _from, address _to, uint256 _tokenId) external nonpayable
|
||||
```
|
||||
|
||||
Complete ERC721 deposit from layer 1 to layer 2 and send NFT to recipient's account on layer 2.
|
||||
Complete ERC721 deposit from layer 1 to layer 2 and send NFT to recipient's account in layer 2.
|
||||
|
||||
*Requirements: - The function should only be called by L2ScrollMessenger. - The function should also only be called by L1ERC721Gateway on layer 1.*
|
||||
*Requirements: - The function should only be called by L2ScrollMessenger. - The function should also only be called by L1ERC721Gateway in layer 1.*
|
||||
|
||||
#### Parameters
|
||||
|
||||
@@ -260,8 +260,8 @@ Update layer 2 to layer 1 token mapping.
|
||||
|
||||
| Name | Type | Description |
|
||||
|---|---|---|
|
||||
| _l2Token | address | The address of corresponding ERC721 token on layer 2. |
|
||||
| _l1Token | address | The address of ERC721 token on layer 1. |
|
||||
| _l2Token | address | undefined |
|
||||
| _l1Token | address | The address of ERC721 token in layer 1. |
|
||||
|
||||
### withdrawERC721
|
||||
|
||||
@@ -310,7 +310,7 @@ Withdraw some ERC721 NFT to caller's account on layer 1.
|
||||
event BatchWithdrawERC721(address indexed l1Token, address indexed l2Token, address indexed from, address to, uint256[] tokenIds)
|
||||
```
|
||||
|
||||
Emitted when the ERC721 NFT is batch transfered to gateway on layer 2.
|
||||
Emitted when the ERC721 NFT is batch transfered to gateway in layer 2.
|
||||
|
||||
|
||||
|
||||
@@ -330,7 +330,7 @@ Emitted when the ERC721 NFT is batch transfered to gateway on layer 2.
|
||||
event FinalizeBatchDepositERC721(address indexed l1Token, address indexed l2Token, address indexed from, address to, uint256[] tokenIds)
|
||||
```
|
||||
|
||||
Emitted when the ERC721 NFT is batch transfered to recipient on layer 2.
|
||||
Emitted when the ERC721 NFT is batch transfered to recipient in layer 2.
|
||||
|
||||
|
||||
|
||||
@@ -350,7 +350,7 @@ Emitted when the ERC721 NFT is batch transfered to recipient on layer 2.
|
||||
event FinalizeDepositERC721(address indexed l1Token, address indexed l2Token, address indexed from, address to, uint256 tokenId)
|
||||
```
|
||||
|
||||
Emitted when the ERC721 NFT is transfered to recipient on layer 2.
|
||||
Emitted when the ERC721 NFT is transfered to recipient in layer 2.
|
||||
|
||||
|
||||
|
||||
@@ -411,8 +411,8 @@ Emitted when token mapping for ERC721 token is updated.
|
||||
|
||||
| Name | Type | Description |
|
||||
|---|---|---|
|
||||
| _l2Token | address | The address of corresponding ERC721 token on layer 2. |
|
||||
| _l1Token | address | The address of ERC721 token on layer 1. |
|
||||
| _l2Token | address | undefined |
|
||||
| _l1Token | address | The address of ERC721 token in layer 1. |
|
||||
|
||||
### WithdrawERC721
|
||||
|
||||
@@ -420,7 +420,7 @@ Emitted when token mapping for ERC721 token is updated.
|
||||
event WithdrawERC721(address indexed l1Token, address indexed l2Token, address indexed from, address to, uint256 tokenId)
|
||||
```
|
||||
|
||||
Emitted when the ERC721 NFT is transfered to gateway on layer 2.
|
||||
Emitted when the ERC721 NFT is transfered to gateway in layer 2.
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
The `L2ScrollMessenger` contract can: 1. send messages from layer 2 to layer 1; 2. relay messages from layer 1 layer 2; 3. drop expired message due to sequencer problems.
|
||||
|
||||
*It should be a predeployed contract on layer 2 and should hold infinite amount of Ether (Specifically, `uint256(-1)`), which can be initialized in Genesis Block.*
|
||||
*It should be a predeployed contract in layer 2 and should hold infinite amount of Ether (Specifically, `uint256(-1)`), which can be initialized in Genesis Block.*
|
||||
|
||||
## Methods
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
> L2StandardERC20Gateway
|
||||
|
||||
The `L2StandardERC20Gateway` is used to withdraw standard ERC20 tokens on layer 2 and finalize deposit the tokens from layer 1.
|
||||
The `L2StandardERC20Gateway` is used to withdraw standard ERC20 tokens in layer 2 and finalize deposit the tokens from layer 1.
|
||||
|
||||
*The withdrawn ERC20 tokens will be burned directly. On finalizing deposit, the corresponding token will be minted and transfered to the recipient. Any ERC20 that requires non-standard functionality should use a separate gateway.*
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
> L2WETHGateway
|
||||
|
||||
The `L2WETHGateway` contract is used to withdraw `WETH` token on layer 2 and finalize deposit `WETH` from layer 1.
|
||||
The `L2WETHGateway` contract is used to withdraw `WETH` token in layer 2 and finalize deposit `WETH` from layer 1.
|
||||
|
||||
*The WETH tokens are not held in the gateway. It will first be unwrapped as Ether and then the Ether will be sent to the `L2ScrollMessenger` contract. On finalizing deposit, the Ether will be transfered from `L2ScrollMessenger`, then wrapped as WETH and finally transfer to recipient.*
|
||||
|
||||
|
||||
@@ -116,7 +116,7 @@ const config: HardhatUserConfig = {
|
||||
"IL2ERC1155Gateway",
|
||||
"IScrollStandardERC20Factory",
|
||||
"IZKRollup",
|
||||
"WrappedEther",
|
||||
"WETH9",
|
||||
],
|
||||
},
|
||||
};
|
||||
|
||||
@@ -95,16 +95,16 @@ describe("EnforcedTxGateway.spec", async () => {
|
||||
});
|
||||
});
|
||||
|
||||
context("#setPause", async () => {
|
||||
context("#setPaused", async () => {
|
||||
it("should revert, when non-owner call", async () => {
|
||||
await expect(gateway.connect(signer).setPause(false)).to.revertedWith("Ownable: caller is not the owner");
|
||||
await expect(gateway.connect(signer).setPaused(false)).to.revertedWith("Ownable: caller is not the owner");
|
||||
});
|
||||
|
||||
it("should succeed", async () => {
|
||||
expect(await gateway.paused()).to.eq(false);
|
||||
await expect(gateway.setPause(true)).to.emit(gateway, "Paused").withArgs(deployer.address);
|
||||
await expect(gateway.setPaused(true)).to.emit(gateway, "Paused").withArgs(deployer.address);
|
||||
expect(await gateway.paused()).to.eq(true);
|
||||
await expect(gateway.setPause(false)).to.emit(gateway, "Unpaused").withArgs(deployer.address);
|
||||
await expect(gateway.setPaused(false)).to.emit(gateway, "Unpaused").withArgs(deployer.address);
|
||||
expect(await gateway.paused()).to.eq(false);
|
||||
});
|
||||
});
|
||||
@@ -112,7 +112,7 @@ describe("EnforcedTxGateway.spec", async () => {
|
||||
|
||||
context("#sendTransaction, by EOA", async () => {
|
||||
it("should revert, when contract is paused", async () => {
|
||||
await gateway.setPause(true);
|
||||
await gateway.setPaused(true);
|
||||
await expect(
|
||||
gateway.connect(signer)["sendTransaction(address,uint256,uint256,bytes)"](signer.address, 0, 0, "0x")
|
||||
).to.revertedWith("Pausable: paused");
|
||||
@@ -246,7 +246,7 @@ describe("EnforcedTxGateway.spec", async () => {
|
||||
};
|
||||
|
||||
it("should revert, when contract is paused", async () => {
|
||||
await gateway.setPause(true);
|
||||
await gateway.setPaused(true);
|
||||
await expect(
|
||||
gateway
|
||||
.connect(deployer)
|
||||
|
||||
@@ -119,16 +119,10 @@ describe("L1BlockContainer", async () => {
|
||||
const [deployer] = await ethers.getSigners();
|
||||
const L1BlockContainer = await ethers.getContractFactory("L1BlockContainer", deployer);
|
||||
container = await L1BlockContainer.deploy(deployer.address);
|
||||
|
||||
const Whitelist = await ethers.getContractFactory("Whitelist", deployer);
|
||||
const whitelist = await Whitelist.deploy(deployer.address);
|
||||
await whitelist.updateWhitelistStatus([deployer.address], true);
|
||||
|
||||
await container.updateWhitelist(whitelist.address);
|
||||
});
|
||||
|
||||
it("should revert, when sender not allowed", async () => {
|
||||
const [, signer] = await ethers.getSigners();
|
||||
const [deployer] = await ethers.getSigners();
|
||||
await container.initialize(
|
||||
test.parentHash,
|
||||
test.blockHeight - 1,
|
||||
@@ -136,8 +130,11 @@ describe("L1BlockContainer", async () => {
|
||||
test.baseFee,
|
||||
test.stateRoot
|
||||
);
|
||||
const Whitelist = await ethers.getContractFactory("Whitelist", deployer);
|
||||
const whitelist = await Whitelist.deploy(deployer.address);
|
||||
await container.updateWhitelist(whitelist.address);
|
||||
|
||||
await expect(container.connect(signer).importBlockHeader(constants.HashZero, [], false)).to.revertedWith(
|
||||
await expect(container.importBlockHeader(constants.HashZero, [], false)).to.revertedWith(
|
||||
"Not whitelisted sender"
|
||||
);
|
||||
});
|
||||
|
||||
@@ -10,8 +10,8 @@ async function main() {
|
||||
|
||||
if (!addressFile.get("WETH")) {
|
||||
console.log(">> Deploy WETH");
|
||||
const WrappedEther = await ethers.getContractFactory("WrappedEther", deployer);
|
||||
const weth = await WrappedEther.deploy();
|
||||
const WETH9 = await ethers.getContractFactory("WETH9", deployer);
|
||||
const weth = await WETH9.deploy();
|
||||
console.log(`>> waiting for transaction: ${weth.deployTransaction.hash}`);
|
||||
await weth.deployed();
|
||||
console.log(`✅ WETH deployed at ${weth.address}`);
|
||||
|
||||
@@ -15,6 +15,7 @@ import {L2GatewayRouter} from "../../src/L2/gateways/L2GatewayRouter.sol";
|
||||
import {L2ScrollMessenger} from "../../src/L2/L2ScrollMessenger.sol";
|
||||
import {L2StandardERC20Gateway} from "../../src/L2/gateways/L2StandardERC20Gateway.sol";
|
||||
import {L2WETHGateway} from "../../src/L2/gateways/L2WETHGateway.sol";
|
||||
import {L1BlockContainer} from "../../src/L2/predeploys/L1BlockContainer.sol";
|
||||
import {L1GasPriceOracle} from "../../src/L2/predeploys/L1GasPriceOracle.sol";
|
||||
import {L2MessageQueue} from "../../src/L2/predeploys/L2MessageQueue.sol";
|
||||
import {L2TxFeeVault} from "../../src/L2/predeploys/L2TxFeeVault.sol";
|
||||
@@ -30,10 +31,12 @@ contract DeployL2BridgeContracts is Script {
|
||||
address L2_WETH_ADDR = vm.envAddress("L2_WETH_ADDR");
|
||||
|
||||
L1GasPriceOracle oracle;
|
||||
L1BlockContainer container;
|
||||
L2MessageQueue queue;
|
||||
ProxyAdmin proxyAdmin;
|
||||
|
||||
// predeploy contracts
|
||||
address L1_BLOCK_CONTAINER_PREDEPLOY_ADDR = vm.envOr("L1_BLOCK_CONTAINER_PREDEPLOY_ADDR", address(0));
|
||||
address L1_GAS_PRICE_ORACLE_PREDEPLOY_ADDR = vm.envOr("L1_GAS_PRICE_ORACLE_PREDEPLOY_ADDR", address(0));
|
||||
address L2_MESSAGE_QUEUE_PREDEPLOY_ADDR = vm.envOr("L2_MESSAGE_QUEUE_PREDEPLOY_ADDR", address(0));
|
||||
address L2_TX_FEE_VAULT_PREDEPLOY_ADDR = vm.envOr("L2_TX_FEE_VAULT_PREDEPLOY_ADDR", address(0));
|
||||
@@ -44,6 +47,7 @@ contract DeployL2BridgeContracts is Script {
|
||||
|
||||
// predeploys
|
||||
deployL1GasPriceOracle();
|
||||
deployL1BlockContainer();
|
||||
deployL2MessageQueue();
|
||||
deployTxFeeVault();
|
||||
deployL2Whitelist();
|
||||
@@ -76,6 +80,19 @@ contract DeployL2BridgeContracts is Script {
|
||||
logAddress("L1_GAS_PRICE_ORACLE_ADDR", address(oracle));
|
||||
}
|
||||
|
||||
function deployL1BlockContainer() internal {
|
||||
if (L1_BLOCK_CONTAINER_PREDEPLOY_ADDR != address(0)) {
|
||||
container = L1BlockContainer(L1_BLOCK_CONTAINER_PREDEPLOY_ADDR);
|
||||
logAddress("L1_BLOCK_CONTAINER_ADDR", address(L1_BLOCK_CONTAINER_PREDEPLOY_ADDR));
|
||||
return;
|
||||
}
|
||||
|
||||
address owner = vm.addr(L2_DEPLOYER_PRIVATE_KEY);
|
||||
container = new L1BlockContainer(owner);
|
||||
|
||||
logAddress("L1_BLOCK_CONTAINER_ADDR", address(container));
|
||||
}
|
||||
|
||||
function deployL2MessageQueue() internal {
|
||||
if (L2_MESSAGE_QUEUE_PREDEPLOY_ADDR != address(0)) {
|
||||
queue = L2MessageQueue(L2_MESSAGE_QUEUE_PREDEPLOY_ADDR);
|
||||
|
||||
@@ -1,30 +1,30 @@
|
||||
// SPDX-License-Identifier: UNLICENSED
|
||||
pragma solidity ^0.8.10;
|
||||
|
||||
import {Script} from "forge-std/Script.sol";
|
||||
import {console} from "forge-std/console.sol";
|
||||
import { Script } from "forge-std/Script.sol";
|
||||
import { console } from "forge-std/console.sol";
|
||||
|
||||
import {WrappedEther} from "../../src/L2/predeploys/WrappedEther.sol";
|
||||
import { WETH9 } from "../../src/L2/predeploys/WETH9.sol";
|
||||
|
||||
contract DeployWeth is Script {
|
||||
address L1_WETH_ADDR = vm.envAddress("L1_WETH_ADDR");
|
||||
address L2_WETH_ADDR = vm.envAddress("L2_WETH_ADDR");
|
||||
address L1_WETH_ADDR = vm.envAddress("L1_WETH_ADDR");
|
||||
address L2_WETH_ADDR = vm.envAddress("L2_WETH_ADDR");
|
||||
|
||||
function run() external {
|
||||
// deploy weth only if we're running a private L1 network
|
||||
if (L1_WETH_ADDR == address(0)) {
|
||||
uint256 L1_WETH_DEPLOYER_PRIVATE_KEY = vm.envUint("L1_WETH_DEPLOYER_PRIVATE_KEY");
|
||||
vm.startBroadcast(L1_WETH_DEPLOYER_PRIVATE_KEY);
|
||||
WrappedEther weth = new WrappedEther();
|
||||
L1_WETH_ADDR = address(weth);
|
||||
vm.stopBroadcast();
|
||||
}
|
||||
|
||||
logAddress("L1_WETH_ADDR", L1_WETH_ADDR);
|
||||
logAddress("L2_WETH_ADDR", L2_WETH_ADDR);
|
||||
function run() external {
|
||||
// deploy weth only if we're running a private L1 network
|
||||
if (L1_WETH_ADDR == address(0)) {
|
||||
uint256 L1_WETH_DEPLOYER_PRIVATE_KEY = vm.envUint("L1_WETH_DEPLOYER_PRIVATE_KEY");
|
||||
vm.startBroadcast(L1_WETH_DEPLOYER_PRIVATE_KEY);
|
||||
WETH9 weth = new WETH9();
|
||||
L1_WETH_ADDR = address(weth);
|
||||
vm.stopBroadcast();
|
||||
}
|
||||
|
||||
function logAddress(string memory name, address addr) internal view {
|
||||
console.log(string(abi.encodePacked(name, "=", vm.toString(address(addr)))));
|
||||
}
|
||||
logAddress("L1_WETH_ADDR", L1_WETH_ADDR);
|
||||
logAddress("L2_WETH_ADDR", L2_WETH_ADDR);
|
||||
}
|
||||
|
||||
function logAddress(string memory name, address addr) internal view {
|
||||
console.log(string(abi.encodePacked(name, "=", vm.toString(address(addr)))));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,8 +22,8 @@ contract InitializeL1BridgeContracts is Script {
|
||||
uint256 CHAIN_ID_L2 = vm.envUint("CHAIN_ID_L2");
|
||||
uint256 MAX_L2_TX_IN_CHUNK = vm.envUint("MAX_L2_TX_IN_CHUNK");
|
||||
address L1_ROLLUP_OPERATOR_ADDR = vm.envAddress("L1_ROLLUP_OPERATOR_ADDR");
|
||||
|
||||
address L1_FEE_VAULT_ADDR = vm.envAddress("L1_FEE_VAULT_ADDR");
|
||||
address L1_WETH_ADDR = vm.envAddress("L1_WETH_ADDR");
|
||||
|
||||
address L1_WHITELIST_ADDR = vm.envAddress("L1_WHITELIST_ADDR");
|
||||
address L1_SCROLL_CHAIN_PROXY_ADDR = vm.envAddress("L1_SCROLL_CHAIN_PROXY_ADDR");
|
||||
@@ -61,15 +61,9 @@ contract InitializeL1BridgeContracts is Script {
|
||||
MAX_L2_TX_IN_CHUNK
|
||||
);
|
||||
ScrollChain(L1_SCROLL_CHAIN_PROXY_ADDR).updateSequencer(L1_ROLLUP_OPERATOR_ADDR, true);
|
||||
ScrollChain(L1_SCROLL_CHAIN_PROXY_ADDR).updateProver(L1_ROLLUP_OPERATOR_ADDR, true);
|
||||
|
||||
// initialize L2GasPriceOracle
|
||||
L2GasPriceOracle(L2_GAS_PRICE_ORACLE_PROXY_ADDR).initialize(
|
||||
21000, // _txGas
|
||||
53000, // _txGasContractCreation
|
||||
4, // _zeroGas
|
||||
16 // _nonZeroGas
|
||||
);
|
||||
L2GasPriceOracle(L2_GAS_PRICE_ORACLE_PROXY_ADDR).initialize(0, 0, 0, 0);
|
||||
L2GasPriceOracle(L2_GAS_PRICE_ORACLE_PROXY_ADDR).updateWhitelist(L1_WHITELIST_ADDR);
|
||||
|
||||
// initialize L1MessageQueue
|
||||
@@ -143,15 +137,6 @@ contract InitializeL1BridgeContracts is Script {
|
||||
L1_SCROLL_MESSENGER_PROXY_ADDR
|
||||
);
|
||||
|
||||
// set WETH gateway in router
|
||||
{
|
||||
address[] memory _tokens = new address[](1);
|
||||
_tokens[0] = L1_WETH_ADDR;
|
||||
address[] memory _gateways = new address[](1);
|
||||
_gateways[0] = L1_WETH_GATEWAY_PROXY_ADDR;
|
||||
L1GatewayRouter(L1_GATEWAY_ROUTER_PROXY_ADDR).setERC20Gateway(_tokens, _gateways);
|
||||
}
|
||||
|
||||
vm.stopBroadcast();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import {L2StandardERC20Gateway} from "../../src/L2/gateways/L2StandardERC20Gatew
|
||||
import {L2WETHGateway} from "../../src/L2/gateways/L2WETHGateway.sol";
|
||||
import {L2MessageQueue} from "../../src/L2/predeploys/L2MessageQueue.sol";
|
||||
import {L2TxFeeVault} from "../../src/L2/predeploys/L2TxFeeVault.sol";
|
||||
import {L1BlockContainer} from "../../src/L2/predeploys/L1BlockContainer.sol";
|
||||
import {L1GasPriceOracle} from "../../src/L2/predeploys/L1GasPriceOracle.sol";
|
||||
import {Whitelist} from "../../src/L2/predeploys/Whitelist.sol";
|
||||
import {ScrollStandardERC20Factory} from "../../src/libraries/token/ScrollStandardERC20Factory.sol";
|
||||
@@ -20,8 +21,6 @@ import {ScrollStandardERC20Factory} from "../../src/libraries/token/ScrollStanda
|
||||
contract InitializeL2BridgeContracts is Script {
|
||||
uint256 deployerPrivateKey = vm.envUint("L2_DEPLOYER_PRIVATE_KEY");
|
||||
|
||||
address L2_WETH_ADDR = vm.envAddress("L2_WETH_ADDR");
|
||||
|
||||
address L1_SCROLL_MESSENGER_PROXY_ADDR = vm.envAddress("L1_SCROLL_MESSENGER_PROXY_ADDR");
|
||||
address L1_GATEWAY_ROUTER_PROXY_ADDR = vm.envAddress("L1_GATEWAY_ROUTER_PROXY_ADDR");
|
||||
address L1_CUSTOM_ERC20_GATEWAY_PROXY_ADDR = vm.envAddress("L1_CUSTOM_ERC20_GATEWAY_PROXY_ADDR");
|
||||
@@ -32,6 +31,7 @@ contract InitializeL2BridgeContracts is Script {
|
||||
address L1_WETH_GATEWAY_PROXY_ADDR = vm.envAddress("L1_WETH_GATEWAY_PROXY_ADDR");
|
||||
|
||||
address L2_TX_FEE_VAULT_ADDR = vm.envAddress("L2_TX_FEE_VAULT_ADDR");
|
||||
address L1_BLOCK_CONTAINER_ADDR = vm.envAddress("L1_BLOCK_CONTAINER_ADDR");
|
||||
address L1_GAS_PRICE_ORACLE_ADDR = vm.envAddress("L1_GAS_PRICE_ORACLE_ADDR");
|
||||
address L2_WHITELIST_ADDR = vm.envAddress("L2_WHITELIST_ADDR");
|
||||
address L2_MESSAGE_QUEUE_ADDR = vm.envAddress("L2_MESSAGE_QUEUE_ADDR");
|
||||
@@ -50,11 +50,15 @@ contract InitializeL2BridgeContracts is Script {
|
||||
vm.startBroadcast(deployerPrivateKey);
|
||||
|
||||
// initialize L2MessageQueue
|
||||
L2MessageQueue(L2_MESSAGE_QUEUE_ADDR).initialize(L2_SCROLL_MESSENGER_PROXY_ADDR);
|
||||
L2MessageQueue(L2_MESSAGE_QUEUE_ADDR).initialize();
|
||||
L2MessageQueue(L2_MESSAGE_QUEUE_ADDR).updateMessenger(L2_SCROLL_MESSENGER_PROXY_ADDR);
|
||||
|
||||
// initialize L2TxFeeVault
|
||||
L2TxFeeVault(payable(L2_TX_FEE_VAULT_ADDR)).updateMessenger(L2_SCROLL_MESSENGER_PROXY_ADDR);
|
||||
|
||||
// initialize L1BlockContainer
|
||||
L1BlockContainer(L1_BLOCK_CONTAINER_ADDR).updateWhitelist(L2_WHITELIST_ADDR);
|
||||
|
||||
// initialize L1GasPriceOracle
|
||||
L1GasPriceOracle(L1_GAS_PRICE_ORACLE_ADDR).updateWhitelist(L2_WHITELIST_ADDR);
|
||||
|
||||
@@ -111,15 +115,6 @@ contract InitializeL2BridgeContracts is Script {
|
||||
L2_SCROLL_MESSENGER_PROXY_ADDR
|
||||
);
|
||||
|
||||
// set WETH gateway in router
|
||||
{
|
||||
address[] memory _tokens = new address[](1);
|
||||
_tokens[0] = L2_WETH_ADDR;
|
||||
address[] memory _gateways = new address[](1);
|
||||
_gateways[0] = L2_WETH_GATEWAY_PROXY_ADDR;
|
||||
L2GatewayRouter(L2_GATEWAY_ROUTER_PROXY_ADDR).setERC20Gateway(_tokens, _gateways);
|
||||
}
|
||||
|
||||
// initialize ScrollStandardERC20Factory
|
||||
ScrollStandardERC20Factory(L2_SCROLL_STANDARD_ERC20_FACTORY_ADDR).transferOwnership(
|
||||
L2_STANDARD_ERC20_GATEWAY_PROXY_ADDR
|
||||
|
||||
@@ -182,8 +182,8 @@ contract L1ScrollMessenger is PausableUpgradeable, ScrollMessengerBase, IL1Scrol
|
||||
address _refundAddress
|
||||
) external payable override whenNotPaused notInExecution {
|
||||
// We will use a different `queueIndex` for the replaced message. However, the original `queueIndex` or `nonce`
|
||||
// is encoded in the `_message`. We will check the `xDomainCalldata` on layer 2 to avoid duplicated execution.
|
||||
// So, only one message will succeed on layer 2. If one of the message is executed successfully, the other one
|
||||
// is encoded in the `_message`. We will check the `xDomainCalldata` in layer 2 to avoid duplicated execution.
|
||||
// So, only one message will succeed in layer 2. If one of the message is executed successfully, the other one
|
||||
// will revert with "Message was already successfully executed".
|
||||
address _messageQueue = messageQueue;
|
||||
address _counterpart = counterpart;
|
||||
|
||||
@@ -152,7 +152,7 @@ contract EnforcedTxGateway is OwnableUpgradeable, ReentrancyGuardUpgradeable, Pa
|
||||
|
||||
/// @notice Pause or unpause this contract.
|
||||
/// @param _status Pause this contract if it is true, otherwise unpause this contract.
|
||||
function setPause(bool _status) external onlyOwner {
|
||||
function setPaused(bool _status) external onlyOwner {
|
||||
if (_status) {
|
||||
_pause();
|
||||
} else {
|
||||
|
||||
@@ -2,17 +2,17 @@
|
||||
|
||||
pragma solidity ^0.8.16;
|
||||
|
||||
/// @title The interface for the ERC1155 cross chain gateway on layer 1.
|
||||
/// @title The interface for the ERC1155 cross chain gateway in layer 1.
|
||||
interface IL1ERC1155Gateway {
|
||||
/**********
|
||||
* Events *
|
||||
**********/
|
||||
|
||||
/// @notice Emitted when the ERC1155 NFT is transfered to recipient on layer 1.
|
||||
/// @param _l1Token The address of ERC1155 NFT on layer 1.
|
||||
/// @param _l2Token The address of ERC1155 NFT on layer 2.
|
||||
/// @param _from The address of sender on layer 2.
|
||||
/// @param _to The address of recipient on layer 1.
|
||||
/// @notice Emitted when the ERC1155 NFT is transfered to recipient in layer 1.
|
||||
/// @param _l1Token The address of ERC1155 NFT in layer 1.
|
||||
/// @param _l2Token The address of ERC1155 NFT in layer 2.
|
||||
/// @param _from The address of sender in layer 2.
|
||||
/// @param _to The address of recipient in layer 1.
|
||||
/// @param _tokenId The token id of the ERC1155 NFT to withdraw from layer 2.
|
||||
/// @param _amount The number of token to withdraw from layer 2.
|
||||
event FinalizeWithdrawERC1155(
|
||||
@@ -24,11 +24,11 @@ interface IL1ERC1155Gateway {
|
||||
uint256 _amount
|
||||
);
|
||||
|
||||
/// @notice Emitted when the ERC1155 NFT is batch transfered to recipient on layer 1.
|
||||
/// @param _l1Token The address of ERC1155 NFT on layer 1.
|
||||
/// @param _l2Token The address of ERC1155 NFT on layer 2.
|
||||
/// @param _from The address of sender on layer 2.
|
||||
/// @param _to The address of recipient on layer 1.
|
||||
/// @notice Emitted when the ERC1155 NFT is batch transfered to recipient in layer 1.
|
||||
/// @param _l1Token The address of ERC1155 NFT in layer 1.
|
||||
/// @param _l2Token The address of ERC1155 NFT in layer 2.
|
||||
/// @param _from The address of sender in layer 2.
|
||||
/// @param _to The address of recipient in layer 1.
|
||||
/// @param _tokenIds The list of token ids of the ERC1155 NFT to withdraw from layer 2.
|
||||
/// @param _amounts The list of corresponding number of token to withdraw from layer 2.
|
||||
event FinalizeBatchWithdrawERC1155(
|
||||
@@ -40,13 +40,13 @@ interface IL1ERC1155Gateway {
|
||||
uint256[] _amounts
|
||||
);
|
||||
|
||||
/// @notice Emitted when the ERC1155 NFT is deposited to gateway on layer 1.
|
||||
/// @param _l1Token The address of ERC1155 NFT on layer 1.
|
||||
/// @param _l2Token The address of ERC1155 NFT on layer 2.
|
||||
/// @param _from The address of sender on layer 1.
|
||||
/// @param _to The address of recipient on layer 2.
|
||||
/// @param _tokenId The token id of the ERC1155 NFT to deposit on layer 1.
|
||||
/// @param _amount The number of token to deposit on layer 1.
|
||||
/// @notice Emitted when the ERC1155 NFT is deposited to gateway in layer 1.
|
||||
/// @param _l1Token The address of ERC1155 NFT in layer 1.
|
||||
/// @param _l2Token The address of ERC1155 NFT in layer 2.
|
||||
/// @param _from The address of sender in layer 1.
|
||||
/// @param _to The address of recipient in layer 2.
|
||||
/// @param _tokenId The token id of the ERC1155 NFT to deposit in layer 1.
|
||||
/// @param _amount The number of token to deposit in layer 1.
|
||||
event DepositERC1155(
|
||||
address indexed _l1Token,
|
||||
address indexed _l2Token,
|
||||
@@ -56,13 +56,13 @@ interface IL1ERC1155Gateway {
|
||||
uint256 _amount
|
||||
);
|
||||
|
||||
/// @notice Emitted when the ERC1155 NFT is batch deposited to gateway on layer 1.
|
||||
/// @param _l1Token The address of ERC1155 NFT on layer 1.
|
||||
/// @param _l2Token The address of ERC1155 NFT on layer 2.
|
||||
/// @param _from The address of sender on layer 1.
|
||||
/// @param _to The address of recipient on layer 2.
|
||||
/// @param _tokenIds The list of token ids of the ERC1155 NFT to deposit on layer 1.
|
||||
/// @param _amounts The list of corresponding number of token to deposit on layer 1.
|
||||
/// @notice Emitted when the ERC1155 NFT is batch deposited to gateway in layer 1.
|
||||
/// @param _l1Token The address of ERC1155 NFT in layer 1.
|
||||
/// @param _l2Token The address of ERC1155 NFT in layer 2.
|
||||
/// @param _from The address of sender in layer 1.
|
||||
/// @param _to The address of recipient in layer 2.
|
||||
/// @param _tokenIds The list of token ids of the ERC1155 NFT to deposit in layer 1.
|
||||
/// @param _amounts The list of corresponding number of token to deposit in layer 1.
|
||||
event BatchDepositERC1155(
|
||||
address indexed _l1Token,
|
||||
address indexed _l2Token,
|
||||
@@ -91,7 +91,7 @@ interface IL1ERC1155Gateway {
|
||||
*************************/
|
||||
|
||||
/// @notice Deposit some ERC1155 NFT to caller's account on layer 2.
|
||||
/// @param _token The address of ERC1155 NFT on layer 1.
|
||||
/// @param _token The address of ERC1155 NFT in layer 1.
|
||||
/// @param _tokenId The token id to deposit.
|
||||
/// @param _amount The amount of token to deposit.
|
||||
/// @param _gasLimit Estimated gas limit required to complete the deposit on layer 2.
|
||||
@@ -103,8 +103,8 @@ interface IL1ERC1155Gateway {
|
||||
) external payable;
|
||||
|
||||
/// @notice Deposit some ERC1155 NFT to a recipient's account on layer 2.
|
||||
/// @param _token The address of ERC1155 NFT on layer 1.
|
||||
/// @param _to The address of recipient on layer 2.
|
||||
/// @param _token The address of ERC1155 NFT in layer 1.
|
||||
/// @param _to The address of recipient in layer 2.
|
||||
/// @param _tokenId The token id to deposit.
|
||||
/// @param _amount The amount of token to deposit.
|
||||
/// @param _gasLimit Estimated gas limit required to complete the deposit on layer 2.
|
||||
@@ -117,7 +117,7 @@ interface IL1ERC1155Gateway {
|
||||
) external payable;
|
||||
|
||||
/// @notice Deposit a list of some ERC1155 NFT to caller's account on layer 2.
|
||||
/// @param _token The address of ERC1155 NFT on layer 1.
|
||||
/// @param _token The address of ERC1155 NFT in layer 1.
|
||||
/// @param _tokenIds The list of token ids to deposit.
|
||||
/// @param _amounts The list of corresponding number of token to deposit.
|
||||
/// @param _gasLimit Estimated gas limit required to complete the deposit on layer 2.
|
||||
@@ -129,8 +129,8 @@ interface IL1ERC1155Gateway {
|
||||
) external payable;
|
||||
|
||||
/// @notice Deposit a list of some ERC1155 NFT to a recipient's account on layer 2.
|
||||
/// @param _token The address of ERC1155 NFT on layer 1.
|
||||
/// @param _to The address of recipient on layer 2.
|
||||
/// @param _token The address of ERC1155 NFT in layer 1.
|
||||
/// @param _to The address of recipient in layer 2.
|
||||
/// @param _tokenIds The list of token ids to deposit.
|
||||
/// @param _amounts The list of corresponding number of token to deposit.
|
||||
/// @param _gasLimit Estimated gas limit required to complete the deposit on layer 2.
|
||||
@@ -142,13 +142,13 @@ interface IL1ERC1155Gateway {
|
||||
uint256 _gasLimit
|
||||
) external payable;
|
||||
|
||||
/// @notice Complete ERC1155 withdraw from layer 2 to layer 1 and send fund to recipient's account on layer 1.
|
||||
/// @notice Complete ERC1155 withdraw from layer 2 to layer 1 and send fund to recipient's account in layer 1.
|
||||
/// The function should only be called by L1ScrollMessenger.
|
||||
/// The function should also only be called by L2ERC1155Gateway on layer 2.
|
||||
/// The function should also only be called by L2ERC1155Gateway in layer 2.
|
||||
/// @param _l1Token The address of corresponding layer 1 token.
|
||||
/// @param _l2Token The address of corresponding layer 2 token.
|
||||
/// @param _from The address of account who withdraw the token on layer 2.
|
||||
/// @param _to The address of recipient on layer 1 to receive the token.
|
||||
/// @param _from The address of account who withdraw the token in layer 2.
|
||||
/// @param _to The address of recipient in layer 1 to receive the token.
|
||||
/// @param _tokenId The token id to withdraw.
|
||||
/// @param _amount The amount of token to withdraw.
|
||||
function finalizeWithdrawERC1155(
|
||||
@@ -160,13 +160,13 @@ interface IL1ERC1155Gateway {
|
||||
uint256 _amount
|
||||
) external;
|
||||
|
||||
/// @notice Complete ERC1155 batch withdraw from layer 2 to layer 1 and send fund to recipient's account on layer 1.
|
||||
/// @notice Complete ERC1155 batch withdraw from layer 2 to layer 1 and send fund to recipient's account in layer 1.
|
||||
/// The function should only be called by L1ScrollMessenger.
|
||||
/// The function should also only be called by L2ERC1155Gateway on layer 2.
|
||||
/// The function should also only be called by L2ERC1155Gateway in layer 2.
|
||||
/// @param _l1Token The address of corresponding layer 1 token.
|
||||
/// @param _l2Token The address of corresponding layer 2 token.
|
||||
/// @param _from The address of account who withdraw the token on layer 2.
|
||||
/// @param _to The address of recipient on layer 1 to receive the token.
|
||||
/// @param _from The address of account who withdraw the token in layer 2.
|
||||
/// @param _to The address of recipient in layer 1 to receive the token.
|
||||
/// @param _tokenIds The list of token ids to withdraw.
|
||||
/// @param _amounts The list of corresponding number of token to withdraw.
|
||||
function finalizeBatchWithdrawERC1155(
|
||||
|
||||
@@ -2,17 +2,17 @@
|
||||
|
||||
pragma solidity ^0.8.16;
|
||||
|
||||
/// @title The interface for the ERC721 cross chain gateway on layer 1.
|
||||
/// @title The interface for the ERC721 cross chain gateway in layer 1.
|
||||
interface IL1ERC721Gateway {
|
||||
/**********
|
||||
* Events *
|
||||
**********/
|
||||
|
||||
/// @notice Emitted when the ERC721 NFT is transfered to recipient on layer 1.
|
||||
/// @param _l1Token The address of ERC721 NFT on layer 1.
|
||||
/// @param _l2Token The address of ERC721 NFT on layer 2.
|
||||
/// @param _from The address of sender on layer 2.
|
||||
/// @param _to The address of recipient on layer 1.
|
||||
/// @notice Emitted when the ERC721 NFT is transfered to recipient in layer 1.
|
||||
/// @param _l1Token The address of ERC721 NFT in layer 1.
|
||||
/// @param _l2Token The address of ERC721 NFT in layer 2.
|
||||
/// @param _from The address of sender in layer 2.
|
||||
/// @param _to The address of recipient in layer 1.
|
||||
/// @param _tokenId The token id of the ERC721 NFT to withdraw from layer 2.
|
||||
event FinalizeWithdrawERC721(
|
||||
address indexed _l1Token,
|
||||
@@ -22,11 +22,11 @@ interface IL1ERC721Gateway {
|
||||
uint256 _tokenId
|
||||
);
|
||||
|
||||
/// @notice Emitted when the ERC721 NFT is batch transfered to recipient on layer 1.
|
||||
/// @param _l1Token The address of ERC721 NFT on layer 1.
|
||||
/// @param _l2Token The address of ERC721 NFT on layer 2.
|
||||
/// @param _from The address of sender on layer 2.
|
||||
/// @param _to The address of recipient on layer 1.
|
||||
/// @notice Emitted when the ERC721 NFT is batch transfered to recipient in layer 1.
|
||||
/// @param _l1Token The address of ERC721 NFT in layer 1.
|
||||
/// @param _l2Token The address of ERC721 NFT in layer 2.
|
||||
/// @param _from The address of sender in layer 2.
|
||||
/// @param _to The address of recipient in layer 1.
|
||||
/// @param _tokenIds The list of token ids of the ERC721 NFT to withdraw from layer 2.
|
||||
event FinalizeBatchWithdrawERC721(
|
||||
address indexed _l1Token,
|
||||
@@ -36,12 +36,12 @@ interface IL1ERC721Gateway {
|
||||
uint256[] _tokenIds
|
||||
);
|
||||
|
||||
/// @notice Emitted when the ERC721 NFT is deposited to gateway on layer 1.
|
||||
/// @param _l1Token The address of ERC721 NFT on layer 1.
|
||||
/// @param _l2Token The address of ERC721 NFT on layer 2.
|
||||
/// @param _from The address of sender on layer 1.
|
||||
/// @param _to The address of recipient on layer 2.
|
||||
/// @param _tokenId The token id of the ERC721 NFT to deposit on layer 1.
|
||||
/// @notice Emitted when the ERC721 NFT is deposited to gateway in layer 1.
|
||||
/// @param _l1Token The address of ERC721 NFT in layer 1.
|
||||
/// @param _l2Token The address of ERC721 NFT in layer 2.
|
||||
/// @param _from The address of sender in layer 1.
|
||||
/// @param _to The address of recipient in layer 2.
|
||||
/// @param _tokenId The token id of the ERC721 NFT to deposit in layer 1.
|
||||
event DepositERC721(
|
||||
address indexed _l1Token,
|
||||
address indexed _l2Token,
|
||||
@@ -50,12 +50,12 @@ interface IL1ERC721Gateway {
|
||||
uint256 _tokenId
|
||||
);
|
||||
|
||||
/// @notice Emitted when the ERC721 NFT is batch deposited to gateway on layer 1.
|
||||
/// @param _l1Token The address of ERC721 NFT on layer 1.
|
||||
/// @param _l2Token The address of ERC721 NFT on layer 2.
|
||||
/// @param _from The address of sender on layer 1.
|
||||
/// @param _to The address of recipient on layer 2.
|
||||
/// @param _tokenIds The list of token ids of the ERC721 NFT to deposit on layer 1.
|
||||
/// @notice Emitted when the ERC721 NFT is batch deposited to gateway in layer 1.
|
||||
/// @param _l1Token The address of ERC721 NFT in layer 1.
|
||||
/// @param _l2Token The address of ERC721 NFT in layer 2.
|
||||
/// @param _from The address of sender in layer 1.
|
||||
/// @param _to The address of recipient in layer 2.
|
||||
/// @param _tokenIds The list of token ids of the ERC721 NFT to deposit in layer 1.
|
||||
event BatchDepositERC721(
|
||||
address indexed _l1Token,
|
||||
address indexed _l2Token,
|
||||
@@ -81,7 +81,7 @@ interface IL1ERC721Gateway {
|
||||
*****************************/
|
||||
|
||||
/// @notice Deposit some ERC721 NFT to caller's account on layer 2.
|
||||
/// @param _token The address of ERC721 NFT on layer 1.
|
||||
/// @param _token The address of ERC721 NFT in layer 1.
|
||||
/// @param _tokenId The token id to deposit.
|
||||
/// @param _gasLimit Estimated gas limit required to complete the deposit on layer 2.
|
||||
function depositERC721(
|
||||
@@ -91,8 +91,8 @@ interface IL1ERC721Gateway {
|
||||
) external payable;
|
||||
|
||||
/// @notice Deposit some ERC721 NFT to a recipient's account on layer 2.
|
||||
/// @param _token The address of ERC721 NFT on layer 1.
|
||||
/// @param _to The address of recipient on layer 2.
|
||||
/// @param _token The address of ERC721 NFT in layer 1.
|
||||
/// @param _to The address of recipient in layer 2.
|
||||
/// @param _tokenId The token id to deposit.
|
||||
/// @param _gasLimit Estimated gas limit required to complete the deposit on layer 2.
|
||||
function depositERC721(
|
||||
@@ -103,7 +103,7 @@ interface IL1ERC721Gateway {
|
||||
) external payable;
|
||||
|
||||
/// @notice Deposit a list of some ERC721 NFT to caller's account on layer 2.
|
||||
/// @param _token The address of ERC721 NFT on layer 1.
|
||||
/// @param _token The address of ERC721 NFT in layer 1.
|
||||
/// @param _tokenIds The list of token ids to deposit.
|
||||
/// @param _gasLimit Estimated gas limit required to complete the deposit on layer 2.
|
||||
function batchDepositERC721(
|
||||
@@ -113,8 +113,8 @@ interface IL1ERC721Gateway {
|
||||
) external payable;
|
||||
|
||||
/// @notice Deposit a list of some ERC721 NFT to a recipient's account on layer 2.
|
||||
/// @param _token The address of ERC721 NFT on layer 1.
|
||||
/// @param _to The address of recipient on layer 2.
|
||||
/// @param _token The address of ERC721 NFT in layer 1.
|
||||
/// @param _to The address of recipient in layer 2.
|
||||
/// @param _tokenIds The list of token ids to deposit.
|
||||
/// @param _gasLimit Estimated gas limit required to complete the deposit on layer 2.
|
||||
function batchDepositERC721(
|
||||
@@ -124,14 +124,14 @@ interface IL1ERC721Gateway {
|
||||
uint256 _gasLimit
|
||||
) external payable;
|
||||
|
||||
/// @notice Complete ERC721 withdraw from layer 2 to layer 1 and send NFT to recipient's account on layer 1.
|
||||
/// @notice Complete ERC721 withdraw from layer 2 to layer 1 and send NFT to recipient's account in layer 1.
|
||||
/// @dev Requirements:
|
||||
/// - The function should only be called by L1ScrollMessenger.
|
||||
/// - The function should also only be called by L2ERC721Gateway on layer 2.
|
||||
/// - The function should also only be called by L2ERC721Gateway in layer 2.
|
||||
/// @param _l1Token The address of corresponding layer 1 token.
|
||||
/// @param _l2Token The address of corresponding layer 2 token.
|
||||
/// @param _from The address of account who withdraw the token on layer 2.
|
||||
/// @param _to The address of recipient on layer 1 to receive the token.
|
||||
/// @param _from The address of account who withdraw the token in layer 2.
|
||||
/// @param _to The address of recipient in layer 1 to receive the token.
|
||||
/// @param _tokenId The token id to withdraw.
|
||||
function finalizeWithdrawERC721(
|
||||
address _l1Token,
|
||||
@@ -141,14 +141,14 @@ interface IL1ERC721Gateway {
|
||||
uint256 _tokenId
|
||||
) external;
|
||||
|
||||
/// @notice Complete ERC721 batch withdraw from layer 2 to layer 1 and send NFT to recipient's account on layer 1.
|
||||
/// @notice Complete ERC721 batch withdraw from layer 2 to layer 1 and send NFT to recipient's account in layer 1.
|
||||
/// @dev Requirements:
|
||||
/// - The function should only be called by L1ScrollMessenger.
|
||||
/// - The function should also only be called by L2ERC721Gateway on layer 2.
|
||||
/// - The function should also only be called by L2ERC721Gateway in layer 2.
|
||||
/// @param _l1Token The address of corresponding layer 1 token.
|
||||
/// @param _l2Token The address of corresponding layer 2 token.
|
||||
/// @param _from The address of account who withdraw the token on layer 2.
|
||||
/// @param _to The address of recipient on layer 1 to receive the token.
|
||||
/// @param _from The address of account who withdraw the token in layer 2.
|
||||
/// @param _to The address of recipient in layer 1 to receive the token.
|
||||
/// @param _tokenIds The list of token ids to withdraw.
|
||||
function finalizeBatchWithdrawERC721(
|
||||
address _l1Token,
|
||||
|
||||
@@ -14,7 +14,7 @@ import {ScrollGatewayBase} from "../../libraries/gateway/ScrollGatewayBase.sol";
|
||||
import {L1ERC20Gateway} from "./L1ERC20Gateway.sol";
|
||||
|
||||
/// @title L1CustomERC20Gateway
|
||||
/// @notice The `L1CustomERC20Gateway` is used to deposit custom ERC20 compatible tokens on layer 1 and
|
||||
/// @notice The `L1CustomERC20Gateway` is used to deposit custom ERC20 compatible tokens in layer 1 and
|
||||
/// finalize withdraw the tokens from layer 2.
|
||||
/// @dev The deposited tokens are held in this gateway. On finalizing withdraw, the corresponding
|
||||
/// tokens will be transfer to the recipient directly.
|
||||
@@ -26,8 +26,8 @@ contract L1CustomERC20Gateway is OwnableUpgradeable, ScrollGatewayBase, L1ERC20G
|
||||
**********/
|
||||
|
||||
/// @notice Emitted when token mapping for ERC20 token is updated.
|
||||
/// @param _l1Token The address of ERC20 token on layer 1.
|
||||
/// @param _l2Token The address of corresponding ERC20 token on layer 2.
|
||||
/// @param _l1Token The address of ERC20 token in layer 1.
|
||||
/// @param _l2Token The address of corresponding ERC20 token in layer 2.
|
||||
event UpdateTokenMapping(address _l1Token, address _l2Token);
|
||||
|
||||
/*************
|
||||
@@ -74,8 +74,8 @@ contract L1CustomERC20Gateway is OwnableUpgradeable, ScrollGatewayBase, L1ERC20G
|
||||
************************/
|
||||
|
||||
/// @notice Update layer 1 to layer 2 token mapping.
|
||||
/// @param _l1Token The address of ERC20 token on layer 1.
|
||||
/// @param _l2Token The address of corresponding ERC20 token on layer 2.
|
||||
/// @param _l1Token The address of ERC20 token in layer 1.
|
||||
/// @param _l2Token The address of corresponding ERC20 token in layer 2.
|
||||
function updateTokenMapping(address _l1Token, address _l2Token) external onlyOwner {
|
||||
require(_l2Token != address(0), "token address cannot be 0");
|
||||
|
||||
@@ -127,9 +127,14 @@ contract L1CustomERC20Gateway is OwnableUpgradeable, ScrollGatewayBase, L1ERC20G
|
||||
(_from, _amount, _data) = _transferERC20In(_token, _amount, _data);
|
||||
|
||||
// 2. Generate message passed to L2CustomERC20Gateway.
|
||||
bytes memory _message = abi.encodeCall(
|
||||
IL2ERC20Gateway.finalizeDepositERC20,
|
||||
(_token, _l2Token, _from, _to, _amount, _data)
|
||||
bytes memory _message = abi.encodeWithSelector(
|
||||
IL2ERC20Gateway.finalizeDepositERC20.selector,
|
||||
_token,
|
||||
_l2Token,
|
||||
_from,
|
||||
_to,
|
||||
_amount,
|
||||
_data
|
||||
);
|
||||
|
||||
// 3. Send message to L1ScrollMessenger.
|
||||
|
||||
@@ -14,7 +14,7 @@ import {IMessageDropCallback} from "../../libraries/callbacks/IMessageDropCallba
|
||||
import {ScrollGatewayBase} from "../../libraries/gateway/ScrollGatewayBase.sol";
|
||||
|
||||
/// @title L1ERC1155Gateway
|
||||
/// @notice The `L1ERC1155Gateway` is used to deposit ERC1155 compatible NFT on layer 1 and
|
||||
/// @notice The `L1ERC1155Gateway` is used to deposit ERC1155 compatible NFT in layer 1 and
|
||||
/// finalize withdraw the NFTs from layer 2.
|
||||
/// @dev The deposited NFTs are held in this gateway. On finalizing withdraw, the corresponding
|
||||
/// NFT will be transfer to the recipient directly.
|
||||
@@ -32,8 +32,8 @@ contract L1ERC1155Gateway is
|
||||
**********/
|
||||
|
||||
/// @notice Emitted when token mapping for ERC1155 token is updated.
|
||||
/// @param _l1Token The address of ERC1155 token on layer 1.
|
||||
/// @param _l2Token The address of corresponding ERC1155 token on layer 2.
|
||||
/// @param _l1Token The address of ERC1155 token in layer 1.
|
||||
/// @param _l1Token The address of corresponding ERC1155 token in layer 2.
|
||||
event UpdateTokenMapping(address _l1Token, address _l2Token);
|
||||
|
||||
/*************
|
||||
@@ -172,8 +172,8 @@ contract L1ERC1155Gateway is
|
||||
************************/
|
||||
|
||||
/// @notice Update layer 2 to layer 2 token mapping.
|
||||
/// @param _l1Token The address of ERC1155 token on layer 1.
|
||||
/// @param _l2Token The address of corresponding ERC1155 token on layer 2.
|
||||
/// @param _l1Token The address of ERC1155 token in layer 1.
|
||||
/// @param _l1Token The address of corresponding ERC1155 token in layer 2.
|
||||
function updateTokenMapping(address _l1Token, address _l2Token) external onlyOwner {
|
||||
require(_l2Token != address(0), "token address cannot be 0");
|
||||
|
||||
@@ -187,8 +187,8 @@ contract L1ERC1155Gateway is
|
||||
**********************/
|
||||
|
||||
/// @dev Internal function to deposit ERC1155 NFT to layer 2.
|
||||
/// @param _token The address of ERC1155 NFT on layer 1.
|
||||
/// @param _to The address of recipient on layer 2.
|
||||
/// @param _token The address of ERC1155 NFT in layer 1.
|
||||
/// @param _to The address of recipient in layer 2.
|
||||
/// @param _tokenId The token id to deposit.
|
||||
/// @param _amount The amount of token to deposit.
|
||||
/// @param _gasLimit Estimated gas limit required to complete the deposit on layer 2.
|
||||
@@ -208,9 +208,14 @@ contract L1ERC1155Gateway is
|
||||
IERC1155Upgradeable(_token).safeTransferFrom(msg.sender, address(this), _tokenId, _amount, "");
|
||||
|
||||
// 2. Generate message passed to L2ERC1155Gateway.
|
||||
bytes memory _message = abi.encodeCall(
|
||||
IL2ERC1155Gateway.finalizeDepositERC1155,
|
||||
(_token, _l2Token, msg.sender, _to, _tokenId, _amount)
|
||||
bytes memory _message = abi.encodeWithSelector(
|
||||
IL2ERC1155Gateway.finalizeDepositERC1155.selector,
|
||||
_token,
|
||||
_l2Token,
|
||||
msg.sender,
|
||||
_to,
|
||||
_tokenId,
|
||||
_amount
|
||||
);
|
||||
|
||||
// 3. Send message to L1ScrollMessenger.
|
||||
@@ -220,8 +225,8 @@ contract L1ERC1155Gateway is
|
||||
}
|
||||
|
||||
/// @dev Internal function to batch deposit ERC1155 NFT to layer 2.
|
||||
/// @param _token The address of ERC1155 NFT on layer 1.
|
||||
/// @param _to The address of recipient on layer 2.
|
||||
/// @param _token The address of ERC1155 NFT in layer 1.
|
||||
/// @param _to The address of recipient in layer 2.
|
||||
/// @param _tokenIds The list of token ids to deposit.
|
||||
/// @param _amounts The list of corresponding number of token to deposit.
|
||||
/// @param _gasLimit Estimated gas limit required to complete the deposit on layer 2.
|
||||
@@ -246,9 +251,14 @@ contract L1ERC1155Gateway is
|
||||
IERC1155Upgradeable(_token).safeBatchTransferFrom(msg.sender, address(this), _tokenIds, _amounts, "");
|
||||
|
||||
// 2. Generate message passed to L2ERC1155Gateway.
|
||||
bytes memory _message = abi.encodeCall(
|
||||
IL2ERC1155Gateway.finalizeBatchDepositERC1155,
|
||||
(_token, _l2Token, msg.sender, _to, _tokenIds, _amounts)
|
||||
bytes memory _message = abi.encodeWithSelector(
|
||||
IL2ERC1155Gateway.finalizeBatchDepositERC1155.selector,
|
||||
_token,
|
||||
_l2Token,
|
||||
msg.sender,
|
||||
_to,
|
||||
_tokenIds,
|
||||
_amounts
|
||||
);
|
||||
|
||||
// 3. Send message to L1ScrollMessenger.
|
||||
|
||||
@@ -14,7 +14,7 @@ import {IMessageDropCallback} from "../../libraries/callbacks/IMessageDropCallba
|
||||
import {ScrollGatewayBase} from "../../libraries/gateway/ScrollGatewayBase.sol";
|
||||
|
||||
/// @title L1ERC721Gateway
|
||||
/// @notice The `L1ERC721Gateway` is used to deposit ERC721 compatible NFT on layer 1 and
|
||||
/// @notice The `L1ERC721Gateway` is used to deposit ERC721 compatible NFT in layer 1 and
|
||||
/// finalize withdraw the NFTs from layer 2.
|
||||
/// @dev The deposited NFTs are held in this gateway. On finalizing withdraw, the corresponding
|
||||
/// NFT will be transfer to the recipient directly.
|
||||
@@ -32,8 +32,8 @@ contract L1ERC721Gateway is
|
||||
**********/
|
||||
|
||||
/// @notice Emitted when token mapping for ERC721 token is updated.
|
||||
/// @param _l1Token The address of ERC721 token on layer 1.
|
||||
/// @param _l2Token The address of corresponding ERC721 token on layer 2.
|
||||
/// @param _l1Token The address of ERC721 token in layer 1.
|
||||
/// @param _l1Token The address of corresponding ERC721 token in layer 2.
|
||||
event UpdateTokenMapping(address _l1Token, address _l2Token);
|
||||
|
||||
/*************
|
||||
@@ -168,8 +168,8 @@ contract L1ERC721Gateway is
|
||||
************************/
|
||||
|
||||
/// @notice Update layer 2 to layer 2 token mapping.
|
||||
/// @param _l1Token The address of ERC721 token on layer 1.
|
||||
/// @param _l2Token The address of corresponding ERC721 token on layer 2.
|
||||
/// @param _l1Token The address of ERC721 token in layer 1.
|
||||
/// @param _l1Token The address of corresponding ERC721 token in layer 2.
|
||||
function updateTokenMapping(address _l1Token, address _l2Token) external onlyOwner {
|
||||
require(_l2Token != address(0), "token address cannot be 0");
|
||||
|
||||
@@ -183,8 +183,8 @@ contract L1ERC721Gateway is
|
||||
**********************/
|
||||
|
||||
/// @dev Internal function to deposit ERC721 NFT to layer 2.
|
||||
/// @param _token The address of ERC721 NFT on layer 1.
|
||||
/// @param _to The address of recipient on layer 2.
|
||||
/// @param _token The address of ERC721 NFT in layer 1.
|
||||
/// @param _to The address of recipient in layer 2.
|
||||
/// @param _tokenId The token id to deposit.
|
||||
/// @param _gasLimit Estimated gas limit required to complete the deposit on layer 2.
|
||||
function _depositERC721(
|
||||
@@ -200,9 +200,13 @@ contract L1ERC721Gateway is
|
||||
IERC721Upgradeable(_token).safeTransferFrom(msg.sender, address(this), _tokenId);
|
||||
|
||||
// 2. Generate message passed to L2ERC721Gateway.
|
||||
bytes memory _message = abi.encodeCall(
|
||||
IL2ERC721Gateway.finalizeDepositERC721,
|
||||
(_token, _l2Token, msg.sender, _to, _tokenId)
|
||||
bytes memory _message = abi.encodeWithSelector(
|
||||
IL2ERC721Gateway.finalizeDepositERC721.selector,
|
||||
_token,
|
||||
_l2Token,
|
||||
msg.sender,
|
||||
_to,
|
||||
_tokenId
|
||||
);
|
||||
|
||||
// 3. Send message to L1ScrollMessenger.
|
||||
@@ -212,8 +216,8 @@ contract L1ERC721Gateway is
|
||||
}
|
||||
|
||||
/// @dev Internal function to batch deposit ERC721 NFT to layer 2.
|
||||
/// @param _token The address of ERC721 NFT on layer 1.
|
||||
/// @param _to The address of recipient on layer 2.
|
||||
/// @param _token The address of ERC721 NFT in layer 1.
|
||||
/// @param _to The address of recipient in layer 2.
|
||||
/// @param _tokenIds The list of token ids to deposit.
|
||||
/// @param _gasLimit Estimated gas limit required to complete the deposit on layer 2.
|
||||
function _batchDepositERC721(
|
||||
@@ -233,9 +237,13 @@ contract L1ERC721Gateway is
|
||||
}
|
||||
|
||||
// 2. Generate message passed to L2ERC721Gateway.
|
||||
bytes memory _message = abi.encodeCall(
|
||||
IL2ERC721Gateway.finalizeBatchDepositERC721,
|
||||
(_token, _l2Token, msg.sender, _to, _tokenIds)
|
||||
bytes memory _message = abi.encodeWithSelector(
|
||||
IL2ERC721Gateway.finalizeBatchDepositERC721.selector,
|
||||
_token,
|
||||
_l2Token,
|
||||
msg.sender,
|
||||
_to,
|
||||
_tokenIds
|
||||
);
|
||||
|
||||
// 3. Send message to L1ScrollMessenger.
|
||||
|
||||
@@ -14,7 +14,7 @@ import {ScrollGatewayBase} from "../../libraries/gateway/ScrollGatewayBase.sol";
|
||||
// solhint-disable avoid-low-level-calls
|
||||
|
||||
/// @title L1ETHGateway
|
||||
/// @notice The `L1ETHGateway` is used to deposit ETH on layer 1 and
|
||||
/// @notice The `L1ETHGateway` is used to deposit ETH in layer 1 and
|
||||
/// finalize withdraw ETH from layer 2.
|
||||
/// @dev The deposited ETH tokens are held in this gateway. On finalizing withdraw, the corresponding
|
||||
/// ETH will be transfer to the recipient directly.
|
||||
@@ -127,7 +127,13 @@ contract L1ETHGateway is Initializable, ScrollGatewayBase, IL1ETHGateway, IMessa
|
||||
}
|
||||
|
||||
// 2. Generate message passed to L1ScrollMessenger.
|
||||
bytes memory _message = abi.encodeCall(IL2ETHGateway.finalizeDepositETH, (_from, _to, _amount, _data));
|
||||
bytes memory _message = abi.encodeWithSelector(
|
||||
IL2ETHGateway.finalizeDepositETH.selector,
|
||||
_from,
|
||||
_to,
|
||||
_amount,
|
||||
_data
|
||||
);
|
||||
|
||||
IL1ScrollMessenger(messenger).sendMessage{value: msg.value}(counterpart, _amount, _message, _gasLimit, _from);
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ import {ScrollGatewayBase} from "../../libraries/gateway/ScrollGatewayBase.sol";
|
||||
import {L1ERC20Gateway} from "./L1ERC20Gateway.sol";
|
||||
|
||||
/// @title L1StandardERC20Gateway
|
||||
/// @notice The `L1StandardERC20Gateway` is used to deposit standard ERC20 tokens on layer 1 and
|
||||
/// @notice The `L1StandardERC20Gateway` is used to deposit standard ERC20 tokens in layer 1 and
|
||||
/// finalize withdraw the tokens from layer 2.
|
||||
/// @dev The deposited ERC20 tokens are held in this gateway. On finalizing withdraw, the corresponding
|
||||
/// token will be transfer to the recipient directly. Any ERC20 that requires non-standard functionality
|
||||
@@ -150,9 +150,14 @@ contract L1StandardERC20Gateway is Initializable, ScrollGatewayBase, L1ERC20Gate
|
||||
} else {
|
||||
_l2Data = abi.encode(false, _data);
|
||||
}
|
||||
bytes memory _message = abi.encodeCall(
|
||||
IL2ERC20Gateway.finalizeDepositERC20,
|
||||
(_token, _l2Token, _from, _to, _amount, _l2Data)
|
||||
bytes memory _message = abi.encodeWithSelector(
|
||||
IL2ERC20Gateway.finalizeDepositERC20.selector,
|
||||
_token,
|
||||
_l2Token,
|
||||
_from,
|
||||
_to,
|
||||
_amount,
|
||||
_l2Data
|
||||
);
|
||||
|
||||
// 3. Send message to L1ScrollMessenger.
|
||||
|
||||
@@ -15,7 +15,7 @@ import {ScrollGatewayBase} from "../../libraries/gateway/ScrollGatewayBase.sol";
|
||||
import {L1ERC20Gateway} from "./L1ERC20Gateway.sol";
|
||||
|
||||
/// @title L1WETHGateway
|
||||
/// @notice The `L1WETHGateway` contract is used to deposit `WETH` token on layer 1 and
|
||||
/// @notice The `L1WETHGateway` contract is used to deposit `WETH` token in layer 1 and
|
||||
/// finalize withdraw `WETH` from layer 2.
|
||||
/// @dev The deposited WETH tokens are not held in the gateway. It will first be unwrapped
|
||||
/// as Ether and then the Ether will be sent to the `L1ScrollMessenger` contract.
|
||||
@@ -121,9 +121,14 @@ contract L1WETHGateway is Initializable, ScrollGatewayBase, L1ERC20Gateway {
|
||||
IWETH(_token).withdraw(_amount);
|
||||
|
||||
// 2. Generate message passed to L2WETHGateway.
|
||||
bytes memory _message = abi.encodeCall(
|
||||
IL2ERC20Gateway.finalizeDepositERC20,
|
||||
(_token, l2WETH, _from, _to, _amount, _data)
|
||||
bytes memory _message = abi.encodeWithSelector(
|
||||
IL2ERC20Gateway.finalizeDepositERC20.selector,
|
||||
_token,
|
||||
l2WETH,
|
||||
_from,
|
||||
_to,
|
||||
_amount,
|
||||
_data
|
||||
);
|
||||
|
||||
// 3. Send message to L1ScrollMessenger.
|
||||
|
||||
@@ -20,8 +20,8 @@ interface IScrollChain {
|
||||
/// @notice Emitted when a batch is finalized.
|
||||
/// @param batchIndex The index of the batch.
|
||||
/// @param batchHash The hash of the batch
|
||||
/// @param stateRoot The state root on layer 2 after this batch.
|
||||
/// @param withdrawRoot The merkle root on layer2 after this batch.
|
||||
/// @param stateRoot The state root in layer 2 after this batch.
|
||||
/// @param withdrawRoot The merkle root in layer2 after this batch.
|
||||
event FinalizeBatch(uint256 indexed batchIndex, bytes32 indexed batchHash, bytes32 stateRoot, bytes32 withdrawRoot);
|
||||
|
||||
/*************************
|
||||
|
||||
@@ -15,8 +15,6 @@ import {AddressAliasHelper} from "../libraries/common/AddressAliasHelper.sol";
|
||||
import {IScrollMessenger} from "../libraries/IScrollMessenger.sol";
|
||||
import {ScrollMessengerBase} from "../libraries/ScrollMessengerBase.sol";
|
||||
|
||||
// solhint-disable reason-string
|
||||
|
||||
/// @title L2ScrollMessenger
|
||||
/// @notice The `L2ScrollMessenger` contract can:
|
||||
///
|
||||
@@ -24,7 +22,7 @@ import {ScrollMessengerBase} from "../libraries/ScrollMessengerBase.sol";
|
||||
/// 2. relay messages from layer 1 layer 2;
|
||||
/// 3. drop expired message due to sequencer problems.
|
||||
///
|
||||
/// @dev It should be a predeployed contract on layer 2 and should hold infinite amount
|
||||
/// @dev It should be a predeployed contract in layer 2 and should hold infinite amount
|
||||
/// of Ether (Specifically, `uint256(-1)`), which can be initialized in Genesis Block.
|
||||
contract L2ScrollMessenger is ScrollMessengerBase, PausableUpgradeable, IL2ScrollMessenger {
|
||||
/**********
|
||||
@@ -78,7 +76,6 @@ contract L2ScrollMessenger is ScrollMessengerBase, PausableUpgradeable, IL2Scrol
|
||||
/*****************************
|
||||
* Public Mutating Functions *
|
||||
*****************************/
|
||||
|
||||
/// @inheritdoc IScrollMessenger
|
||||
function sendMessage(
|
||||
address _to,
|
||||
@@ -137,8 +134,6 @@ contract L2ScrollMessenger is ScrollMessengerBase, PausableUpgradeable, IL2Scrol
|
||||
/// @dev This function can only called by contract owner.
|
||||
/// @param _maxFailedExecutionTimes The new max failed execution times.
|
||||
function updateMaxFailedExecutionTimes(uint256 _maxFailedExecutionTimes) external onlyOwner {
|
||||
require(_maxFailedExecutionTimes > 0, "maxFailedExecutionTimes cannot be zero");
|
||||
|
||||
maxFailedExecutionTimes = _maxFailedExecutionTimes;
|
||||
|
||||
emit UpdateMaxFailedExecutionTimes(_maxFailedExecutionTimes);
|
||||
|
||||
@@ -2,18 +2,18 @@
|
||||
|
||||
pragma solidity ^0.8.16;
|
||||
|
||||
/// @title The interface for the ERC1155 cross chain gateway on layer 2.
|
||||
/// @title The interface for the ERC1155 cross chain gateway in layer 2.
|
||||
interface IL2ERC1155Gateway {
|
||||
/**********
|
||||
* Events *
|
||||
**********/
|
||||
|
||||
/// @notice Emitted when the ERC1155 NFT is transfered to recipient on layer 2.
|
||||
/// @param l1Token The address of ERC1155 NFT on layer 1.
|
||||
/// @param l2Token The address of ERC1155 NFT on layer 2.
|
||||
/// @param from The address of sender on layer 1.
|
||||
/// @param to The address of recipient on layer 2.
|
||||
/// @param tokenId The token id of the ERC1155 NFT deposited on layer 1.
|
||||
/// @notice Emitted when the ERC1155 NFT is transfered to recipient in layer 2.
|
||||
/// @param l1Token The address of ERC1155 NFT in layer 1.
|
||||
/// @param l2Token The address of ERC1155 NFT in layer 2.
|
||||
/// @param from The address of sender in layer 1.
|
||||
/// @param to The address of recipient in layer 2.
|
||||
/// @param tokenId The token id of the ERC1155 NFT deposited in layer 1.
|
||||
/// @param amount The amount of token deposited.
|
||||
event FinalizeDepositERC1155(
|
||||
address indexed l1Token,
|
||||
@@ -24,12 +24,12 @@ interface IL2ERC1155Gateway {
|
||||
uint256 amount
|
||||
);
|
||||
|
||||
/// @notice Emitted when the ERC1155 NFT is batch transfered to recipient on layer 2.
|
||||
/// @param l1Token The address of ERC1155 NFT on layer 1.
|
||||
/// @param l2Token The address of ERC1155 NFT on layer 2.
|
||||
/// @param from The address of sender on layer 1.
|
||||
/// @param to The address of recipient on layer 2.
|
||||
/// @param tokenIds The list of token ids of the ERC1155 NFT deposited on layer 1.
|
||||
/// @notice Emitted when the ERC1155 NFT is batch transfered to recipient in layer 2.
|
||||
/// @param l1Token The address of ERC1155 NFT in layer 1.
|
||||
/// @param l2Token The address of ERC1155 NFT in layer 2.
|
||||
/// @param from The address of sender in layer 1.
|
||||
/// @param to The address of recipient in layer 2.
|
||||
/// @param tokenIds The list of token ids of the ERC1155 NFT deposited in layer 1.
|
||||
/// @param amounts The list of corresponding amounts deposited.
|
||||
event FinalizeBatchDepositERC1155(
|
||||
address indexed l1Token,
|
||||
@@ -40,12 +40,12 @@ interface IL2ERC1155Gateway {
|
||||
uint256[] amounts
|
||||
);
|
||||
|
||||
/// @notice Emitted when the ERC1155 NFT is transfered to gateway on layer 2.
|
||||
/// @param l1Token The address of ERC1155 NFT on layer 1.
|
||||
/// @param l2Token The address of ERC1155 NFT on layer 2.
|
||||
/// @param from The address of sender on layer 2.
|
||||
/// @param to The address of recipient on layer 1.
|
||||
/// @param tokenId The token id of the ERC1155 NFT to withdraw on layer 2.
|
||||
/// @notice Emitted when the ERC1155 NFT is transfered to gateway in layer 2.
|
||||
/// @param l1Token The address of ERC1155 NFT in layer 1.
|
||||
/// @param l2Token The address of ERC1155 NFT in layer 2.
|
||||
/// @param from The address of sender in layer 2.
|
||||
/// @param to The address of recipient in layer 1.
|
||||
/// @param tokenId The token id of the ERC1155 NFT to withdraw in layer 2.
|
||||
/// @param amount The amount of token to withdraw.
|
||||
event WithdrawERC1155(
|
||||
address indexed l1Token,
|
||||
@@ -56,12 +56,12 @@ interface IL2ERC1155Gateway {
|
||||
uint256 amount
|
||||
);
|
||||
|
||||
/// @notice Emitted when the ERC1155 NFT is batch transfered to gateway on layer 2.
|
||||
/// @param l1Token The address of ERC1155 NFT on layer 1.
|
||||
/// @param l2Token The address of ERC1155 NFT on layer 2.
|
||||
/// @param from The address of sender on layer 2.
|
||||
/// @param to The address of recipient on layer 1.
|
||||
/// @param tokenIds The list of token ids of the ERC1155 NFT to withdraw on layer 2.
|
||||
/// @notice Emitted when the ERC1155 NFT is batch transfered to gateway in layer 2.
|
||||
/// @param l1Token The address of ERC1155 NFT in layer 1.
|
||||
/// @param l2Token The address of ERC1155 NFT in layer 2.
|
||||
/// @param from The address of sender in layer 2.
|
||||
/// @param to The address of recipient in layer 1.
|
||||
/// @param tokenIds The list of token ids of the ERC1155 NFT to withdraw in layer 2.
|
||||
/// @param amounts The list of corresponding amounts to withdraw.
|
||||
event BatchWithdrawERC1155(
|
||||
address indexed l1Token,
|
||||
@@ -77,7 +77,7 @@ interface IL2ERC1155Gateway {
|
||||
*****************************/
|
||||
|
||||
/// @notice Withdraw some ERC1155 NFT to caller's account on layer 1.
|
||||
/// @param token The address of ERC1155 NFT on layer 2.
|
||||
/// @param token The address of ERC1155 NFT in layer 2.
|
||||
/// @param tokenId The token id to withdraw.
|
||||
/// @param amount The amount of token to withdraw.
|
||||
/// @param gasLimit Unused, but included for potential forward compatibility considerations.
|
||||
@@ -89,8 +89,8 @@ interface IL2ERC1155Gateway {
|
||||
) external payable;
|
||||
|
||||
/// @notice Withdraw some ERC1155 NFT to caller's account on layer 1.
|
||||
/// @param token The address of ERC1155 NFT on layer 2.
|
||||
/// @param to The address of recipient on layer 1.
|
||||
/// @param token The address of ERC1155 NFT in layer 2.
|
||||
/// @param to The address of recipient in layer 1.
|
||||
/// @param tokenId The token id to withdraw.
|
||||
/// @param amount The amount of token to withdraw.
|
||||
/// @param gasLimit Unused, but included for potential forward compatibility considerations.
|
||||
@@ -103,7 +103,7 @@ interface IL2ERC1155Gateway {
|
||||
) external payable;
|
||||
|
||||
/// @notice Batch withdraw a list of ERC1155 NFT to caller's account on layer 1.
|
||||
/// @param token The address of ERC1155 NFT on layer 2.
|
||||
/// @param token The address of ERC1155 NFT in layer 2.
|
||||
/// @param tokenIds The list of token ids to withdraw.
|
||||
/// @param amounts The list of corresponding amounts to withdraw.
|
||||
/// @param gasLimit Unused, but included for potential forward compatibility considerations.
|
||||
@@ -115,8 +115,8 @@ interface IL2ERC1155Gateway {
|
||||
) external payable;
|
||||
|
||||
/// @notice Batch withdraw a list of ERC1155 NFT to caller's account on layer 1.
|
||||
/// @param token The address of ERC1155 NFT on layer 2.
|
||||
/// @param to The address of recipient on layer 1.
|
||||
/// @param token The address of ERC1155 NFT in layer 2.
|
||||
/// @param to The address of recipient in layer 1.
|
||||
/// @param tokenIds The list of token ids to withdraw.
|
||||
/// @param amounts The list of corresponding amounts to withdraw.
|
||||
/// @param gasLimit Unused, but included for potential forward compatibility considerations.
|
||||
@@ -128,14 +128,14 @@ interface IL2ERC1155Gateway {
|
||||
uint256 gasLimit
|
||||
) external payable;
|
||||
|
||||
/// @notice Complete ERC1155 deposit from layer 1 to layer 2 and send NFT to recipient's account on layer 2.
|
||||
/// @notice Complete ERC1155 deposit from layer 1 to layer 2 and send NFT to recipient's account in layer 2.
|
||||
/// @dev Requirements:
|
||||
/// - The function should only be called by L2ScrollMessenger.
|
||||
/// - The function should also only be called by L1ERC1155Gateway on layer 1.
|
||||
/// - The function should also only be called by L1ERC1155Gateway in layer 1.
|
||||
/// @param l1Token The address of corresponding layer 1 token.
|
||||
/// @param l2Token The address of corresponding layer 2 token.
|
||||
/// @param from The address of account who deposits the token on layer 1.
|
||||
/// @param to The address of recipient on layer 2 to receive the token.
|
||||
/// @param from The address of account who deposits the token in layer 1.
|
||||
/// @param to The address of recipient in layer 2 to receive the token.
|
||||
/// @param tokenId The token id to deposit.
|
||||
/// @param amount The amount of token to deposit.
|
||||
function finalizeDepositERC1155(
|
||||
@@ -147,14 +147,14 @@ interface IL2ERC1155Gateway {
|
||||
uint256 amount
|
||||
) external;
|
||||
|
||||
/// @notice Complete ERC1155 deposit from layer 1 to layer 2 and send NFT to recipient's account on layer 2.
|
||||
/// @notice Complete ERC1155 deposit from layer 1 to layer 2 and send NFT to recipient's account in layer 2.
|
||||
/// @dev Requirements:
|
||||
/// - The function should only be called by L2ScrollMessenger.
|
||||
/// - The function should also only be called by L1ERC1155Gateway on layer 1.
|
||||
/// - The function should also only be called by L1ERC1155Gateway in layer 1.
|
||||
/// @param l1Token The address of corresponding layer 1 token.
|
||||
/// @param l2Token The address of corresponding layer 2 token.
|
||||
/// @param from The address of account who deposits the token on layer 1.
|
||||
/// @param to The address of recipient on layer 2 to receive the token.
|
||||
/// @param from The address of account who deposits the token in layer 1.
|
||||
/// @param to The address of recipient in layer 2 to receive the token.
|
||||
/// @param tokenIds The list of token ids to deposit.
|
||||
/// @param amounts The list of corresponding amounts to deposit.
|
||||
function finalizeBatchDepositERC1155(
|
||||
|
||||
@@ -2,18 +2,18 @@
|
||||
|
||||
pragma solidity ^0.8.16;
|
||||
|
||||
/// @title The interface for the ERC721 cross chain gateway on layer 2.
|
||||
/// @title The interface for the ERC721 cross chain gateway in layer 2.
|
||||
interface IL2ERC721Gateway {
|
||||
/**********
|
||||
* Events *
|
||||
**********/
|
||||
|
||||
/// @notice Emitted when the ERC721 NFT is transfered to recipient on layer 2.
|
||||
/// @param l1Token The address of ERC721 NFT on layer 1.
|
||||
/// @param l2Token The address of ERC721 NFT on layer 2.
|
||||
/// @param from The address of sender on layer 1.
|
||||
/// @param to The address of recipient on layer 2.
|
||||
/// @param tokenId The token id of the ERC721 NFT deposited on layer 1.
|
||||
/// @notice Emitted when the ERC721 NFT is transfered to recipient in layer 2.
|
||||
/// @param l1Token The address of ERC721 NFT in layer 1.
|
||||
/// @param l2Token The address of ERC721 NFT in layer 2.
|
||||
/// @param from The address of sender in layer 1.
|
||||
/// @param to The address of recipient in layer 2.
|
||||
/// @param tokenId The token id of the ERC721 NFT deposited in layer 1.
|
||||
event FinalizeDepositERC721(
|
||||
address indexed l1Token,
|
||||
address indexed l2Token,
|
||||
@@ -22,12 +22,12 @@ interface IL2ERC721Gateway {
|
||||
uint256 tokenId
|
||||
);
|
||||
|
||||
/// @notice Emitted when the ERC721 NFT is batch transfered to recipient on layer 2.
|
||||
/// @param l1Token The address of ERC721 NFT on layer 1.
|
||||
/// @param l2Token The address of ERC721 NFT on layer 2.
|
||||
/// @param from The address of sender on layer 1.
|
||||
/// @param to The address of recipient on layer 2.
|
||||
/// @param tokenIds The list of token ids of the ERC721 NFT deposited on layer 1.
|
||||
/// @notice Emitted when the ERC721 NFT is batch transfered to recipient in layer 2.
|
||||
/// @param l1Token The address of ERC721 NFT in layer 1.
|
||||
/// @param l2Token The address of ERC721 NFT in layer 2.
|
||||
/// @param from The address of sender in layer 1.
|
||||
/// @param to The address of recipient in layer 2.
|
||||
/// @param tokenIds The list of token ids of the ERC721 NFT deposited in layer 1.
|
||||
event FinalizeBatchDepositERC721(
|
||||
address indexed l1Token,
|
||||
address indexed l2Token,
|
||||
@@ -36,12 +36,12 @@ interface IL2ERC721Gateway {
|
||||
uint256[] tokenIds
|
||||
);
|
||||
|
||||
/// @notice Emitted when the ERC721 NFT is transfered to gateway on layer 2.
|
||||
/// @param l1Token The address of ERC721 NFT on layer 1.
|
||||
/// @param l2Token The address of ERC721 NFT on layer 2.
|
||||
/// @param from The address of sender on layer 2.
|
||||
/// @param to The address of recipient on layer 1.
|
||||
/// @param tokenId The token id of the ERC721 NFT to withdraw on layer 2.
|
||||
/// @notice Emitted when the ERC721 NFT is transfered to gateway in layer 2.
|
||||
/// @param l1Token The address of ERC721 NFT in layer 1.
|
||||
/// @param l2Token The address of ERC721 NFT in layer 2.
|
||||
/// @param from The address of sender in layer 2.
|
||||
/// @param to The address of recipient in layer 1.
|
||||
/// @param tokenId The token id of the ERC721 NFT to withdraw in layer 2.
|
||||
event WithdrawERC721(
|
||||
address indexed l1Token,
|
||||
address indexed l2Token,
|
||||
@@ -50,12 +50,12 @@ interface IL2ERC721Gateway {
|
||||
uint256 tokenId
|
||||
);
|
||||
|
||||
/// @notice Emitted when the ERC721 NFT is batch transfered to gateway on layer 2.
|
||||
/// @param l1Token The address of ERC721 NFT on layer 1.
|
||||
/// @param l2Token The address of ERC721 NFT on layer 2.
|
||||
/// @param from The address of sender on layer 2.
|
||||
/// @param to The address of recipient on layer 1.
|
||||
/// @param tokenIds The list of token ids of the ERC721 NFT to withdraw on layer 2.
|
||||
/// @notice Emitted when the ERC721 NFT is batch transfered to gateway in layer 2.
|
||||
/// @param l1Token The address of ERC721 NFT in layer 1.
|
||||
/// @param l2Token The address of ERC721 NFT in layer 2.
|
||||
/// @param from The address of sender in layer 2.
|
||||
/// @param to The address of recipient in layer 1.
|
||||
/// @param tokenIds The list of token ids of the ERC721 NFT to withdraw in layer 2.
|
||||
event BatchWithdrawERC721(
|
||||
address indexed l1Token,
|
||||
address indexed l2Token,
|
||||
@@ -69,7 +69,7 @@ interface IL2ERC721Gateway {
|
||||
*****************************/
|
||||
|
||||
/// @notice Withdraw some ERC721 NFT to caller's account on layer 1.
|
||||
/// @param token The address of ERC721 NFT on layer 2.
|
||||
/// @param token The address of ERC721 NFT in layer 2.
|
||||
/// @param tokenId The token id to withdraw.
|
||||
/// @param gasLimit Unused, but included for potential forward compatibility considerations.
|
||||
function withdrawERC721(
|
||||
@@ -79,8 +79,8 @@ interface IL2ERC721Gateway {
|
||||
) external payable;
|
||||
|
||||
/// @notice Withdraw some ERC721 NFT to caller's account on layer 1.
|
||||
/// @param token The address of ERC721 NFT on layer 2.
|
||||
/// @param to The address of recipient on layer 1.
|
||||
/// @param token The address of ERC721 NFT in layer 2.
|
||||
/// @param to The address of recipient in layer 1.
|
||||
/// @param tokenId The token id to withdraw.
|
||||
/// @param gasLimit Unused, but included for potential forward compatibility considerations.
|
||||
function withdrawERC721(
|
||||
@@ -91,7 +91,7 @@ interface IL2ERC721Gateway {
|
||||
) external payable;
|
||||
|
||||
/// @notice Batch withdraw a list of ERC721 NFT to caller's account on layer 1.
|
||||
/// @param token The address of ERC721 NFT on layer 2.
|
||||
/// @param token The address of ERC721 NFT in layer 2.
|
||||
/// @param tokenIds The list of token ids to withdraw.
|
||||
/// @param gasLimit Unused, but included for potential forward compatibility considerations.
|
||||
function batchWithdrawERC721(
|
||||
@@ -101,8 +101,8 @@ interface IL2ERC721Gateway {
|
||||
) external payable;
|
||||
|
||||
/// @notice Batch withdraw a list of ERC721 NFT to caller's account on layer 1.
|
||||
/// @param token The address of ERC721 NFT on layer 2.
|
||||
/// @param to The address of recipient on layer 1.
|
||||
/// @param token The address of ERC721 NFT in layer 2.
|
||||
/// @param to The address of recipient in layer 1.
|
||||
/// @param tokenIds The list of token ids to withdraw.
|
||||
/// @param gasLimit Unused, but included for potential forward compatibility considerations.
|
||||
function batchWithdrawERC721(
|
||||
@@ -112,14 +112,14 @@ interface IL2ERC721Gateway {
|
||||
uint256 gasLimit
|
||||
) external payable;
|
||||
|
||||
/// @notice Complete ERC721 deposit from layer 1 to layer 2 and send NFT to recipient's account on layer 2.
|
||||
/// @notice Complete ERC721 deposit from layer 1 to layer 2 and send NFT to recipient's account in layer 2.
|
||||
/// @dev Requirements:
|
||||
/// - The function should only be called by L2ScrollMessenger.
|
||||
/// - The function should also only be called by L1ERC721Gateway on layer 1.
|
||||
/// - The function should also only be called by L1ERC721Gateway in layer 1.
|
||||
/// @param l1Token The address of corresponding layer 1 token.
|
||||
/// @param l2Token The address of corresponding layer 2 token.
|
||||
/// @param from The address of account who withdraw the token on layer 1.
|
||||
/// @param to The address of recipient on layer 2 to receive the token.
|
||||
/// @param from The address of account who withdraw the token in layer 1.
|
||||
/// @param to The address of recipient in layer 2 to receive the token.
|
||||
/// @param tokenId The token id to withdraw.
|
||||
function finalizeDepositERC721(
|
||||
address l1Token,
|
||||
@@ -129,14 +129,14 @@ interface IL2ERC721Gateway {
|
||||
uint256 tokenId
|
||||
) external;
|
||||
|
||||
/// @notice Complete ERC721 deposit from layer 1 to layer 2 and send NFT to recipient's account on layer 2.
|
||||
/// @notice Complete ERC721 deposit from layer 1 to layer 2 and send NFT to recipient's account in layer 2.
|
||||
/// @dev Requirements:
|
||||
/// - The function should only be called by L2ScrollMessenger.
|
||||
/// - The function should also only be called by L1ERC721Gateway on layer 1.
|
||||
/// - The function should also only be called by L1ERC721Gateway in layer 1.
|
||||
/// @param l1Token The address of corresponding layer 1 token.
|
||||
/// @param l2Token The address of corresponding layer 2 token.
|
||||
/// @param from The address of account who withdraw the token on layer 1.
|
||||
/// @param to The address of recipient on layer 2 to receive the token.
|
||||
/// @param from The address of account who withdraw the token in layer 1.
|
||||
/// @param to The address of recipient in layer 2 to receive the token.
|
||||
/// @param tokenIds The list of token ids to withdraw.
|
||||
function finalizeBatchDepositERC721(
|
||||
address l1Token,
|
||||
|
||||
@@ -12,7 +12,7 @@ import {ScrollGatewayBase, IScrollGateway} from "../../libraries/gateway/ScrollG
|
||||
import {IScrollERC20} from "../../libraries/token/IScrollERC20.sol";
|
||||
|
||||
/// @title L2ERC20Gateway
|
||||
/// @notice The `L2ERC20Gateway` is used to withdraw custom ERC20 compatible tokens on layer 2 and
|
||||
/// @notice The `L2ERC20Gateway` is used to withdraw custom ERC20 compatible tokens in layer 2 and
|
||||
/// finalize deposit the tokens from layer 1.
|
||||
/// @dev The withdrawn tokens tokens will be burned directly. On finalizing deposit, the corresponding
|
||||
/// tokens will be minted and transfered to the recipient.
|
||||
@@ -22,8 +22,8 @@ contract L2CustomERC20Gateway is OwnableUpgradeable, ScrollGatewayBase, L2ERC20G
|
||||
**********/
|
||||
|
||||
/// @notice Emitted when token mapping for ERC20 token is updated.
|
||||
/// @param _l2Token The address of corresponding ERC20 token on layer 2.
|
||||
/// @param _l1Token The address of ERC20 token on layer 1.
|
||||
/// @param _l2Token The address of corresponding ERC20 token in layer 2.
|
||||
/// @param _l1Token The address of ERC20 token in layer 1.
|
||||
event UpdateTokenMapping(address _l2Token, address _l1Token);
|
||||
|
||||
/*************
|
||||
@@ -95,8 +95,8 @@ contract L2CustomERC20Gateway is OwnableUpgradeable, ScrollGatewayBase, L2ERC20G
|
||||
************************/
|
||||
|
||||
/// @notice Update layer 2 to layer 1 token mapping.
|
||||
/// @param _l2Token The address of corresponding ERC20 token on layer 2.
|
||||
/// @param _l1Token The address of ERC20 token on layer 1.
|
||||
/// @param _l2Token The address of corresponding ERC20 token in layer 2.
|
||||
/// @param _l1Token The address of ERC20 token in layer 1.
|
||||
function updateTokenMapping(address _l2Token, address _l1Token) external onlyOwner {
|
||||
require(_l1Token != address(0), "token address cannot be 0");
|
||||
|
||||
@@ -132,9 +132,14 @@ contract L2CustomERC20Gateway is OwnableUpgradeable, ScrollGatewayBase, L2ERC20G
|
||||
IScrollERC20(_token).burn(_from, _amount);
|
||||
|
||||
// 3. Generate message passed to L1StandardERC20Gateway.
|
||||
bytes memory _message = abi.encodeCall(
|
||||
IL1ERC20Gateway.finalizeWithdrawERC20,
|
||||
(_l1Token, _token, _from, _to, _amount, _data)
|
||||
bytes memory _message = abi.encodeWithSelector(
|
||||
IL1ERC20Gateway.finalizeWithdrawERC20.selector,
|
||||
_l1Token,
|
||||
_token,
|
||||
_from,
|
||||
_to,
|
||||
_amount,
|
||||
_data
|
||||
);
|
||||
|
||||
// 4. send message to L2ScrollMessenger
|
||||
|
||||
@@ -13,7 +13,7 @@ import {ScrollGatewayBase, IScrollGateway} from "../../libraries/gateway/ScrollG
|
||||
import {IScrollERC1155} from "../../libraries/token/IScrollERC1155.sol";
|
||||
|
||||
/// @title L2ERC1155Gateway
|
||||
/// @notice The `L2ERC1155Gateway` is used to withdraw ERC1155 compatible NFTs on layer 2 and
|
||||
/// @notice The `L2ERC1155Gateway` is used to withdraw ERC1155 compatible NFTs in layer 2 and
|
||||
/// finalize deposit the NFTs from layer 1.
|
||||
/// @dev The withdrawn NFTs tokens will be burned directly. On finalizing deposit, the corresponding
|
||||
/// NFT will be minted and transfered to the recipient.
|
||||
@@ -25,8 +25,8 @@ contract L2ERC1155Gateway is OwnableUpgradeable, ERC1155HolderUpgradeable, Scrol
|
||||
**********/
|
||||
|
||||
/// @notice Emitted when token mapping for ERC1155 token is updated.
|
||||
/// @param _l2Token The address of corresponding ERC1155 token on layer 2.
|
||||
/// @param _l1Token The address of ERC1155 token on layer 1.
|
||||
/// @param _l1Token The address of corresponding ERC1155 token in layer 2.
|
||||
/// @param _l1Token The address of ERC1155 token in layer 1.
|
||||
event UpdateTokenMapping(address _l2Token, address _l1Token);
|
||||
|
||||
/*************
|
||||
@@ -137,8 +137,8 @@ contract L2ERC1155Gateway is OwnableUpgradeable, ERC1155HolderUpgradeable, Scrol
|
||||
************************/
|
||||
|
||||
/// @notice Update layer 2 to layer 1 token mapping.
|
||||
/// @param _l2Token The address of corresponding ERC1155 token on layer 2.
|
||||
/// @param _l1Token The address of ERC1155 token on layer 1.
|
||||
/// @param _l1Token The address of corresponding ERC1155 token in layer 2.
|
||||
/// @param _l1Token The address of ERC1155 token in layer 1.
|
||||
function updateTokenMapping(address _l2Token, address _l1Token) external onlyOwner {
|
||||
require(_l1Token != address(0), "token address cannot be 0");
|
||||
|
||||
@@ -152,8 +152,8 @@ contract L2ERC1155Gateway is OwnableUpgradeable, ERC1155HolderUpgradeable, Scrol
|
||||
**********************/
|
||||
|
||||
/// @dev Internal function to withdraw ERC1155 NFT to layer 2.
|
||||
/// @param _token The address of ERC1155 NFT on layer 1.
|
||||
/// @param _to The address of recipient on layer 2.
|
||||
/// @param _token The address of ERC1155 NFT in layer 1.
|
||||
/// @param _to The address of recipient in layer 2.
|
||||
/// @param _tokenId The token id to withdraw.
|
||||
/// @param _amount The amount of token to withdraw.
|
||||
/// @param _gasLimit Estimated gas limit required to complete the withdraw on layer 2.
|
||||
@@ -173,9 +173,14 @@ contract L2ERC1155Gateway is OwnableUpgradeable, ERC1155HolderUpgradeable, Scrol
|
||||
IScrollERC1155(_token).burn(msg.sender, _tokenId, _amount);
|
||||
|
||||
// 2. Generate message passed to L1ERC1155Gateway.
|
||||
bytes memory _message = abi.encodeCall(
|
||||
IL1ERC1155Gateway.finalizeWithdrawERC1155,
|
||||
(_l1Token, _token, msg.sender, _to, _tokenId, _amount)
|
||||
bytes memory _message = abi.encodeWithSelector(
|
||||
IL1ERC1155Gateway.finalizeWithdrawERC1155.selector,
|
||||
_l1Token,
|
||||
_token,
|
||||
msg.sender,
|
||||
_to,
|
||||
_tokenId,
|
||||
_amount
|
||||
);
|
||||
|
||||
// 3. Send message to L2ScrollMessenger.
|
||||
@@ -185,8 +190,8 @@ contract L2ERC1155Gateway is OwnableUpgradeable, ERC1155HolderUpgradeable, Scrol
|
||||
}
|
||||
|
||||
/// @dev Internal function to batch withdraw ERC1155 NFT to layer 2.
|
||||
/// @param _token The address of ERC1155 NFT on layer 1.
|
||||
/// @param _to The address of recipient on layer 2.
|
||||
/// @param _token The address of ERC1155 NFT in layer 1.
|
||||
/// @param _to The address of recipient in layer 2.
|
||||
/// @param _tokenIds The list of token ids to withdraw.
|
||||
/// @param _amounts The list of corresponding number of token to withdraw.
|
||||
/// @param _gasLimit Estimated gas limit required to complete the withdraw on layer 1.
|
||||
@@ -211,9 +216,14 @@ contract L2ERC1155Gateway is OwnableUpgradeable, ERC1155HolderUpgradeable, Scrol
|
||||
IScrollERC1155(_token).batchBurn(msg.sender, _tokenIds, _amounts);
|
||||
|
||||
// 2. Generate message passed to L1ERC1155Gateway.
|
||||
bytes memory _message = abi.encodeCall(
|
||||
IL1ERC1155Gateway.finalizeBatchWithdrawERC1155,
|
||||
(_l1Token, _token, msg.sender, _to, _tokenIds, _amounts)
|
||||
bytes memory _message = abi.encodeWithSelector(
|
||||
IL1ERC1155Gateway.finalizeBatchWithdrawERC1155.selector,
|
||||
_l1Token,
|
||||
_token,
|
||||
msg.sender,
|
||||
_to,
|
||||
_tokenIds,
|
||||
_amounts
|
||||
);
|
||||
|
||||
// 3. Send message to L2ScrollMessenger.
|
||||
|
||||
@@ -13,7 +13,7 @@ import {ScrollGatewayBase, IScrollGateway} from "../../libraries/gateway/ScrollG
|
||||
import {IScrollERC721} from "../../libraries/token/IScrollERC721.sol";
|
||||
|
||||
/// @title L2ERC721Gateway
|
||||
/// @notice The `L2ERC721Gateway` is used to withdraw ERC721 compatible NFTs on layer 2 and
|
||||
/// @notice The `L2ERC721Gateway` is used to withdraw ERC721 compatible NFTs in layer 2 and
|
||||
/// finalize deposit the NFTs from layer 1.
|
||||
/// @dev The withdrawn NFTs tokens will be burned directly. On finalizing deposit, the corresponding
|
||||
/// NFT will be minted and transfered to the recipient.
|
||||
@@ -25,8 +25,8 @@ contract L2ERC721Gateway is OwnableUpgradeable, ERC721HolderUpgradeable, ScrollG
|
||||
**********/
|
||||
|
||||
/// @notice Emitted when token mapping for ERC721 token is updated.
|
||||
/// @param _l2Token The address of corresponding ERC721 token on layer 2.
|
||||
/// @param _l1Token The address of ERC721 token on layer 1.
|
||||
/// @param _l1Token The address of corresponding ERC721 token in layer 2.
|
||||
/// @param _l1Token The address of ERC721 token in layer 1.
|
||||
event UpdateTokenMapping(address _l2Token, address _l1Token);
|
||||
|
||||
/*************
|
||||
@@ -132,8 +132,8 @@ contract L2ERC721Gateway is OwnableUpgradeable, ERC721HolderUpgradeable, ScrollG
|
||||
************************/
|
||||
|
||||
/// @notice Update layer 2 to layer 1 token mapping.
|
||||
/// @param _l2Token The address of corresponding ERC721 token on layer 2.
|
||||
/// @param _l1Token The address of ERC721 token on layer 1.
|
||||
/// @param _l1Token The address of corresponding ERC721 token in layer 2.
|
||||
/// @param _l1Token The address of ERC721 token in layer 1.
|
||||
function updateTokenMapping(address _l2Token, address _l1Token) external onlyOwner {
|
||||
require(_l1Token != address(0), "token address cannot be 0");
|
||||
|
||||
@@ -147,8 +147,8 @@ contract L2ERC721Gateway is OwnableUpgradeable, ERC721HolderUpgradeable, ScrollG
|
||||
**********************/
|
||||
|
||||
/// @dev Internal function to withdraw ERC721 NFT to layer 1.
|
||||
/// @param _token The address of ERC721 NFT on layer 2.
|
||||
/// @param _to The address of recipient on layer 1.
|
||||
/// @param _token The address of ERC721 NFT in layer 2.
|
||||
/// @param _to The address of recipient in layer 1.
|
||||
/// @param _tokenId The token id to withdraw.
|
||||
/// @param _gasLimit Estimated gas limit required to complete the withdraw on layer 1.
|
||||
function _withdrawERC721(
|
||||
@@ -166,9 +166,13 @@ contract L2ERC721Gateway is OwnableUpgradeable, ERC721HolderUpgradeable, ScrollG
|
||||
IScrollERC721(_token).burn(_tokenId);
|
||||
|
||||
// 2. Generate message passed to L1ERC721Gateway.
|
||||
bytes memory _message = abi.encodeCall(
|
||||
IL1ERC721Gateway.finalizeWithdrawERC721,
|
||||
(_l1Token, _token, msg.sender, _to, _tokenId)
|
||||
bytes memory _message = abi.encodeWithSelector(
|
||||
IL1ERC721Gateway.finalizeWithdrawERC721.selector,
|
||||
_l1Token,
|
||||
_token,
|
||||
msg.sender,
|
||||
_to,
|
||||
_tokenId
|
||||
);
|
||||
|
||||
// 3. Send message to L2ScrollMessenger.
|
||||
@@ -178,8 +182,8 @@ contract L2ERC721Gateway is OwnableUpgradeable, ERC721HolderUpgradeable, ScrollG
|
||||
}
|
||||
|
||||
/// @dev Internal function to batch withdraw ERC721 NFT to layer 1.
|
||||
/// @param _token The address of ERC721 NFT on layer 2.
|
||||
/// @param _to The address of recipient on layer 1.
|
||||
/// @param _token The address of ERC721 NFT in layer 2.
|
||||
/// @param _to The address of recipient in layer 1.
|
||||
/// @param _tokenIds The list of token ids to withdraw.
|
||||
/// @param _gasLimit Estimated gas limit required to complete the withdraw on layer 1.
|
||||
function _batchWithdrawERC721(
|
||||
@@ -201,9 +205,13 @@ contract L2ERC721Gateway is OwnableUpgradeable, ERC721HolderUpgradeable, ScrollG
|
||||
}
|
||||
|
||||
// 2. Generate message passed to L1ERC721Gateway.
|
||||
bytes memory _message = abi.encodeCall(
|
||||
IL1ERC721Gateway.finalizeBatchWithdrawERC721,
|
||||
(_l1Token, _token, msg.sender, _to, _tokenIds)
|
||||
bytes memory _message = abi.encodeWithSelector(
|
||||
IL1ERC721Gateway.finalizeBatchWithdrawERC721.selector,
|
||||
_l1Token,
|
||||
_token,
|
||||
msg.sender,
|
||||
_to,
|
||||
_tokenIds
|
||||
);
|
||||
|
||||
// 3. Send message to L2ScrollMessenger.
|
||||
|
||||
@@ -11,7 +11,7 @@ import {IL2ETHGateway} from "./IL2ETHGateway.sol";
|
||||
import {ScrollGatewayBase} from "../../libraries/gateway/ScrollGatewayBase.sol";
|
||||
|
||||
/// @title L2ETHGateway
|
||||
/// @notice The `L2ETHGateway` contract is used to withdraw ETH token on layer 2 and
|
||||
/// @notice The `L2ETHGateway` contract is used to withdraw ETH token in layer 2 and
|
||||
/// finalize deposit ETH from layer 1.
|
||||
/// @dev The ETH are not held in the gateway. The ETH will be sent to the `L2ScrollMessenger` contract.
|
||||
/// On finalizing deposit, the Ether will be transfered from `L2ScrollMessenger`, then transfer to recipient.
|
||||
@@ -100,7 +100,13 @@ contract L2ETHGateway is Initializable, ScrollGatewayBase, IL2ETHGateway {
|
||||
(_from, _data) = abi.decode(_data, (address, bytes));
|
||||
}
|
||||
|
||||
bytes memory _message = abi.encodeCall(IL1ETHGateway.finalizeWithdrawETH, (_from, _to, _amount, _data));
|
||||
bytes memory _message = abi.encodeWithSelector(
|
||||
IL1ETHGateway.finalizeWithdrawETH.selector,
|
||||
_from,
|
||||
_to,
|
||||
_amount,
|
||||
_data
|
||||
);
|
||||
IL2ScrollMessenger(messenger).sendMessage{value: msg.value}(counterpart, _amount, _message, _gasLimit);
|
||||
|
||||
emit WithdrawETH(_from, _to, _amount, _data);
|
||||
|
||||
@@ -45,7 +45,6 @@ contract L2GatewayRouter is OwnableUpgradeable, IL2GatewayRouter {
|
||||
// it can be zero during initialization
|
||||
if (_defaultERC20Gateway != address(0)) {
|
||||
defaultERC20Gateway = _defaultERC20Gateway;
|
||||
emit SetDefaultERC20Gateway(_defaultERC20Gateway);
|
||||
}
|
||||
|
||||
// it can be zero during initialization
|
||||
|
||||
@@ -16,7 +16,7 @@ import {IScrollStandardERC20Factory} from "../../libraries/token/IScrollStandard
|
||||
import {ScrollGatewayBase, IScrollGateway} from "../../libraries/gateway/ScrollGatewayBase.sol";
|
||||
|
||||
/// @title L2StandardERC20Gateway
|
||||
/// @notice The `L2StandardERC20Gateway` is used to withdraw standard ERC20 tokens on layer 2 and
|
||||
/// @notice The `L2StandardERC20Gateway` is used to withdraw standard ERC20 tokens in layer 2 and
|
||||
/// finalize deposit the tokens from layer 1.
|
||||
/// @dev The withdrawn ERC20 tokens will be burned directly. On finalizing deposit, the corresponding
|
||||
/// token will be minted and transfered to the recipient. Any ERC20 that requires non-standard functionality
|
||||
@@ -148,9 +148,14 @@ contract L2StandardERC20Gateway is Initializable, ScrollGatewayBase, L2ERC20Gate
|
||||
IScrollERC20(_token).burn(_from, _amount);
|
||||
|
||||
// 3. Generate message passed to L1StandardERC20Gateway.
|
||||
bytes memory _message = abi.encodeCall(
|
||||
IL1ERC20Gateway.finalizeWithdrawERC20,
|
||||
(_l1Token, _token, _from, _to, _amount, _data)
|
||||
bytes memory _message = abi.encodeWithSelector(
|
||||
IL1ERC20Gateway.finalizeWithdrawERC20.selector,
|
||||
_l1Token,
|
||||
_token,
|
||||
_from,
|
||||
_to,
|
||||
_amount,
|
||||
_data
|
||||
);
|
||||
|
||||
// 4. send message to L2ScrollMessenger
|
||||
|
||||
@@ -13,7 +13,7 @@ import {IL1ERC20Gateway} from "../../L1/gateways/IL1ERC20Gateway.sol";
|
||||
import {ScrollGatewayBase, IScrollGateway} from "../../libraries/gateway/ScrollGatewayBase.sol";
|
||||
|
||||
/// @title L2WETHGateway
|
||||
/// @notice The `L2WETHGateway` contract is used to withdraw `WETH` token on layer 2 and
|
||||
/// @notice The `L2WETHGateway` contract is used to withdraw `WETH` token in layer 2 and
|
||||
/// finalize deposit `WETH` from layer 1.
|
||||
/// @dev The WETH tokens are not held in the gateway. It will first be unwrapped as Ether and
|
||||
/// then the Ether will be sent to the `L2ScrollMessenger` contract.
|
||||
@@ -123,9 +123,14 @@ contract L2WETHGateway is Initializable, ScrollGatewayBase, L2ERC20Gateway {
|
||||
|
||||
// 3. Generate message passed to L2StandardERC20Gateway.
|
||||
address _l1WETH = l1WETH;
|
||||
bytes memory _message = abi.encodeCall(
|
||||
IL1ERC20Gateway.finalizeWithdrawERC20,
|
||||
(_l1WETH, _token, _from, _to, _amount, _data)
|
||||
bytes memory _message = abi.encodeWithSelector(
|
||||
IL1ERC20Gateway.finalizeWithdrawERC20.selector,
|
||||
_l1WETH,
|
||||
_token,
|
||||
_from,
|
||||
_to,
|
||||
_amount,
|
||||
_data
|
||||
);
|
||||
|
||||
// 4. Send message to L1ScrollMessenger.
|
||||
|
||||
@@ -15,7 +15,7 @@ import {ScrollGatewayBase, IScrollGateway} from "../../../libraries/gateway/Scro
|
||||
import {L2ERC20Gateway} from "../L2ERC20Gateway.sol";
|
||||
|
||||
/// @title L2USDCGateway
|
||||
/// @notice The `L2USDCGateway` contract is used to withdraw `USDC` token on layer 2 and
|
||||
/// @notice The `L2USDCGateway` contract is used to withdraw `USDC` token in layer 2 and
|
||||
/// finalize deposit `USDC` from layer 1.
|
||||
contract L2USDCGateway is OwnableUpgradeable, ScrollGatewayBase, L2ERC20Gateway {
|
||||
using SafeERC20Upgradeable for IERC20Upgradeable;
|
||||
@@ -143,9 +143,14 @@ contract L2USDCGateway is OwnableUpgradeable, ScrollGatewayBase, L2ERC20Gateway
|
||||
|
||||
// 3. Generate message passed to L1USDCGateway.
|
||||
address _l1USDC = l1USDC;
|
||||
bytes memory _message = abi.encodeCall(
|
||||
IL1ERC20Gateway.finalizeWithdrawERC20,
|
||||
(_l1USDC, _token, _from, _to, _amount, _data)
|
||||
bytes memory _message = abi.encodeWithSelector(
|
||||
IL1ERC20Gateway.finalizeWithdrawERC20.selector,
|
||||
_l1USDC,
|
||||
_token,
|
||||
_from,
|
||||
_to,
|
||||
_amount,
|
||||
_data
|
||||
);
|
||||
|
||||
// 4. Send message to L1ScrollMessenger.
|
||||
|
||||
@@ -115,7 +115,13 @@ contract L1BlockContainer is OwnableBase, IL1BlockContainer {
|
||||
bytes calldata _blockHeaderRLP,
|
||||
bool _updateGasPriceOracle
|
||||
) external {
|
||||
require(whitelist.isSenderAllowed(msg.sender), "Not whitelisted sender");
|
||||
{
|
||||
IWhitelist _whitelist = whitelist;
|
||||
require(
|
||||
address(_whitelist) == address(0) || _whitelist.isSenderAllowed(msg.sender),
|
||||
"Not whitelisted sender"
|
||||
);
|
||||
}
|
||||
|
||||
// The encoding order in block header is
|
||||
// 1. ParentHash: 32 bytes
|
||||
|
||||
@@ -69,8 +69,14 @@ contract L1GasPriceOracle is OwnableBase, IL1GasPriceOracle {
|
||||
}
|
||||
|
||||
/// @inheritdoc IL1GasPriceOracle
|
||||
/// @dev The `_data` is the RLP-encoded transaction with signature. And we also reserve additional
|
||||
/// 4 bytes in the non-zero bytes to store the number of bytes in the RLP-encoded transaction.
|
||||
/// @dev The extra 74 bytes on top of the RLP encoded unsigned transaction data consist of
|
||||
/// 4 bytes Add 4 bytes in the beginning for transaction data length
|
||||
/// 1 byte RLP V prefix
|
||||
/// 3 bytes V
|
||||
/// 1 bytes RLP R prefix
|
||||
/// 32 bytes R
|
||||
/// 1 bytes RLP S prefix
|
||||
/// 32 bytes S
|
||||
function getL1GasUsed(bytes memory _data) public view override returns (uint256) {
|
||||
uint256 _total = 0;
|
||||
uint256 _length = _data.length;
|
||||
@@ -82,7 +88,8 @@ contract L1GasPriceOracle is OwnableBase, IL1GasPriceOracle {
|
||||
_total += 16;
|
||||
}
|
||||
}
|
||||
return _total + overhead + (4 * 16);
|
||||
uint256 _unsigned = _total + overhead;
|
||||
return _unsigned + (74 * 16);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,45 +12,22 @@ import {OwnableBase} from "../../libraries/common/OwnableBase.sol";
|
||||
/// _verifyStorageProof function, which verifies the existence of the transaction hash in this
|
||||
/// contract's `sentMessages` mapping.
|
||||
contract L2MessageQueue is AppendOnlyMerkleTree, OwnableBase {
|
||||
/**********
|
||||
* Events *
|
||||
**********/
|
||||
|
||||
/// @notice Emitted when a new message is added to the merkle tree.
|
||||
/// @param index The index of the corresponding message.
|
||||
/// @param messageHash The hash of the corresponding message.
|
||||
event AppendMessage(uint256 index, bytes32 messageHash);
|
||||
|
||||
/*************
|
||||
* Variables *
|
||||
*************/
|
||||
|
||||
/// @notice The address of L2ScrollMessenger contract.
|
||||
address public messenger;
|
||||
|
||||
/***************
|
||||
* Constructor *
|
||||
***************/
|
||||
|
||||
constructor(address _owner) {
|
||||
_transferOwnership(_owner);
|
||||
}
|
||||
|
||||
/// @notice Initialize the state of `L2MessageQueue`
|
||||
/// @dev You are not allowed to initialize when there are some messages appended.
|
||||
/// @param _messenger The address of messenger to update.
|
||||
function initialize(address _messenger) external onlyOwner {
|
||||
require(nextMessageIndex == 0, "cannot initialize");
|
||||
|
||||
function initialize() external {
|
||||
_initializeMerkleTree();
|
||||
|
||||
messenger = _messenger;
|
||||
}
|
||||
|
||||
/*****************************
|
||||
* Public Mutating Functions *
|
||||
*****************************/
|
||||
|
||||
/// @notice record the message to merkle tree and compute the new root.
|
||||
/// @param _messageHash The hash of the new added message.
|
||||
function appendMessage(bytes32 _messageHash) external returns (bytes32) {
|
||||
@@ -63,4 +40,13 @@ contract L2MessageQueue is AppendOnlyMerkleTree, OwnableBase {
|
||||
|
||||
return _currentRoot;
|
||||
}
|
||||
|
||||
/// @notice Update the address of messenger.
|
||||
/// @dev You are not allowed to update messenger when there are some messages appended.
|
||||
/// @param _messenger The address of messenger to update.
|
||||
function updateMessenger(address _messenger) external onlyOwner {
|
||||
require(nextMessageIndex == 0, "cannot update messenger");
|
||||
|
||||
messenger = _messenger;
|
||||
}
|
||||
}
|
||||
|
||||
778
contracts/src/L2/predeploys/WETH9.sol
Normal file
778
contracts/src/L2/predeploys/WETH9.sol
Normal file
@@ -0,0 +1,778 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
// Copyright (C) 2015, 2016, 2017 Dapphub
|
||||
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
pragma solidity =0.8.16;
|
||||
|
||||
// solhint-disable reason-string
|
||||
|
||||
contract WETH9 {
|
||||
string public name = "Wrapped Ether";
|
||||
string public symbol = "WETH";
|
||||
uint8 public decimals = 18;
|
||||
|
||||
event Approval(address indexed src, address indexed guy, uint256 wad);
|
||||
event Transfer(address indexed src, address indexed dst, uint256 wad);
|
||||
event Deposit(address indexed dst, uint256 wad);
|
||||
event Withdrawal(address indexed src, uint256 wad);
|
||||
|
||||
mapping(address => uint256) public balanceOf;
|
||||
mapping(address => mapping(address => uint256)) public allowance;
|
||||
|
||||
receive() external payable {
|
||||
deposit();
|
||||
}
|
||||
|
||||
function deposit() public payable {
|
||||
unchecked {
|
||||
balanceOf[msg.sender] += msg.value;
|
||||
}
|
||||
|
||||
emit Deposit(msg.sender, msg.value);
|
||||
}
|
||||
|
||||
function withdraw(uint256 wad) public {
|
||||
require(balanceOf[msg.sender] >= wad);
|
||||
|
||||
unchecked {
|
||||
balanceOf[msg.sender] -= wad;
|
||||
}
|
||||
|
||||
(bool success, ) = msg.sender.call{value: wad}("");
|
||||
require(success, "withdraw ETH failed");
|
||||
|
||||
emit Withdrawal(msg.sender, wad);
|
||||
}
|
||||
|
||||
function totalSupply() public view returns (uint256) {
|
||||
return address(this).balance;
|
||||
}
|
||||
|
||||
function approve(address guy, uint256 wad) public returns (bool) {
|
||||
allowance[msg.sender][guy] = wad;
|
||||
|
||||
emit Approval(msg.sender, guy, wad);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function transfer(address dst, uint256 wad) public returns (bool) {
|
||||
return transferFrom(msg.sender, dst, wad);
|
||||
}
|
||||
|
||||
function transferFrom(
|
||||
address src,
|
||||
address dst,
|
||||
uint256 wad
|
||||
) public returns (bool) {
|
||||
require(balanceOf[src] >= wad);
|
||||
|
||||
if (src != msg.sender && allowance[src][msg.sender] != type(uint256).max) {
|
||||
require(allowance[src][msg.sender] >= wad);
|
||||
|
||||
unchecked {
|
||||
allowance[src][msg.sender] -= wad;
|
||||
}
|
||||
}
|
||||
|
||||
unchecked {
|
||||
balanceOf[src] -= wad;
|
||||
balanceOf[dst] += wad;
|
||||
}
|
||||
|
||||
emit Transfer(src, dst, wad);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 3, 29 June 2007
|
||||
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
Preamble
|
||||
|
||||
The GNU General Public License is a free, copyleft license for
|
||||
software and other kinds of works.
|
||||
|
||||
The licenses for most software and other practical works are designed
|
||||
to take away your freedom to share and change the works. By contrast,
|
||||
the GNU General Public License is intended to guarantee your freedom to
|
||||
share and change all versions of a program--to make sure it remains free
|
||||
software for all its users. We, the Free Software Foundation, use the
|
||||
GNU General Public License for most of our software; it applies also to
|
||||
any other work released this way by its authors. You can apply it to
|
||||
your programs, too.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
them if you wish), that you receive source code or can get it if you
|
||||
want it, that you can change the software or use pieces of it in new
|
||||
free programs, and that you know you can do these things.
|
||||
|
||||
To protect your rights, we need to prevent others from denying you
|
||||
these rights or asking you to surrender the rights. Therefore, you have
|
||||
certain responsibilities if you distribute copies of the software, or if
|
||||
you modify it: responsibilities to respect the freedom of others.
|
||||
|
||||
For example, if you distribute copies of such a program, whether
|
||||
gratis or for a fee, you must pass on to the recipients the same
|
||||
freedoms that you received. You must make sure that they, too, receive
|
||||
or can get the source code. And you must show them these terms so they
|
||||
know their rights.
|
||||
|
||||
Developers that use the GNU GPL protect your rights with two steps:
|
||||
(1) assert copyright on the software, and (2) offer you this License
|
||||
giving you legal permission to copy, distribute and/or modify it.
|
||||
|
||||
For the developers' and authors' protection, the GPL clearly explains
|
||||
that there is no warranty for this free software. For both users' and
|
||||
authors' sake, the GPL requires that modified versions be marked as
|
||||
changed, so that their problems will not be attributed erroneously to
|
||||
authors of previous versions.
|
||||
|
||||
Some devices are designed to deny users access to install or run
|
||||
modified versions of the software inside them, although the manufacturer
|
||||
can do so. This is fundamentally incompatible with the aim of
|
||||
protecting users' freedom to change the software. The systematic
|
||||
pattern of such abuse occurs in the area of products for individuals to
|
||||
use, which is precisely where it is most unacceptable. Therefore, we
|
||||
have designed this version of the GPL to prohibit the practice for those
|
||||
products. If such problems arise substantially in other domains, we
|
||||
stand ready to extend this provision to those domains in future versions
|
||||
of the GPL, as needed to protect the freedom of users.
|
||||
|
||||
Finally, every program is threatened constantly by software patents.
|
||||
States should not allow patents to restrict development and use of
|
||||
software on general-purpose computers, but in those that do, we wish to
|
||||
avoid the special danger that patents applied to a free program could
|
||||
make it effectively proprietary. To prevent this, the GPL assures that
|
||||
patents cannot be used to render the program non-free.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
TERMS AND CONDITIONS
|
||||
|
||||
0. Definitions.
|
||||
|
||||
"This License" refers to version 3 of the GNU General Public License.
|
||||
|
||||
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||
works, such as semiconductor masks.
|
||||
|
||||
"The Program" refers to any copyrightable work licensed under this
|
||||
License. Each licensee is addressed as "you". "Licensees" and
|
||||
"recipients" may be individuals or organizations.
|
||||
|
||||
To "modify" a work means to copy from or adapt all or part of the work
|
||||
in a fashion requiring copyright permission, other than the making of an
|
||||
exact copy. The resulting work is called a "modified version" of the
|
||||
earlier work or a work "based on" the earlier work.
|
||||
|
||||
A "covered work" means either the unmodified Program or a work based
|
||||
on the Program.
|
||||
|
||||
To "propagate" a work means to do anything with it that, without
|
||||
permission, would make you directly or secondarily liable for
|
||||
infringement under applicable copyright law, except executing it on a
|
||||
computer or modifying a private copy. Propagation includes copying,
|
||||
distribution (with or without modification), making available to the
|
||||
public, and in some countries other activities as well.
|
||||
|
||||
To "convey" a work means any kind of propagation that enables other
|
||||
parties to make or receive copies. Mere interaction with a user through
|
||||
a computer network, with no transfer of a copy, is not conveying.
|
||||
|
||||
An interactive user interface displays "Appropriate Legal Notices"
|
||||
to the extent that it includes a convenient and prominently visible
|
||||
feature that (1) displays an appropriate copyright notice, and (2)
|
||||
tells the user that there is no warranty for the work (except to the
|
||||
extent that warranties are provided), that licensees may convey the
|
||||
work under this License, and how to view a copy of this License. If
|
||||
the interface presents a list of user commands or options, such as a
|
||||
menu, a prominent item in the list meets this criterion.
|
||||
|
||||
1. Source Code.
|
||||
|
||||
The "source code" for a work means the preferred form of the work
|
||||
for making modifications to it. "Object code" means any non-source
|
||||
form of a work.
|
||||
|
||||
A "Standard Interface" means an interface that either is an official
|
||||
standard defined by a recognized standards body, or, in the case of
|
||||
interfaces specified for a particular programming language, one that
|
||||
is widely used among developers working in that language.
|
||||
|
||||
The "System Libraries" of an executable work include anything, other
|
||||
than the work as a whole, that (a) is included in the normal form of
|
||||
packaging a Major Component, but which is not part of that Major
|
||||
Component, and (b) serves only to enable use of the work with that
|
||||
Major Component, or to implement a Standard Interface for which an
|
||||
implementation is available to the public in source code form. A
|
||||
"Major Component", in this context, means a major essential component
|
||||
(kernel, window system, and so on) of the specific operating system
|
||||
(if any) on which the executable work runs, or a compiler used to
|
||||
produce the work, or an object code interpreter used to run it.
|
||||
|
||||
The "Corresponding Source" for a work in object code form means all
|
||||
the source code needed to generate, install, and (for an executable
|
||||
work) run the object code and to modify the work, including scripts to
|
||||
control those activities. However, it does not include the work's
|
||||
System Libraries, or general-purpose tools or generally available free
|
||||
programs which are used unmodified in performing those activities but
|
||||
which are not part of the work. For example, Corresponding Source
|
||||
includes interface definition files associated with source files for
|
||||
the work, and the source code for shared libraries and dynamically
|
||||
linked subprograms that the work is specifically designed to require,
|
||||
such as by intimate data communication or control flow between those
|
||||
subprograms and other parts of the work.
|
||||
|
||||
The Corresponding Source need not include anything that users
|
||||
can regenerate automatically from other parts of the Corresponding
|
||||
Source.
|
||||
|
||||
The Corresponding Source for a work in source code form is that
|
||||
same work.
|
||||
|
||||
2. Basic Permissions.
|
||||
|
||||
All rights granted under this License are granted for the term of
|
||||
copyright on the Program, and are irrevocable provided the stated
|
||||
conditions are met. This License explicitly affirms your unlimited
|
||||
permission to run the unmodified Program. The output from running a
|
||||
covered work is covered by this License only if the output, given its
|
||||
content, constitutes a covered work. This License acknowledges your
|
||||
rights of fair use or other equivalent, as provided by copyright law.
|
||||
|
||||
You may make, run and propagate covered works that you do not
|
||||
convey, without conditions so long as your license otherwise remains
|
||||
in force. You may convey covered works to others for the sole purpose
|
||||
of having them make modifications exclusively for you, or provide you
|
||||
with facilities for running those works, provided that you comply with
|
||||
the terms of this License in conveying all material for which you do
|
||||
not control copyright. Those thus making or running the covered works
|
||||
for you must do so exclusively on your behalf, under your direction
|
||||
and control, on terms that prohibit them from making any copies of
|
||||
your copyrighted material outside their relationship with you.
|
||||
|
||||
Conveying under any other circumstances is permitted solely under
|
||||
the conditions stated below. Sublicensing is not allowed; section 10
|
||||
makes it unnecessary.
|
||||
|
||||
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||
|
||||
No covered work shall be deemed part of an effective technological
|
||||
measure under any applicable law fulfilling obligations under article
|
||||
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
||||
similar laws prohibiting or restricting circumvention of such
|
||||
measures.
|
||||
|
||||
When you convey a covered work, you waive any legal power to forbid
|
||||
circumvention of technological measures to the extent such circumvention
|
||||
is effected by exercising rights under this License with respect to
|
||||
the covered work, and you disclaim any intention to limit operation or
|
||||
modification of the work as a means of enforcing, against the work's
|
||||
users, your or third parties' legal rights to forbid circumvention of
|
||||
technological measures.
|
||||
|
||||
4. Conveying Verbatim Copies.
|
||||
|
||||
You may convey verbatim copies of the Program's source code as you
|
||||
receive it, in any medium, provided that you conspicuously and
|
||||
appropriately publish on each copy an appropriate copyright notice;
|
||||
keep intact all notices stating that this License and any
|
||||
non-permissive terms added in accord with section 7 apply to the code;
|
||||
keep intact all notices of the absence of any warranty; and give all
|
||||
recipients a copy of this License along with the Program.
|
||||
|
||||
You may charge any price or no price for each copy that you convey,
|
||||
and you may offer support or warranty protection for a fee.
|
||||
|
||||
5. Conveying Modified Source Versions.
|
||||
|
||||
You may convey a work based on the Program, or the modifications to
|
||||
produce it from the Program, in the form of source code under the
|
||||
terms of section 4, provided that you also meet all of these conditions:
|
||||
|
||||
a) The work must carry prominent notices stating that you modified
|
||||
it, and giving a relevant date.
|
||||
|
||||
b) The work must carry prominent notices stating that it is
|
||||
released under this License and any conditions added under section
|
||||
7. This requirement modifies the requirement in section 4 to
|
||||
"keep intact all notices".
|
||||
|
||||
c) You must license the entire work, as a whole, under this
|
||||
License to anyone who comes into possession of a copy. This
|
||||
License will therefore apply, along with any applicable section 7
|
||||
additional terms, to the whole of the work, and all its parts,
|
||||
regardless of how they are packaged. This License gives no
|
||||
permission to license the work in any other way, but it does not
|
||||
invalidate such permission if you have separately received it.
|
||||
|
||||
d) If the work has interactive user interfaces, each must display
|
||||
Appropriate Legal Notices; however, if the Program has interactive
|
||||
interfaces that do not display Appropriate Legal Notices, your
|
||||
work need not make them do so.
|
||||
|
||||
A compilation of a covered work with other separate and independent
|
||||
works, which are not by their nature extensions of the covered work,
|
||||
and which are not combined with it such as to form a larger program,
|
||||
in or on a volume of a storage or distribution medium, is called an
|
||||
"aggregate" if the compilation and its resulting copyright are not
|
||||
used to limit the access or legal rights of the compilation's users
|
||||
beyond what the individual works permit. Inclusion of a covered work
|
||||
in an aggregate does not cause this License to apply to the other
|
||||
parts of the aggregate.
|
||||
|
||||
6. Conveying Non-Source Forms.
|
||||
|
||||
You may convey a covered work in object code form under the terms
|
||||
of sections 4 and 5, provided that you also convey the
|
||||
machine-readable Corresponding Source under the terms of this License,
|
||||
in one of these ways:
|
||||
|
||||
a) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by the
|
||||
Corresponding Source fixed on a durable physical medium
|
||||
customarily used for software interchange.
|
||||
|
||||
b) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by a
|
||||
written offer, valid for at least three years and valid for as
|
||||
long as you offer spare parts or customer support for that product
|
||||
model, to give anyone who possesses the object code either (1) a
|
||||
copy of the Corresponding Source for all the software in the
|
||||
product that is covered by this License, on a durable physical
|
||||
medium customarily used for software interchange, for a price no
|
||||
more than your reasonable cost of physically performing this
|
||||
conveying of source, or (2) access to copy the
|
||||
Corresponding Source from a network server at no charge.
|
||||
|
||||
c) Convey individual copies of the object code with a copy of the
|
||||
written offer to provide the Corresponding Source. This
|
||||
alternative is allowed only occasionally and noncommercially, and
|
||||
only if you received the object code with such an offer, in accord
|
||||
with subsection 6b.
|
||||
|
||||
d) Convey the object code by offering access from a designated
|
||||
place (gratis or for a charge), and offer equivalent access to the
|
||||
Corresponding Source in the same way through the same place at no
|
||||
further charge. You need not require recipients to copy the
|
||||
Corresponding Source along with the object code. If the place to
|
||||
copy the object code is a network server, the Corresponding Source
|
||||
may be on a different server (operated by you or a third party)
|
||||
that supports equivalent copying facilities, provided you maintain
|
||||
clear directions next to the object code saying where to find the
|
||||
Corresponding Source. Regardless of what server hosts the
|
||||
Corresponding Source, you remain obligated to ensure that it is
|
||||
available for as long as needed to satisfy these requirements.
|
||||
|
||||
e) Convey the object code using peer-to-peer transmission, provided
|
||||
you inform other peers where the object code and Corresponding
|
||||
Source of the work are being offered to the general public at no
|
||||
charge under subsection 6d.
|
||||
|
||||
A separable portion of the object code, whose source code is excluded
|
||||
from the Corresponding Source as a System Library, need not be
|
||||
included in conveying the object code work.
|
||||
|
||||
A "User Product" is either (1) a "consumer product", which means any
|
||||
tangible personal property which is normally used for personal, family,
|
||||
or household purposes, or (2) anything designed or sold for incorporation
|
||||
into a dwelling. In determining whether a product is a consumer product,
|
||||
doubtful cases shall be resolved in favor of coverage. For a particular
|
||||
product received by a particular user, "normally used" refers to a
|
||||
typical or common use of that class of product, regardless of the status
|
||||
of the particular user or of the way in which the particular user
|
||||
actually uses, or expects or is expected to use, the product. A product
|
||||
is a consumer product regardless of whether the product has substantial
|
||||
commercial, industrial or non-consumer uses, unless such uses represent
|
||||
the only significant mode of use of the product.
|
||||
|
||||
"Installation Information" for a User Product means any methods,
|
||||
procedures, authorization keys, or other information required to install
|
||||
and execute modified versions of a covered work in that User Product from
|
||||
a modified version of its Corresponding Source. The information must
|
||||
suffice to ensure that the continued functioning of the modified object
|
||||
code is in no case prevented or interfered with solely because
|
||||
modification has been made.
|
||||
|
||||
If you convey an object code work under this section in, or with, or
|
||||
specifically for use in, a User Product, and the conveying occurs as
|
||||
part of a transaction in which the right of possession and use of the
|
||||
User Product is transferred to the recipient in perpetuity or for a
|
||||
fixed term (regardless of how the transaction is characterized), the
|
||||
Corresponding Source conveyed under this section must be accompanied
|
||||
by the Installation Information. But this requirement does not apply
|
||||
if neither you nor any third party retains the ability to install
|
||||
modified object code on the User Product (for example, the work has
|
||||
been installed in ROM).
|
||||
|
||||
The requirement to provide Installation Information does not include a
|
||||
requirement to continue to provide support service, warranty, or updates
|
||||
for a work that has been modified or installed by the recipient, or for
|
||||
the User Product in which it has been modified or installed. Access to a
|
||||
network may be denied when the modification itself materially and
|
||||
adversely affects the operation of the network or violates the rules and
|
||||
protocols for communication across the network.
|
||||
|
||||
Corresponding Source conveyed, and Installation Information provided,
|
||||
in accord with this section must be in a format that is publicly
|
||||
documented (and with an implementation available to the public in
|
||||
source code form), and must require no special password or key for
|
||||
unpacking, reading or copying.
|
||||
|
||||
7. Additional Terms.
|
||||
|
||||
"Additional permissions" are terms that supplement the terms of this
|
||||
License by making exceptions from one or more of its conditions.
|
||||
Additional permissions that are applicable to the entire Program shall
|
||||
be treated as though they were included in this License, to the extent
|
||||
that they are valid under applicable law. If additional permissions
|
||||
apply only to part of the Program, that part may be used separately
|
||||
under those permissions, but the entire Program remains governed by
|
||||
this License without regard to the additional permissions.
|
||||
|
||||
When you convey a copy of a covered work, you may at your option
|
||||
remove any additional permissions from that copy, or from any part of
|
||||
it. (Additional permissions may be written to require their own
|
||||
removal in certain cases when you modify the work.) You may place
|
||||
additional permissions on material, added by you to a covered work,
|
||||
for which you have or can give appropriate copyright permission.
|
||||
|
||||
Notwithstanding any other provision of this License, for material you
|
||||
add to a covered work, you may (if authorized by the copyright holders of
|
||||
that material) supplement the terms of this License with terms:
|
||||
|
||||
a) Disclaiming warranty or limiting liability differently from the
|
||||
terms of sections 15 and 16 of this License; or
|
||||
|
||||
b) Requiring preservation of specified reasonable legal notices or
|
||||
author attributions in that material or in the Appropriate Legal
|
||||
Notices displayed by works containing it; or
|
||||
|
||||
c) Prohibiting misrepresentation of the origin of that material, or
|
||||
requiring that modified versions of such material be marked in
|
||||
reasonable ways as different from the original version; or
|
||||
|
||||
d) Limiting the use for publicity purposes of names of licensors or
|
||||
authors of the material; or
|
||||
|
||||
e) Declining to grant rights under trademark law for use of some
|
||||
trade names, trademarks, or service marks; or
|
||||
|
||||
f) Requiring indemnification of licensors and authors of that
|
||||
material by anyone who conveys the material (or modified versions of
|
||||
it) with contractual assumptions of liability to the recipient, for
|
||||
any liability that these contractual assumptions directly impose on
|
||||
those licensors and authors.
|
||||
|
||||
All other non-permissive additional terms are considered "further
|
||||
restrictions" within the meaning of section 10. If the Program as you
|
||||
received it, or any part of it, contains a notice stating that it is
|
||||
governed by this License along with a term that is a further
|
||||
restriction, you may remove that term. If a license document contains
|
||||
a further restriction but permits relicensing or conveying under this
|
||||
License, you may add to a covered work material governed by the terms
|
||||
of that license document, provided that the further restriction does
|
||||
not survive such relicensing or conveying.
|
||||
|
||||
If you add terms to a covered work in accord with this section, you
|
||||
must place, in the relevant source files, a statement of the
|
||||
additional terms that apply to those files, or a notice indicating
|
||||
where to find the applicable terms.
|
||||
|
||||
Additional terms, permissive or non-permissive, may be stated in the
|
||||
form of a separately written license, or stated as exceptions;
|
||||
the above requirements apply either way.
|
||||
|
||||
8. Termination.
|
||||
|
||||
You may not propagate or modify a covered work except as expressly
|
||||
provided under this License. Any attempt otherwise to propagate or
|
||||
modify it is void, and will automatically terminate your rights under
|
||||
this License (including any patent licenses granted under the third
|
||||
paragraph of section 11).
|
||||
|
||||
However, if you cease all violation of this License, then your
|
||||
license from a particular copyright holder is reinstated (a)
|
||||
provisionally, unless and until the copyright holder explicitly and
|
||||
finally terminates your license, and (b) permanently, if the copyright
|
||||
holder fails to notify you of the violation by some reasonable means
|
||||
prior to 60 days after the cessation.
|
||||
|
||||
Moreover, your license from a particular copyright holder is
|
||||
reinstated permanently if the copyright holder notifies you of the
|
||||
violation by some reasonable means, this is the first time you have
|
||||
received notice of violation of this License (for any work) from that
|
||||
copyright holder, and you cure the violation prior to 30 days after
|
||||
your receipt of the notice.
|
||||
|
||||
Termination of your rights under this section does not terminate the
|
||||
licenses of parties who have received copies or rights from you under
|
||||
this License. If your rights have been terminated and not permanently
|
||||
reinstated, you do not qualify to receive new licenses for the same
|
||||
material under section 10.
|
||||
|
||||
9. Acceptance Not Required for Having Copies.
|
||||
|
||||
You are not required to accept this License in order to receive or
|
||||
run a copy of the Program. Ancillary propagation of a covered work
|
||||
occurring solely as a consequence of using peer-to-peer transmission
|
||||
to receive a copy likewise does not require acceptance. However,
|
||||
nothing other than this License grants you permission to propagate or
|
||||
modify any covered work. These actions infringe copyright if you do
|
||||
not accept this License. Therefore, by modifying or propagating a
|
||||
covered work, you indicate your acceptance of this License to do so.
|
||||
|
||||
10. Automatic Licensing of Downstream Recipients.
|
||||
|
||||
Each time you convey a covered work, the recipient automatically
|
||||
receives a license from the original licensors, to run, modify and
|
||||
propagate that work, subject to this License. You are not responsible
|
||||
for enforcing compliance by third parties with this License.
|
||||
|
||||
An "entity transaction" is a transaction transferring control of an
|
||||
organization, or substantially all assets of one, or subdividing an
|
||||
organization, or merging organizations. If propagation of a covered
|
||||
work results from an entity transaction, each party to that
|
||||
transaction who receives a copy of the work also receives whatever
|
||||
licenses to the work the party's predecessor in interest had or could
|
||||
give under the previous paragraph, plus a right to possession of the
|
||||
Corresponding Source of the work from the predecessor in interest, if
|
||||
the predecessor has it or can get it with reasonable efforts.
|
||||
|
||||
You may not impose any further restrictions on the exercise of the
|
||||
rights granted or affirmed under this License. For example, you may
|
||||
not impose a license fee, royalty, or other charge for exercise of
|
||||
rights granted under this License, and you may not initiate litigation
|
||||
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
||||
any patent claim is infringed by making, using, selling, offering for
|
||||
sale, or importing the Program or any portion of it.
|
||||
|
||||
11. Patents.
|
||||
|
||||
A "contributor" is a copyright holder who authorizes use under this
|
||||
License of the Program or a work on which the Program is based. The
|
||||
work thus licensed is called the contributor's "contributor version".
|
||||
|
||||
A contributor's "essential patent claims" are all patent claims
|
||||
owned or controlled by the contributor, whether already acquired or
|
||||
hereafter acquired, that would be infringed by some manner, permitted
|
||||
by this License, of making, using, or selling its contributor version,
|
||||
but do not include claims that would be infringed only as a
|
||||
consequence of further modification of the contributor version. For
|
||||
purposes of this definition, "control" includes the right to grant
|
||||
patent sublicenses in a manner consistent with the requirements of
|
||||
this License.
|
||||
|
||||
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
||||
patent license under the contributor's essential patent claims, to
|
||||
make, use, sell, offer for sale, import and otherwise run, modify and
|
||||
propagate the contents of its contributor version.
|
||||
|
||||
In the following three paragraphs, a "patent license" is any express
|
||||
agreement or commitment, however denominated, not to enforce a patent
|
||||
(such as an express permission to practice a patent or covenant not to
|
||||
sue for patent infringement). To "grant" such a patent license to a
|
||||
party means to make such an agreement or commitment not to enforce a
|
||||
patent against the party.
|
||||
|
||||
If you convey a covered work, knowingly relying on a patent license,
|
||||
and the Corresponding Source of the work is not available for anyone
|
||||
to copy, free of charge and under the terms of this License, through a
|
||||
publicly available network server or other readily accessible means,
|
||||
then you must either (1) cause the Corresponding Source to be so
|
||||
available, or (2) arrange to deprive yourself of the benefit of the
|
||||
patent license for this particular work, or (3) arrange, in a manner
|
||||
consistent with the requirements of this License, to extend the patent
|
||||
license to downstream recipients. "Knowingly relying" means you have
|
||||
actual knowledge that, but for the patent license, your conveying the
|
||||
covered work in a country, or your recipient's use of the covered work
|
||||
in a country, would infringe one or more identifiable patents in that
|
||||
country that you have reason to believe are valid.
|
||||
|
||||
If, pursuant to or in connection with a single transaction or
|
||||
arrangement, you convey, or propagate by procuring conveyance of, a
|
||||
covered work, and grant a patent license to some of the parties
|
||||
receiving the covered work authorizing them to use, propagate, modify
|
||||
or convey a specific copy of the covered work, then the patent license
|
||||
you grant is automatically extended to all recipients of the covered
|
||||
work and works based on it.
|
||||
|
||||
A patent license is "discriminatory" if it does not include within
|
||||
the scope of its coverage, prohibits the exercise of, or is
|
||||
conditioned on the non-exercise of one or more of the rights that are
|
||||
specifically granted under this License. You may not convey a covered
|
||||
work if you are a party to an arrangement with a third party that is
|
||||
in the business of distributing software, under which you make payment
|
||||
to the third party based on the extent of your activity of conveying
|
||||
the work, and under which the third party grants, to any of the
|
||||
parties who would receive the covered work from you, a discriminatory
|
||||
patent license (a) in connection with copies of the covered work
|
||||
conveyed by you (or copies made from those copies), or (b) primarily
|
||||
for and in connection with specific products or compilations that
|
||||
contain the covered work, unless you entered into that arrangement,
|
||||
or that patent license was granted, prior to 28 March 2007.
|
||||
|
||||
Nothing in this License shall be construed as excluding or limiting
|
||||
any implied license or other defenses to infringement that may
|
||||
otherwise be available to you under applicable patent law.
|
||||
|
||||
12. No Surrender of Others' Freedom.
|
||||
|
||||
If conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot convey a
|
||||
covered work so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you may
|
||||
not convey it at all. For example, if you agree to terms that obligate you
|
||||
to collect a royalty for further conveying from those to whom you convey
|
||||
the Program, the only way you could satisfy both those terms and this
|
||||
License would be to refrain entirely from conveying the Program.
|
||||
|
||||
13. Use with the GNU Affero General Public License.
|
||||
|
||||
Notwithstanding any other provision of this License, you have
|
||||
permission to link or combine any covered work with a work licensed
|
||||
under version 3 of the GNU Affero General Public License into a single
|
||||
combined work, and to convey the resulting work. The terms of this
|
||||
License will continue to apply to the part which is the covered work,
|
||||
but the special requirements of the GNU Affero General Public License,
|
||||
section 13, concerning interaction through a network will apply to the
|
||||
combination as such.
|
||||
|
||||
14. Revised Versions of this License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions of
|
||||
the GNU General Public License from time to time. Such new versions will
|
||||
be similar in spirit to the present version, but may differ in detail to
|
||||
address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the
|
||||
Program specifies that a certain numbered version of the GNU General
|
||||
Public License "or any later version" applies to it, you have the
|
||||
option of following the terms and conditions either of that numbered
|
||||
version or of any later version published by the Free Software
|
||||
Foundation. If the Program does not specify a version number of the
|
||||
GNU General Public License, you may choose any version ever published
|
||||
by the Free Software Foundation.
|
||||
|
||||
If the Program specifies that a proxy can decide which future
|
||||
versions of the GNU General Public License can be used, that proxy's
|
||||
public statement of acceptance of a version permanently authorizes you
|
||||
to choose that version for the Program.
|
||||
|
||||
Later license versions may give you additional or different
|
||||
permissions. However, no additional obligations are imposed on any
|
||||
author or copyright holder as a result of your choosing to follow a
|
||||
later version.
|
||||
|
||||
15. Disclaimer of Warranty.
|
||||
|
||||
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
||||
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
||||
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
|
||||
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
|
||||
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
|
||||
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
|
||||
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||
|
||||
16. Limitation of Liability.
|
||||
|
||||
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
|
||||
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
|
||||
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
|
||||
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
|
||||
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
|
||||
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
|
||||
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
|
||||
SUCH DAMAGES.
|
||||
|
||||
17. Interpretation of Sections 15 and 16.
|
||||
|
||||
If the disclaimer of warranty and limitation of liability provided
|
||||
above cannot be given local legal effect according to their terms,
|
||||
reviewing courts shall apply local law that most closely approximates
|
||||
an absolute waiver of all civil liability in connection with the
|
||||
Program, unless a warranty or assumption of liability accompanies a
|
||||
copy of the Program in return for a fee.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
How to Apply These Terms to Your New Programs
|
||||
|
||||
If you develop a new program, and you want it to be of the greatest
|
||||
possible use to the public, the best way to achieve this is to make it
|
||||
free software which everyone can redistribute and change under these terms.
|
||||
|
||||
To do so, attach the following notices to the program. It is safest
|
||||
to attach them to the start of each source file to most effectively
|
||||
state the exclusion of warranty; and each file should have at least
|
||||
the "copyright" line and a pointer to where the full notice is found.
|
||||
|
||||
<one line to give the program's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
If the program does terminal interaction, make it output a short
|
||||
notice like this when it starts in an interactive mode:
|
||||
|
||||
<program> Copyright (C) <year> <name of author>
|
||||
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||
This is free software, and you are welcome to redistribute it
|
||||
under certain conditions; type `show c' for details.
|
||||
|
||||
The hypothetical commands `show w' and `show c' should show the appropriate
|
||||
parts of the General Public License. Of course, your program's commands
|
||||
might be different; for a GUI interface, you would use an "about box".
|
||||
|
||||
You should also get your employer (if you work as a programmer) or school,
|
||||
if any, to sign a "copyright disclaimer" for the program, if necessary.
|
||||
For more information on this, and how to apply and follow the GNU GPL, see
|
||||
<http://www.gnu.org/licenses/>.
|
||||
|
||||
The GNU General Public License does not permit incorporating your program
|
||||
into proprietary programs. If your program is a subroutine library, you
|
||||
may consider it more useful to permit linking proprietary applications with
|
||||
the library. If this is what you want to do, use the GNU Lesser General
|
||||
Public License instead of this License. But first, please read
|
||||
<http://www.gnu.org/philosophy/why-not-lgpl.html>.
|
||||
|
||||
*/
|
||||
@@ -15,7 +15,7 @@ contract Whitelist is OwnableBase, IWhitelist {
|
||||
mapping(address => bool) private isWhitelisted;
|
||||
|
||||
constructor(address _owner) {
|
||||
_transferOwnership(_owner);
|
||||
owner = _owner;
|
||||
}
|
||||
|
||||
/// @notice See {IWhitelist-isSenderAllowed}
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
pragma solidity ^0.8.0;
|
||||
|
||||
import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
|
||||
import {ERC20Permit} from "@openzeppelin/contracts/token/ERC20/extensions/draft-ERC20Permit.sol";
|
||||
|
||||
// solhint-disable reason-string
|
||||
// solhint-disable no-empty-blocks
|
||||
|
||||
/// @author Inspired by WETH9 (https://github.com/dapphub/ds-weth/blob/master/src/weth9.sol)
|
||||
contract WrappedEther is ERC20Permit {
|
||||
/// @notice Emitted when user deposits Ether to this contract.
|
||||
/// @param dst The address of depositor.
|
||||
/// @param wad The amount of Ether in wei deposited.
|
||||
event Deposit(address indexed dst, uint256 wad);
|
||||
|
||||
/// @notice Emitted when user withdraws some Ether from this contract.
|
||||
/// @param src The address of caller.
|
||||
/// @param wad The amount of Ether in wei withdrawn.
|
||||
event Withdrawal(address indexed src, uint256 wad);
|
||||
|
||||
constructor() ERC20Permit("Wrapped Ether") ERC20("Wrapped Ether", "WETH") {}
|
||||
|
||||
receive() external payable {
|
||||
deposit();
|
||||
}
|
||||
|
||||
function deposit() public payable {
|
||||
_mint(msg.sender, msg.value);
|
||||
|
||||
emit Deposit(msg.sender, msg.value);
|
||||
}
|
||||
|
||||
function withdraw(uint256 wad) external {
|
||||
_burn(msg.sender, wad);
|
||||
|
||||
(bool success, ) = msg.sender.call{value: wad}("");
|
||||
require(success, "withdraw ETH failed");
|
||||
|
||||
emit Withdrawal(msg.sender, wad);
|
||||
}
|
||||
}
|
||||
@@ -3,8 +3,7 @@
|
||||
pragma solidity ^0.8.16;
|
||||
|
||||
library AddressAliasHelper {
|
||||
/// @dev The offset added to the address in L1.
|
||||
uint160 internal constant OFFSET = uint160(0x1111000000000000000000000000000000001111);
|
||||
uint160 constant offset = uint160(0x1111000000000000000000000000000000001111);
|
||||
|
||||
/// @notice Utility function that converts the address in the L1 that submitted a tx to
|
||||
/// the inbox to the msg.sender viewed in the L2
|
||||
@@ -12,7 +11,7 @@ library AddressAliasHelper {
|
||||
/// @return l2Address L2 address as viewed in msg.sender
|
||||
function applyL1ToL2Alias(address l1Address) internal pure returns (address l2Address) {
|
||||
unchecked {
|
||||
l2Address = address(uint160(l1Address) + OFFSET);
|
||||
l2Address = address(uint160(l1Address) + offset);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +21,7 @@ library AddressAliasHelper {
|
||||
/// @return l1Address the address in the L1 that triggered the tx to L2
|
||||
function undoL1ToL2Alias(address l2Address) internal pure returns (address l1Address) {
|
||||
unchecked {
|
||||
l1Address = address(uint160(l2Address) - OFFSET);
|
||||
l1Address = address(uint160(l2Address) - offset);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,8 +29,6 @@ abstract contract AppendOnlyMerkleTree {
|
||||
}
|
||||
|
||||
function _appendMessageHash(bytes32 _messageHash) internal returns (uint256, bytes32) {
|
||||
require(zeroHashes[1] != bytes32(0), "call before initialization");
|
||||
|
||||
uint256 _currentMessageIndex = nextMessageIndex;
|
||||
bytes32 _hash = _messageHash;
|
||||
uint256 _height = 0;
|
||||
|
||||
16
contracts/src/libraries/oracle/IGasOracle.sol
Normal file
16
contracts/src/libraries/oracle/IGasOracle.sol
Normal file
@@ -0,0 +1,16 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
pragma solidity ^0.8.16;
|
||||
|
||||
interface IGasOracle {
|
||||
/// @notice Estimate fee for cross chain message call.
|
||||
/// @param _sender The address of sender who invoke the call.
|
||||
/// @param _to The target address to receive the call.
|
||||
/// @param _message The message will be passed to the target address.
|
||||
function estimateMessageFee(
|
||||
address _sender,
|
||||
address _to,
|
||||
bytes memory _message,
|
||||
uint256 _gasLimit
|
||||
) external view returns (uint256);
|
||||
}
|
||||
102
contracts/src/libraries/oracle/SimpleGasOracle.sol
Normal file
102
contracts/src/libraries/oracle/SimpleGasOracle.sol
Normal file
@@ -0,0 +1,102 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
pragma solidity =0.8.16;
|
||||
|
||||
import {OwnableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
|
||||
|
||||
import "./IGasOracle.sol";
|
||||
|
||||
/// @title Simple Gas Oracle
|
||||
contract SimpleGasOracle is OwnableUpgradeable, IGasOracle {
|
||||
/**********
|
||||
* Events *
|
||||
**********/
|
||||
|
||||
/// @notice Emitted when owner update default FeeConfig.
|
||||
/// @param _baseFees The amount base fee to pay.
|
||||
/// @param _feesPerByte The amount fee to pay per message byte.
|
||||
event UpdateDefaultFeeConfig(uint256 _baseFees, uint256 _feesPerByte);
|
||||
|
||||
/// @notice Emitted when owner update custom FeeConfig.
|
||||
/// @param _sender The address of custom message sender.
|
||||
/// @param _baseFees The amount base fee to pay.
|
||||
/// @param _feesPerByte The amount fee to pay per message byte.
|
||||
event UpdateCustomFeeConfig(address indexed _sender, uint256 _baseFees, uint256 _feesPerByte);
|
||||
|
||||
/*************
|
||||
* Variables *
|
||||
*************/
|
||||
|
||||
struct FeeConfig {
|
||||
uint128 baseFees;
|
||||
uint128 feesPerByte;
|
||||
}
|
||||
|
||||
/// @notice The default cross chain message FeeConfig.
|
||||
FeeConfig public defaultFeeConfig;
|
||||
|
||||
/// @notice Mapping from sender address to custom FeeConfig.
|
||||
mapping(address => FeeConfig) public customFeeConfig;
|
||||
|
||||
/// @notice Whether the sender should user custom FeeConfig.
|
||||
mapping(address => bool) public hasCustomConfig;
|
||||
|
||||
/***************
|
||||
* Constructor *
|
||||
***************/
|
||||
|
||||
function initialize() external initializer {
|
||||
OwnableUpgradeable.__Ownable_init();
|
||||
}
|
||||
|
||||
/*************************
|
||||
* Public View Functions *
|
||||
*************************/
|
||||
|
||||
/// @inheritdoc IGasOracle
|
||||
function estimateMessageFee(
|
||||
address _sender,
|
||||
address,
|
||||
bytes memory _message,
|
||||
uint256
|
||||
) external view override returns (uint256) {
|
||||
FeeConfig memory _feeConfig;
|
||||
if (hasCustomConfig[_sender]) {
|
||||
_feeConfig = customFeeConfig[_sender];
|
||||
} else {
|
||||
_feeConfig = defaultFeeConfig;
|
||||
}
|
||||
|
||||
unchecked {
|
||||
return _feeConfig.baseFees + uint256(_feeConfig.feesPerByte) * _message.length;
|
||||
}
|
||||
}
|
||||
|
||||
/************************
|
||||
* Restricted Functions *
|
||||
************************/
|
||||
|
||||
/// @notice Update default fee config.
|
||||
/// @param _baseFees The amount of baseFees to update.
|
||||
/// @param _feesPerByte The amount of fees per byte to update.
|
||||
function updateDefaultFeeConfig(uint128 _baseFees, uint128 _feesPerByte) external onlyOwner {
|
||||
defaultFeeConfig = FeeConfig(_baseFees, _feesPerByte);
|
||||
|
||||
emit UpdateDefaultFeeConfig(_baseFees, _feesPerByte);
|
||||
}
|
||||
|
||||
/// @notice Update custom fee config for sender.
|
||||
/// @param _sender The address of sender to update custom FeeConfig.
|
||||
/// @param _baseFees The amount of baseFees to update.
|
||||
/// @param _feesPerByte The amount of fees per byte to update.
|
||||
function updateCustomFeeConfig(
|
||||
address _sender,
|
||||
uint128 _baseFees,
|
||||
uint128 _feesPerByte
|
||||
) external onlyOwner {
|
||||
customFeeConfig[_sender] = FeeConfig(_baseFees, _feesPerByte);
|
||||
hasCustomConfig[_sender] = true;
|
||||
|
||||
emit UpdateCustomFeeConfig(_sender, _baseFees, _feesPerByte);
|
||||
}
|
||||
}
|
||||
@@ -99,7 +99,7 @@ contract L1GasPriceOracleTest is DSTestPlus {
|
||||
|
||||
oracle.setOverhead(_overhead);
|
||||
|
||||
uint256 _gasUsed = _overhead + 4 * 16;
|
||||
uint256 _gasUsed = _overhead + 74 * 16;
|
||||
for (uint256 i = 0; i < _data.length; i++) {
|
||||
if (_data[i] == 0) _gasUsed += 4;
|
||||
else _gasUsed += 16;
|
||||
@@ -122,7 +122,7 @@ contract L1GasPriceOracleTest is DSTestPlus {
|
||||
oracle.setScalar(_scalar);
|
||||
oracle.setL1BaseFee(_baseFee);
|
||||
|
||||
uint256 _gasUsed = _overhead + 4 * 16;
|
||||
uint256 _gasUsed = _overhead + 74 * 16;
|
||||
for (uint256 i = 0; i < _data.length; i++) {
|
||||
if (_data[i] == 0) _gasUsed += 4;
|
||||
else _gasUsed += 16;
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user