From 75c051af04a430f836d17435fd4a65fb56aae902 Mon Sep 17 00:00:00 2001 From: turtlebasket Date: Fri, 2 Sep 2022 21:03:03 +0000 Subject: [PATCH] WIP type updates --- docs/static/openapi.yml | 35 ++++ go.mod | 1 + go.sum | 2 + proto/cosmostest/auction.proto | 5 +- x/cosmostest/keeper/auction_state.go | 37 +++- x/cosmostest/keeper/msg_server_new_auction.go | 12 +- x/cosmostest/types/auction.pb.go | 168 ++++++++++++++++-- 7 files changed, 234 insertions(+), 26 deletions(-) diff --git a/docs/static/openapi.yml b/docs/static/openapi.yml index e33bc8d..ebf602f 100644 --- a/docs/static/openapi.yml +++ b/docs/static/openapi.yml @@ -20159,9 +20159,16 @@ paths: type: string owner: type: string + leaseStart: + type: string + format: uint64 leaseEnd: type: string format: uint64 + closed: + type: boolean + remaining: + type: string pagination: type: object properties: @@ -20301,9 +20308,16 @@ paths: type: string owner: type: string + leaseStart: + type: string + format: uint64 leaseEnd: type: string format: uint64 + closed: + type: boolean + remaining: + type: string default: description: An unexpected error response. schema: @@ -47490,9 +47504,16 @@ definitions: type: string owner: type: string + leaseStart: + type: string + format: uint64 leaseEnd: type: string format: uint64 + closed: + type: boolean + remaining: + type: string cosmostest.cosmostest.Bid: type: object properties: @@ -47546,9 +47567,16 @@ definitions: type: string owner: type: string + leaseStart: + type: string + format: uint64 leaseEnd: type: string format: uint64 + closed: + type: boolean + remaining: + type: string pagination: type: object properties: @@ -47614,9 +47642,16 @@ definitions: type: string owner: type: string + leaseStart: + type: string + format: uint64 leaseEnd: type: string format: uint64 + closed: + type: boolean + remaining: + type: string cosmostest.cosmostest.QueryGetNextAuctionResponse: type: object properties: diff --git a/go.mod b/go.mod index ff93dce..9ab26ca 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 07b1083..ad897ba 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/proto/cosmostest/auction.proto b/proto/cosmostest/auction.proto index 33e462a..2d56b6b 100644 --- a/proto/cosmostest/auction.proto +++ b/proto/cosmostest/auction.proto @@ -14,5 +14,8 @@ message Auction { string ceiling = 6; string denom = 7; string owner = 8; - uint64 leaseEnd = 9; + uint64 leaseStart = 9; + uint64 leaseEnd = 10; + bool closed = 11; + string remaining = 12; } diff --git a/x/cosmostest/keeper/auction_state.go b/x/cosmostest/keeper/auction_state.go index 70e76a3..5748863 100644 --- a/x/cosmostest/keeper/auction_state.go +++ b/x/cosmostest/keeper/auction_state.go @@ -1,8 +1,10 @@ package keeper import ( + "cosmos-test/x/cosmostest/math" "cosmos-test/x/cosmostest/memdb" "math/big" + "time" "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types" @@ -25,7 +27,7 @@ func (k *Keeper) FinalizeExpiredAuctions(ctx types.Context) { auction, found := k.GetAuction(ctx, auctionId) // make sure the auction exists on-chain if !found { - return errors.Errorf("auction %s not found on-chain", auctionId) + return errors.Errorf("auction %s not found", auctionId) } if uint64(ctx.BlockHeight()) >= auction.Deadline { @@ -35,6 +37,9 @@ func (k *Keeper) FinalizeExpiredAuctions(ctx types.Context) { return errors.Errorf("could not get highest bid for auction %s: %s", auctionId, err) } + // Remainig Unpaid: Full bid amount + auction.Remaining = auction.Best.Amount + // clear auction memdb.BidDB.ClearAuction(auctionId) @@ -62,6 +67,34 @@ func (k *Keeper) FinalizeExpiredAuctions(ctx types.Context) { }) } -func (k *Keeper) VestAuctionPayments(ctx types.Context) { +func (k *Keeper) PayAuctionAmountDue(ctx types.Context, auctionId string) error { + auction, found := k.GetAuction(ctx, auctionId) + if !found { + return errors.Errorf("auction %s not found", auctionId) + } + blockTime := ctx.BlockTime() + deadline := time.Unix(int64(auction.Deadline), 0) + + if blockTime.After(deadline) { + return nil + } else { + amtTotal := new(big.Int) + amtTotal.SetString(auction.Best.Amount, 10) + amtRemaining := new(big.Int) + amtTotal.SetString(auction.Remaining, 10) + amt, err := math.CalcAmountVestableLinear( + amtTotal, + amtRemaining, + ctx.BlockTime(), + time.Unix(int64(auction.LeaseStart), 0), + time.Unix(int64(auction.LeaseEnd), 0), + ) + if err != nil { + return err + } + coins := sdk.NewCoins(sdk.NewCoin(auction.Denom, sdk.NewIntFromBigInt(amt))) + err = k.bank.SendCoinsFromModuleToAccount(ctx, "cosmostest", sdk.AccAddress(auction.Best.Owner), coins) + return err + } } diff --git a/x/cosmostest/keeper/msg_server_new_auction.go b/x/cosmostest/keeper/msg_server_new_auction.go index 2167b78..23b86f9 100644 --- a/x/cosmostest/keeper/msg_server_new_auction.go +++ b/x/cosmostest/keeper/msg_server_new_auction.go @@ -27,10 +27,14 @@ func (k msgServer) NewAuction(goCtx context.Context, msg *types.MsgNewAuction) ( Name: msg.Name, Description: msg.Description, // Best: new(types.Bid), - Deadline: uint64(ctx.BlockHeight()) + auctionconfig.AuctionTime, - Denom: msg.Denom, - Owner: msg.Creator, - Ceiling: msg.Ceiling, + 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", } senderAddr, err := sdk.AccAddressFromBech32(msg.Creator) diff --git a/x/cosmostest/types/auction.pb.go b/x/cosmostest/types/auction.pb.go index cf2e418..aa762e9 100644 --- a/x/cosmostest/types/auction.pb.go +++ b/x/cosmostest/types/auction.pb.go @@ -31,7 +31,10 @@ type Auction struct { Ceiling string `protobuf:"bytes,6,opt,name=ceiling,proto3" json:"ceiling,omitempty"` Denom string `protobuf:"bytes,7,opt,name=denom,proto3" json:"denom,omitempty"` Owner string `protobuf:"bytes,8,opt,name=owner,proto3" json:"owner,omitempty"` - LeaseEnd uint64 `protobuf:"varint,9,opt,name=leaseEnd,proto3" json:"leaseEnd,omitempty"` + LeaseStart uint64 `protobuf:"varint,9,opt,name=leaseStart,proto3" json:"leaseStart,omitempty"` + LeaseEnd uint64 `protobuf:"varint,10,opt,name=leaseEnd,proto3" json:"leaseEnd,omitempty"` + Closed bool `protobuf:"varint,11,opt,name=closed,proto3" json:"closed,omitempty"` + Remaining string `protobuf:"bytes,12,opt,name=remaining,proto3" json:"remaining,omitempty"` } func (m *Auction) Reset() { *m = Auction{} } @@ -123,6 +126,13 @@ func (m *Auction) GetOwner() string { return "" } +func (m *Auction) GetLeaseStart() uint64 { + if m != nil { + return m.LeaseStart + } + return 0 +} + func (m *Auction) GetLeaseEnd() uint64 { if m != nil { return m.LeaseEnd @@ -130,6 +140,20 @@ func (m *Auction) GetLeaseEnd() uint64 { return 0 } +func (m *Auction) GetClosed() bool { + if m != nil { + return m.Closed + } + return false +} + +func (m *Auction) GetRemaining() string { + if m != nil { + return m.Remaining + } + return "" +} + func init() { proto.RegisterType((*Auction)(nil), "cosmostest.cosmostest.Auction") } @@ -137,24 +161,27 @@ func init() { func init() { proto.RegisterFile("cosmostest/auction.proto", fileDescriptor_631f6f59914101d9) } var fileDescriptor_631f6f59914101d9 = []byte{ - // 271 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x90, 0xb1, 0x4e, 0xc3, 0x30, - 0x10, 0x86, 0xe3, 0x92, 0x36, 0xad, 0xbb, 0x59, 0x45, 0x3a, 0x65, 0xb0, 0x22, 0xa6, 0x2c, 0xb8, - 0x12, 0x2c, 0xac, 0x54, 0xe2, 0x05, 0x32, 0xb2, 0x25, 0xf1, 0x09, 0x59, 0x4a, 0xec, 0x28, 0x36, - 0xa2, 0xcc, 0xbc, 0x00, 0x8f, 0xc5, 0xd8, 0x91, 0x11, 0x25, 0x2f, 0x82, 0x62, 0xab, 0x34, 0x03, - 0xdb, 0x7d, 0xff, 0xdd, 0xe9, 0xff, 0xf5, 0x53, 0xa8, 0x8d, 0x6d, 0x8d, 0x75, 0x68, 0xdd, 0xbe, - 0x7c, 0xad, 0x9d, 0x32, 0x5a, 0x74, 0xbd, 0x71, 0x86, 0x5d, 0x5f, 0x36, 0xe2, 0x32, 0xa6, 0xbb, - 0xd9, 0x43, 0xa5, 0x64, 0x38, 0xbe, 0xf9, 0x58, 0xd0, 0xe4, 0x31, 0xbc, 0xb3, 0x1d, 0x5d, 0x2a, - 0x2d, 0xf1, 0x08, 0x24, 0x23, 0xf9, 0xa6, 0x08, 0xc0, 0x18, 0x8d, 0x75, 0xd9, 0x22, 0x2c, 0xbc, - 0xe8, 0x67, 0x96, 0xd1, 0xad, 0x44, 0x5b, 0xf7, 0xaa, 0x9b, 0x1e, 0xe1, 0xca, 0xaf, 0xe6, 0x12, - 0x13, 0x34, 0xae, 0xd0, 0x3a, 0x88, 0x33, 0x92, 0x6f, 0xef, 0x52, 0xf1, 0x6f, 0x26, 0x71, 0x50, - 0xb2, 0xf0, 0x77, 0x2c, 0xa5, 0x6b, 0x89, 0xa5, 0x6c, 0x94, 0x46, 0x58, 0x66, 0x24, 0x8f, 0x8b, - 0x3f, 0x66, 0x40, 0x93, 0x1a, 0x55, 0xa3, 0xf4, 0x0b, 0xac, 0xbc, 0xd3, 0x19, 0xa7, 0xc4, 0x12, - 0xb5, 0x69, 0x21, 0x09, 0x89, 0x3d, 0x4c, 0xaa, 0x79, 0xd3, 0xd8, 0xc3, 0x3a, 0xa8, 0x1e, 0x26, - 0x87, 0x06, 0x4b, 0x8b, 0x4f, 0x5a, 0xc2, 0x26, 0x38, 0x9c, 0xf9, 0xf0, 0xf0, 0x35, 0x70, 0x72, - 0x1a, 0x38, 0xf9, 0x19, 0x38, 0xf9, 0x1c, 0x79, 0x74, 0x1a, 0x79, 0xf4, 0x3d, 0xf2, 0xe8, 0x99, - 0x87, 0xb4, 0xb7, 0xbe, 0xb6, 0xe3, 0x7e, 0xd6, 0xa1, 0x7b, 0xef, 0xd0, 0x56, 0x2b, 0x5f, 0xe3, - 0xfd, 0x6f, 0x00, 0x00, 0x00, 0xff, 0xff, 0x23, 0x2c, 0x20, 0x51, 0x8f, 0x01, 0x00, 0x00, + // 312 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x91, 0xbf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6, 0xc6, 0x49, 0x34, 0x0e, 0xd0, 0xa6, 0xf6, 0xc5, 0x50, 0x83, 0xd3, 0x98, 0x06, + 0xe0, 0x02, 0xa0, 0x22, 0xe9, 0xe8, 0xc1, 0xcb, 0xc6, 0xe3, 0x2c, 0xec, 0xe8, 0x25, 0xad, 0x41, + 0xa0, 0x7b, 0xa3, 0x10, 0xa2, 0xc1, 0x89, 0xf9, 0x39, 0x8c, 0xcb, 0xca, 0x3a, 0x52, 0x38, 0x4f, + 0x59, 0x36, 0x2d, 0x7e, 0x88, 0x5f, 0xc0, 0xac, 0xa1, 0x5a, 0x6a, 0xd3, 0xba, 0xfd, 0x0f, 0xdb, + 0xba, 0x60, 0x7d, 0xfb, 0x7e, 0x10, 0x6c, 0x7f, 0x10, 0xec, 0xeb, 0x20, 0xd8, 0xdb, 0x51, 0x0c, + 0xf6, 0x47, 0x31, 0xf8, 0x38, 0x8a, 0xc1, 0xa3, 0x88, 0x7f, 0x70, 0x15, 0x8e, 0xb1, 0x5b, 0xf5, + 0x2e, 0xe3, 0x5f, 0xb7, 0xe4, 0x36, 0xe3, 0x70, 0x9c, 0x9b, 0xef, 0x00, 0x00, 0x00, 0xff, 0xff, + 0x98, 0x0b, 0x92, 0x45, 0xe5, 0x01, 0x00, 0x00, } func (m *Auction) Marshal() (dAtA []byte, err error) { @@ -177,9 +204,31 @@ func (m *Auction) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if len(m.Remaining) > 0 { + i -= len(m.Remaining) + copy(dAtA[i:], m.Remaining) + i = encodeVarintAuction(dAtA, i, uint64(len(m.Remaining))) + i-- + dAtA[i] = 0x62 + } + if m.Closed { + i-- + if m.Closed { + dAtA[i] = 1 + } else { + dAtA[i] = 0 + } + i-- + dAtA[i] = 0x58 + } if m.LeaseEnd != 0 { i = encodeVarintAuction(dAtA, i, uint64(m.LeaseEnd)) i-- + dAtA[i] = 0x50 + } + if m.LeaseStart != 0 { + i = encodeVarintAuction(dAtA, i, uint64(m.LeaseStart)) + i-- dAtA[i] = 0x48 } if len(m.Owner) > 0 { @@ -292,9 +341,19 @@ func (m *Auction) Size() (n int) { if l > 0 { n += 1 + l + sovAuction(uint64(l)) } + if m.LeaseStart != 0 { + n += 1 + sovAuction(uint64(m.LeaseStart)) + } if m.LeaseEnd != 0 { n += 1 + sovAuction(uint64(m.LeaseEnd)) } + if m.Closed { + n += 2 + } + l = len(m.Remaining) + if l > 0 { + n += 1 + l + sovAuction(uint64(l)) + } return n } @@ -581,6 +640,25 @@ func (m *Auction) Unmarshal(dAtA []byte) error { m.Owner = string(dAtA[iNdEx:postIndex]) iNdEx = postIndex case 9: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field LeaseStart", wireType) + } + m.LeaseStart = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowAuction + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.LeaseStart |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + case 10: if wireType != 0 { return fmt.Errorf("proto: wrong wireType = %d for field LeaseEnd", wireType) } @@ -599,6 +677,58 @@ func (m *Auction) Unmarshal(dAtA []byte) error { break } } + case 11: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field Closed", wireType) + } + var v int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowAuction + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + v |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + m.Closed = bool(v != 0) + case 12: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Remaining", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowAuction + } + 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 ErrInvalidLengthAuction + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthAuction + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Remaining = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipAuction(dAtA[iNdEx:])