Fix timestamp and blockNumber for EVM and Solana blochains (#199)

This commit is contained in:
ftocal 2023-03-21 15:59:40 -03:00 committed by GitHub
parent 5527268969
commit cbeeba1681
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 22 deletions

View File

@ -2,6 +2,7 @@ package solana
import ( import (
"context" "context"
"time"
"github.com/gagliardetto/solana-go" "github.com/gagliardetto/solana-go"
"github.com/gagliardetto/solana-go/rpc" "github.com/gagliardetto/solana-go/rpc"
@ -17,6 +18,7 @@ type SolanaSDK struct {
type GetBlockResult struct { type GetBlockResult struct {
IsConfirmed bool IsConfirmed bool
Transactions []rpc.TransactionWithMeta Transactions []rpc.TransactionWithMeta
BlockTime *time.Time
} }
func NewSolanaSDK(url string, rl ratelimit.Limiter) *SolanaSDK { func NewSolanaSDK(url string, rl ratelimit.Limiter) *SolanaSDK {
@ -50,7 +52,12 @@ func (s *SolanaSDK) GetBlock(ctx context.Context, block uint64) (*GetBlockResult
// Per the API, nil just means the block is not confirmed. // Per the API, nil just means the block is not confirmed.
return &GetBlockResult{IsConfirmed: false}, nil return &GetBlockResult{IsConfirmed: false}, nil
} }
return &GetBlockResult{IsConfirmed: true, Transactions: out.Transactions}, nil var blockTime *time.Time
if out.BlockTime != nil {
t := out.BlockTime.Time()
blockTime = &t
}
return &GetBlockResult{IsConfirmed: true, Transactions: out.Transactions, BlockTime: blockTime}, nil
} }
func (s *SolanaSDK) GetSignaturesForAddress(ctx context.Context, address solana.PublicKey) ([]*rpc.TransactionSignature, error) { func (s *SolanaSDK) GetSignaturesForAddress(ctx context.Context, address solana.PublicKey) ([]*rpc.TransactionSignature, error) {

View File

@ -168,16 +168,6 @@ func (w *EVMWatcher) processBlock(ctx context.Context, currentBlock int64, lastB
continue continue
} }
// get the timestamp.
unixTime, err := strconv.ParseInt(remove0x(tx.Timestamp), 16, 64)
var timestamp *time.Time
if err != nil {
w.logger.Error("cannot convert to timestamp", zap.Error(err), zap.String("tx", tx.Hash))
} else {
tm := time.Unix(unixTime, 0)
timestamp = &tm
}
// create global transaction. // create global transaction.
updatedAt := time.Now() updatedAt := time.Now()
globalTx := storage.TransactionUpdate{ globalTx := storage.TransactionUpdate{
@ -189,8 +179,8 @@ func (w *EVMWatcher) processBlock(ctx context.Context, currentBlock int64, lastB
TxHash: remove0x(tx.Hash), TxHash: remove0x(tx.Hash),
To: tx.To, To: tx.To,
From: tx.From, From: tx.From,
BlockNumber: tx.BlockNumber, BlockNumber: w.getBlockNumber(tx.BlockNumber, tx.Hash),
Timestamp: timestamp, Timestamp: w.getTimestamp(tx.Timestamp, tx.Hash),
UpdatedAt: &updatedAt, UpdatedAt: &updatedAt,
}, },
} }
@ -298,6 +288,25 @@ func (w *EVMWatcher) parseInput(input string) (*vaa.VAA, error) {
return vaa, nil return vaa, nil
} }
func (w *EVMWatcher) getBlockNumber(s string, txHash string) string {
value, err := strconv.ParseInt(remove0x(s), 16, 64)
if err != nil {
w.logger.Error("cannot convert to int", zap.Error(err), zap.String("tx", txHash))
return s
}
return strconv.FormatInt(value, 10)
}
func (w *EVMWatcher) getTimestamp(s string, txHash string) *time.Time {
value, err := strconv.ParseInt(remove0x(s), 16, 64)
if err != nil {
w.logger.Error("cannot convert to timestamp", zap.Error(err), zap.String("tx", txHash))
return nil
}
tm := time.Unix(value, 0)
return &tm
}
func remove0x(input string) string { func remove0x(input string) string {
return strings.Replace(input, "0x", "", -1) return strings.Replace(input, "0x", "", -1)
} }

View File

@ -186,7 +186,7 @@ func (w *SolanaWatcher) processBlock(ctx context.Context, fromBlock uint64, toBl
return errors.New("block not confirmed") return errors.New("block not confirmed")
} }
for txNum, txRpc := range result.Transactions { for txNum, txRpc := range result.Transactions {
w.processTransaction(ctx, txRpc, block, txNum) w.processTransaction(ctx, txRpc, block, txNum, result.BlockTime)
} }
// update the last block number processed in the database. // update the last block number processed in the database.
watcherBlock := storage.WatcherBlock{ watcherBlock := storage.WatcherBlock{
@ -202,7 +202,7 @@ func (w *SolanaWatcher) processBlock(ctx context.Context, fromBlock uint64, toBl
} }
} }
func (w *SolanaWatcher) processTransaction(ctx context.Context, txRpc rpc.TransactionWithMeta, block uint64, txNum int) { func (w *SolanaWatcher) processTransaction(ctx context.Context, txRpc rpc.TransactionWithMeta, block uint64, txNum int, blockTime *time.Time) {
if txRpc.Meta.Err != nil { if txRpc.Meta.Err != nil {
w.logger.Debug("Transaction failed, skipping it", w.logger.Debug("Transaction failed, skipping it",
zap.Uint64("block", block), zap.Uint64("block", block),
@ -311,12 +311,6 @@ func (w *SolanaWatcher) processTransaction(ctx context.Context, txRpc rpc.Transa
continue continue
} }
// create global transaction.
var timestamp *time.Time
if txRpc.BlockTime != nil {
tm := time.Unix(int64(*txRpc.BlockTime), 0)
timestamp = &tm
}
updatedAt := time.Now() updatedAt := time.Now()
globalTx := storage.TransactionUpdate{ globalTx := storage.TransactionUpdate{
ID: data.MessageID(), ID: data.MessageID(),
@ -326,7 +320,7 @@ func (w *SolanaWatcher) processTransaction(ctx context.Context, txRpc rpc.Transa
Method: instruccionID.Name(), Method: instruccionID.Name(),
TxHash: txSignature.String(), TxHash: txSignature.String(),
BlockNumber: strconv.FormatUint(block, 10), BlockNumber: strconv.FormatUint(block, 10),
Timestamp: timestamp, Timestamp: blockTime,
UpdatedAt: &updatedAt, UpdatedAt: &updatedAt,
}, },
} }