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 7, 2024
1 parent 12dff05 commit 69195f5
Show file tree
Hide file tree
Showing 4 changed files with 125 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
87 changes: 87 additions & 0 deletions pkg/api/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
package api

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

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

type walletResponse struct {
Expand All @@ -20,6 +24,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 +55,82 @@ 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 {
Coin *string `map:"coin" validate:"required"`
Address *common.Address `map:"address" validate:"required"`
}{}

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

ctx := r.Context()
var bzz bool
// check if coin is xdai or bzz
// check whitelisted

if bzz {
currentBalance, err := s.erc20Service.BalanceOf(ctx, s.ethereumAddress)
if err != nil {
logger.Error(err, "unable to get balance")
jsonhttp.InternalServerError(w, "unable to get balance")
return
}

if queries.Amount.Cmp(currentBalance) > 0 {
logger.Error(err, "not enough balance")
jsonhttp.InternalServerError(w, "not enough balance")
return
}

txHash, err := s.erc20Service.Withdraw(ctx, *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})
return
}

nativeToken, err := s.chainBackend.BalanceAt(r.Context(), 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")
jsonhttp.InternalServerError(w, "unable to acquire balance from the chain backend")
return
}

if queries.Amount.Cmp(nativeToken) > 0 {
logger.Error(err, "not enough balance")
jsonhttp.InternalServerError(w, "not enough balance")
return
}

txHash, err := withdraw(ctx, s.chainBackend, *path.Address, queries.Amount)
if err != nil {
logger.Error(err, "withdraw")
jsonhttp.InternalServerError(w, "pending nonce")
return
}

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

func withdraw(context.Context, transaction.Backend, common.Address, *big.Int) (common.Hash, error) {
return common.Hash{}, nil
}
24 changes: 24 additions & 0 deletions pkg/settlement/swap/erc20/erc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var (
type Service interface {
BalanceOf(ctx context.Context, address common.Address) (*big.Int, error)
Transfer(ctx context.Context, address common.Address, value *big.Int) (common.Hash, error)
Withdraw(ctx context.Context, address common.Address, value *big.Int) (common.Hash, error)
}

type erc20Service struct {
Expand Down Expand Up @@ -91,3 +92,26 @@ func (c *erc20Service) Transfer(ctx context.Context, address common.Address, val

return txHash, nil
}

func (c *erc20Service) Withdraw(ctx context.Context, address common.Address, value *big.Int) (common.Hash, error) {
callData, err := erc20ABI.Pack("transfer", c.address, value)
if err != nil {
return common.Hash{}, err
}

request := &transaction.TxRequest{
To: &address,
Data: callData,
GasPrice: sctx.GetGasPrice(ctx),
GasLimit: 90000,
Value: big.NewInt(0),
Description: "token transfer",
}

txHash, err := c.transactionService.Send(ctx, request, transaction.DefaultTipBoostPercent)
if err != nil {
return common.Hash{}, err
}

return txHash, nil
}
8 changes: 8 additions & 0 deletions pkg/settlement/swap/erc20/mock/erc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
type Service struct {
balanceOfFunc func(ctx context.Context, address common.Address) (*big.Int, error)
transferFunc func(ctx context.Context, address common.Address, value *big.Int) (common.Hash, error)
withdrawFunc func(ctx context.Context, address common.Address, value *big.Int) (common.Hash, error)
}

func WithBalanceOfFunc(f func(ctx context.Context, address common.Address) (*big.Int, error)) Option {
Expand Down Expand Up @@ -52,6 +53,13 @@ func (s *Service) Transfer(ctx context.Context, address common.Address, value *b
return common.Hash{}, errors.New("Error")
}

func (s *Service) Withdraw(ctx context.Context, address common.Address, value *big.Int) (common.Hash, error) {
if s.transferFunc != nil {
return s.withdrawFunc(ctx, address, value)
}
return common.Hash{}, errors.New("Error")
}

// Option is the option passed to the mock Chequebook service
type Option interface {
apply(*Service)
Expand Down

0 comments on commit 69195f5

Please sign in to comment.