New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: enable withdraw all stake endpoint #3662
Changes from all commits
9826f8e
6a5ce2a
c9c8066
3b20b24
4c93952
01aa48b
e7e54e5
1b4b77b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,10 +6,11 @@ package api | |
|
||
import ( | ||
"errors" | ||
"github.com/ethersphere/bee/pkg/bigint" | ||
"math/big" | ||
"net/http" | ||
|
||
"github.com/ethersphere/bee/pkg/bigint" | ||
|
||
"github.com/ethersphere/bee/pkg/jsonhttp" | ||
"github.com/ethersphere/bee/pkg/storageincentives/staking" | ||
"github.com/gorilla/mux" | ||
|
@@ -36,6 +37,10 @@ type stakeDepositResponse struct { | |
TxHash string `json:"txhash"` | ||
} | ||
|
||
type withdrawAllStakeResponse struct { | ||
TxHash string `json:"txhash"` | ||
} | ||
|
||
func (s *Service) stakingDepositHandler(w http.ResponseWriter, r *http.Request) { | ||
logger := s.logger.WithName("post_stake_deposit").Build() | ||
|
||
|
@@ -90,3 +95,23 @@ func (s *Service) getStakedAmountHandler(w http.ResponseWriter, r *http.Request) | |
|
||
jsonhttp.OK(w, getStakeResponse{StakedAmount: bigint.Wrap(stakedAmount)}) | ||
} | ||
|
||
func (s *Service) withdrawAllStakeHandler(w http.ResponseWriter, r *http.Request) { | ||
logger := s.logger.WithName("delete_withdraw_all_stake").Build() | ||
|
||
txHash, err := s.stakingContract.WithdrawAllStake(r.Context()) | ||
if err != nil { | ||
if errors.Is(err, staking.ErrInsufficientStake) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just wondering: if we withdraw the entire amount of the stake, can we gen the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nope, this edge case deals with the fact that we did not have any stake to begin with |
||
logger.Debug("insufficient stake", "overlayAddr", s.overlay, "error", err) | ||
logger.Error(nil, "insufficient stake") | ||
jsonhttp.BadRequest(w, "insufficient stake to withdraw") | ||
return | ||
} | ||
logger.Debug("withdraw stake failed", "error", err) | ||
logger.Error(nil, "withdraw stake failed") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The log message should be the same as the debug message. |
||
jsonhttp.InternalServerError(w, "cannot withdraw stake") | ||
return | ||
} | ||
|
||
jsonhttp.OK(w, withdrawAllStakeResponse{TxHash: txHash.String()}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,15 +27,19 @@ var ( | |
|
||
ErrInsufficientStakeAmount = errors.New("insufficient stake amount") | ||
ErrInsufficientFunds = errors.New("insufficient token balance") | ||
ErrInsufficientStake = errors.New("insufficient stake") | ||
ErrNotImplemented = errors.New("not implemented") | ||
ErrNotPaused = errors.New("contract is not paused") | ||
|
||
approveDescription = "Approve tokens for stake deposit operations" | ||
depositStakeDescription = "Deposit Stake" | ||
approveDescription = "Approve tokens for stake deposit operations" | ||
depositStakeDescription = "Deposit Stake" | ||
withdrawStakeDescription = "Withdraw stake" | ||
) | ||
|
||
type Contract interface { | ||
DepositStake(ctx context.Context, stakedAmount *big.Int) (common.Hash, error) | ||
GetStake(ctx context.Context) (*big.Int, error) | ||
WithdrawAllStake(ctx context.Context) (common.Hash, error) | ||
} | ||
|
||
type contract struct { | ||
|
@@ -158,42 +162,46 @@ func (c *contract) getStake(ctx context.Context, overlay swarm.Address) (*big.In | |
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(results) == 0 { | ||
return nil, errors.New("unexpected empty results") | ||
} | ||
|
||
return abi.ConvertType(results[0], new(big.Int)).(*big.Int), nil | ||
} | ||
|
||
func (c *contract) DepositStake(ctx context.Context, stakedAmount *big.Int) (txHash common.Hash, err error) { | ||
func (c *contract) DepositStake(ctx context.Context, stakedAmount *big.Int) (common.Hash, error) { | ||
prevStakedAmount, err := c.GetStake(ctx) | ||
if err != nil { | ||
return | ||
return common.Hash{}, err | ||
} | ||
|
||
if len(prevStakedAmount.Bits()) == 0 { | ||
if stakedAmount.Cmp(MinimumStakeAmount) == -1 { | ||
err = ErrInsufficientStakeAmount | ||
return | ||
return common.Hash{}, ErrInsufficientStakeAmount | ||
} | ||
} | ||
|
||
balance, err := c.getBalance(ctx) | ||
if err != nil { | ||
return | ||
return common.Hash{}, err | ||
} | ||
|
||
if balance.Cmp(stakedAmount) < 0 { | ||
err = ErrInsufficientFunds | ||
return | ||
return common.Hash{}, ErrInsufficientFunds | ||
} | ||
|
||
_, err = c.sendApproveTransaction(ctx, stakedAmount) | ||
if err != nil { | ||
return | ||
return common.Hash{}, err | ||
} | ||
|
||
receipt, err := c.sendDepositStakeTransaction(ctx, c.owner, stakedAmount, c.overlayNonce) | ||
if receipt != nil { | ||
txHash = receipt.TxHash | ||
if err != nil { | ||
return common.Hash{}, err | ||
} | ||
return | ||
|
||
return receipt.TxHash, nil | ||
} | ||
|
||
func (c *contract) GetStake(ctx context.Context) (*big.Int, error) { | ||
|
@@ -222,5 +230,86 @@ func (c *contract) getBalance(ctx context.Context) (*big.Int, error) { | |
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(results) == 0 { | ||
return nil, errors.New("unexpected empty results") | ||
} | ||
|
||
return abi.ConvertType(results[0], new(big.Int)).(*big.Int), nil | ||
} | ||
|
||
func (c *contract) WithdrawAllStake(ctx context.Context) (txHash common.Hash, err error) { | ||
This conversation was marked as resolved.
Show resolved
Hide resolved
|
||
isPaused, err := c.paused(ctx) | ||
if err != nil { | ||
return | ||
} | ||
if !isPaused { | ||
return common.Hash{}, ErrNotPaused | ||
} | ||
|
||
stakedAmount, err := c.getStake(ctx, c.overlay) | ||
if err != nil { | ||
return | ||
} | ||
|
||
if stakedAmount.Cmp(big.NewInt(0)) <= 0 { | ||
return common.Hash{}, ErrInsufficientStake | ||
} | ||
|
||
_, err = c.sendApproveTransaction(ctx, stakedAmount) | ||
if err != nil { | ||
return common.Hash{}, err | ||
} | ||
|
||
receipt, err := c.withdrawFromStake(ctx, stakedAmount) | ||
if err != nil { | ||
return common.Hash{}, err | ||
} | ||
if receipt != nil { | ||
txHash = receipt.TxHash | ||
} | ||
return txHash, nil | ||
} | ||
|
||
func (c *contract) withdrawFromStake(ctx context.Context, stakedAmount *big.Int) (*types.Receipt, error) { | ||
var overlayAddr [32]byte | ||
copy(overlayAddr[:], c.overlay.Bytes()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the abi packing needs a [32]byte and not a byte slice There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nope, it needs byte slice, just checked |
||
|
||
callData, err := c.stakingContractABI.Pack("withdrawFromStake", overlayAddr, stakedAmount) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
receipt, err := c.sendTransaction(ctx, callData, withdrawStakeDescription) | ||
if err != nil { | ||
return nil, fmt.Errorf("withdraw stake: stakedAmount %d: %w", stakedAmount, err) | ||
} | ||
|
||
return receipt, nil | ||
} | ||
|
||
func (c *contract) paused(ctx context.Context) (bool, error) { | ||
callData, err := c.stakingContractABI.Pack("paused") | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
result, err := c.transactionService.Call(ctx, &transaction.TxRequest{ | ||
To: &c.stakingContractAddress, | ||
Data: callData, | ||
}) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
results, err := c.stakingContractABI.Unpack("paused", result) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
if len(results) == 0 { | ||
return false, errors.New("unexpected empty results") | ||
} | ||
|
||
return results[0].(bool), nil | ||
This conversation was marked as resolved.
Show resolved
Hide resolved
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor: we can either drop the
an
or usetransaction
(singular)