0g-chain/x/hard/legacy/v0_16/migrate.go
Ruaridh ffef832d45
Upgrade to sdk v0.44.5 and add IBC (#1106)
- Upgrade cosmos-sdk to v0.44.5 from v0.39.2
- Add Legacy Tx Endpoint for backwards compatibility
- Add IBC v1.2.3 Support

Co-authored-by: DracoLi <draco@dracoli.com>
Co-authored-by: drklee3 <derrick@dlee.dev>
Co-authored-by: denalimarsh <denalimarsh@gmail.com>
Co-authored-by: Draco Li <draco@kava.io>
Co-authored-by: Nick DeLuca <nickdeluca08@gmail.com>
Co-authored-by: Kevin Davis <karzak@users.noreply.github.com>
Co-authored-by: Denali Marsh <denali@kava.io>
2022-01-07 17:39:27 -07:00

102 lines
3.4 KiB
Go

package v0_16
import (
v015hard "github.com/kava-labs/kava/x/hard/legacy/v0_15"
v016hard "github.com/kava-labs/kava/x/hard/types"
)
func migrateParams(params v015hard.Params) v016hard.Params {
moneyMarkets := make([]v016hard.MoneyMarket, len(params.MoneyMarkets))
for i, mm := range params.MoneyMarkets {
moneyMarkets[i] = v016hard.MoneyMarket{
Denom: mm.Denom,
BorrowLimit: v016hard.BorrowLimit{
HasMaxLimit: mm.BorrowLimit.HasMaxLimit,
MaximumLimit: mm.BorrowLimit.MaximumLimit,
LoanToValue: mm.BorrowLimit.LoanToValue,
},
SpotMarketID: mm.SpotMarketID,
ConversionFactor: mm.ConversionFactor,
InterestRateModel: v016hard.InterestRateModel{
BaseRateAPY: mm.InterestRateModel.BaseRateAPY,
BaseMultiplier: mm.InterestRateModel.BaseMultiplier,
Kink: mm.InterestRateModel.Kink,
JumpMultiplier: mm.InterestRateModel.JumpMultiplier,
},
ReserveFactor: mm.ReserveFactor,
KeeperRewardPercentage: mm.KeeperRewardPercentage,
}
}
return v016hard.Params{
MoneyMarkets: moneyMarkets,
MinimumBorrowUSDValue: params.MinimumBorrowUSDValue,
}
}
func migrateDeposits(oldDeposits v015hard.Deposits) v016hard.Deposits {
deposits := make(v016hard.Deposits, len(oldDeposits))
for i, deposit := range oldDeposits {
interestFactors := make(v016hard.SupplyInterestFactors, len(deposit.Index))
for j, interestFactor := range deposit.Index {
interestFactors[j] = v016hard.SupplyInterestFactor{
Denom: interestFactor.Denom,
Value: interestFactor.Value,
}
}
deposits[i] = v016hard.Deposit{
Depositor: deposit.Depositor,
Amount: deposit.Amount,
Index: interestFactors,
}
}
return deposits
}
func migratePrevAccTimes(oldPrevAccTimes v015hard.GenesisAccumulationTimes) v016hard.GenesisAccumulationTimes {
prevAccTimes := make(v016hard.GenesisAccumulationTimes, len(oldPrevAccTimes))
for i, prevAccTime := range oldPrevAccTimes {
prevAccTimes[i] = v016hard.GenesisAccumulationTime{
CollateralType: prevAccTime.CollateralType,
PreviousAccumulationTime: prevAccTime.PreviousAccumulationTime,
SupplyInterestFactor: prevAccTime.SupplyInterestFactor,
BorrowInterestFactor: prevAccTime.BorrowInterestFactor,
}
}
return prevAccTimes
}
func migrateBorrows(oldBorrows v015hard.Borrows) v016hard.Borrows {
borrows := make(v016hard.Borrows, len(oldBorrows))
for i, borrow := range oldBorrows {
interestFactors := make(v016hard.BorrowInterestFactors, len(borrow.Index))
for j, interestFactor := range borrow.Index {
interestFactors[j] = v016hard.BorrowInterestFactor{
Denom: interestFactor.Denom,
Value: interestFactor.Value,
}
}
borrows[i] = v016hard.Borrow{
Borrower: borrow.Borrower,
Amount: borrow.Amount,
Index: interestFactors,
}
}
return borrows
}
// Migrate converts v0.15 hard state and returns it in v0.16 format
func Migrate(oldState v015hard.GenesisState) *v016hard.GenesisState {
return &v016hard.GenesisState{
Params: migrateParams(oldState.Params),
PreviousAccumulationTimes: migratePrevAccTimes(oldState.PreviousAccumulationTimes),
Deposits: migrateDeposits(oldState.Deposits),
Borrows: migrateBorrows(oldState.Borrows),
TotalSupplied: oldState.TotalSupplied,
TotalBorrowed: oldState.TotalBorrowed,
TotalReserves: oldState.TotalReserves,
}
}