From 7dbbee811f45f5c13eb593371dc7206eb72d1cc0 Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Tue, 3 Dec 2024 23:28:26 -0300 Subject: [PATCH] feat: return tx hash to the faucet API (#4436) * add tx hash to the faucet * add changelog * return tx hash for TryRetrieve method --- changelog.md | 1 + ignite/cmd/chain_faucet.go | 6 ++++-- ignite/pkg/cosmosfaucet/client_http.go | 3 +-- ignite/pkg/cosmosfaucet/http_faucet.go | 14 +++++++++----- ignite/pkg/cosmosfaucet/transfer.go | 14 +++++++------- ignite/pkg/cosmosfaucet/try_retrieve.go | 10 +++++----- 6 files changed, 27 insertions(+), 21 deletions(-) diff --git a/changelog.md b/changelog.md index e68b37570b..44a0895975 100644 --- a/changelog.md +++ b/changelog.md @@ -20,6 +20,7 @@ - [#4327](https://github.com/ignite/cli/pull/4327) Use the TxConfig from simState instead create a new one - [#4326](https://github.com/ignite/cli/pull/4326) Add `buf.build` version to `ignite version` command - [#4289](https://github.com/ignite/cli/pull/4289), [#4423](https://github.com/ignite/cli/pull/4423) Cosmos SDK v0.52 support +- [#4436](https://github.com/ignite/cli/pull/4436) Return tx hash to the faucet API ### Changes diff --git a/ignite/cmd/chain_faucet.go b/ignite/cmd/chain_faucet.go index ecc858bbf1..6adf78799d 100644 --- a/ignite/cmd/chain_faucet.go +++ b/ignite/cmd/chain_faucet.go @@ -62,9 +62,11 @@ func chainFaucetHandler(cmd *cobra.Command, args []string) error { } // perform transfer from faucet - if err := faucet.Transfer(cmd.Context(), toAddress, parsedCoins); err != nil { + hash, err := faucet.Transfer(cmd.Context(), toAddress, parsedCoins) + if err != nil { return err } - return session.Println("📨 Coins sent.") + _ = session.Println("📨 Coins sent.") + return session.Printf("Transaction Hash: %s\n", hash) } diff --git a/ignite/pkg/cosmosfaucet/client_http.go b/ignite/pkg/cosmosfaucet/client_http.go index f914dc102a..dfd86d435c 100644 --- a/ignite/pkg/cosmosfaucet/client_http.go +++ b/ignite/pkg/cosmosfaucet/client_http.go @@ -52,8 +52,7 @@ func (c HTTPClient) Transfer(ctx context.Context, req TransferRequest) (Transfer } var res TransferResponse - err = json.NewDecoder(hres.Body).Decode(&res) - return res, err + return res, json.NewDecoder(hres.Body).Decode(&res) } // FaucetInfo fetch the faucet info for clients to determine if this is a real faucet and diff --git a/ignite/pkg/cosmosfaucet/http_faucet.go b/ignite/pkg/cosmosfaucet/http_faucet.go index 004437dd8a..13aa87ea27 100644 --- a/ignite/pkg/cosmosfaucet/http_faucet.go +++ b/ignite/pkg/cosmosfaucet/http_faucet.go @@ -28,6 +28,7 @@ func NewTransferRequest(accountAddress string, coins []string) TransferRequest { } type TransferResponse struct { + Hash string `json:"hash,omitempty"` Error string `json:"error,omitempty"` } @@ -48,14 +49,15 @@ func (f Faucet) faucetHandler(w http.ResponseWriter, r *http.Request) { } // try performing the transfer - if err := f.Transfer(r.Context(), req.AccountAddress, coins); err != nil { + hash, err := f.Transfer(r.Context(), req.AccountAddress, coins) + if err != nil { if errors.Is(err, context.Canceled) { return } responseError(w, http.StatusInternalServerError, err) - } else { - responseSuccess(w) + return } + responseSuccess(w, hash) } // FaucetInfoResponse is the faucet info payload. @@ -93,8 +95,10 @@ func (f Faucet) coinsFromRequest(req TransferRequest) (sdk.Coins, error) { return coins, nil } -func responseSuccess(w http.ResponseWriter) { - _ = xhttp.ResponseJSON(w, http.StatusOK, TransferResponse{}) +func responseSuccess(w http.ResponseWriter, hash string) { + _ = xhttp.ResponseJSON(w, http.StatusOK, TransferResponse{ + Hash: hash, + }) } func responseError(w http.ResponseWriter, code int, err error) { diff --git a/ignite/pkg/cosmosfaucet/transfer.go b/ignite/pkg/cosmosfaucet/transfer.go index d5145428a3..21e3f519e4 100644 --- a/ignite/pkg/cosmosfaucet/transfer.go +++ b/ignite/pkg/cosmosfaucet/transfer.go @@ -66,7 +66,7 @@ func (f Faucet) TotalTransferredAmount(ctx context.Context, toAccountAddress, de } // Transfer transfers amount of tokens from the faucet account to toAccountAddress. -func (f *Faucet) Transfer(ctx context.Context, toAccountAddress string, coins sdk.Coins) error { +func (f *Faucet) Transfer(ctx context.Context, toAccountAddress string, coins sdk.Coins) (string, error) { transferMutex.Lock() defer transferMutex.Unlock() @@ -75,12 +75,12 @@ func (f *Faucet) Transfer(ctx context.Context, toAccountAddress string, coins sd for _, c := range coins { totalSent, err := f.TotalTransferredAmount(ctx, toAccountAddress, c.Denom) if err != nil { - return err + return "", err } coinMax, found := f.coinsMax[c.Denom] if found && !coinMax.IsNil() && !coinMax.Equal(sdkmath.NewInt(0)) { if totalSent.GTE(coinMax) { - return errors.Errorf( + return "", errors.Errorf( "account has reached to the max. allowed amount (%d) for %q denom", coinMax, c.Denom, @@ -88,7 +88,7 @@ func (f *Faucet) Transfer(ctx context.Context, toAccountAddress string, coins sd } if (totalSent.Add(c.Amount)).GT(coinMax) { - return errors.Errorf( + return "", errors.Errorf( `ask less amount for %q denom. account is reaching to the limit (%d) that faucet can tolerate`, c.Denom, coinMax, @@ -102,13 +102,13 @@ func (f *Faucet) Transfer(ctx context.Context, toAccountAddress string, coins sd // perform transfer for all coins fromAccount, err := f.runner.ShowAccount(ctx, f.accountName) if err != nil { - return err + return "", err } txHash, err := f.runner.BankSend(ctx, fromAccount.Address, toAccountAddress, transfer.String(), chaincmd.BankSendWithFees(f.feeAmount)) if err != nil { - return err + return "", err } // wait for send tx to be confirmed - return f.runner.WaitTx(ctx, txHash, time.Second, 30) + return txHash, f.runner.WaitTx(ctx, txHash, time.Second, 30) } diff --git a/ignite/pkg/cosmosfaucet/try_retrieve.go b/ignite/pkg/cosmosfaucet/try_retrieve.go index 8dd2338570..c8197a34cc 100644 --- a/ignite/pkg/cosmosfaucet/try_retrieve.go +++ b/ignite/pkg/cosmosfaucet/try_retrieve.go @@ -23,7 +23,7 @@ func TryRetrieve( rpcAddress, faucetAddress, accountAddress string, -) error { +) (string, error) { var faucetURL *url.URL var err error @@ -35,7 +35,7 @@ func TryRetrieve( faucetURL, err = discoverFaucetURL(ctx, chainID, rpcAddress) } if err != nil { - return err + return "", err } ctx, cancel := context.WithTimeout(ctx, faucetTimeout) @@ -47,13 +47,13 @@ func TryRetrieve( AccountAddress: accountAddress, }) if err != nil { - return errors.Wrap(err, "faucet is not operational") + return "", errors.Wrap(err, "faucet is not operational") } if resp.Error != "" { - return errors.Errorf("faucet is not operational: %s", resp.Error) + return "", errors.Errorf("faucet is not operational: %s", resp.Error) } - return nil + return resp.Hash, nil } func discoverFaucetURL(ctx context.Context, chainID, rpcAddress string) (*url.URL, error) {