mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-26 08:15:19 +00:00
Hard: delete borrow/deposit from store on 0 amount (#766)
* delete borrow/deposit on 0 balance * add/update tests to ensure deletion * delete from LTV index
This commit is contained in:
parent
293bfd6f1a
commit
fba6b8c4f2
@ -268,6 +268,10 @@ func (k Keeper) IsWithinValidLtvRange(ctx sdk.Context, deposit types.Deposit, bo
|
|||||||
// UpdateBorrowAndLtvIndex updates a borrow and its LTV index value in the store
|
// UpdateBorrowAndLtvIndex updates a borrow and its LTV index value in the store
|
||||||
func (k Keeper) UpdateBorrowAndLtvIndex(ctx sdk.Context, borrow types.Borrow, newLtv, oldLtv sdk.Dec) {
|
func (k Keeper) UpdateBorrowAndLtvIndex(ctx sdk.Context, borrow types.Borrow, newLtv, oldLtv sdk.Dec) {
|
||||||
k.RemoveFromLtvIndex(ctx, oldLtv, borrow.Borrower)
|
k.RemoveFromLtvIndex(ctx, oldLtv, borrow.Borrower)
|
||||||
|
if borrow.Amount.Empty() {
|
||||||
|
k.DeleteBorrow(ctx, borrow)
|
||||||
|
return
|
||||||
|
}
|
||||||
k.SetBorrow(ctx, borrow)
|
k.SetBorrow(ctx, borrow)
|
||||||
k.InsertIntoLtvIndex(ctx, newLtv, borrow.Borrower)
|
k.InsertIntoLtvIndex(ctx, newLtv, borrow.Borrower)
|
||||||
}
|
}
|
||||||
@ -275,6 +279,10 @@ func (k Keeper) UpdateBorrowAndLtvIndex(ctx sdk.Context, borrow types.Borrow, ne
|
|||||||
// UpdateDepositAndLtvIndex updates a deposit and its LTV index value in the store
|
// UpdateDepositAndLtvIndex updates a deposit and its LTV index value in the store
|
||||||
func (k Keeper) UpdateDepositAndLtvIndex(ctx sdk.Context, deposit types.Deposit, newLtv, oldLtv sdk.Dec) {
|
func (k Keeper) UpdateDepositAndLtvIndex(ctx sdk.Context, deposit types.Deposit, newLtv, oldLtv sdk.Dec) {
|
||||||
k.RemoveFromLtvIndex(ctx, oldLtv, deposit.Depositor)
|
k.RemoveFromLtvIndex(ctx, oldLtv, deposit.Depositor)
|
||||||
|
if deposit.Amount.Empty() {
|
||||||
|
k.DeleteDeposit(ctx, deposit)
|
||||||
|
return
|
||||||
|
}
|
||||||
k.SetDeposit(ctx, deposit)
|
k.SetDeposit(ctx, deposit)
|
||||||
k.InsertIntoLtvIndex(ctx, newLtv, deposit.Depositor)
|
k.InsertIntoLtvIndex(ctx, newLtv, deposit.Depositor)
|
||||||
}
|
}
|
||||||
|
@ -29,6 +29,7 @@ func (suite *KeeperTestSuite) TestRepay() {
|
|||||||
|
|
||||||
type errArgs struct {
|
type errArgs struct {
|
||||||
expectPass bool
|
expectPass bool
|
||||||
|
expectDelete bool
|
||||||
contains string
|
contains string
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -53,6 +54,7 @@ func (suite *KeeperTestSuite) TestRepay() {
|
|||||||
},
|
},
|
||||||
errArgs{
|
errArgs{
|
||||||
expectPass: true,
|
expectPass: true,
|
||||||
|
expectDelete: false,
|
||||||
contains: "",
|
contains: "",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -68,6 +70,7 @@ func (suite *KeeperTestSuite) TestRepay() {
|
|||||||
},
|
},
|
||||||
errArgs{
|
errArgs{
|
||||||
expectPass: true,
|
expectPass: true,
|
||||||
|
expectDelete: true,
|
||||||
contains: "",
|
contains: "",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -83,6 +86,7 @@ func (suite *KeeperTestSuite) TestRepay() {
|
|||||||
},
|
},
|
||||||
errArgs{
|
errArgs{
|
||||||
expectPass: true,
|
expectPass: true,
|
||||||
|
expectDelete: true,
|
||||||
contains: "",
|
contains: "",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -98,6 +102,7 @@ func (suite *KeeperTestSuite) TestRepay() {
|
|||||||
},
|
},
|
||||||
errArgs{
|
errArgs{
|
||||||
expectPass: false,
|
expectPass: false,
|
||||||
|
expectDelete: false,
|
||||||
contains: "account can only repay up to 0bnb",
|
contains: "account can only repay up to 0bnb",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -113,6 +118,7 @@ func (suite *KeeperTestSuite) TestRepay() {
|
|||||||
},
|
},
|
||||||
errArgs{
|
errArgs{
|
||||||
expectPass: false,
|
expectPass: false,
|
||||||
|
expectDelete: false,
|
||||||
contains: "account can only repay up to 50000000ukava",
|
contains: "account can only repay up to 50000000ukava",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -232,9 +238,15 @@ func (suite *KeeperTestSuite) TestRepay() {
|
|||||||
suite.Require().Equal(expectedModuleCoins, mAcc.GetCoins())
|
suite.Require().Equal(expectedModuleCoins, mAcc.GetCoins())
|
||||||
|
|
||||||
// Check user's borrow object
|
// Check user's borrow object
|
||||||
borrow, _ := suite.keeper.GetBorrow(suite.ctx, tc.args.borrower)
|
borrow, foundBorrow := suite.keeper.GetBorrow(suite.ctx, tc.args.borrower)
|
||||||
expectedBorrowCoins := tc.args.borrowCoins.Sub(repaymentCoins)
|
expectedBorrowCoins := tc.args.borrowCoins.Sub(repaymentCoins)
|
||||||
|
|
||||||
|
if tc.errArgs.expectDelete {
|
||||||
|
suite.Require().False(foundBorrow)
|
||||||
|
} else {
|
||||||
|
suite.Require().True(foundBorrow)
|
||||||
suite.Require().Equal(expectedBorrowCoins, borrow.Amount)
|
suite.Require().Equal(expectedBorrowCoins, borrow.Amount)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
suite.Require().Error(err)
|
suite.Require().Error(err)
|
||||||
suite.Require().True(strings.Contains(err.Error(), tc.errArgs.contains))
|
suite.Require().True(strings.Contains(err.Error(), tc.errArgs.contains))
|
||||||
@ -250,7 +262,8 @@ func (suite *KeeperTestSuite) TestRepay() {
|
|||||||
suite.Require().Equal(expectedModuleCoins, mAcc.GetCoins())
|
suite.Require().Equal(expectedModuleCoins, mAcc.GetCoins())
|
||||||
|
|
||||||
// Check user's borrow object (no repay coins)
|
// Check user's borrow object (no repay coins)
|
||||||
borrow, _ := suite.keeper.GetBorrow(suite.ctx, tc.args.borrower)
|
borrow, foundBorrow := suite.keeper.GetBorrow(suite.ctx, tc.args.borrower)
|
||||||
|
suite.Require().True(foundBorrow)
|
||||||
suite.Require().Equal(tc.args.borrowCoins, borrow.Amount)
|
suite.Require().Equal(tc.args.borrowCoins, borrow.Amount)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -24,11 +24,11 @@ func (suite *KeeperTestSuite) TestWithdraw() {
|
|||||||
createDeposit bool
|
createDeposit bool
|
||||||
expectedAccountBalance sdk.Coins
|
expectedAccountBalance sdk.Coins
|
||||||
expectedModAccountBalance sdk.Coins
|
expectedModAccountBalance sdk.Coins
|
||||||
depositExists bool
|
|
||||||
finalDepositAmount sdk.Coins
|
finalDepositAmount sdk.Coins
|
||||||
}
|
}
|
||||||
type errArgs struct {
|
type errArgs struct {
|
||||||
expectPass bool
|
expectPass bool
|
||||||
|
expectDelete bool
|
||||||
contains string
|
contains string
|
||||||
}
|
}
|
||||||
type withdrawTest struct {
|
type withdrawTest struct {
|
||||||
@ -47,11 +47,11 @@ func (suite *KeeperTestSuite) TestWithdraw() {
|
|||||||
createDeposit: true,
|
createDeposit: true,
|
||||||
expectedAccountBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(900)), sdk.NewCoin("btcb", sdk.NewInt(1000))),
|
expectedAccountBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(900)), sdk.NewCoin("btcb", sdk.NewInt(1000))),
|
||||||
expectedModAccountBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(100))),
|
expectedModAccountBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(100))),
|
||||||
depositExists: true,
|
|
||||||
finalDepositAmount: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(100))),
|
finalDepositAmount: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(100))),
|
||||||
},
|
},
|
||||||
errArgs{
|
errArgs{
|
||||||
expectPass: true,
|
expectPass: true,
|
||||||
|
expectDelete: false,
|
||||||
contains: "",
|
contains: "",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -65,11 +65,11 @@ func (suite *KeeperTestSuite) TestWithdraw() {
|
|||||||
createDeposit: true,
|
createDeposit: true,
|
||||||
expectedAccountBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(1000)), sdk.NewCoin("btcb", sdk.NewInt(1000))),
|
expectedAccountBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(1000)), sdk.NewCoin("btcb", sdk.NewInt(1000))),
|
||||||
expectedModAccountBalance: sdk.Coins(nil),
|
expectedModAccountBalance: sdk.Coins(nil),
|
||||||
depositExists: false,
|
|
||||||
finalDepositAmount: sdk.Coins{},
|
finalDepositAmount: sdk.Coins{},
|
||||||
},
|
},
|
||||||
errArgs{
|
errArgs{
|
||||||
expectPass: true,
|
expectPass: true,
|
||||||
|
expectDelete: true,
|
||||||
contains: "",
|
contains: "",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -83,11 +83,11 @@ func (suite *KeeperTestSuite) TestWithdraw() {
|
|||||||
createDeposit: true,
|
createDeposit: true,
|
||||||
expectedAccountBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(1000)), sdk.NewCoin("btcb", sdk.NewInt(1000))),
|
expectedAccountBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(1000)), sdk.NewCoin("btcb", sdk.NewInt(1000))),
|
||||||
expectedModAccountBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(1000))),
|
expectedModAccountBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(1000))),
|
||||||
depositExists: false,
|
|
||||||
finalDepositAmount: sdk.Coins{},
|
finalDepositAmount: sdk.Coins{},
|
||||||
},
|
},
|
||||||
errArgs{
|
errArgs{
|
||||||
expectPass: true,
|
expectPass: true,
|
||||||
|
expectDelete: true,
|
||||||
contains: "",
|
contains: "",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -101,11 +101,11 @@ func (suite *KeeperTestSuite) TestWithdraw() {
|
|||||||
createDeposit: true,
|
createDeposit: true,
|
||||||
expectedAccountBalance: sdk.Coins{},
|
expectedAccountBalance: sdk.Coins{},
|
||||||
expectedModAccountBalance: sdk.Coins{},
|
expectedModAccountBalance: sdk.Coins{},
|
||||||
depositExists: false,
|
|
||||||
finalDepositAmount: sdk.Coins{},
|
finalDepositAmount: sdk.Coins{},
|
||||||
},
|
},
|
||||||
errArgs{
|
errArgs{
|
||||||
expectPass: false,
|
expectPass: false,
|
||||||
|
expectDelete: false,
|
||||||
contains: "no coins of this type deposited",
|
contains: "no coins of this type deposited",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -198,11 +198,11 @@ func (suite *KeeperTestSuite) TestWithdraw() {
|
|||||||
mAcc := suite.getModuleAccount(types.ModuleAccountName)
|
mAcc := suite.getModuleAccount(types.ModuleAccountName)
|
||||||
suite.Require().Equal(tc.args.expectedModAccountBalance, mAcc.GetCoins())
|
suite.Require().Equal(tc.args.expectedModAccountBalance, mAcc.GetCoins())
|
||||||
testDeposit, f := suite.keeper.GetDeposit(suite.ctx, tc.args.depositor)
|
testDeposit, f := suite.keeper.GetDeposit(suite.ctx, tc.args.depositor)
|
||||||
if tc.args.depositExists {
|
if tc.errArgs.expectDelete {
|
||||||
|
suite.Require().False(f)
|
||||||
|
} else {
|
||||||
suite.Require().True(f)
|
suite.Require().True(f)
|
||||||
suite.Require().Equal(tc.args.finalDepositAmount, testDeposit.Amount)
|
suite.Require().Equal(tc.args.finalDepositAmount, testDeposit.Amount)
|
||||||
} else {
|
|
||||||
suite.Require().False(f)
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
suite.Require().Error(err)
|
suite.Require().Error(err)
|
||||||
|
Loading…
Reference in New Issue
Block a user