gpu-compute-chain/x/cosmostest/keeper/msg_server_new_auction.go

71 lines
1.9 KiB
Go
Raw Normal View History

2022-08-27 14:19:03 -07:00
package keeper
import (
"context"
2022-08-27 19:48:00 -07:00
"errors"
"fmt"
"math/big"
2022-08-27 19:48:00 -07:00
"strconv"
2022-08-27 14:19:03 -07:00
2022-08-31 15:40:28 -07:00
"cosmos-test/x/cosmostest/auctionconfig"
2022-08-27 14:19:03 -07:00
"cosmos-test/x/cosmostest/types"
2022-08-27 19:48:00 -07:00
2022-08-27 14:19:03 -07:00
sdk "github.com/cosmos/cosmos-sdk/types"
)
func (k msgServer) NewAuction(goCtx context.Context, msg *types.MsgNewAuction) (*types.MsgNewAuctionResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
2022-08-27 19:48:00 -07:00
next, found := k.Keeper.GetNextAuction(ctx)
if !found {
return nil, errors.New("Auction not found")
}
index := strconv.FormatUint(next.AuctionId, 10)
auction := types.Auction{
Index: index,
Name: msg.Name,
Description: msg.Description,
// Best: new(types.Bid),
2022-09-02 14:03:03 -07:00
Deadline: uint64(ctx.BlockHeight()) + auctionconfig.AuctionTime,
Denom: msg.Denom,
Owner: msg.Creator,
Ceiling: msg.Ceiling,
LeaseStart: uint64(ctx.BlockTime().Unix()),
LeaseEnd: msg.LeaseEnd,
Closed: false,
Remaining: "0",
}
2022-09-01 00:08:30 -07:00
senderAddr, err := sdk.AccAddressFromBech32(msg.Creator)
if err != nil {
return nil, fmt.Errorf("sender address `%s` format invalid (bech32 required)", msg.Creator)
}
2022-09-01 00:08:30 -07:00
spendable := k.bank.SpendableCoins(ctx, senderAddr)
// if balance does not exceed or equal proposed auction ceiling...
ceiling := new(big.Int)
ceiling.SetString(auction.Ceiling, 10)
if spendable.AmountOf(auction.Denom).BigInt().Cmp(ceiling) == -1 {
return nil, fmt.Errorf("not enough balance to set ceiling %s%s", msg.Ceiling, auction.Denom)
2022-08-27 19:48:00 -07:00
}
2022-09-01 00:08:30 -07:00
coins := sdk.NewCoins(sdk.Coin{
Amount: spendable.AmountOf(auction.Ceiling),
Denom: auction.Denom,
})
if err := k.bank.SendCoinsFromAccountToModule(ctx, senderAddr, "cosmostest", coins); err != nil {
return nil, fmt.Errorf("failed to transfer %s%s", auction.Ceiling, auction.Denom)
}
2022-08-27 19:48:00 -07:00
k.Keeper.SetAuction(ctx, auction)
next.AuctionId++
k.Keeper.SetNextAuction(ctx, types.NextAuction{AuctionId: next.AuctionId})
2022-08-27 14:19:03 -07:00
2022-08-27 19:48:00 -07:00
return &types.MsgNewAuctionResponse{
AuctionId: strconv.FormatUint(next.AuctionId, 10),
}, nil
2022-08-27 14:19:03 -07:00
}