-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #170 from confio/156-handlers
Add PoE staking query plugin
- Loading branch information
Showing
14 changed files
with
593 additions
and
44 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package app | ||
|
||
import ( | ||
"github.com/CosmWasm/wasmd/x/wasm" | ||
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper" | ||
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types" | ||
wasmvmtypes "github.com/CosmWasm/wasmvm/types" | ||
"github.com/cosmos/cosmos-sdk/codec" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" | ||
|
||
poewasm "github.com/confio/tgrade/x/poe/wasm" | ||
twasmkeeper "github.com/confio/tgrade/x/twasm/keeper" | ||
twasmtypes "github.com/confio/tgrade/x/twasm/types" | ||
) | ||
|
||
func SetupWasmHandlers(cdc codec.Marshaler, | ||
bankKeeper twasmtypes.BankKeeper, | ||
govRouter govtypes.Router, | ||
result twasmkeeper.TgradeWasmHandlerKeeper, | ||
poeKeeper poewasm.ViewKeeper, | ||
) []wasmkeeper.Option { | ||
queryPluginOpt := wasmkeeper.WithQueryPlugins(&wasmkeeper.QueryPlugins{ | ||
Staking: poewasm.StakingQuerier(poeKeeper), | ||
}) | ||
|
||
extMessageHandlerOpt := wasmkeeper.WithMessageHandlerDecorator(func(nested wasmkeeper.Messenger) wasmkeeper.Messenger { | ||
return wasmkeeper.NewMessageHandlerChain( | ||
// disable staking messages | ||
wasmkeeper.MessageHandlerFunc(func(ctx sdk.Context, contractAddr sdk.AccAddress, contractIBCPortID string, msg wasmvmtypes.CosmosMsg) (events []sdk.Event, data [][]byte, err error) { | ||
if msg.Staking != nil { | ||
return nil, nil, sdkerrors.Wrap(wasmtypes.ErrExecuteFailed, "not supported, yet") | ||
} | ||
return nil, nil, wasmtypes.ErrUnknownMsg | ||
}), | ||
nested, | ||
// append our custom message handler | ||
twasmkeeper.NewTgradeHandler(cdc, result, bankKeeper, govRouter), | ||
) | ||
}) | ||
return []wasm.Option{ | ||
queryPluginOpt, | ||
extMessageHandlerOpt, | ||
} | ||
} |
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
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,131 @@ | ||
package wasm | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
wasmvmtypes "github.com/CosmWasm/wasmvm/types" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
|
||
"github.com/confio/tgrade/x/poe/keeper" | ||
) | ||
|
||
type ViewKeeper interface { | ||
GetBondDenom(ctx sdk.Context) string | ||
DistributionContract(ctx sdk.Context) keeper.DistributionContract | ||
ValsetContract(ctx sdk.Context) keeper.ValsetContract | ||
StakeContract(ctx sdk.Context) keeper.StakeContract | ||
} | ||
|
||
func StakingQuerier(poeKeeper ViewKeeper) func(ctx sdk.Context, request *wasmvmtypes.StakingQuery) ([]byte, error) { | ||
return func(ctx sdk.Context, request *wasmvmtypes.StakingQuery) ([]byte, error) { | ||
if request.BondedDenom != nil { | ||
denom := poeKeeper.GetBondDenom(ctx) | ||
res := wasmvmtypes.BondedDenomResponse{ | ||
Denom: denom, | ||
} | ||
return json.Marshal(res) | ||
} | ||
zero := sdk.ZeroDec().String() | ||
if request.AllValidators != nil { | ||
validators, err := poeKeeper.ValsetContract(ctx).ListValidators(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
wasmVals := make([]wasmvmtypes.Validator, len(validators)) | ||
for i, v := range validators { | ||
wasmVals[i] = wasmvmtypes.Validator{ | ||
Address: v.OperatorAddress, | ||
Commission: zero, | ||
MaxCommission: zero, | ||
MaxChangeRate: zero, | ||
} | ||
} | ||
res := wasmvmtypes.AllValidatorsResponse{ | ||
Validators: wasmVals, | ||
} | ||
return json.Marshal(res) | ||
} | ||
if request.Validator != nil { | ||
valAddr, err := sdk.AccAddressFromBech32(request.Validator.Address) | ||
if err != nil { | ||
return nil, err | ||
} | ||
v, err := poeKeeper.ValsetContract(ctx).QueryValidator(ctx, valAddr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
res := wasmvmtypes.ValidatorResponse{} | ||
if v != nil { | ||
res.Validator = &wasmvmtypes.Validator{ | ||
Address: v.OperatorAddress, | ||
Commission: zero, | ||
MaxCommission: zero, | ||
MaxChangeRate: zero, | ||
} | ||
} | ||
return json.Marshal(res) | ||
} | ||
if request.AllDelegations != nil { | ||
delegator, err := sdk.AccAddressFromBech32(request.AllDelegations.Delegator) | ||
if err != nil { | ||
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, request.AllDelegations.Delegator) | ||
} | ||
stakedAmount, err := poeKeeper.StakeContract(ctx).QueryStakedAmount(ctx, delegator) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var res wasmvmtypes.AllDelegationsResponse | ||
if stakedAmount != nil { | ||
res.Delegations = []wasmvmtypes.Delegation{{ | ||
Delegator: delegator.String(), | ||
Validator: delegator.String(), | ||
Amount: wasmvmtypes.NewCoin(stakedAmount.Uint64(), poeKeeper.GetBondDenom(ctx)), | ||
}} | ||
} | ||
return json.Marshal(res) | ||
} | ||
if request.Delegation != nil { | ||
delegator, err := sdk.AccAddressFromBech32(request.Delegation.Delegator) | ||
if err != nil { | ||
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, request.Delegation.Delegator) | ||
} | ||
validator, err := sdk.AccAddressFromBech32(request.Delegation.Validator) | ||
if err != nil { | ||
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, request.Delegation.Validator) | ||
} | ||
|
||
var res wasmvmtypes.DelegationResponse | ||
if !delegator.Equals(validator) { // no match | ||
return json.Marshal(res) | ||
} | ||
stakeContract := poeKeeper.StakeContract(ctx) | ||
stakedAmount, err := stakeContract.QueryStakedAmount(ctx, delegator) | ||
if err != nil { | ||
return nil, sdkerrors.Wrap(err, "query staked amount") | ||
} | ||
reward, err := poeKeeper.DistributionContract(ctx).ValidatorOutstandingReward(ctx, delegator) | ||
if err != nil { | ||
return nil, sdkerrors.Wrap(err, "query outstanding reward") | ||
} | ||
if stakedAmount == nil { | ||
zeroInt := sdk.ZeroInt() | ||
stakedAmount = &zeroInt | ||
} | ||
// there can be unclaimed rewards while all stacked amounts were unbound | ||
if stakedAmount.GT(sdk.ZeroInt()) || reward.Amount.GT(sdk.ZeroInt()) { | ||
bondDenom := poeKeeper.GetBondDenom(ctx) | ||
stakedCoin := wasmvmtypes.NewCoin(stakedAmount.Uint64(), bondDenom) | ||
res.Delegation = &wasmvmtypes.FullDelegation{ | ||
Delegator: delegator.String(), | ||
Validator: delegator.String(), | ||
Amount: stakedCoin, | ||
CanRedelegate: wasmvmtypes.NewCoin(0, bondDenom), | ||
AccumulatedRewards: wasmvmtypes.Coins{wasmvmtypes.NewCoin(reward.Amount.Uint64(), reward.Denom)}, | ||
} | ||
} | ||
return json.Marshal(res) | ||
} | ||
return nil, wasmvmtypes.UnsupportedRequest{Kind: "unknown Staking variant"} | ||
} | ||
} |
Oops, something went wrong.