2021-06-21 21:05:17 +00:00
|
|
|
package keeper_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
2024-05-01 03:17:24 +00:00
|
|
|
"github.com/0glabs/0g-chain/x/incentive/types"
|
2021-06-21 21:05:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// UpdateHardBorrowIndexDenomsTests runs unit tests for the keeper.UpdateHardBorrowIndexDenoms method
|
|
|
|
type UpdateHardBorrowIndexDenomsTests struct {
|
|
|
|
unitTester
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateHardBorrowIndexDenoms(t *testing.T) {
|
|
|
|
suite.Run(t, new(UpdateHardBorrowIndexDenomsTests))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *UpdateHardBorrowIndexDenomsTests) TestClaimIndexesAreRemovedForDenomsNoLongerBorrowed() {
|
|
|
|
claim := types.HardLiquidityProviderClaim{
|
|
|
|
BaseMultiClaim: types.BaseMultiClaim{
|
|
|
|
Owner: arbitraryAddress(),
|
|
|
|
},
|
|
|
|
BorrowRewardIndexes: nonEmptyMultiRewardIndexes,
|
|
|
|
}
|
2021-07-07 16:50:14 +00:00
|
|
|
suite.storeHardClaim(claim)
|
2021-06-21 21:05:17 +00:00
|
|
|
suite.storeGlobalBorrowIndexes(claim.BorrowRewardIndexes)
|
|
|
|
|
|
|
|
// remove one denom from the indexes already in the borrow
|
|
|
|
expectedIndexes := claim.BorrowRewardIndexes[1:]
|
2021-07-26 19:07:24 +00:00
|
|
|
borrow := NewBorrowBuilder(claim.Owner).
|
|
|
|
WithArbitrarySourceShares(extractCollateralTypes(expectedIndexes)...).
|
|
|
|
Build()
|
2021-06-21 21:05:17 +00:00
|
|
|
|
|
|
|
suite.keeper.UpdateHardBorrowIndexDenoms(suite.ctx, borrow)
|
|
|
|
|
|
|
|
syncedClaim, _ := suite.keeper.GetHardLiquidityProviderClaim(suite.ctx, claim.Owner)
|
|
|
|
suite.Equal(expectedIndexes, syncedClaim.BorrowRewardIndexes)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *UpdateHardBorrowIndexDenomsTests) TestClaimIndexesAreAddedForNewlyBorrowedDenoms() {
|
|
|
|
claim := types.HardLiquidityProviderClaim{
|
|
|
|
BaseMultiClaim: types.BaseMultiClaim{
|
|
|
|
Owner: arbitraryAddress(),
|
|
|
|
},
|
|
|
|
BorrowRewardIndexes: nonEmptyMultiRewardIndexes,
|
|
|
|
}
|
2021-07-07 16:50:14 +00:00
|
|
|
suite.storeHardClaim(claim)
|
2021-06-21 21:05:17 +00:00
|
|
|
globalIndexes := appendUniqueMultiRewardIndex(claim.BorrowRewardIndexes)
|
|
|
|
suite.storeGlobalBorrowIndexes(globalIndexes)
|
|
|
|
|
2021-07-26 19:07:24 +00:00
|
|
|
borrow := NewBorrowBuilder(claim.Owner).
|
|
|
|
WithArbitrarySourceShares(extractCollateralTypes(globalIndexes)...).
|
|
|
|
Build()
|
2021-06-21 21:05:17 +00:00
|
|
|
|
|
|
|
suite.keeper.UpdateHardBorrowIndexDenoms(suite.ctx, borrow)
|
|
|
|
|
|
|
|
syncedClaim, _ := suite.keeper.GetHardLiquidityProviderClaim(suite.ctx, claim.Owner)
|
|
|
|
suite.Equal(globalIndexes, syncedClaim.BorrowRewardIndexes)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *UpdateHardBorrowIndexDenomsTests) TestClaimIndexesAreUnchangedWhenBorrowedDenomsUnchanged() {
|
|
|
|
claim := types.HardLiquidityProviderClaim{
|
|
|
|
BaseMultiClaim: types.BaseMultiClaim{
|
|
|
|
Owner: arbitraryAddress(),
|
|
|
|
},
|
|
|
|
BorrowRewardIndexes: nonEmptyMultiRewardIndexes,
|
|
|
|
}
|
2021-07-07 16:50:14 +00:00
|
|
|
suite.storeHardClaim(claim)
|
2021-06-21 21:05:17 +00:00
|
|
|
// Set global indexes with same denoms but different values.
|
|
|
|
// UpdateHardBorrowIndexDenoms should ignore the new values.
|
|
|
|
suite.storeGlobalBorrowIndexes(increaseAllRewardFactors(claim.BorrowRewardIndexes))
|
|
|
|
|
2021-07-26 19:07:24 +00:00
|
|
|
borrow := NewBorrowBuilder(claim.Owner).
|
|
|
|
WithArbitrarySourceShares(extractCollateralTypes(claim.BorrowRewardIndexes)...).
|
|
|
|
Build()
|
2021-06-21 21:05:17 +00:00
|
|
|
|
|
|
|
suite.keeper.UpdateHardBorrowIndexDenoms(suite.ctx, borrow)
|
|
|
|
|
|
|
|
syncedClaim, _ := suite.keeper.GetHardLiquidityProviderClaim(suite.ctx, claim.Owner)
|
|
|
|
suite.Equal(claim.BorrowRewardIndexes, syncedClaim.BorrowRewardIndexes)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *UpdateHardBorrowIndexDenomsTests) TestEmptyClaimIndexesAreAddedForNewlyBorrowedButNotRewardedDenoms() {
|
|
|
|
claim := types.HardLiquidityProviderClaim{
|
|
|
|
BaseMultiClaim: types.BaseMultiClaim{
|
|
|
|
Owner: arbitraryAddress(),
|
|
|
|
},
|
|
|
|
BorrowRewardIndexes: nonEmptyMultiRewardIndexes,
|
|
|
|
}
|
2021-07-07 16:50:14 +00:00
|
|
|
suite.storeHardClaim(claim)
|
2021-06-21 21:05:17 +00:00
|
|
|
suite.storeGlobalBorrowIndexes(claim.BorrowRewardIndexes)
|
|
|
|
|
|
|
|
// add a denom to the borrowed amount that is not in the global or claim's indexes
|
|
|
|
expectedIndexes := appendUniqueEmptyMultiRewardIndex(claim.BorrowRewardIndexes)
|
|
|
|
borrowedDenoms := extractCollateralTypes(expectedIndexes)
|
2021-07-26 19:07:24 +00:00
|
|
|
borrow := NewBorrowBuilder(claim.Owner).
|
|
|
|
WithArbitrarySourceShares(borrowedDenoms...).
|
|
|
|
Build()
|
2021-06-21 21:05:17 +00:00
|
|
|
|
|
|
|
suite.keeper.UpdateHardBorrowIndexDenoms(suite.ctx, borrow)
|
|
|
|
|
|
|
|
syncedClaim, _ := suite.keeper.GetHardLiquidityProviderClaim(suite.ctx, claim.Owner)
|
|
|
|
suite.Equal(expectedIndexes, syncedClaim.BorrowRewardIndexes)
|
|
|
|
}
|