|
| 1 | +package v3 |
| 2 | + |
| 3 | +import ( |
| 4 | + sdkmath "cosmossdk.io/math" |
| 5 | + storetypes "cosmossdk.io/store/types" |
| 6 | + "github.com/cosmos/cosmos-sdk/codec" |
| 7 | + sdk "github.com/cosmos/cosmos-sdk/types" |
| 8 | + |
| 9 | + "github.com/sideprotocol/side/x/lending/types" |
| 10 | +) |
| 11 | + |
| 12 | +// MigrateStore migrates the x/lending module state from the consensus version 2 to |
| 13 | +// version 3 |
| 14 | +func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec) error { |
| 15 | + migrateLoans(ctx, storeKey, cdc) |
| 16 | + migratePools(ctx, storeKey, cdc) |
| 17 | + registerReferrer(ctx, storeKey, cdc) |
| 18 | + |
| 19 | + return nil |
| 20 | +} |
| 21 | + |
| 22 | +// migrateLoans performs the loan migration |
| 23 | +func migrateLoans(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec) { |
| 24 | + store := ctx.KVStore(storeKey) |
| 25 | + |
| 26 | + iterator := storetypes.KVStorePrefixIterator(store, types.LoanKeyPrefix) |
| 27 | + defer iterator.Close() |
| 28 | + |
| 29 | + for ; iterator.Valid(); iterator.Next() { |
| 30 | + // unmarshal loan to v1 |
| 31 | + var loanV1 types.LoanV1 |
| 32 | + cdc.MustUnmarshal(iterator.Value(), &loanV1) |
| 33 | + |
| 34 | + // build new loan |
| 35 | + loan := &types.Loan{ |
| 36 | + VaultAddress: loanV1.VaultAddress, |
| 37 | + Borrower: loanV1.Borrower, |
| 38 | + BorrowerPubKey: loanV1.BorrowerPubKey, |
| 39 | + BorrowerAuthPubKey: loanV1.BorrowerAuthPubKey, |
| 40 | + DCM: loanV1.DCM, |
| 41 | + MaturityTime: loanV1.MaturityTime, |
| 42 | + FinalTimeout: loanV1.FinalTimeout, |
| 43 | + PoolId: loanV1.PoolId, |
| 44 | + BorrowAmount: loanV1.BorrowAmount, |
| 45 | + RequestFee: loanV1.RequestFee, |
| 46 | + OriginationFee: loanV1.OriginationFee, |
| 47 | + Interest: loanV1.Interest, |
| 48 | + ProtocolFee: loanV1.ProtocolFee, |
| 49 | + Maturity: loanV1.Maturity, |
| 50 | + BorrowAPR: loanV1.BorrowAPR, |
| 51 | + StartBorrowIndex: loanV1.StartBorrowIndex, |
| 52 | + LiquidationPrice: loanV1.LiquidationPrice, |
| 53 | + DlcEventId: loanV1.DlcEventId, |
| 54 | + Authorizations: loanV1.Authorizations, |
| 55 | + CollateralAmount: loanV1.CollateralAmount, |
| 56 | + LiquidationId: loanV1.LiquidationId, |
| 57 | + Referrer: nil, |
| 58 | + CreateAt: loanV1.CreateAt, |
| 59 | + DisburseAt: loanV1.DisburseAt, |
| 60 | + Status: loanV1.Status, |
| 61 | + } |
| 62 | + |
| 63 | + // update status |
| 64 | + if loan.Status >= types.LoanStatus(2) { |
| 65 | + loan.Status = loan.Status + 1 |
| 66 | + } |
| 67 | + |
| 68 | + // update loan |
| 69 | + store.Set(iterator.Key(), cdc.MustMarshal(loan)) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +// migratePools performs the pool migration |
| 74 | +func migratePools(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec) { |
| 75 | + store := ctx.KVStore(storeKey) |
| 76 | + |
| 77 | + iterator := storetypes.KVStorePrefixIterator(store, types.PoolKeyPrefix) |
| 78 | + defer iterator.Close() |
| 79 | + |
| 80 | + for ; iterator.Valid(); iterator.Next() { |
| 81 | + // unmarshal pool to v1 |
| 82 | + var poolV1 types.LendingPoolV1 |
| 83 | + cdc.MustUnmarshal(iterator.Value(), &poolV1) |
| 84 | + |
| 85 | + // build new pool |
| 86 | + pool := &types.LendingPool{ |
| 87 | + Id: poolV1.Id, |
| 88 | + Supply: poolV1.Supply, |
| 89 | + AvailableAmount: poolV1.AvailableAmount, |
| 90 | + BorrowedAmount: poolV1.BorrowedAmount, |
| 91 | + TotalBorrowed: poolV1.TotalBorrowed, |
| 92 | + ReserveAmount: poolV1.ReserveAmount, |
| 93 | + TotalReserve: poolV1.TotalReserve, |
| 94 | + TotalYTokens: poolV1.TotalYTokens, |
| 95 | + Tranches: poolV1.Tranches, |
| 96 | + Config: types.PoolConfig{ |
| 97 | + CollateralAsset: poolV1.Config.CollateralAsset, |
| 98 | + LendingAsset: poolV1.Config.LendingAsset, |
| 99 | + SupplyCap: poolV1.Config.SupplyCap, |
| 100 | + BorrowCap: poolV1.Config.BorrowCap, |
| 101 | + MinBorrowAmount: poolV1.Config.MinBorrowAmount, |
| 102 | + MaxBorrowAmount: poolV1.Config.MaxBorrowAmount, |
| 103 | + Tranches: poolV1.Config.Tranches, |
| 104 | + RequestFee: poolV1.Config.RequestFee, |
| 105 | + OriginationFeeFactor: 1, // 0.1% |
| 106 | + ReserveFactor: poolV1.Config.ReserveFactor, |
| 107 | + MaxLtv: poolV1.Config.MaxLtv, |
| 108 | + LiquidationThreshold: poolV1.Config.LiquidationThreshold, |
| 109 | + Paused: poolV1.Config.Paused, |
| 110 | + }, |
| 111 | + Status: poolV1.Status, |
| 112 | + } |
| 113 | + |
| 114 | + // update pool |
| 115 | + store.Set(iterator.Key(), cdc.MustMarshal(pool)) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +// registerReferrer performs the referrer pre-registration |
| 120 | +func registerReferrer(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec) { |
| 121 | + store := ctx.KVStore(storeKey) |
| 122 | + |
| 123 | + referrer := &types.Referrer{ |
| 124 | + Name: "side", |
| 125 | + ReferralCode: "SIDE1234", |
| 126 | + Address: "tb1q37m9xfqscwral5km478geh4xzgsyw7nevmhaxx", |
| 127 | + ReferralFeeFactor: sdkmath.LegacyMustNewDecFromStr("0.2"), |
| 128 | + } |
| 129 | + |
| 130 | + store.Set(types.ReferrerKey(referrer.ReferralCode), cdc.MustMarshal(referrer)) |
| 131 | +} |
0 commit comments