Skip to content

Commit

Permalink
fix: unit tests and withdrawableStake
Browse files Browse the repository at this point in the history
  • Loading branch information
istae committed Jul 22, 2024
1 parent 74fbe6e commit de841bd
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 272 deletions.
2 changes: 1 addition & 1 deletion pkg/api/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ type (
WalletResponse = walletResponse
WalletTxResponse = walletTxResponse
GetStakeResponse = getStakeResponse
WithdrawAllStakeResponse = stakeTransactionReponse
StakeTransactionReponse = stakeTransactionReponse
StatusSnapshotResponse = statusSnapshotResponse
StatusResponse = statusResponse
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ func (s *Service) mountBusinessDebug() {
s.stakingAccessHandler,
s.gasConfigMiddleware("migrate stake"),
web.FinalHandler(jsonhttp.MethodHandler{
"POST": http.HandlerFunc(s.migrateStakeHandler), // TODO
"POST": http.HandlerFunc(s.migrateStakeHandler),
})),
)

Expand Down
4 changes: 2 additions & 2 deletions pkg/api/staking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func TestWithdrawStake(t *testing.T) {
)
ts, _, _, _ := newTestServer(t, testServerOptions{StakingContract: contract})
jsonhttptest.Request(t, ts, http.MethodDelete, "/stake", http.StatusOK, jsonhttptest.WithExpectedJSONResponse(
&api.WithdrawAllStakeResponse{TxHash: txHash.String()}))
&api.StakeTransactionReponse{TxHash: txHash.String()}))
})

t.Run("with invalid stake amount", func(t *testing.T) {
Expand Down Expand Up @@ -252,7 +252,7 @@ func TestMigrateStake(t *testing.T) {
)
ts, _, _, _ := newTestServer(t, testServerOptions{StakingContract: contract})
jsonhttptest.Request(t, ts, http.MethodPost, "/stake/migrate", http.StatusOK, jsonhttptest.WithExpectedJSONResponse(
&api.WithdrawAllStakeResponse{TxHash: txHash.String()}))
&api.StakeTransactionReponse{TxHash: txHash.String()}))
})

t.Run("with invalid stake amount", func(t *testing.T) {
Expand Down
33 changes: 29 additions & 4 deletions pkg/storageincentives/staking/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func (c *contract) sendDepositStakeTransaction(ctx context.Context, stakedAmount
return receipt, nil
}

func (c *contract) getStake(ctx context.Context) (*big.Int, error) {
func (c *contract) getEffectiveStake(ctx context.Context) (*big.Int, error) {
callData, err := c.stakingContractABI.Pack("nodeEffectiveStake", c.owner)
if err != nil {
return nil, err
Expand All @@ -201,6 +201,31 @@ func (c *contract) getStake(ctx context.Context) (*big.Int, error) {
return abi.ConvertType(results[0], new(big.Int)).(*big.Int), nil
}

func (c *contract) getwithdrawableStake(ctx context.Context) (*big.Int, error) {
callData, err := c.stakingContractABI.Pack("withdrawableStake")
if err != nil {
return nil, err
}
result, err := c.transactionService.Call(ctx, &transaction.TxRequest{
To: &c.stakingContractAddress,
Data: callData,
})
if err != nil {
return nil, fmt.Errorf("get stake: %w", err)
}

results, err := c.stakingContractABI.Unpack("withdrawableStake", result)
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) (common.Hash, error) {
prevStakedAmount, err := c.GetStake(ctx)
if err != nil {
Expand Down Expand Up @@ -247,7 +272,7 @@ func (c *contract) ChangeStakeOverlay(ctx context.Context, nonce common.Hash) (c
}

func (c *contract) GetStake(ctx context.Context) (*big.Int, error) {
stakedAmount, err := c.getStake(ctx)
stakedAmount, err := c.getEffectiveStake(ctx)
if err != nil {
return nil, fmt.Errorf("staking contract: failed to get stake: %w", err)
}
Expand Down Expand Up @@ -281,7 +306,7 @@ func (c *contract) getBalance(ctx context.Context) (*big.Int, error) {
}

func (c *contract) WithdrawStake(ctx context.Context) (txHash common.Hash, err error) {
stakedAmount, err := c.getStake(ctx)
stakedAmount, err := c.getwithdrawableStake(ctx)
if err != nil {
return
}
Expand Down Expand Up @@ -309,7 +334,7 @@ func (c *contract) MigrateStake(ctx context.Context) (txHash common.Hash, err er
return common.Hash{}, ErrNotPaused
}

stakedAmount, err := c.getStake(ctx)
stakedAmount, err := c.getEffectiveStake(ctx)
if err != nil {
return
}
Expand Down
Loading

0 comments on commit de841bd

Please sign in to comment.