Skip to content

Commit

Permalink
fix: x/leverage FundOracle could produce invalid coins (#652)
Browse files Browse the repository at this point in the history
  • Loading branch information
toteki authored Mar 15, 2022
1 parent 1ed9306 commit ea150c9
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions x/leverage/keeper/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,21 @@ func (k Keeper) EquivalentTokenValue(ctx sdk.Context, fromCoin sdk.Coin, toDenom
), nil
}

// FundOracle transfers coins to the oracle module account, as long as the
// leverage module account has sufficient unreserved assets.
func (k Keeper) FundOracle(ctx sdk.Context, rewards sdk.Coins) error {
// Reduce rewards if they exceed unresered module balance
for i, coin := range rewards {
// FundOracle transfers requested coins to the oracle module account, as
// long as the leverage module account has sufficient unreserved assets.
func (k Keeper) FundOracle(ctx sdk.Context, requested sdk.Coins) error {
rewards := sdk.Coins{}

// reduce rewards if they exceed unreserved module balance
for _, coin := range requested {
reserved := k.GetReserveAmount(ctx, coin.Denom)
balance := k.ModuleBalance(ctx, coin.Denom)

amountToTransfer := sdk.MinInt(coin.Amount, balance.Sub(reserved))
amountToTransfer = sdk.MaxInt(amountToTransfer, sdk.ZeroInt())

rewards[i].Amount = amountToTransfer
if amountToTransfer.IsPositive() {
rewards = rewards.Add(sdk.NewCoin(coin.Denom, amountToTransfer))
}
}

// Because this action is not caused by a message, logging and
Expand Down

0 comments on commit ea150c9

Please sign in to comment.