Skip to content

Commit

Permalink
get gas cost in uusdc when verifying a submitted tx
Browse files Browse the repository at this point in the history
  • Loading branch information
mattac21 committed Nov 19, 2024
1 parent 559014b commit 3cb6336
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions txverifier/txverifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"fmt"
"math/big"
"time"

dbtypes "github.com/skip-mev/go-fast-solver/db"
Expand All @@ -30,15 +31,21 @@ type Database interface {
SetSubmittedTxStatus(ctx context.Context, arg db.SetSubmittedTxStatusParams) (db.SubmittedTx, error)
}

type Oracle interface {
GasCostUUSDC(ctx context.Context, txFee *big.Int, chainID string) (*big.Int, error)
}

type TxVerifier struct {
db Database
clientManager *clientmanager.ClientManager
oracle Oracle
}

func NewTxVerifier(ctx context.Context, db Database, clientManager *clientmanager.ClientManager) (*TxVerifier, error) {
func NewTxVerifier(ctx context.Context, db Database, clientManager *clientmanager.ClientManager, oracle Oracle) (*TxVerifier, error) {
return &TxVerifier{
db: db,
clientManager: clientManager,
oracle: oracle,
}, nil
}

Expand Down Expand Up @@ -92,27 +99,41 @@ func (r *TxVerifier) VerifyTx(ctx context.Context, submittedTx db.SubmittedTx) e
if err != nil {
return fmt.Errorf("failed to get client: %w", err)
}
_, failure, err := bridgeClient.GetTxResult(ctx, submittedTx.TxHash)
gasCost, failure, err := bridgeClient.GetTxResult(ctx, submittedTx.TxHash)
if err != nil {
return fmt.Errorf("failed to get tx result: %w", err)
} else if failure != nil {
lmt.Logger(ctx).Error("tx failed", zap.String("failure", failure.String()))

cost, err := r.oracle.GasCostUUSDC(ctx, gasCost, submittedTx.ChainID)
if err != nil {
return fmt.Errorf("getting gas cost in uusdc for failed tx %s on chain %s: %w", submittedTx.TxHash, submittedTx.ChainID, err)
}

metrics.FromContext(ctx).IncTransactionVerified(false, submittedTx.ChainID)
if _, err := r.db.SetSubmittedTxStatus(ctx, db.SetSubmittedTxStatusParams{
TxStatus: dbtypes.TxStatusFailed,
TxHash: submittedTx.TxHash,
ChainID: submittedTx.ChainID,
TxStatusMessage: sql.NullString{String: failure.String(), Valid: true},
TxCostUusdc: sql.NullString{String: cost.String(), Valid: true},
}); err != nil {
return fmt.Errorf("failed to set tx status to failed: %w", err)
}
return fmt.Errorf("tx failed: %s", failure.String())
} else {
metrics.FromContext(ctx).IncTransactionVerified(true, submittedTx.ChainID)

cost, err := r.oracle.GasCostUUSDC(ctx, gasCost, submittedTx.ChainID)
if err != nil {
return fmt.Errorf("getting gas cost in uusdc for tx %s on chain %s: %w", submittedTx.TxHash, submittedTx.ChainID, err)
}

if _, err := r.db.SetSubmittedTxStatus(ctx, db.SetSubmittedTxStatusParams{
TxStatus: dbtypes.TxStatusSuccess,
TxHash: submittedTx.TxHash,
ChainID: submittedTx.ChainID,
TxStatus: dbtypes.TxStatusSuccess,
TxHash: submittedTx.TxHash,
ChainID: submittedTx.ChainID,
TxCostUusdc: sql.NullString{String: cost.String(), Valid: true},
}); err != nil {
return fmt.Errorf("failed to set tx status to success: %w", err)
}
Expand Down

0 comments on commit 3cb6336

Please sign in to comment.