This commit is contained in:
Sunny Aggarwal 2018-03-25 01:12:44 +01:00
parent 884b827730
commit 8fe34097eb
3 changed files with 43 additions and 12 deletions

View File

@ -1,6 +1,8 @@
package bank
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
)
@ -23,13 +25,11 @@ func (ck CoinKeeper) SubtractCoins(ctx sdk.Context, addr sdk.Address, amt sdk.Co
return amt, sdk.ErrUnknownAddress(addr.String())
}
// coins := acc.GetCoins()
// newCoins := coins.Minus(amt)
// if !newCoins.IsNotNegative() {
// return amt, sdk.ErrInsufficientCoins(fmt.Sprintf("%s < %s", coins, amt))
// }
newCoins := sdk.Coins{}
coins := acc.GetCoins()
newCoins := coins.Minus(amt)
if !newCoins.IsNotNegative() {
return amt, sdk.ErrInsufficientCoins(fmt.Sprintf("%s < %s", coins, amt))
}
// acc.SetCoins(newCoins)
ck.am.SetAccount(ctx, acc)

View File

@ -94,3 +94,34 @@ func (k Keeper) Unbond(ctx sdk.Context, addr sdk.Address) (crypto.PubKey, int64,
return bi.PubKey, bi.Power, nil
}
// FOR TESTING PURPOSES -------------------------------------------------
func (k Keeper) bondWithoutCoins(ctx sdk.Context, addr sdk.Address, pubKey crypto.PubKey, stake sdk.Coin) (int64, sdk.Error) {
if stake.Denom != stakingToken {
return 0, ErrIncorrectStakingToken()
}
bi := k.getBondInfo(ctx, addr)
if bi.isEmpty() {
bi = bondInfo{
PubKey: pubKey,
Power: 0,
}
}
bi.Power = bi.Power + stake.Amount
k.setBondInfo(ctx, addr, bi)
return bi.Power, nil
}
func (k Keeper) unbondWithoutCoins(ctx sdk.Context, addr sdk.Address) (crypto.PubKey, int64, sdk.Error) {
bi := k.getBondInfo(ctx, addr)
if bi.isEmpty() {
return crypto.PubKey{}, 0, ErrInvalidUnbond()
}
k.deleteBondInfo(ctx, addr)
return bi.PubKey, bi.Power, nil
}

View File

@ -65,19 +65,19 @@ func TestBonding(t *testing.T) {
privKey := crypto.GenPrivKeyEd25519()
pubKey := privKey.PubKey()
_, _, err := stakeKeeper.Unbond(ctx, addr)
_, _, err := stakeKeeper.unbondWithoutCoins(ctx, addr)
assert.Equal(t, err, ErrInvalidUnbond())
_, err = stakeKeeper.Bond(ctx, addr, pubKey, sdk.Coin{"steak", 10})
_, err = stakeKeeper.bondWithoutCoins(ctx, addr, pubKey, sdk.Coin{"steak", 10})
assert.Nil(t, err)
power, err := stakeKeeper.Bond(ctx, addr, pubKey, sdk.Coin{"steak", 10})
power, err := stakeKeeper.bondWithoutCoins(ctx, addr, pubKey, sdk.Coin{"steak", 10})
assert.Equal(t, int64(20), power)
pk, _, err := stakeKeeper.Unbond(ctx, addr)
pk, _, err := stakeKeeper.unbondWithoutCoins(ctx, addr)
assert.Nil(t, err)
assert.Equal(t, pubKey, pk)
_, _, err = stakeKeeper.Unbond(ctx, addr)
_, _, err = stakeKeeper.unbondWithoutCoins(ctx, addr)
assert.Equal(t, err, ErrInvalidUnbond())
}