-
Notifications
You must be signed in to change notification settings - Fork 12
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
Add custom query for slash ratios #84
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -14,21 +14,27 @@ import ( | |
"github.com/osmosis-labs/mesh-security-sdk/x/meshsecurity/contract" | ||
) | ||
|
||
// abstract query keeper | ||
type viewKeeper interface { | ||
GetMaxCapLimit(ctx sdk.Context, actor sdk.AccAddress) sdk.Coin | ||
GetTotalDelegated(ctx sdk.Context, actor sdk.AccAddress) sdk.Coin | ||
} | ||
type ( | ||
// abstract query keeper | ||
viewKeeper interface { | ||
GetMaxCapLimit(ctx sdk.Context, actor sdk.AccAddress) sdk.Coin | ||
GetTotalDelegated(ctx sdk.Context, actor sdk.AccAddress) sdk.Coin | ||
} | ||
slashingKeeper interface { | ||
SlashFractionDoubleSign(ctx sdk.Context) (res sdk.Dec) | ||
SlashFractionDowntime(ctx sdk.Context) (res sdk.Dec) | ||
} | ||
) | ||
|
||
// NewQueryDecorator constructor to build a chained custom querier. | ||
// The mesh-security custom query handler is placed at the first position | ||
// and delegates to the next in chain for any queries that do not match | ||
// the mesh-security custom query namespace. | ||
// | ||
// To be used with `wasmkeeper.WithQueryHandlerDecorator(meshseckeeper.NewQueryDecorator(app.MeshSecKeeper)))` | ||
func NewQueryDecorator(k viewKeeper) func(wasmkeeper.WasmVMQueryHandler) wasmkeeper.WasmVMQueryHandler { | ||
func NewQueryDecorator(k viewKeeper, sk slashingKeeper) func(wasmkeeper.WasmVMQueryHandler) wasmkeeper.WasmVMQueryHandler { | ||
return func(next wasmkeeper.WasmVMQueryHandler) wasmkeeper.WasmVMQueryHandler { | ||
return ChainedCustomQuerier(k, next) | ||
return ChainedCustomQuerier(k, sk, next) | ||
} | ||
} | ||
|
||
|
@@ -38,9 +44,12 @@ func NewQueryDecorator(k viewKeeper) func(wasmkeeper.WasmVMQueryHandler) wasmkee | |
// | ||
// This CustomQuerier is designed as an extension point. See the NewQueryDecorator impl how to | ||
// set this up for wasmd. | ||
func ChainedCustomQuerier(k viewKeeper, next wasmkeeper.WasmVMQueryHandler) wasmkeeper.WasmVMQueryHandler { | ||
func ChainedCustomQuerier(k viewKeeper, sk slashingKeeper, next wasmkeeper.WasmVMQueryHandler) wasmkeeper.WasmVMQueryHandler { | ||
if k == nil { | ||
panic("keeper must not be nil") | ||
panic("ms keeper must not be nil") | ||
} | ||
if sk == nil { | ||
panic("slashing Keeper must not be nil") | ||
} | ||
if next == nil { | ||
panic("next handler must not be nil") | ||
|
@@ -53,22 +62,31 @@ func ChainedCustomQuerier(k viewKeeper, next wasmkeeper.WasmVMQueryHandler) wasm | |
if err := json.Unmarshal(request.Custom, &contractQuery); err != nil { | ||
return nil, errorsmod.Wrap(err, "mesh-security query") | ||
} | ||
if contractQuery.VirtualStake == nil || contractQuery.VirtualStake.BondStatus == nil { | ||
query := contractQuery.VirtualStake | ||
if query == nil { | ||
return next.HandleQuery(ctx, caller, request) | ||
} | ||
contractAddr, err := sdk.AccAddressFromBech32(contractQuery.VirtualStake.BondStatus.Contract) | ||
if err != nil { | ||
return nil, sdkerrors.ErrInvalidAddress.Wrap(contractQuery.VirtualStake.BondStatus.Contract) | ||
} | ||
res := contract.BondStatusResponse{ | ||
MaxCap: wasmkeeper.ConvertSdkCoinToWasmCoin(k.GetMaxCapLimit(ctx, contractAddr)), | ||
Delegated: wasmkeeper.ConvertSdkCoinToWasmCoin(k.GetTotalDelegated(ctx, contractAddr)), | ||
} | ||
bz, err := json.Marshal(res) | ||
if err != nil { | ||
return nil, errorsmod.Wrap(err, "mesh-security max cap query response") | ||
|
||
var res any | ||
switch { | ||
case query.BondStatus != nil: | ||
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. nice! |
||
contractAddr, err := sdk.AccAddressFromBech32(query.BondStatus.Contract) | ||
if err != nil { | ||
return nil, sdkerrors.ErrInvalidAddress.Wrap(query.BondStatus.Contract) | ||
} | ||
res = contract.BondStatusResponse{ | ||
MaxCap: wasmkeeper.ConvertSdkCoinToWasmCoin(k.GetMaxCapLimit(ctx, contractAddr)), | ||
Delegated: wasmkeeper.ConvertSdkCoinToWasmCoin(k.GetTotalDelegated(ctx, contractAddr)), | ||
} | ||
case query.SlashRatio != nil: | ||
res = contract.SlashRatioResponse{ | ||
SlashFractionDowntime: sk.SlashFractionDowntime(ctx).String(), | ||
SlashFractionDoubleSign: sk.SlashFractionDoubleSign(ctx).String(), | ||
} | ||
default: | ||
return nil, wasmvmtypes.UnsupportedRequest{Kind: "unknown virtual_stake query variant"} | ||
} | ||
return bz, nil | ||
return json.Marshal(res) | ||
}) | ||
} | ||
|
||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
👍