Closed trajan0x closed 2 months ago
Add API to Submitter:
ethergo/submitter/submitter.go
, add a new method to the TransactionSubmitter
interface and its implementation in txSubmitterImpl
to return the transaction hash for a given nonce.
func (t *txSubmitterImpl) GetTxHashByNonce(ctx context.Context, chainID *big.Int, nonce uint64) (string, error) {
txs, err := t.db.GetNonceAttemptsByStatus(ctx, t.signer.Address(), chainID, nonce, db.Confirmed)
if err != nil {
return "", fmt.Errorf("could not get nonce attempts by status: %w", err)
}
if len(txs) == 0 {
return "", fmt.Errorf("no transactions found for nonce %d", nonce)
}
return txs[0].Hash().Hex(), nil
}
Configure API in relapi/server.go
:
relapi/server.go
to handle the new API endpoint.
const getTxHashByNonceRoute = "/txhash/by_nonce"
func (r *RelayerAPIServer) Run(ctx context.Context) error { // ... existing code ... engine.GET(getTxHashByNonceRoute, h.GetTxHashByNonce) // ... existing code ... }
func (h Handler) GetTxHashByNonce(c gin.Context) { chainID := c.Query("chain_id") nonce := c.Query("nonce") // Convert chainID and nonce to appropriate types // Call the submitter's GetTxHashByNonce method // Return the result as JSON }
Write Client for Submitter API:
relapi/client.go
, add a new method to the RelayerClient
interface and its implementation in relayerClient
to call the new API endpoint.
func (r *relayerClient) GetTxHashByNonce(ctx context.Context, chainID string, nonce string) (string, error) {
var res struct { Hash string `json:"hash"` }
resp, err := r.client.R().SetContext(ctx).
SetQueryParam("chain_id", chainID).
SetQueryParam("nonce", nonce).
SetResult(&res).
Get(getTxHashByNonceRoute)
if err != nil {
return "", fmt.Errorf("failed to get tx hash by nonce: %w", err)
}
if resp.StatusCode() != http.StatusOK {
return "", fmt.Errorf("unexpected status code: %d", resp.StatusCode())
}
return res.Hash, nil
}
CLI Loop to Retrieve TX Hash:
github.com/charmbracelet/huh/spinner
to create a spinner while polling the new API endpoint in a loop until the transaction hash is retrieved.
func main() {
client := loadConfig()
chainID := "1" // Example chain ID
nonce := "123" // Example nonce
var txHash string
var err error
s := spinner.New()
s.Title("Retrieving transaction hash...")
s.Action(func() {
for {
txHash, err = client.GetTxHashByNonce(context.Background(), chainID, nonce)
if err == nil && txHash != "" {
break
}
time.Sleep(2 * time.Second) // Polling interval
}
}).Run()
if err != nil {
panic(err)
}
fmt.Printf("Transaction hash: %s\n", txHash)
}
/services/rfq/relayer/relapi/server.go /services/rfq/relayer/relapi/client.go /ethergo/submitter/submitter.go /contrib/opbot/signoz/example/main.go /contrib/promexporter /ethergo/submitter /services/rfq/relayer/relapi
resolved in #2845
Add on for #2815
Gin()
and how it's used, particularly incontrib/promexporter
)relapi/server.go
and returnGetNonceStatus
for a given nonce as jsonrelapi/client.go
)github.com/charmbracelet/huh/spinner
for this (see an example here: this part is purely optional