From 6edefc5da961a76db6dde82400f99ce6bc75e5ad Mon Sep 17 00:00:00 2001 From: notanatol Date: Thu, 7 Mar 2024 07:10:41 -0600 Subject: [PATCH] fix: native token withdraw --- pkg/api/wallet.go | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/pkg/api/wallet.go b/pkg/api/wallet.go index 51ff775a74b..7420b7bac6f 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" ) @@ -80,9 +82,17 @@ func (s *Service) walletWithdrawHandler(w http.ResponseWriter, r *http.Request) return } - 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") @@ -91,7 +101,7 @@ func (s *Service) walletWithdrawHandler(w http.ResponseWriter, r *http.Request) } if bzz { - currentBalance, err := s.erc20Service.BalanceOf(ctx, s.ethereumAddress) + currentBalance, err := s.erc20Service.BalanceOf(r.Context(), s.ethereumAddress) if err != nil { logger.Error(err, "unable to get balance") jsonhttp.InternalServerError(w, "unable to get balance") @@ -104,7 +114,7 @@ func (s *Service) walletWithdrawHandler(w http.ResponseWriter, r *http.Request) return } - txHash, err := s.erc20Service.Withdraw(ctx, *queries.Address, queries.Amount) + txHash, err := s.erc20Service.Withdraw(r.Context(), *queries.Address, queries.Amount) if err != nil { logger.Error(err, "unable to transfer") jsonhttp.InternalServerError(w, "unable to transfer amount") @@ -128,7 +138,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(r.Context(), s.transaction, *queries.Address, queries.Amount) if err != nil { logger.Error(err, "withdraw") jsonhttp.InternalServerError(w, "withdraw") @@ -138,6 +148,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) }