Skip to content

Commit 57908c3

Browse files
committed
improve pool config update
1 parent 7fdb0bd commit 57908c3

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

x/lending/keeper/msg_server_pool.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,9 @@ func (m msgServer) UpdatePoolConfig(goCtx context.Context, msg *types.MsgUpdateP
181181
}
182182

183183
pool := m.GetPool(ctx, msg.PoolId)
184+
184185
m.UpdatePoolStatus(ctx, pool, &msg.Config)
186+
m.OnPoolTranchesConfigChanged(ctx, pool, msg.Config.Tranches)
185187

186188
pool.Config = msg.Config
187189
m.SetPool(ctx, pool)

x/lending/keeper/pool.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,17 @@ func (k Keeper) UpdatePoolStatus(ctx sdk.Context, pool *types.LendingPool, newCo
200200
}
201201
}
202202

203+
// OnPoolTranchesConfigChanged is called when the pool tranches config changes
204+
func (k Keeper) OnPoolTranchesConfigChanged(ctx sdk.Context, pool *types.LendingPool, newTranchesConfig []types.PoolTrancheConfig) {
205+
for _, newConfig := range newTranchesConfig {
206+
_, found := types.GetTrancheConfig(pool.Config.Tranches, newConfig.Maturity)
207+
if !found {
208+
// add the new tranche to pool if the tranche does not exist
209+
pool.Tranches = append(pool.Tranches, types.NewTranche(newConfig))
210+
}
211+
}
212+
}
213+
203214
// NormalizePool normalizes the given pool
204215
func (k Keeper) NormalizePool(ctx sdk.Context, pool *types.LendingPool) {
205216
if pool.TotalBorrowed.IsNegative() {

x/lending/types/lending.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,13 +264,20 @@ func GetTranche(tranches []PoolTranche, maturity int64) (*PoolTranche, bool) {
264264
return nil, false
265265
}
266266

267+
// NewTranche creates a new tranche from the given tranche config
268+
func NewTranche(config PoolTrancheConfig) PoolTranche {
269+
return PoolTranche{
270+
Maturity: config.Maturity,
271+
BorrowIndex: InitialBorrowIndex,
272+
}
273+
}
274+
267275
// NewTranches initializes the pool tranches from the given tranche configs
268276
func NewTranches(trancheConfigs []PoolTrancheConfig) []PoolTranche {
269277
tranches := make([]PoolTranche, len(trancheConfigs))
270278

271279
for i, config := range trancheConfigs {
272-
tranches[i].Maturity = config.Maturity
273-
tranches[i].BorrowIndex = InitialBorrowIndex
280+
tranches[i] = NewTranche(config)
274281
}
275282

276283
return tranches

0 commit comments

Comments
 (0)