2022-07-28 16:39:57 +00:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
2024-05-01 03:17:24 +00:00
|
|
|
"github.com/0glabs/0g-chain/x/earn/types"
|
2022-07-28 16:39:57 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// HardStrategy defines the strategy that deposits assets to Hard
|
|
|
|
type HardStrategy Keeper
|
|
|
|
|
|
|
|
var _ Strategy = (*HardStrategy)(nil)
|
|
|
|
|
2022-09-12 17:50:35 +00:00
|
|
|
// GetStrategyType returns the strategy type
|
2022-07-28 16:39:57 +00:00
|
|
|
func (s *HardStrategy) GetStrategyType() types.StrategyType {
|
|
|
|
return types.STRATEGY_TYPE_HARD
|
|
|
|
}
|
|
|
|
|
2022-09-12 17:50:35 +00:00
|
|
|
// GetEstimatedTotalAssets returns the current value of all assets deposited
|
|
|
|
// in hard.
|
2022-07-28 16:39:57 +00:00
|
|
|
func (s *HardStrategy) GetEstimatedTotalAssets(ctx sdk.Context, denom string) (sdk.Coin, error) {
|
|
|
|
macc := s.accountKeeper.GetModuleAccount(ctx, types.ModuleName)
|
|
|
|
deposit, found := s.hardKeeper.GetSyncedDeposit(ctx, macc.GetAddress())
|
|
|
|
if !found {
|
|
|
|
// Return 0 if no deposit exists for module account
|
|
|
|
return sdk.NewCoin(denom, sdk.ZeroInt()), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only return the deposit for the vault denom.
|
|
|
|
for _, coin := range deposit.Amount {
|
|
|
|
if coin.Denom == denom {
|
|
|
|
return coin, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return 0 if no deposit exists for the vault denom
|
|
|
|
return sdk.NewCoin(denom, sdk.ZeroInt()), nil
|
|
|
|
}
|
|
|
|
|
2022-09-12 17:50:35 +00:00
|
|
|
// Deposit deposits the specified amount of coins into hard.
|
2022-07-28 16:39:57 +00:00
|
|
|
func (s *HardStrategy) Deposit(ctx sdk.Context, amount sdk.Coin) error {
|
|
|
|
macc := s.accountKeeper.GetModuleAccount(ctx, types.ModuleName)
|
|
|
|
return s.hardKeeper.Deposit(ctx, macc.GetAddress(), sdk.NewCoins(amount))
|
|
|
|
}
|
|
|
|
|
2022-09-12 17:50:35 +00:00
|
|
|
// Withdraw withdraws the specified amount of coins from hard.
|
2022-07-28 16:39:57 +00:00
|
|
|
func (s *HardStrategy) Withdraw(ctx sdk.Context, amount sdk.Coin) error {
|
|
|
|
macc := s.accountKeeper.GetModuleAccount(ctx, types.ModuleName)
|
|
|
|
return s.hardKeeper.Withdraw(ctx, macc.GetAddress(), sdk.NewCoins(amount))
|
|
|
|
}
|