check spendable when making bid, groundwork for built-in transfers

This commit is contained in:
2022-09-01 06:09:36 +00:00
parent e27f1c7737
commit f67118ebf6
8 changed files with 105 additions and 26 deletions

View File

@@ -32,8 +32,14 @@ func (k *Keeper) EndExpiredAuctions(ctx types.Context) {
if err != nil {
return errors.Errorf("could not get highest bid for auction %s: %s", auctionId, err)
}
k.SetAuction(ctx, auction)
// clear auction
memdb.BidDB.ClearAuction(auctionId)
// pay out user
// end auction
k.SetAuction(ctx, auction)
}
return nil

View File

@@ -6,6 +6,7 @@ import (
"github.com/tendermint/tendermint/libs/log"
"cosmos-test/x/cosmostest/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
@@ -17,6 +18,7 @@ type (
storeKey sdk.StoreKey
memKey sdk.StoreKey
paramstore paramtypes.Subspace
bank types.BankKeeper
}
)
@@ -25,7 +27,7 @@ func NewKeeper(
storeKey,
memKey sdk.StoreKey,
ps paramtypes.Subspace,
bank types.BankKeeper,
) *Keeper {
// set KeyTable if it has not already been set
if !ps.HasKeyTable() {
@@ -33,11 +35,11 @@ func NewKeeper(
}
return &Keeper{
cdc: cdc,
storeKey: storeKey,
memKey: memKey,
paramstore: ps,
bank: bank,
}
}

View File

@@ -15,7 +15,7 @@ import (
func (k msgServer) NewBid(goCtx context.Context, msg *types.MsgNewBid) (*types.MsgNewBidResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
_, found := k.Keeper.GetAuction(ctx, msg.AuctionIndex)
auction, found := k.Keeper.GetAuction(ctx, msg.AuctionIndex)
if !found {
return nil, fmt.Errorf("didn't find auction of index %s", msg.AuctionIndex)
}
@@ -38,6 +38,17 @@ func (k msgServer) NewBid(goCtx context.Context, msg *types.MsgNewBid) (*types.M
return nil, fmt.Errorf("bid amount must be greater than 0")
}
addr, err := sdk.AccAddressFromBech32(msg.Creator)
if err != nil {
return nil, fmt.Errorf("sender address `%s` format invalid (bech32 required)", msg.Creator)
}
spendable := k.bank.SpendableCoins(ctx, addr)
// if balance does not exceed or equal proposed bid amount...
if spendable.AmountOf(auction.Denom).BigInt().Cmp(amt) == -1 {
return nil, fmt.Errorf("not enough balance to bid %s%s", msg.Amount, auction.Denom)
}
lowestBid, err := memdb.BidDB.GetLowestBid(msg.AuctionIndex)
// we manually handle KeyNotFound in GetHighestBid, so should return (nil, nil) if not found
if err != nil {