-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Configs] feat: Add staking config parser of gateway staking (#302)
* feat: Add staking config parser of gateway staking * fix: Pass the config path param * fix: Adapt gateway staking e2e tests * fix: Remove trailing comma from stake amount * fix: Create gateway staking file from test step
- Loading branch information
Showing
10 changed files
with
249 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
stake_amount: 1000upokt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
stake_amount: 1000upokt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
stake_amount: 1000upokt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package config | ||
|
||
import sdkerrors "cosmossdk.io/errors" | ||
|
||
var ( | ||
codespace = "gatewayconfig" | ||
ErrGatewayConfigEmptyContent = sdkerrors.Register(codespace, 1, "empty gateway staking config content") | ||
ErrGatewayConfigUnmarshalYAML = sdkerrors.Register(codespace, 2, "config reader cannot unmarshal yaml content") | ||
ErrGatewayConfigInvalidStake = sdkerrors.Register(codespace, 3, "invalid stake in gateway stake config") | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package config | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
// YAMLStakeGateway is the structure describing the gateway stake config file | ||
type YAMLStakeGateway struct { | ||
StakeAmount string `yaml:"stake_amount"` | ||
} | ||
|
||
// GatewayStakeConfig is the structure describing the gateway stake config | ||
type GatewayStakeConfig struct { | ||
StakeAmount sdk.Coin | ||
} | ||
|
||
// ParseGatewayConfig parses the gateway stake yaml config file into a StakeGatewayConfig struct | ||
func ParseGatewayConfig(configContent []byte) (*GatewayStakeConfig, error) { | ||
var stakeConfig *YAMLStakeGateway | ||
|
||
if len(configContent) == 0 { | ||
return nil, ErrGatewayConfigEmptyContent | ||
} | ||
|
||
// Unmarshal the stake config file into a stakeConfig | ||
if err := yaml.Unmarshal(configContent, &stakeConfig); err != nil { | ||
return nil, ErrGatewayConfigUnmarshalYAML.Wrap(err.Error()) | ||
} | ||
|
||
// Validate the stake config | ||
if len(stakeConfig.StakeAmount) == 0 { | ||
return nil, ErrGatewayConfigInvalidStake | ||
} | ||
|
||
// Parse the stake amount to a coin struct | ||
stakeAmount, err := sdk.ParseCoinNormalized(stakeConfig.StakeAmount) | ||
if err != nil { | ||
return nil, ErrGatewayConfigInvalidStake.Wrap(err.Error()) | ||
} | ||
|
||
// Basic validation of the stake amount | ||
if err := stakeAmount.Validate(); err != nil { | ||
return nil, ErrGatewayConfigInvalidStake.Wrap(err.Error()) | ||
} | ||
|
||
if stakeAmount.IsZero() { | ||
return nil, ErrGatewayConfigInvalidStake.Wrap("stake amount cannot be zero") | ||
} | ||
|
||
// Only allow upokt coins staking | ||
if stakeAmount.Denom != "upokt" { | ||
return nil, ErrGatewayConfigInvalidStake.Wrapf( | ||
"invalid stake denom, expecting: upokt, got: %s", | ||
stakeAmount.Denom, | ||
) | ||
} | ||
|
||
return &GatewayStakeConfig{ | ||
StakeAmount: stakeAmount, | ||
}, nil | ||
} |
Oops, something went wrong.