Skip to content
This repository has been archived by the owner on May 11, 2024. It is now read-only.

Commit

Permalink
feat(proposer): check tko balance and fee before proposing (#205)
Browse files Browse the repository at this point in the history
Co-authored-by: David <[email protected]>
  • Loading branch information
cyberhorsey and davidtaikocha authored May 3, 2023
1 parent 98e7107 commit cc0da63
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions proposer/proposer.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Proposer struct {

// Private keys and account addresses
l1ProposerPrivKey *ecdsa.PrivateKey
l1ProposerAddress common.Address
l2SuggestedFeeRecipient common.Address

// Proposing configurations
Expand Down Expand Up @@ -68,6 +69,7 @@ func (p *Proposer) InitFromCli(ctx context.Context, c *cli.Context) error {
// InitFromConfig initializes the proposer instance based on the given configurations.
func InitFromConfig(ctx context.Context, p *Proposer, cfg *Config) (err error) {
p.l1ProposerPrivKey = cfg.L1ProposerPrivKey
p.l1ProposerAddress = crypto.PubkeyToAddress(cfg.L1ProposerPrivKey.PublicKey)
p.l2SuggestedFeeRecipient = cfg.L2SuggestedFeeRecipient
p.proposingInterval = cfg.ProposeInterval
p.proposeEmptyBlocksInterval = cfg.ProposeEmptyBlocksInterval
Expand Down Expand Up @@ -161,6 +163,12 @@ func (p *Proposer) ProposeOp(ctx context.Context) error {
return p.CustomProposeOpHook()
}

log.Info("Comparing proposer TKO balance to block fee")

if err := p.checkTaikoTokenBalance(); err != nil {
return fmt.Errorf("failed to check Taiko token balance: %w", err)
}

// Wait until L2 execution engine is synced at first.
if err := p.rpc.WaitTillL2Synced(ctx); err != nil {
return fmt.Errorf("failed to wait until L2 execution engine synced: %w", err)
Expand Down Expand Up @@ -322,3 +330,21 @@ func getTxOpts(

return opts, nil
}

func (p *Proposer) checkTaikoTokenBalance() error {
fee, err := p.rpc.TaikoL1.GetBlockFee(nil)
if err != nil {
return fmt.Errorf("failed to get block fee: %w", err)
}

balance, err := p.rpc.TaikoL1.GetTaikoTokenBalance(nil, p.l1ProposerAddress)
if err != nil {
return fmt.Errorf("failed to get tko balance: %w", err)
}

if balance.Cmp(new(big.Int).SetUint64(fee)) == -1 {
return fmt.Errorf("proposer does not have enough tko balance to propose")
}

return nil
}

0 comments on commit cc0da63

Please sign in to comment.