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

master
michael 2022-09-01 06:09:36 +00:00
parent e27f1c7737
commit f67118ebf6
8 changed files with 105 additions and 26 deletions

View File

@ -394,6 +394,7 @@ func New(
keys[cosmostestmoduletypes.StoreKey],
keys[cosmostestmoduletypes.MemStoreKey],
app.GetSubspace(cosmostestmoduletypes.ModuleName),
app.BankKeeper,
)
cosmostestModule := cosmostestmodule.NewAppModule(appCodec, app.CosmostestKeeper, app.AccountKeeper, app.BankKeeper)

1
go.mod
View File

@ -87,6 +87,7 @@ require (
github.com/gorilla/handlers v1.5.1 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.11.3 // indirect
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c // indirect
github.com/gtank/merlin v0.1.1 // indirect
github.com/gtank/ristretto255 v0.1.2 // indirect

2
go.sum
View File

@ -734,6 +734,8 @@ github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t
github.com/grpc-ecosystem/grpc-gateway v1.9.5/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/grpc-ecosystem/grpc-gateway v1.16.0 h1:gmcG1KaJ57LophUzW0Hy8NmPhnMZb4M0+kPpLofRdBo=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.11.3 h1:lLT7ZLSzGLI08vc9cpd+tYmNWjdKDqyr/2L+f6U12Fk=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.11.3/go.mod h1:o//XUCC/F+yRGJoPO/VU0GSB0f8Nhgmxx0VIRUvaC0w=
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c h1:6rhixN/i8ZofjG1Y75iExal34USq5p+wiN1tpie8IrU=
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c/go.mod h1:NMPJylDgVpX0MLRlPy15sqSwOFv/U1GZ2m21JhFfek0=
github.com/gtank/merlin v0.1.1-0.20191105220539-8318aed1a79f/go.mod h1:T86dnYJhcGOh5BjZFCJWTDeTK7XW8uE+E21Cy/bIQ+s=

View File

@ -5,6 +5,7 @@ import (
"cosmos-test/x/cosmostest/keeper"
"cosmos-test/x/cosmostest/types"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/store"
@ -36,11 +37,14 @@ func CosmostestKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
memStoreKey,
"CosmostestParams",
)
k := keeper.NewKeeper(
cdc,
storeKey,
memStoreKey,
paramsSubspace,
// probably fix this later o_0
*new(types.BankKeeper),
)
ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger())

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 {

View File

@ -31,7 +31,8 @@ type MsgNewAuction struct {
Creator string `protobuf:"bytes,1,opt,name=creator,proto3" json:"creator,omitempty"`
Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"`
Description string `protobuf:"bytes,3,opt,name=description,proto3" json:"description,omitempty"`
Denom string `protobuf:"bytes,4,opt,name=denom,proto3" json:"denom,omitempty"`
Ceiling string `protobuf:"bytes,4,opt,name=ceiling,proto3" json:"ceiling,omitempty"`
Denom string `protobuf:"bytes,5,opt,name=denom,proto3" json:"denom,omitempty"`
}
func (m *MsgNewAuction) Reset() { *m = MsgNewAuction{} }
@ -88,6 +89,13 @@ func (m *MsgNewAuction) GetDescription() string {
return ""
}
func (m *MsgNewAuction) GetCeiling() string {
if m != nil {
return m.Ceiling
}
return ""
}
func (m *MsgNewAuction) GetDenom() string {
if m != nil {
return m.Denom
@ -245,27 +253,28 @@ func init() {
func init() { proto.RegisterFile("cosmostest/tx.proto", fileDescriptor_2fcd5aa4ac15d93c) }
var fileDescriptor_2fcd5aa4ac15d93c = []byte{
// 306 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x4e, 0xce, 0x2f, 0xce,
0xcd, 0x2f, 0x2e, 0x49, 0x2d, 0x2e, 0xd1, 0x2f, 0xa9, 0xd0, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17,
0x12, 0x45, 0x08, 0xea, 0x21, 0x98, 0x4a, 0xa5, 0x5c, 0xbc, 0xbe, 0xc5, 0xe9, 0x7e, 0xa9, 0xe5,
0x8e, 0xa5, 0xc9, 0x25, 0x99, 0xf9, 0x79, 0x42, 0x12, 0x5c, 0xec, 0xc9, 0x45, 0xa9, 0x89, 0x25,
0xf9, 0x45, 0x12, 0x8c, 0x0a, 0x8c, 0x1a, 0x9c, 0x41, 0x30, 0xae, 0x90, 0x10, 0x17, 0x4b, 0x5e,
0x62, 0x6e, 0xaa, 0x04, 0x13, 0x58, 0x18, 0xcc, 0x16, 0x52, 0xe0, 0xe2, 0x4e, 0x49, 0x2d, 0x4e,
0x2e, 0xca, 0x2c, 0x00, 0x69, 0x96, 0x60, 0x06, 0x4b, 0x21, 0x0b, 0x09, 0x89, 0x70, 0xb1, 0xa6,
0xa4, 0xe6, 0xe5, 0xe7, 0x4a, 0xb0, 0x80, 0xe5, 0x20, 0x1c, 0x25, 0x53, 0x2e, 0x51, 0x14, 0x6b,
0x83, 0x52, 0x8b, 0x0b, 0xf2, 0xf3, 0x8a, 0x53, 0x85, 0x64, 0xb8, 0x38, 0x13, 0x21, 0x42, 0x9e,
0x29, 0x50, 0x07, 0x20, 0x04, 0x94, 0x12, 0xb9, 0x38, 0x21, 0xda, 0x9c, 0x32, 0x53, 0xf0, 0xb8,
0x54, 0x89, 0x8b, 0x07, 0xa6, 0x27, 0x2f, 0x25, 0xb5, 0x02, 0xea, 0x62, 0x14, 0x31, 0x21, 0x31,
0x2e, 0xb6, 0xc4, 0xdc, 0xfc, 0xd2, 0xbc, 0x12, 0xa8, 0xa3, 0xa1, 0x3c, 0x25, 0x61, 0x2e, 0x41,
0xb8, 0x15, 0x30, 0x57, 0x19, 0xed, 0x65, 0xe4, 0x62, 0xf6, 0x2d, 0x4e, 0x17, 0x4a, 0xe0, 0xe2,
0x42, 0x0a, 0x2a, 0x15, 0x3d, 0xac, 0x61, 0xaa, 0x87, 0xe2, 0x33, 0x29, 0x1d, 0x62, 0x54, 0xc1,
0xfd, 0x1f, 0xc2, 0xc5, 0x06, 0xf5, 0x9e, 0x02, 0x5e, 0x7d, 0x4e, 0x99, 0x29, 0x52, 0x1a, 0x84,
0x54, 0xc0, 0x4c, 0x75, 0xb2, 0x38, 0xf1, 0x48, 0x8e, 0xf1, 0xc2, 0x23, 0x39, 0xc6, 0x07, 0x8f,
0xe4, 0x18, 0x27, 0x3c, 0x96, 0x63, 0xb8, 0xf0, 0x58, 0x8e, 0xe1, 0xc6, 0x63, 0x39, 0x86, 0x28,
0x39, 0x88, 0x3e, 0x5d, 0x70, 0x62, 0xa9, 0xd0, 0x47, 0x4e, 0x39, 0x95, 0x05, 0xa9, 0xc5, 0x49,
0x6c, 0xe0, 0xd4, 0x63, 0x0c, 0x08, 0x00, 0x00, 0xff, 0xff, 0x30, 0x36, 0x68, 0x3f, 0x54, 0x02,
0x00, 0x00,
// 321 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x92, 0x3f, 0x4f, 0xf3, 0x30,
0x10, 0xc6, 0xeb, 0xb7, 0x7f, 0x5e, 0xf5, 0x80, 0x01, 0x97, 0x22, 0xab, 0x42, 0x56, 0x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xb5, 0x92, 0xc6, 0xc5, 0x71, 0xca, 0x1b, 0xc0,
0xfe, 0xe6, 0x17, 0xe5, 0x54, 0x67, 0x6f, 0x08, 0xda, 0x73, 0xbd, 0xc4, 0xf7, 0x00, 0xb5, 0x25,
0x1e, 0xfb, 0x3f, 0xae, 0xdb, 0x6f, 0x24, 0x1b, 0x9d, 0xfc, 0xa5, 0x6a, 0x93, 0xff, 0x16, 0x7a,
0x2e, 0xde, 0x78, 0xab, 0x6f, 0x26, 0xd8, 0x68, 0xf2, 0x5b, 0x45, 0xd9, 0x75, 0x76, 0xf9, 0x9e,
0x51, 0xb4, 0xce, 0x28, 0xfa, 0xcc, 0x28, 0x7a, 0xcd, 0x69, 0x6b, 0x9d, 0xd3, 0xd6, 0x47, 0x4e,
0x5b, 0x77, 0xb4, 0xf0, 0x9d, 0xda, 0x3b, 0x4a, 0xa7, 0xf5, 0xa3, 0x7a, 0x8e, 0xb9, 0x7e, 0xe8,
0xd9, 0xc3, 0x3a, 0xff, 0x0a, 0x00, 0x00, 0xff, 0xff, 0x1b, 0x53, 0xda, 0x87, 0x6f, 0x02, 0x00,
0x00,
}
// Reference imports to suppress errors if they are not otherwise used.
@ -409,6 +418,13 @@ func (m *MsgNewAuction) MarshalToSizedBuffer(dAtA []byte) (int, error) {
copy(dAtA[i:], m.Denom)
i = encodeVarintTx(dAtA, i, uint64(len(m.Denom)))
i--
dAtA[i] = 0x2a
}
if len(m.Ceiling) > 0 {
i -= len(m.Ceiling)
copy(dAtA[i:], m.Ceiling)
i = encodeVarintTx(dAtA, i, uint64(len(m.Ceiling)))
i--
dAtA[i] = 0x22
}
if len(m.Description) > 0 {
@ -561,6 +577,10 @@ func (m *MsgNewAuction) Size() (n int) {
if l > 0 {
n += 1 + l + sovTx(uint64(l))
}
l = len(m.Ceiling)
if l > 0 {
n += 1 + l + sovTx(uint64(l))
}
l = len(m.Denom)
if l > 0 {
n += 1 + l + sovTx(uint64(l))
@ -743,6 +763,38 @@ func (m *MsgNewAuction) Unmarshal(dAtA []byte) error {
m.Description = string(dAtA[iNdEx:postIndex])
iNdEx = postIndex
case 4:
if wireType != 2 {
return fmt.Errorf("proto: wrong wireType = %d for field Ceiling", wireType)
}
var stringLen uint64
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return ErrIntOverflowTx
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
stringLen |= uint64(b&0x7F) << shift
if b < 0x80 {
break
}
}
intStringLen := int(stringLen)
if intStringLen < 0 {
return ErrInvalidLengthTx
}
postIndex := iNdEx + intStringLen
if postIndex < 0 {
return ErrInvalidLengthTx
}
if postIndex > l {
return io.ErrUnexpectedEOF
}
m.Ceiling = string(dAtA[iNdEx:postIndex])
iNdEx = postIndex
case 5:
if wireType != 2 {
return fmt.Errorf("proto: wrong wireType = %d for field Denom", wireType)
}