Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print TX Hash from Withdrawals #2835

Closed
trajan0x opened this issue Jul 2, 2024 · 2 comments
Closed

Print TX Hash from Withdrawals #2835

trajan0x opened this issue Jul 2, 2024 · 2 comments
Assignees

Comments

@trajan0x
Copy link
Contributor

trajan0x commented Jul 2, 2024

Add on for #2815

  • Requires adding an optional api to submitter, should return a Handler (you can see an example of this here
  • https://pkg.go.dev/github.com/synapsecns/sanguine/core/metrics#section-readme (see Gin() and how it's used, particularly in contrib/promexporter)
  • Configure this api to start at boot up in relapi/server.go and return GetNonceStatus for a given nonce as json
  • Write a client for this submitter api, attach the client to the rfq api client (relapi/client.go)
  • From the CLI call this on a loop until we get the txhash, consider using github.com/charmbracelet/huh/spinner for this (see an example here: this part is purely optional
Copy link

greptile-apps bot commented Jul 2, 2024

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 · Edit Issue Bot Settings

@golangisfun123
Copy link
Collaborator

resolved in #2845

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants