Skip to content

Commit

Permalink
fix: native token withdraw
Browse files Browse the repository at this point in the history
  • Loading branch information
notanatol committed Mar 7, 2024
1 parent 98bdd0a commit 57b52a2
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions pkg/api/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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)
}

0 comments on commit 57b52a2

Please sign in to comment.