fix ceiling remainder payout + implied auction completion status

master
michael 2022-09-05 21:18:04 +00:00
parent 6c4ad8c55d
commit cf3b28217b
7 changed files with 61 additions and 105 deletions

View File

@ -20165,8 +20165,6 @@ paths:
leaseEnd:
type: string
format: uint64
closed:
type: boolean
remaining:
type: string
pagination:
@ -20261,16 +20259,6 @@ paths:
in: query
required: false
type: boolean
- name: pagination.reverse
description: >-
reverse is set to true if results are to be returned in the
descending order.
Since: cosmos-sdk 0.43
in: query
required: false
type: boolean
tags:
- Query
'/cosmos-test/cosmostest/auction/{index}':
@ -20314,8 +20302,6 @@ paths:
leaseEnd:
type: string
format: uint64
closed:
type: boolean
remaining:
type: string
default:
@ -47510,8 +47496,6 @@ definitions:
leaseEnd:
type: string
format: uint64
closed:
type: boolean
remaining:
type: string
cosmostest.cosmostest.Bid:
@ -47573,8 +47557,6 @@ definitions:
leaseEnd:
type: string
format: uint64
closed:
type: boolean
remaining:
type: string
pagination:
@ -47648,8 +47630,6 @@ definitions:
leaseEnd:
type: string
format: uint64
closed:
type: boolean
remaining:
type: string
cosmostest.cosmostest.QueryGetNextAuctionResponse:

View File

@ -16,6 +16,5 @@ message Auction {
string owner = 8;
uint64 leaseStart = 9;
uint64 leaseEnd = 10;
bool closed = 11;
string remaining = 12;
string remaining = 11;
}

View File

@ -4,14 +4,17 @@
HERE=$(cd $(dirname $BASH_SOURCE) && pwd)
source $HERE/testutil.sh
# echo $BOB
# cosmos-testd q cosmostest show-auction $(get_last_auction_index) | jq
# exit 0
before=$(get_balance $BOB token)
cosmos-testd tx cosmostest new-auction asdf asdf 500 token $(now + 100) \
cosmos-testd tx cosmostest new-auction asdf asdf 200 token $(now + 100) \
-y --from bob \
| expect_success "New auction is created"
# sleep 1
get_balance $BOB token | expect_change -500 $before "Change in Bob's balance"
get_balance $BOB token | expect_change -200 $before "Change in Bob's balance"
cosmos-testd tx cosmostest new-bid $(get_last_auction_index) 100 \
-y --from alice \
@ -41,9 +44,17 @@ cosmos-testd q cosmostest auction-bids $(get_last_auction_index) \
| jq -M ".bids | length" \
| assert_eq 2 "Number of auction bids"
before=$(get_balance $BOB token)
log_info "Waiting 10s until auction expires..."
sleep 10
cosmos-testd tx cosmostest new-bid $(get_last_auction_index) 50 \
-y --from alice \
| expect_fail "Can't add bids after auction expiry"
cosmos-testd q cosmostest show-auction $(get_last_auction_index) \
| jq -rM ".auction.best.amount" \
| assert_eq 90 "Remaining vested amount from finalized top bid"
get_balance $BOB token | expect_change 110 $before "Change in Bob's balance"

View File

@ -122,19 +122,19 @@ function expect_fail {
}
function log_info {
printf "[ INFO ]\t$1\n"
printf "[ INFO ] $1\n"
}
function log_test {
printf "$(fmt_cyan '[ TEST ]')\t\t$1\n"
printf "$(fmt_cyan '[ TEST ]') $1\n"
}
function log_ok {
printf "$(fmt_green '[ OK ]')\t"
printf "$(fmt_green '[ OK ]') "
}
function log_fail {
printf "$(fmt_red '[ FAIL ]')\t"
printf "$(fmt_red '[ FAIL ]') "
}
function fmt_red {

View File

@ -12,7 +12,7 @@ import (
"github.com/pkg/errors"
)
func (k *Keeper) AuctionIsExpired(ctx sdk.Context, auctionId string) (bool, error) {
func (k Keeper) AuctionIsExpired(ctx sdk.Context, auctionId string) (bool, error) {
auction, found := k.GetAuction(ctx, auctionId)
// make sure the auction exists on-chain
@ -23,7 +23,7 @@ func (k *Keeper) AuctionIsExpired(ctx sdk.Context, auctionId string) (bool, erro
return uint64(ctx.BlockHeight()) >= auction.Deadline, nil
}
func (k *Keeper) FinalizeExpiredAuctions(ctx sdk.Context) {
func (k Keeper) FinalizeExpiredAuctions(ctx sdk.Context) {
memdb.BidDB.ForEachAuction(func(auctionId string) error {
auction, found := k.GetAuction(ctx, auctionId)
// make sure the auction exists on-chain
@ -42,9 +42,11 @@ func (k *Keeper) FinalizeExpiredAuctions(ctx sdk.Context) {
auction.Remaining = auction.Best.Amount
// clear auction
memdb.BidDB.ClearAuction(auctionId)
if err := memdb.BidDB.ClearAuction(auctionId); err != nil {
return errors.Errorf("failed to clear auction from memcache: %s", err)
}
// pay out remainder to auction creator
// pay out unpaid remainder to auction creator
ceiling := new(big.Int)
ceiling.SetString(auction.Ceiling, 10)
lowestBidAmt := new(big.Int)
@ -53,12 +55,17 @@ func (k *Keeper) FinalizeExpiredAuctions(ctx sdk.Context) {
if ceiling.Cmp(lowestBidAmt) == 1 {
amtRemaining := new(big.Int)
amtRemaining.Sub(ceiling, lowestBidAmt)
coins := sdk.NewCoins(sdk.Coin{
Amount: sdk.NewIntFromBigInt(amtRemaining),
Denom: auction.Denom,
})
if err := k.bank.SendCoinsFromModuleToAccount(ctx, types.ModuleName, sdk.AccAddress(auction.Owner), coins); err != nil {
coins := sdk.NewCoins(sdk.NewCoin(
auction.Denom,
sdk.NewIntFromBigInt(amtRemaining),
))
recipAddr, err := sdk.AccAddressFromBech32(auction.Owner)
if err != nil {
return errors.Errorf("failed to parse address %s", auction.Owner)
}
if err := k.bank.SendCoinsFromModuleToAccount(ctx, types.ModuleName, recipAddr, coins); err != nil {
log.Printf("Failed to send coins from module: %s\n", err)
// log.Fatalf("Failed to send coins from module: %s\n", err)
}
}

View File

@ -26,6 +26,7 @@ func (k msgServer) NewAuction(goCtx context.Context, msg *types.MsgNewAuction) (
Index: index,
Name: msg.Name,
Description: msg.Description,
// best bid -> null
// Best: new(types.Bid),
Deadline: uint64(ctx.BlockHeight()) + auctionconfig.AuctionTime,
Denom: msg.Denom,
@ -33,8 +34,8 @@ func (k msgServer) NewAuction(goCtx context.Context, msg *types.MsgNewAuction) (
Ceiling: msg.Ceiling,
LeaseStart: uint64(ctx.BlockTime().Unix()),
LeaseEnd: msg.LeaseEnd,
Closed: false,
Remaining: "0",
// remaining payout -> null
// Remaining: "0",
}
senderAddr, err := sdk.AccAddressFromBech32(msg.Creator)
@ -55,7 +56,7 @@ func (k msgServer) NewAuction(goCtx context.Context, msg *types.MsgNewAuction) (
Denom: auction.Denom,
})
if err := k.bank.SendCoinsFromAccountToModule(ctx, senderAddr, types.ModuleName, coins); err != nil {
if err := k.Keeper.bank.SendCoinsFromAccountToModule(ctx, senderAddr, types.ModuleName, coins); err != nil {
return nil, fmt.Errorf("failed to transfer %s%s", auction.Ceiling, auction.Denom)
}

View File

@ -33,8 +33,7 @@ type Auction struct {
Owner string `protobuf:"bytes,8,opt,name=owner,proto3" json:"owner,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"`
Remaining string `protobuf:"bytes,11,opt,name=remaining,proto3" json:"remaining,omitempty"`
}
func (m *Auction) Reset() { *m = Auction{} }
@ -140,13 +139,6 @@ 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
@ -161,27 +153,26 @@ func init() {
func init() { proto.RegisterFile("cosmostest/auction.proto", fileDescriptor_631f6f59914101d9) }
var fileDescriptor_631f6f59914101d9 = []byte{
// 312 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x91, 0xbf, 0x4e, 0xfb, 0x30,
0x10, 0xc7, 0xeb, 0xfe, 0xd2, 0x7f, 0xd7, 0xdf, 0x64, 0x15, 0x74, 0xaa, 0x90, 0x15, 0x31, 0x65,
0x21, 0x95, 0x60, 0x61, 0xa5, 0x12, 0x2f, 0x10, 0x36, 0x36, 0x37, 0x3e, 0x21, 0x4b, 0x89, 0x5d,
0xc5, 0x46, 0x94, 0xb7, 0xe0, 0xb1, 0x18, 0x3b, 0xc2, 0x86, 0xda, 0x17, 0x41, 0xb1, 0x29, 0xc9,
0xc0, 0xe6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1, 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,
// 298 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x91, 0xbf, 0x4e, 0xc3, 0x30,
0x10, 0xc6, 0xeb, 0x92, 0xfe, 0xbb, 0x6e, 0x56, 0x91, 0x4e, 0x15, 0xb2, 0x22, 0xa6, 0x2e, 0xa4,
0x12, 0x2c, 0xac, 0x54, 0xe2, 0x05, 0xca, 0xc6, 0xe6, 0xc6, 0x27, 0x64, 0xa9, 0xb1, 0xab, 0xd8,
0x88, 0xf2, 0x16, 0xbc, 0x12, 0x1b, 0x63, 0x47, 0x46, 0xd4, 0xbe, 0x08, 0xca, 0x59, 0x25, 0x19,
0xd8, 0xee, 0xf7, 0xdd, 0x7d, 0xb9, 0x2f, 0x3e, 0xc0, 0xd2, 0x87, 0xca, 0x87, 0x48, 0x21, 0x2e,
0xf5, 0x6b, 0x19, 0xad, 0x77, 0xc5, 0xae, 0xf6, 0xd1, 0xcb, 0xcb, 0xb6, 0x53, 0xb4, 0xe5, 0x7c,
0xd6, 0x31, 0x6c, 0xac, 0x49, 0xc3, 0xd7, 0x9f, 0x7d, 0x18, 0x3d, 0x24, 0xbb, 0x9c, 0xc1, 0xc0,
0x3a, 0x43, 0x7b, 0x14, 0xb9,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[]byte, err error) {
@ -209,17 +200,7 @@ func (m *Auction) MarshalToSizedBuffer(dAtA []byte) (int, error) {
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
dAtA[i] = 0x5a
}
if m.LeaseEnd != 0 {
i = encodeVarintAuction(dAtA, i, uint64(m.LeaseEnd))
@ -347,9 +328,6 @@ func (m *Auction) Size() (n int) {
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))
@ -678,26 +656,6 @@ func (m *Auction) Unmarshal(dAtA []byte) error {
}
}
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)
}