diff --git a/pkg/api/wallet.go b/pkg/api/wallet.go index 51ff775a74b..46f8df391b0 100644 --- a/pkg/api/wallet.go +++ b/pkg/api/wallet.go @@ -8,12 +8,14 @@ import ( "context" "math/big" "net/http" + "strings" "slices" "github.com/ethereum/go-ethereum/common" "github.com/ethersphere/bee/pkg/bigint" "github.com/ethersphere/bee/pkg/jsonhttp" + "github.com/ethersphere/bee/pkg/sctx" "github.com/ethersphere/bee/pkg/transaction" "github.com/gorilla/mux" ) @@ -82,7 +84,16 @@ func (s *Service) walletWithdrawHandler(w http.ResponseWriter, r *http.Request) ctx := r.Context() var bzz bool - // check if coin is xdai or bzz + + if strings.EqualFold("BZZ", *path.Coin) { + bzz = true + } + + if !strings.EqualFold("xdai", *path.Coin) { + logger.Error(nil, "invalid coin type") + jsonhttp.BadRequest(w, "only BZZ or XDAI options are accepted") + return + } if !slices.Contains(s.whitelistedWithdrawalAddress, *queries.Address) { logger.Error(nil, "provided address not whitelisted") @@ -114,7 +125,7 @@ func (s *Service) walletWithdrawHandler(w http.ResponseWriter, r *http.Request) return } - nativeToken, err := s.chainBackend.BalanceAt(r.Context(), s.ethereumAddress, nil) + nativeToken, err := s.chainBackend.BalanceAt(ctx, s.ethereumAddress, nil) if err != nil { logger.Debug("unable to acquire balance from the chain backend", "error", err) logger.Error(nil, "unable to acquire balance from the chain backend") @@ -128,7 +139,7 @@ func (s *Service) walletWithdrawHandler(w http.ResponseWriter, r *http.Request) return } - txHash, err := withdraw(ctx, s.chainBackend, *queries.Address, queries.Amount) + txHash, err := withdraw(ctx, s.transaction, *queries.Address, queries.Amount) if err != nil { logger.Error(err, "withdraw") jsonhttp.InternalServerError(w, "withdraw") @@ -138,6 +149,15 @@ func (s *Service) walletWithdrawHandler(w http.ResponseWriter, r *http.Request) jsonhttp.OK(w, walletTxResponse{TransactionHash: txHash}) } -func withdraw(context.Context, transaction.Backend, common.Address, *big.Int) (common.Hash, error) { - return common.Hash{}, nil +func withdraw(ctx context.Context, backend transaction.Service, to common.Address, amount *big.Int) (common.Hash, error) { + req := &transaction.TxRequest{ + To: &to, + GasPrice: sctx.GetGasPrice(ctx), + GasLimit: sctx.GetGasLimit(ctx), + MinEstimatedGasLimit: 500_000, + Value: amount, + Description: "native token withdraw", + } + + return backend.Send(ctx, req, transaction.DefaultTipBoostPercent) }