Skip to content

Commit

Permalink
feat: swip 20 staking changes (ethersphere#4728)
Browse files Browse the repository at this point in the history
  • Loading branch information
istae authored Jul 24, 2024
1 parent 0099216 commit f6d294c
Show file tree
Hide file tree
Showing 14 changed files with 794 additions and 253 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/beekeeper.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ env:
REPLICA: 3
RUN_TYPE: "PR RUN"
SETUP_CONTRACT_IMAGE: "ethersphere/bee-localchain"
SETUP_CONTRACT_IMAGE_TAG: "0.9.0-rc3"
SETUP_CONTRACT_IMAGE_TAG: "0.9.1-rc1"
BEELOCAL_BRANCH: "main"
BEEKEEPER_BRANCH: "master"
BEEKEEPER_METRICS_ENABLED: false
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/coreos/go-semver v0.3.0
github.com/ethereum/go-ethereum v1.14.3
github.com/ethersphere/go-price-oracle-abi v0.2.0
github.com/ethersphere/go-storage-incentives-abi v0.9.0-rc3
github.com/ethersphere/go-storage-incentives-abi v0.9.1-rc1
github.com/ethersphere/go-sw3-abi v0.6.5
github.com/ethersphere/langos v1.0.0
github.com/go-playground/validator/v10 v10.11.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ github.com/ethereum/go-ethereum v1.14.3 h1:5zvnAqLtnCZrU9uod1JCvHWJbPMURzYFHfc2e
github.com/ethereum/go-ethereum v1.14.3/go.mod h1:1STrq471D0BQbCX9He0hUj4bHxX2k6mt5nOQJhDNOJ8=
github.com/ethersphere/go-price-oracle-abi v0.2.0 h1:wtIcYLgNZHY4BjYwJCnu93SvJdVAZVvBaKinspyyHvQ=
github.com/ethersphere/go-price-oracle-abi v0.2.0/go.mod h1:sI/Qj4/zJ23/b1enzwMMv0/hLTpPNVNacEwCWjo6yBk=
github.com/ethersphere/go-storage-incentives-abi v0.9.0-rc3 h1:TCCGtf1jODBUusTiH94Nhgw03apgchQZaJ03L/vt4z4=
github.com/ethersphere/go-storage-incentives-abi v0.9.0-rc3/go.mod h1:SXvJVtM4sEsaSKD0jc1ClpDLw8ErPoROZDme4Wrc/Nc=
github.com/ethersphere/go-storage-incentives-abi v0.9.1-rc1 h1:+BfZEv0zIN9MKQeRsZG/y3KruIuPlHHaG01oJ2wAvNA=
github.com/ethersphere/go-storage-incentives-abi v0.9.1-rc1/go.mod h1:SXvJVtM4sEsaSKD0jc1ClpDLw8ErPoROZDme4Wrc/Nc=
github.com/ethersphere/go-sw3-abi v0.6.5 h1:M5dcIe1zQYvGpY2K07UNkNU9Obc4U+A1fz68Ho/Q+XE=
github.com/ethersphere/go-sw3-abi v0.6.5/go.mod h1:BmpsvJ8idQZdYEtWnvxA8POYQ8Rl/NhyCdF0zLMOOJU=
github.com/ethersphere/langos v1.0.0 h1:NBtNKzXTTRSue95uOlzPN4py7Aofs0xWPzyj4AI1Vcc=
Expand Down
40 changes: 34 additions & 6 deletions openapi/Swarm.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
openapi: 3.0.3

info:
version: 7.0.0
version: 7.1.0
title: Bee API
description: "A list of the currently provided Interfaces to interact with the swarm, implementing file operations and sending messages"

Expand Down Expand Up @@ -2033,6 +2033,34 @@ paths:
default:
description: Default response

"/stake/migrate":
post:
summary: Withdraws all past staked amount back to the wallet.
description: Be aware, the endpoint call only be called when the contract is paused and is in the process of being migrated to a new contract.
tags:
- Staking
responses:
"200":
$ref: "SwarmCommon.yaml#/components/schemas/StakeTransactionResponse"
"500":
$ref: "SwarmCommon.yaml#/components/responses/500"
default:
description: Default response

"/stake/withdrawable":
get:
summary: Get the withdrawable staked amount.
description: This endpoint fetches any amount that is possible to withdraw as surplus.
tags:
- Staking
responses:
"200":
$ref: "SwarmCommon.yaml#/components/schemas/GetStakeResponse"
"500":
$ref: "SwarmCommon.yaml#/components/responses/500"
default:
description: Default response

"/stake/{amount}":
post:
summary: Deposit some amount for staking.
Expand All @@ -2049,7 +2077,7 @@ paths:
- $ref: "SwarmCommon.yaml#/components/parameters/GasLimitParameter"
responses:
"200":
$ref: "SwarmCommon.yaml#/components/schemas/StakeDepositResponse"
$ref: "SwarmCommon.yaml#/components/schemas/StakeTransactionResponse"
"400":
$ref: "SwarmCommon.yaml#/components/responses/400"
"500":
Expand All @@ -2060,7 +2088,7 @@ paths:
"/stake":
get:
summary: Get the staked amount.
description: This endpoint fetches the staked amount from the blockchain.
description: This endpoint fetches the total staked amount from the blockchain.
tags:
- Staking
responses:
Expand All @@ -2071,16 +2099,16 @@ paths:
default:
description: Default response
delete:
summary: Withdraw all staked amount.
description: Be aware, this endpoint creates an on-chain transactions and transfers BZZ from the node's Ethereum account and hence directly manipulates the wallet balance.
summary: Withdraw the extra withdrawable staked amount.
description: This endpoint withdraws any amount that is possible to withdraw as surplus.
tags:
- Staking
parameters:
- $ref: "SwarmCommon.yaml#/components/parameters/GasPriceParameter"
- $ref: "SwarmCommon.yaml#/components/parameters/GasLimitParameter"
responses:
"200":
$ref: "SwarmCommon.yaml#/components/schemas/WithdrawAllStakeResponse"
$ref: "SwarmCommon.yaml#/components/schemas/StakeTransactionResponse"
"400":
$ref: "SwarmCommon.yaml#/components/responses/400"
"500":
Expand Down
10 changes: 2 additions & 8 deletions openapi/SwarmCommon.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
openapi: 3.0.3
info:
version: 4.0.0
version: 4.1.0
title: Common Data Types
description: |
\*****bzzz*****
Expand Down Expand Up @@ -605,18 +605,12 @@ components:
stakedAmount:
$ref: "#/components/schemas/BigInt"

StakeDepositResponse:
StakeTransactionResponse:
type: object
properties:
txHash:
$ref: "#/components/schemas/TransactionHash"

WithdrawAllStakeResponse:
type: object
properties:
txHash:
$ref: "#/components/schemas/TransactionHash"

SwarmOnlyReference:
oneOf:
- $ref: "#/components/schemas/SwarmAddress"
Expand Down
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 = withdrawAllStakeResponse
StakeTransactionReponse = stakeTransactionReponse
StatusSnapshotResponse = statusSnapshotResponse
StatusResponse = statusResponse
)
Expand Down
20 changes: 18 additions & 2 deletions pkg/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,22 @@ func (s *Service) mountBusinessDebug() {
web.FinalHandlerFunc(s.healthHandler),
))

handle("/stake/migrate", web.ChainHandlers(
s.stakingAccessHandler,
s.gasConfigMiddleware("migrate stake"),
web.FinalHandler(jsonhttp.MethodHandler{
"POST": http.HandlerFunc(s.migrateStakeHandler),
})),
)

handle("/stake/withdrawable", web.ChainHandlers(
s.stakingAccessHandler,
s.gasConfigMiddleware("get withdrawable stake"),
web.FinalHandler(jsonhttp.MethodHandler{
"GET": http.HandlerFunc(s.getWithdrawableStakeHandler),
})),
)

handle("/stake/{amount}", web.ChainHandlers(
s.stakingAccessHandler,
s.gasConfigMiddleware("deposit stake"),
Expand All @@ -571,8 +587,8 @@ func (s *Service) mountBusinessDebug() {
s.stakingAccessHandler,
s.gasConfigMiddleware("get or withdraw stake"),
web.FinalHandler(jsonhttp.MethodHandler{
"GET": http.HandlerFunc(s.getStakedAmountHandler),
"DELETE": http.HandlerFunc(s.withdrawAllStakeHandler),
"GET": http.HandlerFunc(s.getPotentialStake),
"DELETE": http.HandlerFunc(s.withdrawStakeHandler),
})),
)
handle("/redistributionstate", jsonhttp.MethodHandler{
Expand Down
60 changes: 48 additions & 12 deletions pkg/api/staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ func (s *Service) stakingAccessHandler(h http.Handler) http.Handler {
type getStakeResponse struct {
StakedAmount *bigint.BigInt `json:"stakedAmount"`
}
type stakeDepositResponse struct {
TxHash string `json:"txhash"`
}

type withdrawAllStakeResponse struct {
type stakeTransactionReponse struct {
TxHash string `json:"txhash"`
}

Expand Down Expand Up @@ -77,15 +73,29 @@ func (s *Service) stakingDepositHandler(w http.ResponseWriter, r *http.Request)
jsonhttp.InternalServerError(w, "cannot stake")
return
}
jsonhttp.OK(w, stakeDepositResponse{
jsonhttp.OK(w, stakeTransactionReponse{
TxHash: txHash.String(),
})
}

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

stakedAmount, err := s.stakingContract.GetPotentialStake(r.Context())
if err != nil {
logger.Debug("get staked amount failed", "overlayAddr", s.overlay, "error", err)
logger.Error(nil, "get staked amount failed")
jsonhttp.InternalServerError(w, "get staked amount failed")
return
}

jsonhttp.OK(w, getStakeResponse{StakedAmount: bigint.Wrap(stakedAmount)})
}

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

stakedAmount, err := s.stakingContract.GetStake(r.Context())
stakedAmount, err := s.stakingContract.GetWithdrawableStake(r.Context())
if err != nil {
logger.Debug("get staked amount failed", "overlayAddr", s.overlay, "error", err)
logger.Error(nil, "get staked amount failed")
Expand All @@ -96,10 +106,10 @@ 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()
func (s *Service) withdrawStakeHandler(w http.ResponseWriter, r *http.Request) {
logger := s.logger.WithName("withdraw_stake").Build()

txHash, err := s.stakingContract.WithdrawAllStake(r.Context())
txHash, err := s.stakingContract.WithdrawStake(r.Context())
if err != nil {
if errors.Is(err, staking.ErrInsufficientStake) {
logger.Debug("insufficient stake", "overlayAddr", s.overlay, "error", err)
Expand All @@ -113,5 +123,31 @@ func (s *Service) withdrawAllStakeHandler(w http.ResponseWriter, r *http.Request
return
}

jsonhttp.OK(w, withdrawAllStakeResponse{TxHash: txHash.String()})
jsonhttp.OK(w, stakeTransactionReponse{TxHash: txHash.String()})
}

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

txHash, err := s.stakingContract.MigrateStake(r.Context())
if err != nil {
if errors.Is(err, staking.ErrInsufficientStake) {
logger.Debug("insufficient stake", "overlayAddr", s.overlay, "error", err)
logger.Error(nil, "insufficient stake")
jsonhttp.BadRequest(w, "insufficient stake to migrate")
return
}
if errors.Is(err, staking.ErrNotPaused) {
logger.Debug("contract is not paused", "error", err)
logger.Error(nil, "contract is not paused")
jsonhttp.BadRequest(w, "contract is not paused")
return
}
logger.Debug("migrate stake failed", "error", err)
logger.Error(nil, "migrate stake failed")
jsonhttp.InternalServerError(w, "cannot migrate stake")
return
}

jsonhttp.OK(w, stakeTransactionReponse{TxHash: txHash.String()})
}
Loading

0 comments on commit f6d294c

Please sign in to comment.