-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: x/metoken end block functions (#2167)
* reserves and interest refeactored * tests adapted (not working yet) * endblock funcs * Update x/metoken/keeper/redeem.go Co-authored-by: Adam Moser <63419657+toteki@users.noreply.github.com> * adam comments * fix mocks * safeguard for transferring dust * robert comments * fix * safer exit when asset not found * imports * feat: add SupplyFromModule and WithdrawFromModule to leverage (#2170) * add module-based supply and withdraw to leverage module * cl++ * change return order * godoc++ and function order * mocks * mocks --------- Co-authored-by: Egor Kostetskiy <kosegor@gmail.com> * new integration with x/leverage * fix after merge from main * unit mocks * fix * better logs * couple more comments --------- Co-authored-by: Adam Moser <63419657+toteki@users.noreply.github.com>
- Loading branch information
Showing
21 changed files
with
931 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package errors | ||
|
||
type LeverageError struct { | ||
Err error | ||
IsRecoverable bool | ||
} | ||
|
||
func (le *LeverageError) Error() string { | ||
return le.Err.Error() | ||
} | ||
|
||
func Wrap(err error, isRecoverable bool) *LeverageError { | ||
return &LeverageError{ | ||
Err: err, | ||
IsRecoverable: isRecoverable, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package keeper | ||
|
||
import ( | ||
"errors" | ||
"time" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
"github.com/umee-network/umee/v5/x/metoken" | ||
lerrors "github.com/umee-network/umee/v5/x/metoken/errors" | ||
) | ||
|
||
// ClaimLeverageInterest sends accrued interest from x/leverage module to x/metoken account. | ||
func (k Keeper) ClaimLeverageInterest() error { | ||
if k.ctx.BlockTime().Before(k.getNextInterestClaimTime()) { | ||
return nil | ||
} | ||
|
||
leverageLiquidity, err := k.leverageKeeper.GetAllSupplied(*k.ctx, ModuleAddr()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
indexes := k.GetAllRegisteredIndexes() | ||
for _, index := range indexes { | ||
balances, err := k.IndexBalances(index.Denom) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
updatedBalances := make([]metoken.AssetBalance, 0) | ||
for _, balance := range balances.AssetBalances { | ||
if balance.Leveraged.IsPositive() { | ||
found, liquidity := leverageLiquidity.Find(balance.Denom) | ||
if !found { | ||
continue | ||
} | ||
|
||
// If there is more liquidity in x/leverage than expected, we claim the delta, | ||
// which is the accrued interest | ||
if liquidity.Amount.GT(balance.Leveraged) { | ||
accruedInterest := sdk.NewCoin(balance.Denom, liquidity.Amount.Sub(balance.Leveraged)) | ||
tokensWithdrawn, err := k.withdrawFromLeverage(accruedInterest) | ||
if err != nil { | ||
var leverageError *lerrors.LeverageError | ||
if errors.As(err, &leverageError) && leverageError.IsRecoverable { | ||
k.Logger().Debug( | ||
"claiming interest: couldn't withdraw from leverage", | ||
"error", err.Error(), | ||
"block_time", k.ctx.BlockTime(), | ||
) | ||
continue | ||
} | ||
|
||
return err | ||
} | ||
|
||
balance.Interest = balance.Interest.Add(tokensWithdrawn.Amount) | ||
updatedBalances = append(updatedBalances, balance) | ||
} | ||
} | ||
} | ||
|
||
if err = k.updateBalances(balances, updatedBalances); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
k.setNextInterestClaimTime(k.ctx.BlockTime().Add(time.Duration(k.GetParams().ClaimingFrequency) * time.Second)) | ||
|
||
return nil | ||
} |
Oops, something went wrong.