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 8e93cdc commit 6edefc5
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 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 @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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)
}

0 comments on commit 6edefc5

Please sign in to comment.