Skip to content

Commit

Permalink
wip: wallet handler
Browse files Browse the repository at this point in the history
  • Loading branch information
notanatol committed Mar 6, 2024
1 parent d3e98d8 commit b0301bb
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,12 @@ func (s *Service) mountBusinessDebug(restricted bool) {
handle("/wallet", jsonhttp.MethodHandler{
"GET": http.HandlerFunc(s.walletHandler),
})
handle("/wallet/withdraw/{coin}/{addr}", jsonhttp.MethodHandler{
"POST": web.ChainHandlers(
s.gasConfigMiddleware("wallet withdraw"),
web.FinalHandlerFunc(s.walletWithdrawHandler),
),
})
}
}

Expand Down
39 changes: 39 additions & 0 deletions pkg/api/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
package api

import (
"math/big"
"net/http"

"github.com/ethereum/go-ethereum/common"
"github.com/ethersphere/bee/pkg/bigint"
"github.com/ethersphere/bee/pkg/jsonhttp"
"github.com/gorilla/mux"
)

type walletResponse struct {
Expand All @@ -20,6 +22,10 @@ type walletResponse struct {
WalletAddress common.Address `json:"walletAddress"` // the address of the bee wallet
}

type walletTxResponse struct {
TransactionHash common.Hash `json:"transactionHash"`
}

func (s *Service) walletHandler(w http.ResponseWriter, r *http.Request) {
logger := s.logger.WithName("get_wallet").Build()

Expand Down Expand Up @@ -47,3 +53,36 @@ func (s *Service) walletHandler(w http.ResponseWriter, r *http.Request) {
WalletAddress: s.ethereumAddress,
})
}

func (s *Service) walletWithdrawHandler(w http.ResponseWriter, r *http.Request) {
logger := s.logger.WithName("post_wallet_withdraw").Build()

queries := struct {
Amount *big.Int `map:"amount" validate:"required"`
}{}

if response := s.mapStructure(r.URL.Query(), &queries); response != nil {
response("invalid query params", logger, w)
return
}

path := struct {
Address *common.Address `map:"address" validate:"required"`
}{}

if response := s.mapStructure(mux.Vars(r), &path); response != nil {
response("invalid query params", logger, w)
return
}

// check whitelisted

txHash, err := s.erc20Service.Transfer(r.Context(), *path.Address, queries.Amount)
if err != nil {
logger.Error(err, "unable to transfer")
jsonhttp.InternalServerError(w, "unable to transfer amount")
return
}

jsonhttp.OK(w, walletTxResponse{TransactionHash: txHash})
}

0 comments on commit b0301bb

Please sign in to comment.