fix: Update timestamp when upserting multi tx (#4224)

This commit is contained in:
Cuteivist 2023-10-30 07:53:22 +01:00 committed by GitHub
parent 0cac2af1db
commit fa5765cf83
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 4 deletions

View File

@ -34,6 +34,7 @@ type originTxParams struct {
toNetworkID uint64
toAddress common.Address
crossTxID string
timestamp uint64
}
func upsertHopBridgeOriginTx(ctx context.Context, transactionManager *TransactionManager, params originTxParams) (*MultiTransaction, error) {
@ -59,6 +60,7 @@ func upsertHopBridgeOriginTx(ctx context.Context, transactionManager *Transactio
// Common data
Type: MultiTransactionBridge,
CrossTxID: params.crossTxID,
Timestamp: params.timestamp,
}
_, err := transactionManager.InsertMultiTransaction(multiTx)
@ -72,6 +74,7 @@ func upsertHopBridgeOriginTx(ctx context.Context, transactionManager *Transactio
multiTx.FromAddress = params.fromAddress
multiTx.FromAsset = params.fromAsset
multiTx.FromAmount = (*hexutil.Big)(params.fromAmount)
multiTx.Timestamp = params.timestamp
err := transactionManager.UpdateMultiTransaction(multiTx)
if err != nil {
@ -88,6 +91,7 @@ type destinationTxParams struct {
toAsset string
toAmount *big.Int
crossTxID string
timestamp uint64
}
func upsertHopBridgeDestinationTx(ctx context.Context, transactionManager *TransactionManager, params destinationTxParams) (*MultiTransaction, error) {
@ -112,6 +116,7 @@ func upsertHopBridgeDestinationTx(ctx context.Context, transactionManager *Trans
// Common data
Type: MultiTransactionBridge,
CrossTxID: params.crossTxID,
Timestamp: params.timestamp,
}
_, err := transactionManager.InsertMultiTransaction(multiTx)
@ -122,6 +127,7 @@ func upsertHopBridgeDestinationTx(ctx context.Context, transactionManager *Trans
multiTx.ToTxHash = params.toTxHash
multiTx.ToAsset = params.toAsset
multiTx.ToAmount = (*hexutil.Big)(params.toAmount)
multiTx.Timestamp = params.timestamp
err := transactionManager.UpdateMultiTransaction(multiTx)
if err != nil {
@ -150,6 +156,7 @@ func buildHopBridgeMultitransaction(ctx context.Context, client chain.ClientInte
toNetworkID: toChainID,
toAddress: recipient,
crossTxID: getHopBridgeFromL1CrossTxID(recipient, relayer, subTx.Log.Data),
timestamp: subTx.Timestamp,
}
return upsertHopBridgeOriginTx(ctx, transactionManager, params)
@ -168,6 +175,7 @@ func buildHopBridgeMultitransaction(ctx context.Context, client chain.ClientInte
toAsset: "ETH",
toAmount: toAmount,
crossTxID: getHopBridgeFromL1CrossTxID(recipient, relayer, subTx.Log.Data),
timestamp: subTx.Timestamp,
}
return upsertHopBridgeDestinationTx(ctx, transactionManager, params)
@ -188,6 +196,7 @@ func buildHopBridgeMultitransaction(ctx context.Context, client chain.ClientInte
toNetworkID: toChainID,
toAddress: recipient,
crossTxID: getHopBridgeFromL2CrossTxID(transferID),
timestamp: subTx.Timestamp,
}
return upsertHopBridgeOriginTx(ctx, transactionManager, params)
@ -206,6 +215,7 @@ func buildHopBridgeMultitransaction(ctx context.Context, client chain.ClientInte
toAsset: "ETH",
toAmount: toAmount,
crossTxID: getHopBridgeFromL2CrossTxID(transferID),
timestamp: subTx.Timestamp,
}
return upsertHopBridgeDestinationTx(ctx, transactionManager, params)

View File

@ -210,6 +210,7 @@ func buildUniswapSwapMultitransaction(ctx context.Context, client chain.ClientIn
ToNetworkID: transfer.NetworkID,
ToTxHash: transfer.Receipt.TxHash,
ToAddress: transfer.Address,
Timestamp: transfer.Timestamp,
}
var firstSwapLog, lastSwapLog *types.Log

View File

@ -188,6 +188,13 @@ func rowsToMultiTransactions(rows *sql.Rows) ([]*MultiTransaction, error) {
return multiTransactions, nil
}
func getMultiTransactionTimestamp(multiTransaction *MultiTransaction) uint64 {
if multiTransaction.Timestamp != 0 {
return multiTransaction.Timestamp
}
return uint64(time.Now().Unix())
}
// insertMultiTransaction inserts a multi transaction into the database and updates multi-transaction ID and timestamp
func insertMultiTransaction(db *sql.DB, multiTransaction *MultiTransaction) (MultiTransactionIDType, error) {
insert, err := db.Prepare(fmt.Sprintf(`INSERT INTO multi_transactions (%s)
@ -195,8 +202,7 @@ func insertMultiTransaction(db *sql.DB, multiTransaction *MultiTransaction) (Mul
if err != nil {
return NoMultiTransactionID, err
}
timestamp := time.Now().Unix()
timestamp := getMultiTransactionTimestamp(multiTransaction)
result, err := insert.Exec(
multiTransaction.FromNetworkID,
multiTransaction.FromTxHash,
@ -218,7 +224,7 @@ func insertMultiTransaction(db *sql.DB, multiTransaction *MultiTransaction) (Mul
defer insert.Close()
multiTransactionID, err := result.LastInsertId()
multiTransaction.Timestamp = uint64(timestamp)
multiTransaction.Timestamp = timestamp
multiTransaction.ID = uint(multiTransactionID)
return MultiTransactionIDType(multiTransactionID), err
@ -266,6 +272,7 @@ func updateMultiTransaction(db *sql.DB, multiTransaction *MultiTransaction) erro
if err != nil {
return err
}
timestamp := getMultiTransactionTimestamp(multiTransaction)
_, err = update.Exec(
multiTransaction.ID,
multiTransaction.FromNetworkID,
@ -280,7 +287,7 @@ func updateMultiTransaction(db *sql.DB, multiTransaction *MultiTransaction) erro
multiTransaction.ToAmount.String(),
multiTransaction.Type,
multiTransaction.CrossTxID,
time.Now().Unix(),
timestamp,
)
if err != nil {
return err