Skip to content

Commit

Permalink
Merge pull request #633 from UnUniFi/feat/negative-collected-amount
Browse files Browse the repository at this point in the history
feat: negative collected amount
  • Loading branch information
Senna46 authored Jun 30, 2023
2 parents 3614fe2 + 4973c46 commit ac6624c
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 110 deletions.
1 change: 1 addition & 0 deletions docs/core/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -2883,6 +2883,7 @@ Query defines the gRPC querier service.
| `successful_bid_end_at` | [google.protobuf.Timestamp](#google.protobuf.Timestamp) | | |
| `auto_relisted_count` | [uint64](#uint64) | | |
| `collected_amount` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | | |
| `collected_amount_negative` | [bool] | | |
| `minimum_bidding_period` | [google.protobuf.Duration](#google.protobuf.Duration) | | |

<a name="ununifi.nftmarket.Params"></a>
Expand Down
3 changes: 2 additions & 1 deletion proto/ununifi/nftbackedloan/nftbackedloan.proto
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ message NftListing {
];
uint64 auto_relisted_count = 11;
cosmos.base.v1beta1.Coin collected_amount = 12 [(gogoproto.nullable) = false];
google.protobuf.Duration minimum_bidding_period = 13 [
bool collected_amount_negative = 13;
google.protobuf.Duration minimum_bidding_period = 14 [
(gogoproto.stdduration) = true,
(gogoproto.nullable) = false
];
Expand Down
8 changes: 8 additions & 0 deletions x/nftbackedloan/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func (s *KeeperTestSuite) TestListedNfts() {
Denom: "uguu",
Amount: sdk.ZeroInt(),
},
CollectedAmountNegative: false,
},
NftInfo: types.NftInfo{
Id: "nft2",
Expand All @@ -98,6 +99,7 @@ func (s *KeeperTestSuite) TestListedNfts() {
Denom: "uguu",
Amount: sdk.ZeroInt(),
},
CollectedAmountNegative: false,
},
NftInfo: types.NftInfo{
Id: "nft5",
Expand All @@ -120,6 +122,7 @@ func (s *KeeperTestSuite) TestListedNfts() {
Denom: "uguu",
Amount: sdk.ZeroInt(),
},
CollectedAmountNegative: false,
},
NftInfo: types.NftInfo{
Id: "nft6",
Expand Down Expand Up @@ -157,6 +160,7 @@ func (s *KeeperTestSuite) TestListedNfts() {
Denom: "uguu",
Amount: sdk.ZeroInt(),
},
CollectedAmountNegative: false,
},
NftInfo: types.NftInfo{
Id: "nft7",
Expand Down Expand Up @@ -192,6 +196,7 @@ func (s *KeeperTestSuite) TestListedNfts() {
Denom: "uguu",
Amount: sdk.ZeroInt(),
},
CollectedAmountNegative: false,
},
NftInfo: types.NftInfo{
Id: "nft2",
Expand All @@ -214,6 +219,7 @@ func (s *KeeperTestSuite) TestListedNfts() {
Denom: "uguu",
Amount: sdk.ZeroInt(),
},
CollectedAmountNegative: false,
},
NftInfo: types.NftInfo{
Id: "nft5",
Expand All @@ -236,6 +242,7 @@ func (s *KeeperTestSuite) TestListedNfts() {
Denom: "uguu",
Amount: sdk.ZeroInt(),
},
CollectedAmountNegative: false,
},
NftInfo: types.NftInfo{
Id: "nft6",
Expand All @@ -258,6 +265,7 @@ func (s *KeeperTestSuite) TestListedNfts() {
Denom: "uguu",
Amount: sdk.ZeroInt(),
},
CollectedAmountNegative: false,
},
NftInfo: types.NftInfo{
Id: "nft7",
Expand Down
34 changes: 22 additions & 12 deletions x/nftbackedloan/keeper/nft_listing.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,15 @@ func (k Keeper) ListNft(ctx sdk.Context, msg *types.MsgListNft) error {
// todo: make test
owner := k.nftKeeper.GetOwner(ctx, msg.NftId.ClassId, msg.NftId.NftId)
listing := types.NftListing{
NftId: msg.NftId,
Owner: owner.String(),
State: types.ListingState_LISTING,
BidToken: msg.BidToken,
MinimumDepositRate: msg.MinimumDepositRate,
AutomaticRefinancing: msg.AutomaticRefinancing,
StartedAt: ctx.BlockTime(),
CollectedAmount: sdk.NewCoin(msg.BidToken, sdk.ZeroInt()),
NftId: msg.NftId,
Owner: owner.String(),
State: types.ListingState_LISTING,
BidToken: msg.BidToken,
MinimumDepositRate: msg.MinimumDepositRate,
AutomaticRefinancing: msg.AutomaticRefinancing,
StartedAt: ctx.BlockTime(),
CollectedAmount: sdk.NewCoin(msg.BidToken, sdk.ZeroInt()),
CollectedAmountNegative: false,
// todo: add validation.
// we should to determine maximum bidding period.
MinimumBiddingPeriod: msg.MinimumBiddingPeriod,
Expand Down Expand Up @@ -639,13 +640,17 @@ func (k Keeper) LiquidationProcessExitsWinner(ctx sdk.Context, collectBids,
listing.CollectedAmount = listing.CollectedAmount.Add(collectedDeposit)
}

// win price + forfeited deposits - refund bids' deposits
// surplusAmount := k.GetSurplusAmount(refundBids, winnerBid, listing.CollectedAmount)
totalInterests := refundBids.TotalInterestAmount(now)
if totalInterests.IsNil() {
totalInterests = sdk.NewCoin(listing.BidToken, sdk.ZeroInt())
}
// todo: sub total interests from lister's profit
// Sub total interests from lister's profit
if listing.CollectedAmount.IsLT(totalInterests) {
listing.CollectedAmountNegative = true
listing.CollectedAmount = totalInterests.Sub(listing.CollectedAmount)
} else {
listing.CollectedAmount = listing.CollectedAmount.Sub(totalInterests)
}
err = k.RefundBids(ctx, refundBids, totalInterests, listing)
if err != nil {
return err
Expand Down Expand Up @@ -712,7 +717,12 @@ func (k Keeper) DeliverSuccessfulBids(ctx sdk.Context) {
}

loan := k.GetDebtByNft(ctx, listing.IdBytes())
totalPayAmount := listing.CollectedAmount.Add(bid.BidAmount)
totalPayAmount := bid.BidAmount
if listing.CollectedAmountNegative {
totalPayAmount = totalPayAmount.Sub(listing.CollectedAmount)
} else {
totalPayAmount = totalPayAmount.Add(listing.CollectedAmount)
}
k.ProcessPaymentWithCommissionFee(ctx, listingOwner, totalPayAmount, loan.Loan, listing.NftId)

err = k.DeleteBid(ctx, bid)
Expand Down
11 changes: 11 additions & 0 deletions x/nftbackedloan/keeper/nft_listing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func (suite *KeeperTestSuite) TestNftListingBasics() {
Denom: "uguu",
Amount: sdk.ZeroInt(),
},
CollectedAmountNegative: false,
},
{
NftId: types.NftIdentifier{
Expand All @@ -57,6 +58,7 @@ func (suite *KeeperTestSuite) TestNftListingBasics() {
Denom: "uguu",
Amount: sdk.ZeroInt(),
},
CollectedAmountNegative: false,
},
{
NftId: types.NftIdentifier{
Expand All @@ -76,6 +78,7 @@ func (suite *KeeperTestSuite) TestNftListingBasics() {
Denom: "uguu",
Amount: sdk.ZeroInt(),
},
CollectedAmountNegative: false,
},
{
NftId: types.NftIdentifier{
Expand All @@ -95,6 +98,7 @@ func (suite *KeeperTestSuite) TestNftListingBasics() {
Denom: "uguu",
Amount: sdk.ZeroInt(),
},
CollectedAmountNegative: false,
},
{
NftId: types.NftIdentifier{
Expand All @@ -114,6 +118,7 @@ func (suite *KeeperTestSuite) TestNftListingBasics() {
Denom: "uguu",
Amount: sdk.ZeroInt(),
},
CollectedAmountNegative: false,
},
}

Expand Down Expand Up @@ -1674,6 +1679,7 @@ func (suite *KeeperTestSuite) TestLiquidationProcessExitsWinner() {
Denom: "uguu",
Amount: sdk.ZeroInt(),
},
CollectedAmountNegative: false,
},
winnerBid: types.NftBid{
Id: types.BidId{
Expand Down Expand Up @@ -1717,6 +1723,7 @@ func (suite *KeeperTestSuite) TestLiquidationProcessExitsWinner() {
Denom: "uguu",
Amount: sdk.ZeroInt(),
},
CollectedAmountNegative: false,
},
},
nil,
Expand Down Expand Up @@ -1787,6 +1794,7 @@ func (suite *KeeperTestSuite) TestLiquidationProcessExitsWinner() {
Denom: "uguu",
Amount: sdk.ZeroInt(),
},
CollectedAmountNegative: false,
},
winnerBid: types.NftBid{
Id: types.BidId{
Expand Down Expand Up @@ -1873,6 +1881,7 @@ func (suite *KeeperTestSuite) TestLiquidationProcessExitsWinner() {
Denom: "uguu",
Amount: sdk.ZeroInt(),
},
CollectedAmountNegative: false,
},
},
nil,
Expand Down Expand Up @@ -1965,6 +1974,7 @@ func (suite *KeeperTestSuite) TestLiquidationProcessExitsWinner() {
Denom: "uguu",
Amount: sdk.ZeroInt(),
},
CollectedAmountNegative: false,
},
winnerBid: types.NftBid{
Id: types.BidId{
Expand Down Expand Up @@ -2051,6 +2061,7 @@ func (suite *KeeperTestSuite) TestLiquidationProcessExitsWinner() {
Denom: "uguu",
Amount: sdk.NewInt(50),
},
CollectedAmountNegative: false,
},
},
nil,
Expand Down
Loading

0 comments on commit ac6624c

Please sign in to comment.