Skip to content

Commit f380d4a

Browse files
committed
upgrade to v2.0.0-rc.7
1 parent fd9444c commit f380d4a

8 files changed

Lines changed: 11340 additions & 3013 deletions

File tree

api/side/lending/lending.pulsar.go

Lines changed: 7736 additions & 2489 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/app.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ import (
161161
upgradev2 "github.com/sideprotocol/side/app/upgrades/v2"
162162
upgradev2rc5 "github.com/sideprotocol/side/app/upgrades/v2_rc5"
163163
upgradev2rc6 "github.com/sideprotocol/side/app/upgrades/v2_rc6"
164+
upgradev2rc7 "github.com/sideprotocol/side/app/upgrades/v2_rc7"
164165
)
165166

166167
const (
@@ -1332,6 +1333,7 @@ func (app *App) SetUpgradeHandlers() {
13321333
app.UpgradeKeeper.SetUpgradeHandler(upgradev2.UpgradeName, upgradev2.CreateUpgradeHandler(app.ModuleManager, app.configurator))
13331334
app.UpgradeKeeper.SetUpgradeHandler(upgradev2rc5.UpgradeName, upgradev2rc5.CreateUpgradeHandler(app.ModuleManager, app.configurator))
13341335
app.UpgradeKeeper.SetUpgradeHandler(upgradev2rc6.UpgradeName, upgradev2rc6.CreateUpgradeHandler(app.ModuleManager, app.configurator))
1336+
app.UpgradeKeeper.SetUpgradeHandler(upgradev2rc7.UpgradeName, upgradev2rc7.CreateUpgradeHandler(app.ModuleManager, app.configurator))
13351337

13361338
upgradeInfo, err := app.UpgradeKeeper.ReadUpgradeInfoFromDisk()
13371339
if err != nil {
@@ -1346,6 +1348,10 @@ func (app *App) SetUpgradeHandlers() {
13461348
if upgradeInfo.Name == upgradev2.UpgradeName {
13471349
app.SetStoreLoader(upgradetypes.UpgradeStoreLoader(upgradeInfo.Height, &upgradev2.StoreUpgrades))
13481350
}
1351+
1352+
if upgradeInfo.Name == upgradev2rc7.UpgradeName {
1353+
app.SetStoreLoader(upgradetypes.UpgradeStoreLoader(upgradeInfo.Height, &upgradev2rc7.StoreUpgrades))
1354+
}
13491355
}
13501356

13511357
func GetWasmOpts(appOpts servertypes.AppOptions) []wasmkeeper.Option {

app/upgrades/v2_rc7/upgrade.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package v2_rc7
2+
3+
import (
4+
"context"
5+
6+
storetypes "cosmossdk.io/store/types"
7+
upgradetypes "cosmossdk.io/x/upgrade/types"
8+
"github.com/cosmos/cosmos-sdk/types/module"
9+
10+
farmingtypes "github.com/sideprotocol/side/x/farming/types"
11+
)
12+
13+
// UpgradeName is the upgrade version name
14+
const UpgradeName = "v2.0.0-rc.7"
15+
16+
var StoreUpgrades = storetypes.StoreUpgrades{
17+
Added: []string{
18+
farmingtypes.ModuleName,
19+
},
20+
}
21+
22+
// CreateUpgradeHandler creates the upgrade handler
23+
func CreateUpgradeHandler(
24+
mm *module.Manager,
25+
configurator module.Configurator,
26+
) upgradetypes.UpgradeHandler {
27+
return func(ctx context.Context, _ upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
28+
return mm.RunMigrations(ctx, configurator, vm)
29+
}
30+
}

proto/side/lending/lending.proto

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,55 @@ message PoolConfig {
7777
bool paused = 13;
7878
}
7979

80+
// Pool config v1
81+
message PoolConfigV1 {
82+
// collateral asset metadata
83+
AssetMetadata collateral_asset = 1 [(gogoproto.nullable) = false];
84+
// lending asset metadata
85+
AssetMetadata lending_asset = 2 [(gogoproto.nullable) = false];
86+
// supply cap
87+
string supply_cap = 3 [
88+
(gogoproto.customtype) = "cosmossdk.io/math.Int",
89+
(gogoproto.nullable) = false
90+
];
91+
// borrow cap
92+
string borrow_cap = 4 [
93+
(gogoproto.customtype) = "cosmossdk.io/math.Int",
94+
(gogoproto.nullable) = false
95+
];
96+
// minimum amount to be borrowed
97+
string min_borrow_amount = 5 [
98+
(gogoproto.customtype) = "cosmossdk.io/math.Int",
99+
(gogoproto.nullable) = false
100+
];
101+
// maximum amount to be borrowed
102+
string max_borrow_amount = 6 [
103+
(gogoproto.customtype) = "cosmossdk.io/math.Int",
104+
(gogoproto.nullable) = false
105+
];
106+
// tranches
107+
repeated PoolTrancheConfig tranches = 7 [(gogoproto.nullable) = false];
108+
// request fee
109+
cosmos.base.v1beta1.Coin request_fee = 8 [
110+
(gogoproto.nullable) = false
111+
];
112+
// origination fee
113+
string origination_fee = 9 [
114+
(gogoproto.customtype) = "cosmossdk.io/math.Int",
115+
(gogoproto.nullable) = false
116+
];
117+
// reserve factor permille
118+
uint32 reserve_factor = 10;
119+
// referral fee factor permille
120+
uint32 referral_fee_factor = 11;
121+
// maximum ltv percent
122+
uint32 max_ltv = 12;
123+
// liquidation ltv percent
124+
uint32 liquidation_threshold = 13;
125+
// indicates if the pool is paused
126+
bool paused = 14;
127+
}
128+
80129
// Pool tranche
81130
message PoolTranche {
82131
// maturity duration
@@ -133,6 +182,40 @@ message LendingPool {
133182
PoolStatus status = 11;
134183
}
135184

185+
message LendingPoolV1 {
186+
string id = 1;
187+
cosmos.base.v1beta1.Coin supply = 2 [
188+
(gogoproto.nullable) = false
189+
];
190+
string available_amount = 3 [
191+
(gogoproto.customtype) = "cosmossdk.io/math.Int",
192+
(gogoproto.nullable) = false
193+
];
194+
string borrowed_amount = 4 [
195+
(gogoproto.customtype) = "cosmossdk.io/math.Int",
196+
(gogoproto.nullable) = false
197+
];
198+
string total_borrowed = 5 [
199+
(gogoproto.customtype) = "cosmossdk.io/math.Int",
200+
(gogoproto.nullable) = false
201+
];
202+
string reserve_amount = 6 [
203+
(gogoproto.customtype) = "cosmossdk.io/math.Int",
204+
(gogoproto.nullable) = false
205+
];
206+
string total_reserve = 7 [
207+
(gogoproto.customtype) = "cosmossdk.io/math.Int",
208+
(gogoproto.nullable) = false
209+
];
210+
cosmos.base.v1beta1.Coin total_ytokens = 8 [
211+
(gogoproto.nullable) = false,
212+
(gogoproto.customname) = "TotalYTokens"
213+
];
214+
repeated PoolTranche tranches = 9 [(gogoproto.nullable) = false];
215+
PoolConfigV1 config = 10 [(gogoproto.nullable) = false];
216+
PoolStatus status = 11;
217+
}
218+
136219
// Loan Status
137220
enum LoanStatus {
138221
// Unspecified
@@ -222,6 +305,55 @@ message Loan {
222305
LoanStatus status = 25;
223306
}
224307

308+
message LoanV1 {
309+
string vault_address = 1; // id
310+
string borrower = 2;
311+
string borrowerPubKey = 3;
312+
string borrowerAuthPubKey = 4;
313+
string dcm = 5 [(gogoproto.customname) = "DCM"];
314+
int64 maturity_time = 6;
315+
int64 final_timeout = 7;
316+
string pool_id = 8;
317+
cosmos.base.v1beta1.Coin borrow_amount = 9 [(gogoproto.nullable) = false];
318+
cosmos.base.v1beta1.Coin request_fee = 10 [(gogoproto.nullable) = false];
319+
string origination_fee = 11 [
320+
(gogoproto.customtype) = "cosmossdk.io/math.Int",
321+
(gogoproto.nullable) = false
322+
];
323+
string interest = 12 [
324+
(gogoproto.customtype) = "cosmossdk.io/math.Int",
325+
(gogoproto.nullable) = false
326+
];
327+
string protocol_fee = 13 [
328+
(gogoproto.customtype) = "cosmossdk.io/math.Int",
329+
(gogoproto.nullable) = false
330+
];
331+
int64 maturity = 14;
332+
uint32 borrow_apr = 15 [(gogoproto.customname) = "BorrowAPR"];
333+
int64 min_maturity = 16;
334+
string start_borrow_index = 17 [
335+
(cosmos_proto.scalar) = "cosmos.Dec",
336+
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
337+
(gogoproto.nullable) = false
338+
];
339+
string liquidation_price = 18 [
340+
(cosmos_proto.scalar) = "cosmos.Dec",
341+
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
342+
(gogoproto.nullable) = false
343+
];
344+
uint64 dlc_event_id = 19;
345+
repeated Authorization authorizations = 20 [(gogoproto.nullable) = false];
346+
string collateral_amount = 21 [
347+
(gogoproto.customtype) = "cosmossdk.io/math.Int",
348+
(gogoproto.nullable) = false
349+
];
350+
uint64 liquidation_id = 22;
351+
string referrer = 23;
352+
google.protobuf.Timestamp create_at = 24 [(gogoproto.nullable) = false, (gogoproto.stdtime) = true];
353+
google.protobuf.Timestamp disburse_at = 25 [(gogoproto.nullable) = false, (gogoproto.stdtime) = true];
354+
LoanStatus status = 26;
355+
}
356+
225357
// Referrer defines the referrer
226358
message Referrer {
227359
// Optional name

x/lending/keeper/migrations.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
sdk "github.com/cosmos/cosmos-sdk/types"
55

66
v2 "github.com/sideprotocol/side/x/lending/migrations/v2"
7+
v3 "github.com/sideprotocol/side/x/lending/migrations/v3"
78
)
89

910
// Migrator is a struct for handling in-place store migrations
@@ -20,3 +21,8 @@ func NewMigrator(keeper Keeper) Migrator {
2021
func (m Migrator) Migrate1to2(ctx sdk.Context) error {
2122
return v2.MigrateStore(ctx, m.keeper.storeKey, m.keeper.cdc)
2223
}
24+
25+
// Migrate2to3 migrates from version 2 to 3
26+
func (m Migrator) Migrate2to3(ctx sdk.Context) error {
27+
return v3.MigrateStore(ctx, m.keeper.storeKey, m.keeper.cdc)
28+
}

x/lending/migrations/v3/store.go

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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+
}

x/lending/module/module.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
)
2525

2626
// ConsensusVersion defines the current x/lending module consensus version.
27-
const ConsensusVersion = 2
27+
const ConsensusVersion = 3
2828

2929
var (
3030
_ module.AppModule = AppModule{}
@@ -126,6 +126,9 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
126126
if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil {
127127
panic(fmt.Sprintf("failed to migrate x/%s from version 1 to 2: %v", types.ModuleName, err))
128128
}
129+
if err := cfg.RegisterMigration(types.ModuleName, 2, m.Migrate2to3); err != nil {
130+
panic(fmt.Sprintf("failed to migrate x/%s from version 2 to 3: %v", types.ModuleName, err))
131+
}
129132
}
130133

131134
// RegisterInvariants registers the invariants of the module. If an invariant deviates from its predicted value, the InvariantRegistry triggers appropriate logic (most often the chain will be halted)

0 commit comments

Comments
 (0)