Better calculation of Delegation.CanRedelegate

This commit is contained in:
Ethan Frey
2020-10-06 20:01:45 +02:00
parent dda0bf0f88
commit 0b290cbab4
2 changed files with 73 additions and 5 deletions

View File

@@ -220,17 +220,25 @@ func sdkToFullDelegation(ctx sdk.Context, keeper staking.Keeper, delegation stak
bondDenom := keeper.BondDenom(ctx)
amount := sdk.NewCoin(bondDenom, val.TokensFromShares(delegation.Shares).TruncateInt())
// can relegate? other query for redelegations?
// keeper.GetRedelegation
delegationCoins := convertSdkCoinToWasmCoin(amount)
// FIXME: this is very rough but better than nothing...
// if this (val, delegate) pair is receiving a redelegation, it cannot redelegate more
// otherwise, it can redelegate the full amount
// (there are cases of partial funds redelegated, but this is a start)
redelegateCoins := wasmTypes.NewCoin(0, bondDenom)
if !keeper.HasReceivingRedelegation(ctx, delegation.DelegatorAddress, delegation.ValidatorAddress) {
redelegateCoins = delegationCoins
}
return &wasmTypes.FullDelegation{
Delegator: delegation.DelegatorAddress.String(),
Validator: delegation.ValidatorAddress.String(),
Amount: convertSdkCoinToWasmCoin(amount),
Amount: delegationCoins,
// TODO: AccumulatedRewards
AccumulatedRewards: wasmTypes.Coins{},
// TODO: Determine redelegate
CanRedelegate: wasmTypes.NewCoin(0, bondDenom),
CanRedelegate: redelegateCoins,
}, nil
}

View File

@@ -534,9 +534,69 @@ func TestQueryStakingInfo(t *testing.T) {
// this is a different Coin type, with String not BigInt, compare field by field
require.Equal(t, funds[0].Denom, delInfo2.Amount.Denom)
require.Equal(t, funds[0].Amount.String(), delInfo2.Amount.Amount)
// TODO: fix this - these should return real values!!! Issue #263
require.Len(t, delInfo2.AccumulatedRewards, 0)
require.Equal(t, delInfo2.CanRedelegate, wasmTypes.NewCoin(0, "stake"))
require.Equal(t, wasmTypes.NewCoin(200000, "stake"), delInfo2.CanRedelegate)
}
func TestQueryStakingPlugin(t *testing.T) {
// STEP 1: take a lot of setup from TestReinvest so we have non-zero info
initInfo := initializeStaking(t)
defer initInfo.cleanup()
ctx, valAddr, contractAddr := initInfo.ctx, initInfo.valAddr, initInfo.contractAddr
keeper, stakingKeeper, accKeeper := initInfo.wasmKeeper, initInfo.stakingKeeper, initInfo.accKeeper
distKeeper := initInfo.distKeeper
// initial checks of bonding state
val, found := stakingKeeper.GetValidator(ctx, valAddr)
require.True(t, found)
assert.Equal(t, sdk.NewInt(1000000), val.Tokens)
// full is 2x funds, 1x goes to the contract, other stays on his wallet
full := sdk.NewCoins(sdk.NewInt64Coin("stake", 400000))
funds := sdk.NewCoins(sdk.NewInt64Coin("stake", 200000))
bob := createFakeFundedAccount(ctx, accKeeper, full)
// we will stake 200k to a validator with 1M self-bond
// this means we should get 1/6 of the rewards
bond := StakingHandleMsg{
Bond: &struct{}{},
}
bondBz, err := json.Marshal(bond)
require.NoError(t, err)
_, err = keeper.Execute(ctx, contractAddr, bob, bondBz, funds)
require.NoError(t, err)
// update height a bit to solidify the delegation
ctx = nextBlock(ctx, stakingKeeper)
// we get 1/6, our share should be 40k minus 10% commission = 36k
setValidatorRewards(ctx, stakingKeeper, distKeeper, valAddr, "240000")
// Step 2: Try out the query plugins
query := wasmTypes.StakingQuery{
Delegation: &wasmTypes.DelegationQuery{
Delegator: contractAddr.String(),
Validator: valAddr.String(),
},
}
raw, err := StakingQuerier(stakingKeeper)(ctx, &query)
require.NoError(t, err)
var res wasmTypes.DelegationResponse
mustParse(t, raw, &res)
assert.NotEmpty(t, res.Delegation)
delInfo := res.Delegation
// Note: this ValAddress not AccAddress, may change with #264
require.Equal(t, valAddr.String(), delInfo.Validator)
// note this is not bob (who staked to the contract), but the contract itself
require.Equal(t, contractAddr.String(), delInfo.Delegator)
// this is a different Coin type, with String not BigInt, compare field by field
require.Equal(t, funds[0].Denom, delInfo.Amount.Denom)
require.Equal(t, funds[0].Amount.String(), delInfo.Amount.Amount)
// TODO: fix this - these should return real values!!! Issue #263
require.Equal(t, wasmTypes.NewCoin(200000, "stake"), delInfo.CanRedelegate)
require.Len(t, delInfo.AccumulatedRewards, 0)
}
// adds a few validators and returns a list of validators that are registered