mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-26 00:05:18 +00:00
decrement/increment coin counters on auctions (#809)
This commit is contained in:
parent
7d4235ca87
commit
7465c643b8
@ -187,6 +187,10 @@ func (k Keeper) StartAuctions(ctx sdk.Context, borrower sdk.AccAddress, borrows,
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return liquidatedCoins, err
|
return liquidatedCoins, err
|
||||||
}
|
}
|
||||||
|
// Decrement supplied coins and increment borrowed coins optimistically
|
||||||
|
k.DecrementSuppliedCoins(ctx, sdk.Coins{lot})
|
||||||
|
k.IncrementBorrowedCoins(ctx, sdk.Coins{bid})
|
||||||
|
|
||||||
// Add lot to liquidated coins
|
// Add lot to liquidated coins
|
||||||
liquidatedCoins = liquidatedCoins.Add(lot)
|
liquidatedCoins = liquidatedCoins.Add(lot)
|
||||||
|
|
||||||
@ -232,6 +236,10 @@ func (k Keeper) StartAuctions(ctx sdk.Context, borrower sdk.AccAddress, borrows,
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return liquidatedCoins, err
|
return liquidatedCoins, err
|
||||||
}
|
}
|
||||||
|
// Decrement supplied coins and increment borrowed coins optimistically
|
||||||
|
k.DecrementSuppliedCoins(ctx, sdk.Coins{lot})
|
||||||
|
k.IncrementBorrowedCoins(ctx, sdk.Coins{bid})
|
||||||
|
|
||||||
// Add lot to liquidated coins
|
// Add lot to liquidated coins
|
||||||
liquidatedCoins = liquidatedCoins.Add(lot)
|
liquidatedCoins = liquidatedCoins.Add(lot)
|
||||||
|
|
||||||
|
@ -27,6 +27,8 @@ func (suite *KeeperTestSuite) TestKeeperLiquidation() {
|
|||||||
depositCoins []sdk.Coin
|
depositCoins []sdk.Coin
|
||||||
borrowCoins sdk.Coins
|
borrowCoins sdk.Coins
|
||||||
liquidateAfter int64
|
liquidateAfter int64
|
||||||
|
expectedLiquidatedCoins sdk.Coins
|
||||||
|
expectedBidCoins sdk.Coins
|
||||||
auctionSize sdk.Int
|
auctionSize sdk.Int
|
||||||
expectedKeeperCoins sdk.Coins // coins keeper address should have after successfully liquidating position
|
expectedKeeperCoins sdk.Coins // coins keeper address should have after successfully liquidating position
|
||||||
expectedBorrowerCoins sdk.Coins // additional coins (if any) the borrower address should have after successfully liquidating position
|
expectedBorrowerCoins sdk.Coins // additional coins (if any) the borrower address should have after successfully liquidating position
|
||||||
@ -71,6 +73,8 @@ func (suite *KeeperTestSuite) TestKeeperLiquidation() {
|
|||||||
depositCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(10*KAVA_CF))),
|
depositCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(10*KAVA_CF))),
|
||||||
borrowCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(8*KAVA_CF))),
|
borrowCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(8*KAVA_CF))),
|
||||||
liquidateAfter: oneMonthInSeconds,
|
liquidateAfter: oneMonthInSeconds,
|
||||||
|
expectedLiquidatedCoins: sdk.NewCoins(sdk.NewInt64Coin("ukava", 9500390)),
|
||||||
|
expectedBidCoins: sdk.NewCoins(sdk.NewInt64Coin("ukava", 8004766)),
|
||||||
auctionSize: sdk.NewInt(KAVA_CF * 1000),
|
auctionSize: sdk.NewInt(KAVA_CF * 1000),
|
||||||
expectedKeeperCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(100500020))),
|
expectedKeeperCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(100500020))),
|
||||||
expectedBorrowerCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(98000001))), // initial - deposit + borrow + liquidation leftovers
|
expectedBorrowerCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(98000001))), // initial - deposit + borrow + liquidation leftovers
|
||||||
@ -109,6 +113,8 @@ func (suite *KeeperTestSuite) TestKeeperLiquidation() {
|
|||||||
depositCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(50*KAVA_CF))), // $100 * 0.8 = $80 borrowable
|
depositCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(50*KAVA_CF))), // $100 * 0.8 = $80 borrowable
|
||||||
borrowCoins: sdk.NewCoins(sdk.NewCoin("usdc", sdk.NewInt(20*KAVA_CF)), sdk.NewCoin("ukava", sdk.NewInt(10*KAVA_CF)), sdk.NewCoin("bnb", sdk.NewInt(2*BNB_CF)), sdk.NewCoin("btc", sdk.NewInt(0.2*BTCB_CF))), // $20+$20+$20 = $80 borrowed
|
borrowCoins: sdk.NewCoins(sdk.NewCoin("usdc", sdk.NewInt(20*KAVA_CF)), sdk.NewCoin("ukava", sdk.NewInt(10*KAVA_CF)), sdk.NewCoin("bnb", sdk.NewInt(2*BNB_CF)), sdk.NewCoin("btc", sdk.NewInt(0.2*BTCB_CF))), // $20+$20+$20 = $80 borrowed
|
||||||
liquidateAfter: oneMonthInSeconds,
|
liquidateAfter: oneMonthInSeconds,
|
||||||
|
expectedLiquidatedCoins: sdk.NewCoins(sdk.NewInt64Coin("ukava", 47500032)),
|
||||||
|
expectedBidCoins: sdk.NewCoins(sdk.NewInt64Coin("bnb", 200003287), sdk.NewInt64Coin("btc", 20000032), sdk.NewInt64Coin("ukava", 10000782), sdk.NewInt64Coin("usdc", 20003284)),
|
||||||
auctionSize: sdk.NewInt(KAVA_CF * 1000),
|
auctionSize: sdk.NewInt(KAVA_CF * 1000),
|
||||||
expectedKeeperCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(102500001))),
|
expectedKeeperCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(102500001))),
|
||||||
expectedBorrowerCoins: sdk.NewCoins(sdk.NewCoin("usdc", sdk.NewInt(20*KAVA_CF)), sdk.NewCoin("ukava", sdk.NewInt(60000002)), sdk.NewCoin("bnb", sdk.NewInt(2*BNB_CF)), sdk.NewCoin("btc", sdk.NewInt(0.2*BTCB_CF))), // initial - deposit + borrow + liquidation leftovers
|
expectedBorrowerCoins: sdk.NewCoins(sdk.NewCoin("usdc", sdk.NewInt(20*KAVA_CF)), sdk.NewCoin("ukava", sdk.NewInt(60000002)), sdk.NewCoin("bnb", sdk.NewInt(2*BNB_CF)), sdk.NewCoin("btc", sdk.NewInt(0.2*BTCB_CF))), // initial - deposit + borrow + liquidation leftovers
|
||||||
@ -192,6 +198,8 @@ func (suite *KeeperTestSuite) TestKeeperLiquidation() {
|
|||||||
depositCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(50*KAVA_CF)), sdk.NewCoin("bnb", sdk.NewInt(10*BNB_CF)), sdk.NewCoin("btc", sdk.NewInt(1*BTCB_CF))), // $100 + $100 + $100 = $300 * 0.8 = $240 borrowable // $100 * 0.8 = $80 borrowable
|
depositCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(50*KAVA_CF)), sdk.NewCoin("bnb", sdk.NewInt(10*BNB_CF)), sdk.NewCoin("btc", sdk.NewInt(1*BTCB_CF))), // $100 + $100 + $100 = $300 * 0.8 = $240 borrowable // $100 * 0.8 = $80 borrowable
|
||||||
borrowCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(120*KAVA_CF))), // $240 borrowed
|
borrowCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(120*KAVA_CF))), // $240 borrowed
|
||||||
liquidateAfter: oneMonthInSeconds,
|
liquidateAfter: oneMonthInSeconds,
|
||||||
|
expectedLiquidatedCoins: sdk.NewCoins(sdk.NewInt64Coin("bnb", 950000000), sdk.NewInt64Coin("btc", 95000000), sdk.NewInt64Coin("ukava", 47504818)),
|
||||||
|
expectedBidCoins: sdk.NewCoins(sdk.NewInt64Coin("ukava", 120112133)),
|
||||||
auctionSize: sdk.NewInt(KAVA_CF * 1000),
|
auctionSize: sdk.NewInt(KAVA_CF * 1000),
|
||||||
expectedKeeperCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(102500253)), sdk.NewCoin("bnb", sdk.NewInt(0.5*BNB_CF)), sdk.NewCoin("btc", sdk.NewInt(0.05*BTCB_CF))), // 5% of each seized coin + initial balances
|
expectedKeeperCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(102500253)), sdk.NewCoin("bnb", sdk.NewInt(0.5*BNB_CF)), sdk.NewCoin("btc", sdk.NewInt(0.05*BTCB_CF))), // 5% of each seized coin + initial balances
|
||||||
expectedBorrowerCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(170.000001*KAVA_CF)), sdk.NewCoin("bnb", sdk.NewInt(90*BNB_CF)), sdk.NewCoin("btc", sdk.NewInt(99*BTCB_CF))),
|
expectedBorrowerCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(170.000001*KAVA_CF)), sdk.NewCoin("bnb", sdk.NewInt(90*BNB_CF)), sdk.NewCoin("btc", sdk.NewInt(99*BTCB_CF))),
|
||||||
@ -261,6 +269,8 @@ func (suite *KeeperTestSuite) TestKeeperLiquidation() {
|
|||||||
depositCoins: sdk.NewCoins(sdk.NewCoin("usdc", sdk.NewInt(100*KAVA_CF)), sdk.NewCoin("usdt", sdk.NewInt(100*KAVA_CF)), sdk.NewCoin("usdx", sdk.NewInt(100*KAVA_CF))), // $100 + $100 + $100 = $300 * 0.9 = $270 borrowable
|
depositCoins: sdk.NewCoins(sdk.NewCoin("usdc", sdk.NewInt(100*KAVA_CF)), sdk.NewCoin("usdt", sdk.NewInt(100*KAVA_CF)), sdk.NewCoin("usdx", sdk.NewInt(100*KAVA_CF))), // $100 + $100 + $100 = $300 * 0.9 = $270 borrowable
|
||||||
borrowCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(35*KAVA_CF)), sdk.NewCoin("bnb", sdk.NewInt(10*BNB_CF)), sdk.NewCoin("btc", sdk.NewInt(1*BTCB_CF))), // $270 borrowed
|
borrowCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(35*KAVA_CF)), sdk.NewCoin("bnb", sdk.NewInt(10*BNB_CF)), sdk.NewCoin("btc", sdk.NewInt(1*BTCB_CF))), // $270 borrowed
|
||||||
liquidateAfter: oneMonthInSeconds,
|
liquidateAfter: oneMonthInSeconds,
|
||||||
|
expectedLiquidatedCoins: sdk.NewCoins(sdk.NewInt64Coin("usdc", 95000000), sdk.NewInt64Coin("usdt", 95000000), sdk.NewInt64Coin("usdx", 94999999)),
|
||||||
|
expectedBidCoins: sdk.NewCoins(sdk.NewInt64Coin("bnb", 1000082154), sdk.NewInt64Coin("btc", 100000821), sdk.NewInt64Coin("ukava", 35010052)),
|
||||||
auctionSize: sdk.NewInt(KAVA_CF * 1000),
|
auctionSize: sdk.NewInt(KAVA_CF * 1000),
|
||||||
expectedKeeperCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(100*KAVA_CF)), sdk.NewCoin("usdc", sdk.NewInt(5*KAVA_CF)), sdk.NewCoin("usdt", sdk.NewInt(5*KAVA_CF)), sdk.NewCoin("usdx", sdk.NewInt(5*KAVA_CF))), // 5% of each seized coin + initial balances
|
expectedKeeperCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(100*KAVA_CF)), sdk.NewCoin("usdc", sdk.NewInt(5*KAVA_CF)), sdk.NewCoin("usdt", sdk.NewInt(5*KAVA_CF)), sdk.NewCoin("usdx", sdk.NewInt(5*KAVA_CF))), // 5% of each seized coin + initial balances
|
||||||
expectedBorrowerCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(135*KAVA_CF)), sdk.NewCoin("bnb", sdk.NewInt(10*BNB_CF)), sdk.NewCoin("btc", sdk.NewInt(1*BTCB_CF)), sdk.NewCoin("usdx", sdk.NewInt(0.000001*KAVA_CF))),
|
expectedBorrowerCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(135*KAVA_CF)), sdk.NewCoin("bnb", sdk.NewInt(10*BNB_CF)), sdk.NewCoin("btc", sdk.NewInt(1*BTCB_CF)), sdk.NewCoin("usdx", sdk.NewInt(0.000001*KAVA_CF))),
|
||||||
@ -359,6 +369,8 @@ func (suite *KeeperTestSuite) TestKeeperLiquidation() {
|
|||||||
depositCoins: sdk.NewCoins(sdk.NewCoin("dai", sdk.NewInt(350*KAVA_CF)), sdk.NewCoin("usdc", sdk.NewInt(200*KAVA_CF))),
|
depositCoins: sdk.NewCoins(sdk.NewCoin("dai", sdk.NewInt(350*KAVA_CF)), sdk.NewCoin("usdc", sdk.NewInt(200*KAVA_CF))),
|
||||||
borrowCoins: sdk.NewCoins(sdk.NewCoin("usdt", sdk.NewInt(250*KAVA_CF)), sdk.NewCoin("usdx", sdk.NewInt(245*KAVA_CF))),
|
borrowCoins: sdk.NewCoins(sdk.NewCoin("usdt", sdk.NewInt(250*KAVA_CF)), sdk.NewCoin("usdx", sdk.NewInt(245*KAVA_CF))),
|
||||||
liquidateAfter: oneMonthInSeconds,
|
liquidateAfter: oneMonthInSeconds,
|
||||||
|
expectedLiquidatedCoins: sdk.NewCoins(sdk.NewInt64Coin("dai", 332500000), sdk.NewInt64Coin("usdc", 189999999)),
|
||||||
|
expectedBidCoins: sdk.NewCoins(sdk.NewInt64Coin("usdx", 245487894), sdk.NewInt64Coin("usdt", 250507897)),
|
||||||
auctionSize: sdk.NewInt(KAVA_CF * 100000),
|
auctionSize: sdk.NewInt(KAVA_CF * 100000),
|
||||||
expectedKeeperCoins: sdk.NewCoins(sdk.NewCoin("dai", sdk.NewInt(1017.50*KAVA_CF)), sdk.NewCoin("usdt", sdk.NewInt(1000*KAVA_CF)), sdk.NewCoin("usdc", sdk.NewInt(1010*KAVA_CF)), sdk.NewCoin("usdx", sdk.NewInt(1000*KAVA_CF))),
|
expectedKeeperCoins: sdk.NewCoins(sdk.NewCoin("dai", sdk.NewInt(1017.50*KAVA_CF)), sdk.NewCoin("usdt", sdk.NewInt(1000*KAVA_CF)), sdk.NewCoin("usdc", sdk.NewInt(1010*KAVA_CF)), sdk.NewCoin("usdx", sdk.NewInt(1000*KAVA_CF))),
|
||||||
expectedBorrowerCoins: sdk.NewCoins(sdk.NewCoin("dai", sdk.NewInt(650*KAVA_CF)), sdk.NewCoin("usdc", sdk.NewInt(800000001)), sdk.NewCoin("usdt", sdk.NewInt(1250*KAVA_CF)), sdk.NewCoin("usdx", sdk.NewInt(1245*KAVA_CF))),
|
expectedBorrowerCoins: sdk.NewCoins(sdk.NewCoin("dai", sdk.NewInt(650*KAVA_CF)), sdk.NewCoin("usdc", sdk.NewInt(800000001)), sdk.NewCoin("usdt", sdk.NewInt(1250*KAVA_CF)), sdk.NewCoin("usdx", sdk.NewInt(1245*KAVA_CF))),
|
||||||
@ -427,6 +439,8 @@ func (suite *KeeperTestSuite) TestKeeperLiquidation() {
|
|||||||
depositCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(20*KAVA_CF))), // Deposit 20 KAVA
|
depositCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(20*KAVA_CF))), // Deposit 20 KAVA
|
||||||
borrowCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(5*KAVA_CF))), // Borrow 5 KAVA
|
borrowCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(5*KAVA_CF))), // Borrow 5 KAVA
|
||||||
liquidateAfter: oneMonthInSeconds,
|
liquidateAfter: oneMonthInSeconds,
|
||||||
|
expectedLiquidatedCoins: sdk.Coins{},
|
||||||
|
expectedBidCoins: sdk.Coins{},
|
||||||
auctionSize: sdk.NewInt(KAVA_CF * 1000),
|
auctionSize: sdk.NewInt(KAVA_CF * 1000),
|
||||||
expectedKeeperCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(100.5*KAVA_CF))),
|
expectedKeeperCoins: sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(100.5*KAVA_CF))),
|
||||||
expectedBorrowerCoins: sdk.NewCoins(),
|
expectedBorrowerCoins: sdk.NewCoins(),
|
||||||
@ -616,6 +630,12 @@ func (suite *KeeperTestSuite) TestKeeperLiquidation() {
|
|||||||
_, foundDepositBefore := suite.keeper.GetDeposit(liqCtx, tc.args.borrower)
|
_, foundDepositBefore := suite.keeper.GetDeposit(liqCtx, tc.args.borrower)
|
||||||
suite.Require().True(foundDepositBefore)
|
suite.Require().True(foundDepositBefore)
|
||||||
|
|
||||||
|
// Fetch supplied and borrowed coins pre-liquidation
|
||||||
|
suppliedCoinsPre, foundSuppliedCoinsPre := suite.keeper.GetSuppliedCoins(liqCtx)
|
||||||
|
suite.Require().True(foundSuppliedCoinsPre)
|
||||||
|
borrowedCoinsPre, foundBorrowedCoinsPre := suite.keeper.GetBorrowedCoins(liqCtx)
|
||||||
|
suite.Require().True(foundBorrowedCoinsPre)
|
||||||
|
|
||||||
// Attempt to liquidate
|
// Attempt to liquidate
|
||||||
err = suite.keeper.AttemptKeeperLiquidation(liqCtx, tc.args.keeper, tc.args.borrower)
|
err = suite.keeper.AttemptKeeperLiquidation(liqCtx, tc.args.keeper, tc.args.borrower)
|
||||||
if tc.errArgs.expectPass {
|
if tc.errArgs.expectPass {
|
||||||
@ -640,6 +660,13 @@ func (suite *KeeperTestSuite) TestKeeperLiquidation() {
|
|||||||
auctions := suite.auctionKeeper.GetAllAuctions(liqCtx)
|
auctions := suite.auctionKeeper.GetAllAuctions(liqCtx)
|
||||||
suite.Require().True(len(auctions) > 0)
|
suite.Require().True(len(auctions) > 0)
|
||||||
suite.Require().Equal(tc.args.expectedAuctions, auctions)
|
suite.Require().Equal(tc.args.expectedAuctions, auctions)
|
||||||
|
|
||||||
|
// Check that supplied and borrowed coins have been updated post-liquidation
|
||||||
|
suppliedCoinsPost, _ := suite.keeper.GetSuppliedCoins(liqCtx)
|
||||||
|
suite.Require().Equal(suppliedCoinsPre.Sub(tc.args.expectedLiquidatedCoins), suppliedCoinsPost)
|
||||||
|
borrowedCoinsPost, _ := suite.keeper.GetBorrowedCoins(liqCtx)
|
||||||
|
|
||||||
|
suite.Require().Equal(borrowedCoinsPre.Add(tc.args.expectedBidCoins...), borrowedCoinsPost)
|
||||||
} 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))
|
||||||
|
Loading…
Reference in New Issue
Block a user