mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-11-10 18:15:19 +00:00
4e6f6d1e9c
* spike: incentive/types * spike: incentive/types tests * spike: incentive/types/expected_keepers.go * spike: incentive/keeper * spike: incentive/keeper tests * spike: incentive/sims and incentive/sims tests * spike: incentive/module * spike: incentive/module tests * spike: hard/types * spike: hard/types hooks * spike: hard/types * spike: hard/keeper basics * spike: hard/keeper hooks * integrate hard/keeper/borrow.go * integrate hard/keeper/deposit.go * integrate hard/keeper/liquidation.go * integrate hard/keeper/withdraw.go * integrate hard/keeper/repay.go * spike: hard/sims * spike: hard/sims tests * spike: hard/client * spike: hard/module * integrate app.go * spike: x/hard/keeper compile tests * incentive/keeper test clean up * validate usdx incentive types in genesis * refactoring & fix deposit test * fix liquidaton tests * fix incentive tests for hard supply rewards * fix hard genesis tests * update incentive genesis state and params * update cdp rewards accumulation * update app init order and begin blocker order Co-authored-by: karzak <kjydavis3@gmail.com>
93 lines
2.6 KiB
Go
93 lines
2.6 KiB
Go
package keeper
|
|
|
|
import (
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
|
|
|
"github.com/kava-labs/kava/x/hard/types"
|
|
)
|
|
|
|
// Withdraw returns some or all of a deposit back to original depositor
|
|
func (k Keeper) Withdraw(ctx sdk.Context, depositor sdk.AccAddress, coins sdk.Coins) error {
|
|
// Get current stored LTV based on stored borrows/deposits
|
|
prevLtv, err := k.GetStoreLTV(ctx, depositor)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
deposit, found := k.GetDeposit(ctx, depositor)
|
|
if !found {
|
|
return sdkerrors.Wrapf(types.ErrDepositNotFound, "no deposit found for %s", depositor)
|
|
}
|
|
// Call incentive hook
|
|
k.BeforeDepositModified(ctx, deposit)
|
|
|
|
k.SyncBorrowInterest(ctx, depositor)
|
|
k.SyncSupplyInterest(ctx, depositor)
|
|
|
|
amount, err := k.CalculateWithdrawAmount(deposit.Amount, coins)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
borrow, found := k.GetBorrow(ctx, depositor)
|
|
if !found {
|
|
borrow = types.Borrow{}
|
|
}
|
|
|
|
proposedDeposit := types.NewDeposit(deposit.Depositor, deposit.Amount.Sub(amount), types.SupplyInterestFactors{})
|
|
valid, err := k.IsWithinValidLtvRange(ctx, proposedDeposit, borrow)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !valid {
|
|
return sdkerrors.Wrapf(types.ErrInvalidWithdrawAmount, "proposed withdraw outside loan-to-value range")
|
|
}
|
|
|
|
err = k.supplyKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleAccountName, depositor, amount)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
deposit.Amount = deposit.Amount.Sub(amount)
|
|
newLtv, err := k.CalculateLtv(ctx, deposit, borrow)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
k.UpdateDepositAndLtvIndex(ctx, deposit, newLtv, prevLtv)
|
|
|
|
// Update total supplied amount
|
|
k.DecrementBorrowedCoins(ctx, amount)
|
|
|
|
// Call incentive hook
|
|
k.AfterDepositModified(ctx, deposit)
|
|
|
|
ctx.EventManager().EmitEvent(
|
|
sdk.NewEvent(
|
|
types.EventTypeHardWithdrawal,
|
|
sdk.NewAttribute(sdk.AttributeKeyAmount, amount.String()),
|
|
sdk.NewAttribute(types.AttributeKeyDepositor, depositor.String()),
|
|
),
|
|
)
|
|
return nil
|
|
}
|
|
|
|
// CalculateWithdrawAmount enables full withdraw of deposited coins by adjusting withdraw amount
|
|
// to equal total deposit amount if the requested withdraw amount > current deposit amount
|
|
func (k Keeper) CalculateWithdrawAmount(available sdk.Coins, request sdk.Coins) (sdk.Coins, error) {
|
|
result := sdk.Coins{}
|
|
|
|
if !request.DenomsSubsetOf(available) {
|
|
return result, types.ErrInvalidWithdrawDenom
|
|
}
|
|
|
|
for _, coin := range request {
|
|
if coin.Amount.GT(available.AmountOf(coin.Denom)) {
|
|
result = append(result, sdk.NewCoin(coin.Denom, available.AmountOf(coin.Denom)))
|
|
} else {
|
|
result = append(result, coin)
|
|
}
|
|
}
|
|
return result, nil
|
|
}
|