synapsecns / sanguine

Synapse Monorepo
MIT License
38 stars 29 forks source link

Print TX Hash from Withdrawals #2835

Closed trajan0x closed 2 months ago

trajan0x commented 3 months ago

Add on for #2815

greptile-apps[bot] commented 3 months ago

Implementation Steps

  1. Add API to Submitter:

    • In 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
      }
  2. Configure API in relapi/server.go:

    • Add a new route in 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 }

  3. Write Client for Submitter API:

    • In 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
      }
  4. CLI Loop to Retrieve TX Hash:

    • Use 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)
      }

References

/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

#### About Greptile This response provides a starting point for your research, not a precise solution. Help us improve! Please leave a ๐Ÿ‘ if this is helpful and ๐Ÿ‘Ž if it is irrelevant. [Ask Greptile](https://app.greptile.com/chat/github/synapsecns/sanguine/master) ยท [Edit Issue Bot Settings](https://app.greptile.com/apps/github)
golangisfun123 commented 2 months ago

resolved in #2845