2021-09-01 11:11:15 +00:00
|
|
|
package hard_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
2023-04-05 23:21:59 +00:00
|
|
|
sdkmath "cosmossdk.io/math"
|
2024-02-06 22:54:10 +00:00
|
|
|
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
|
|
|
|
tmtime "github.com/cometbft/cometbft/types/time"
|
2021-09-01 11:11:15 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
|
2024-05-01 03:17:24 +00:00
|
|
|
"github.com/0glabs/0g-chain/app"
|
|
|
|
"github.com/0glabs/0g-chain/x/hard"
|
|
|
|
"github.com/0glabs/0g-chain/x/hard/keeper"
|
|
|
|
"github.com/0glabs/0g-chain/x/hard/types"
|
2021-09-01 11:11:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type GenesisTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
|
|
|
app app.TestApp
|
|
|
|
genTime time.Time
|
|
|
|
ctx sdk.Context
|
2022-01-08 00:39:27 +00:00
|
|
|
keeper keeper.Keeper
|
2021-09-01 11:11:15 +00:00
|
|
|
addrs []sdk.AccAddress
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *GenesisTestSuite) SetupTest() {
|
|
|
|
tApp := app.NewTestApp()
|
|
|
|
suite.genTime = tmtime.Canonical(time.Date(2021, 1, 1, 1, 1, 1, 1, time.UTC))
|
2022-01-08 00:39:27 +00:00
|
|
|
suite.ctx = tApp.NewContext(true, tmproto.Header{Height: 1, Time: suite.genTime})
|
2021-09-01 11:11:15 +00:00
|
|
|
suite.keeper = tApp.GetHardKeeper()
|
|
|
|
suite.app = tApp
|
|
|
|
|
|
|
|
_, addrs := app.GeneratePrivKeyAddressPairs(3)
|
|
|
|
suite.addrs = addrs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *GenesisTestSuite) Test_InitExportGenesis() {
|
|
|
|
loanToValue, _ := sdk.NewDecFromStr("0.6")
|
2022-01-08 00:39:27 +00:00
|
|
|
params := types.NewParams(
|
|
|
|
types.MoneyMarkets{
|
|
|
|
types.NewMoneyMarket(
|
|
|
|
"ukava",
|
|
|
|
types.NewBorrowLimit(
|
|
|
|
false,
|
|
|
|
sdk.NewDec(1e15),
|
|
|
|
loanToValue,
|
|
|
|
),
|
|
|
|
"kava:usd",
|
2023-04-05 23:21:59 +00:00
|
|
|
sdkmath.NewInt(1e6),
|
2022-01-08 00:39:27 +00:00
|
|
|
types.NewInterestRateModel(
|
|
|
|
sdk.MustNewDecFromStr("0.05"),
|
|
|
|
sdk.MustNewDecFromStr("2"),
|
|
|
|
sdk.MustNewDecFromStr("0.8"),
|
|
|
|
sdk.MustNewDecFromStr("10"),
|
|
|
|
),
|
|
|
|
sdk.MustNewDecFromStr("0.05"),
|
|
|
|
sdk.ZeroDec(),
|
|
|
|
),
|
2021-09-01 11:11:15 +00:00
|
|
|
},
|
|
|
|
sdk.NewDec(10),
|
|
|
|
)
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
deposits := types.Deposits{
|
|
|
|
types.NewDeposit(
|
2021-09-01 11:11:15 +00:00
|
|
|
suite.addrs[0],
|
2023-04-05 23:21:59 +00:00
|
|
|
sdk.NewCoins(sdk.NewCoin("ukava", sdkmath.NewInt(1e8))), // 100 ukava
|
2022-01-08 00:39:27 +00:00
|
|
|
types.SupplyInterestFactors{
|
2021-09-01 11:11:15 +00:00
|
|
|
{
|
|
|
|
Denom: "ukava",
|
|
|
|
Value: sdk.NewDec(1),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
}
|
|
|
|
|
|
|
|
var totalSupplied sdk.Coins
|
|
|
|
for _, deposit := range deposits {
|
|
|
|
totalSupplied = totalSupplied.Add(deposit.Amount...)
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
borrows := types.Borrows{
|
|
|
|
types.NewBorrow(
|
2021-09-01 11:11:15 +00:00
|
|
|
suite.addrs[1],
|
2023-04-05 23:21:59 +00:00
|
|
|
sdk.NewCoins(sdk.NewCoin("ukava", sdkmath.NewInt(1e7))), // 10 ukava
|
2022-01-08 00:39:27 +00:00
|
|
|
types.BorrowInterestFactors{
|
2021-09-01 11:11:15 +00:00
|
|
|
{
|
|
|
|
Denom: "ukava",
|
|
|
|
Value: sdk.NewDec(1),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
}
|
|
|
|
|
|
|
|
var totalBorrowed sdk.Coins
|
|
|
|
for _, borrow := range borrows {
|
|
|
|
totalBorrowed = totalBorrowed.Add(borrow.Amount...)
|
|
|
|
}
|
|
|
|
|
|
|
|
supplyInterestFactor := sdk.MustNewDecFromStr("1.0001")
|
|
|
|
borrowInterestFactor := sdk.MustNewDecFromStr("1.1234")
|
2022-01-08 00:39:27 +00:00
|
|
|
accuralTimes := types.GenesisAccumulationTimes{
|
|
|
|
types.NewGenesisAccumulationTime("ukava", suite.genTime, supplyInterestFactor, borrowInterestFactor),
|
2021-09-01 11:11:15 +00:00
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
hardGenesis := types.NewGenesisState(
|
2021-09-01 11:11:15 +00:00
|
|
|
params,
|
|
|
|
accuralTimes,
|
|
|
|
deposits,
|
|
|
|
borrows,
|
|
|
|
totalSupplied,
|
|
|
|
totalBorrowed,
|
2022-01-08 00:39:27 +00:00
|
|
|
sdk.Coins{},
|
2021-09-01 11:11:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
suite.NotPanics(
|
|
|
|
func() {
|
|
|
|
suite.app.InitializeFromGenesisStatesWithTime(
|
|
|
|
suite.genTime,
|
2022-01-08 00:39:27 +00:00
|
|
|
app.GenesisState{types.ModuleName: suite.app.AppCodec().MustMarshalJSON(&hardGenesis)},
|
2021-09-01 11:11:15 +00:00
|
|
|
)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
var expectedDeposits types.Deposits
|
2021-09-01 11:11:15 +00:00
|
|
|
for _, deposit := range deposits {
|
|
|
|
// Deposit coin amounts
|
|
|
|
var depositAmount sdk.Coins
|
|
|
|
for _, coin := range deposit.Amount {
|
|
|
|
accrualTime, found := getGenesisAccumulationTime(coin.Denom, accuralTimes)
|
|
|
|
if !found {
|
|
|
|
panic(fmt.Sprintf("accrual time not found %s", coin.Denom))
|
|
|
|
}
|
|
|
|
expectedAmt := accrualTime.SupplyInterestFactor.MulInt(coin.Amount).RoundInt()
|
|
|
|
depositAmount = depositAmount.Add(sdk.NewCoin(coin.Denom, expectedAmt))
|
|
|
|
}
|
|
|
|
deposit.Amount = depositAmount
|
|
|
|
// Deposit interest factor indexes
|
2022-01-08 00:39:27 +00:00
|
|
|
var indexes types.SupplyInterestFactors
|
2021-09-01 11:11:15 +00:00
|
|
|
for _, index := range deposit.Index {
|
|
|
|
accrualTime, found := getGenesisAccumulationTime(index.Denom, accuralTimes)
|
|
|
|
if !found {
|
|
|
|
panic(fmt.Sprintf("accrual time not found %s", index.Denom))
|
|
|
|
}
|
|
|
|
index.Value = accrualTime.SupplyInterestFactor
|
|
|
|
indexes = append(indexes, index)
|
|
|
|
}
|
|
|
|
deposit.Index = indexes
|
|
|
|
expectedDeposits = append(expectedDeposits, deposit)
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
var expectedBorrows types.Borrows
|
2021-09-01 11:11:15 +00:00
|
|
|
for _, borrow := range borrows {
|
|
|
|
// Borrow coin amounts
|
|
|
|
var borrowAmount sdk.Coins
|
|
|
|
for _, coin := range borrow.Amount {
|
|
|
|
accrualTime, found := getGenesisAccumulationTime(coin.Denom, accuralTimes)
|
|
|
|
if !found {
|
|
|
|
panic(fmt.Sprintf("accrual time not found %s", coin.Denom))
|
|
|
|
}
|
|
|
|
expectedAmt := accrualTime.BorrowInterestFactor.MulInt(coin.Amount).RoundInt()
|
|
|
|
borrowAmount = borrowAmount.Add(sdk.NewCoin(coin.Denom, expectedAmt))
|
|
|
|
|
|
|
|
}
|
|
|
|
borrow.Amount = borrowAmount
|
|
|
|
// Borrow interest factor indexes
|
2022-01-08 00:39:27 +00:00
|
|
|
var indexes types.BorrowInterestFactors
|
2021-09-01 11:11:15 +00:00
|
|
|
for _, index := range borrow.Index {
|
|
|
|
accrualTime, found := getGenesisAccumulationTime(index.Denom, accuralTimes)
|
|
|
|
if !found {
|
|
|
|
panic(fmt.Sprintf("accrual time not found %s", index.Denom))
|
|
|
|
}
|
|
|
|
index.Value = accrualTime.BorrowInterestFactor
|
|
|
|
indexes = append(indexes, index)
|
|
|
|
}
|
|
|
|
borrow.Index = indexes
|
|
|
|
expectedBorrows = append(expectedBorrows, borrow)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedGenesis := hardGenesis
|
|
|
|
expectedGenesis.Deposits = expectedDeposits
|
|
|
|
expectedGenesis.Borrows = expectedBorrows
|
|
|
|
exportedGenesis := hard.ExportGenesis(suite.ctx, suite.keeper)
|
|
|
|
suite.Equal(expectedGenesis, exportedGenesis)
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func getGenesisAccumulationTime(denom string, ts types.GenesisAccumulationTimes) (types.GenesisAccumulationTime, bool) {
|
2021-09-01 11:11:15 +00:00
|
|
|
for _, t := range ts {
|
|
|
|
if t.CollateralType == denom {
|
|
|
|
return t, true
|
|
|
|
}
|
|
|
|
}
|
2022-01-08 00:39:27 +00:00
|
|
|
return types.GenesisAccumulationTime{}, false
|
2021-09-01 11:11:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenesisTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(GenesisTestSuite))
|
|
|
|
}
|