Skip to content

Commit

Permalink
feat: return tx hash to the faucet API (#4436)
Browse files Browse the repository at this point in the history
* add tx hash to the faucet

* add changelog

* return tx hash for TryRetrieve method
  • Loading branch information
Pantani authored Dec 4, 2024
1 parent 2bdbea4 commit 7dbbee8
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 21 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 4 additions & 2 deletions ignite/cmd/chain_faucet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
3 changes: 1 addition & 2 deletions ignite/pkg/cosmosfaucet/client_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 9 additions & 5 deletions ignite/pkg/cosmosfaucet/http_faucet.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func NewTransferRequest(accountAddress string, coins []string) TransferRequest {
}

type TransferResponse struct {
Hash string `json:"hash,omitempty"`
Error string `json:"error,omitempty"`
}

Expand All @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down
14 changes: 7 additions & 7 deletions ignite/pkg/cosmosfaucet/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -75,20 +75,20 @@ 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,
)
}

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,
Expand All @@ -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)
}
10 changes: 5 additions & 5 deletions ignite/pkg/cosmosfaucet/try_retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TryRetrieve(
rpcAddress,
faucetAddress,
accountAddress string,
) error {
) (string, error) {
var faucetURL *url.URL
var err error

Expand All @@ -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)
Expand All @@ -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) {
Expand Down

0 comments on commit 7dbbee8

Please sign in to comment.