Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactore(leverage): rename ExchangeToken #2180

Merged
merged 12 commits into from
Aug 2, 2023
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
- `/umee/ugov/v1/min-gas-price` --> `/umee/ugov/v1/min_gas_price`
- `/umee/ugov/v1/emergency-group` --> `/umee/ugov/v1/emergency_group`
- `/umee/uibc/v1/all-outflows` --> `/umee/uibc/v1/all_outflows`
- [2180](https://github.com/umee-network/umee/pull/2180) Rename leverage `Keeper.ExchangeToken -> ToUToken`, `Keeper.ExchangeUToken -> ToToken` and `Keeper.ExchangeUTokens -> ToTokens`.

## [v5.1.0](https://github.com/umee-network/umee/releases/tag/v5.1.0) - 2023-07-07

Expand Down
3 changes: 2 additions & 1 deletion app/inflation/inflation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
"github.com/golang/mock/gomock"
"gotest.tools/v3/assert"

"github.com/umee-network/umee/v5/app/inflation"
mocks "github.com/umee-network/umee/v5/app/inflation/mocks"
appparams "github.com/umee-network/umee/v5/app/params"
"github.com/umee-network/umee/v5/tests/tsdk"
"github.com/umee-network/umee/v5/util/bpmath"
"github.com/umee-network/umee/v5/util/coin"
"github.com/umee-network/umee/v5/x/ugov"
"gotest.tools/v3/assert"
)

func TestAdjustInflation(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion x/incentive/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ type LeverageKeeper interface {
GetTokenSettings(ctx sdk.Context, denom string) (leveragetypes.Token, error)
// These are used for APY queries only
TotalTokenValue(ctx sdk.Context, coins sdk.Coins, mode leveragetypes.PriceMode) (sdk.Dec, error)
ExchangeUToken(ctx sdk.Context, uToken sdk.Coin) (sdk.Coin, error)
ToToken(ctx sdk.Context, uToken sdk.Coin) (sdk.Coin, error)
}
2 changes: 1 addition & 1 deletion x/incentive/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func (q Querier) ActualRates(
}

// compute oracle price ratio of rewards to reference bond amount
referenceToken, err := k.leverageKeeper.ExchangeUToken(ctx, referenceUToken)
referenceToken, err := k.leverageKeeper.ToToken(ctx, referenceUToken)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions x/incentive/keeper/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ func (m *mockLeverageKeeper) TotalTokenValue(_ sdk.Context, coins sdk.Coins, _ l
return total, nil
}

// ExchangeUToken implements the expected leverage keeper, with uToken exchange rates always equal to 1
func (m *mockLeverageKeeper) ExchangeUToken(_ sdk.Context, uToken sdk.Coin) (sdk.Coin, error) {
// ToToken implements the expected leverage keeper, with uToken exchange rates always equal to 1
func (m *mockLeverageKeeper) ToToken(_ sdk.Context, uToken sdk.Coin) (sdk.Coin, error) {
if !leveragetypes.HasUTokenPrefix(uToken.Denom) {
return uToken, leveragetypes.ErrUToken
}
Expand Down
6 changes: 3 additions & 3 deletions x/leverage/keeper/borrows.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (k Keeper) CalculateBorrowLimit(ctx sdk.Context, collateral sdk.Coins) (sdk

for _, coin := range collateral {
// convert uToken collateral to base assets
baseAsset, err := k.ExchangeUToken(ctx, coin)
baseAsset, err := k.ToToken(ctx, coin)
if err != nil {
return sdk.ZeroDec(), err
}
Expand Down Expand Up @@ -170,7 +170,7 @@ func (k Keeper) VisibleBorrowLimit(ctx sdk.Context, collateral sdk.Coins) (sdk.D

for _, coin := range collateral {
// convert uToken collateral to base assets
baseAsset, err := k.ExchangeUToken(ctx, coin)
baseAsset, err := k.ToToken(ctx, coin)
if err != nil {
return sdk.ZeroDec(), err
}
Expand Down Expand Up @@ -208,7 +208,7 @@ func (k Keeper) CalculateLiquidationThreshold(ctx sdk.Context, collateral sdk.Co

for _, coin := range collateral {
// convert uToken collateral to base assets
baseAsset, err := k.ExchangeUToken(ctx, coin)
baseAsset, err := k.ToToken(ctx, coin)
if err != nil {
return sdk.ZeroDec(), err
}
Expand Down
6 changes: 3 additions & 3 deletions x/leverage/keeper/collateral.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (k Keeper) CalculateCollateralValue(ctx sdk.Context, collateral sdk.Coins,

for _, coin := range collateral {
// convert uToken collateral to base assets
baseAsset, err := k.ExchangeUToken(ctx, coin)
baseAsset, err := k.ToToken(ctx, coin)
if err != nil {
return sdk.ZeroDec(), err
}
Expand All @@ -107,7 +107,7 @@ func (k Keeper) VisibleCollateralValue(ctx sdk.Context, collateral sdk.Coins, mo

for _, coin := range collateral {
// convert uToken collateral to base assets
baseAsset, err := k.ExchangeUToken(ctx, coin)
baseAsset, err := k.ToToken(ctx, coin)
if err != nil {
return sdk.ZeroDec(), err
}
Expand Down Expand Up @@ -251,7 +251,7 @@ func (k Keeper) ModuleMaxWithdraw(ctx sdk.Context, spendableUTokens sdk.Coin) (s

// Get module collateral for the uDenom
totalCollateral := k.GetTotalCollateral(ctx, spendableUTokens.Denom)
totalTokenCollateral, err := k.ExchangeUTokens(ctx, sdk.NewCoins(totalCollateral))
totalTokenCollateral, err := k.ToTokens(ctx, sdk.NewCoins(totalCollateral))
if err != nil {
return sdk.ZeroInt(), err
}
Expand Down
22 changes: 11 additions & 11 deletions x/leverage/keeper/exchange_rate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"github.com/umee-network/umee/v5/x/leverage/types"
)

// ExchangeToken converts an sdk.Coin containing a base asset to its value as a
// uToken.
func (k Keeper) ExchangeToken(ctx sdk.Context, token sdk.Coin) (sdk.Coin, error) {
// ToUToken returns uToken in the amount a user would receive when supplying the token.
// Returns error if the input is not a Token.
func (k Keeper) ToUToken(ctx sdk.Context, token sdk.Coin) (sdk.Coin, error) {
if err := token.Validate(); err != nil {
return sdk.Coin{}, err
}
Expand All @@ -19,14 +19,13 @@ func (k Keeper) ExchangeToken(ctx sdk.Context, token sdk.Coin) (sdk.Coin, error)
}

exchangeRate := k.DeriveExchangeRate(ctx, token.Denom)

uTokenAmount := toDec(token.Amount).Quo(exchangeRate).TruncateInt()
return sdk.NewCoin(uTokenDenom, uTokenAmount), nil
}

// ExchangeUToken converts an sdk.Coin containing a uToken to its value in a base
// token.
func (k Keeper) ExchangeUToken(ctx sdk.Context, uToken sdk.Coin) (sdk.Coin, error) {
// ToToken returns Token in the amount a user would receive when withdrawing the uToken.
// Returns error if the input is not a uToken.
func (k Keeper) ToToken(ctx sdk.Context, uToken sdk.Coin) (sdk.Coin, error) {
if err := uToken.Validate(); err != nil {
return sdk.Coin{}, err
}
Expand All @@ -42,16 +41,17 @@ func (k Keeper) ExchangeUToken(ctx sdk.Context, uToken sdk.Coin) (sdk.Coin, erro
return sdk.NewCoin(tokenDenom, tokenAmount), nil
}

// ExchangeUTokens converts an sdk.Coins containing uTokens to their values in base
// tokens.
func (k Keeper) ExchangeUTokens(ctx sdk.Context, uTokens sdk.Coins) (sdk.Coins, error) {
// ToTokens returns list of Tokens in the amount a user would receive when withdrawing the
// list of uTokens.
// Returns error if any of the inputs are tokens.
func (k Keeper) ToTokens(ctx sdk.Context, uTokens sdk.Coins) (sdk.Coins, error) {
if err := uTokens.Validate(); err != nil {
return sdk.Coins{}, err
}

tokens := sdk.Coins{}
for _, coin := range uTokens {
token, err := k.ExchangeUToken(ctx, coin)
token, err := k.ToToken(ctx, coin)
if err != nil {
return sdk.Coins{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion x/leverage/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ func (q Querier) MaxWithdraw(
// On non-nil error here, max withdraw is zero.
uToken, _, err := q.Keeper.userMaxWithdraw(ctx, addr, denom)
if err == nil && uToken.IsPositive() {
token, err := q.Keeper.ExchangeUToken(ctx, uToken)
token, err := q.Keeper.ToToken(ctx, uToken)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion x/leverage/keeper/incentive.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
// DonateCollateral burns some collateral uTokens already present in the module, then adds their equivalent amount
// in tokens reserves. Currently, this is only used as the penalty for incentive module's MsgEmergencyUnbond.
func (k Keeper) DonateCollateral(ctx sdk.Context, fromAddr sdk.AccAddress, uToken sdk.Coin) error {
token, err := k.ExchangeUToken(ctx, uToken)
token, err := k.ToToken(ctx, uToken)
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions x/leverage/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (k Keeper) Supply(ctx sdk.Context, supplierAddr sdk.AccAddress, coin sdk.Co
}

// determine uToken amount to mint
uToken, err := k.ExchangeToken(ctx, coin)
uToken, err := k.ToUToken(ctx, coin)
if err != nil {
return sdk.Coin{}, err
}
Expand Down Expand Up @@ -129,7 +129,7 @@ func (k Keeper) SupplyFromModule(ctx sdk.Context, fromModule string, coin sdk.Co
}

// determine uToken amount to mint
uToken, err := k.ExchangeToken(ctx, coin)
uToken, err := k.ToUToken(ctx, coin)
if err != nil {
return sdk.Coin{}, true, err
}
Expand Down Expand Up @@ -176,7 +176,7 @@ func (k Keeper) Withdraw(ctx sdk.Context, supplierAddr sdk.AccAddress, uToken sd
}

// calculate base asset amount to withdraw
token, err := k.ExchangeUToken(ctx, uToken)
token, err := k.ToToken(ctx, uToken)
if err != nil {
return sdk.Coin{}, isFromCollateral, err
}
Expand Down Expand Up @@ -256,7 +256,7 @@ func (k Keeper) WithdrawToModule(ctx sdk.Context, toModule string, uToken sdk.Co
}

// calculate base asset amount to withdraw
token, err := k.ExchangeUToken(ctx, uToken)
token, err := k.ToToken(ctx, uToken)
if err != nil {
return sdk.Coin{}, true, err
}
Expand Down
4 changes: 2 additions & 2 deletions x/leverage/keeper/limits.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
func (k *Keeper) userMaxWithdraw(ctx sdk.Context, addr sdk.AccAddress, denom string) (sdk.Coin, sdk.Coin, error) {
uDenom := types.ToUTokenDenom(denom)
availableTokens := sdk.NewCoin(denom, k.AvailableLiquidity(ctx, denom))
availableUTokens, err := k.ExchangeToken(ctx, availableTokens)
availableUTokens, err := k.ToUToken(ctx, availableTokens)
if err != nil {
return sdk.Coin{}, sdk.Coin{}, err
}
Expand Down Expand Up @@ -282,7 +282,7 @@ func (k Keeper) ModuleAvailableLiquidity(ctx sdk.Context, denom string) (sdkmath

// Get module collateral for the associated uToken
totalCollateral := k.GetTotalCollateral(ctx, types.ToUTokenDenom(denom))
totalTokenCollateral, err := k.ExchangeUTokens(ctx, sdk.NewCoins(totalCollateral))
totalTokenCollateral, err := k.ToTokens(ctx, sdk.NewCoins(totalCollateral))
if err != nil {
return sdkmath.Int{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion x/leverage/keeper/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (k Keeper) VisibleUTokensValue(ctx sdk.Context, uTokens sdk.Coins, mode typ
tokens := sdk.NewCoins()

for _, u := range uTokens {
t, err := k.ExchangeUToken(ctx, u)
t, err := k.ToToken(ctx, u)
if err != nil {
return sdk.ZeroDec(), err
}
Expand Down
6 changes: 3 additions & 3 deletions x/leverage/keeper/supply.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (k Keeper) GetSupplied(ctx sdk.Context, supplierAddr sdk.AccAddress, denom
collateral := k.GetCollateral(ctx, supplierAddr, uDenom)

// convert uTokens to tokens
return k.ExchangeUToken(ctx, balance.Add(collateral))
return k.ToToken(ctx, balance.Add(collateral))
}

// GetAllSupplied returns the total tokens supplied by a user, including
Expand All @@ -38,7 +38,7 @@ func (k Keeper) GetAllSupplied(ctx sdk.Context, supplierAddr sdk.AccAddress) (sd
}

// convert the sum of found uTokens to base tokens
return k.ExchangeUTokens(ctx, collateral.Add(uTokens...))
return k.ToTokens(ctx, collateral.Add(uTokens...))
}

// GetTotalSupply returns the total supplied by all suppliers in a given denom,
Expand All @@ -50,7 +50,7 @@ func (k Keeper) GetTotalSupply(ctx sdk.Context, denom string) (sdk.Coin, error)

// convert associated uToken's total supply to base tokens
uTokenDenom := types.ToUTokenDenom(denom)
return k.ExchangeUToken(ctx, k.GetUTokenSupply(ctx, uTokenDenom))
return k.ToToken(ctx, k.GetUTokenSupply(ctx, uTokenDenom))
}

// checkMaxSupply returns the appropriate error if a token denom's
Expand Down
4 changes: 2 additions & 2 deletions x/metoken/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ type BankKeeper interface {
// LeverageKeeper interface for interacting with x/leverage
type LeverageKeeper interface {
GetTokenSettings(ctx sdk.Context, denom string) (ltypes.Token, error)
ExchangeToken(ctx sdk.Context, token sdk.Coin) (sdk.Coin, error)
ExchangeUToken(ctx sdk.Context, uToken sdk.Coin) (sdk.Coin, error)
ToUToken(ctx sdk.Context, token sdk.Coin) (sdk.Coin, error)
ToToken(ctx sdk.Context, uToken sdk.Coin) (sdk.Coin, error)
SupplyFromModule(ctx sdk.Context, fromModule string, coin sdk.Coin) (sdk.Coin, bool, error)
WithdrawToModule(ctx sdk.Context, toModule string, uToken sdk.Coin) (sdk.Coin, bool, error)
ModuleMaxWithdraw(ctx sdk.Context, spendableUTokens sdk.Coin) (sdkmath.Int, error)
Expand Down
2 changes: 1 addition & 1 deletion x/metoken/keeper/intest/interest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestInterestClaiming(t *testing.T) {
)
require.NoError(t, err)

uTokens, err := app.LeverageKeeper.ExchangeToken(ctx, borrowerSupply)
uTokens, err := app.LeverageKeeper.ToUToken(ctx, borrowerSupply)
require.NoError(t, err)

err = app.LeverageKeeper.Collateralize(ctx, borrower, uTokens)
Expand Down
12 changes: 10 additions & 2 deletions x/metoken/keeper/intest/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"
"time"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"

sdkmath "cosmossdk.io/math"
Expand Down Expand Up @@ -42,8 +43,15 @@ func initTestSuite(t *testing.T, registry []metoken.Index, balances []metoken.In
},
).WithBlockTime(time.Now())

oracleMock := mocks.NewMockOracleKeeper()
oracleMock.AllMedianPricesFunc.SetDefaultHook(mocks.ValidPricesFunc())
ctrl := gomock.NewController(t)
defer ctrl.Finish()

oracleMock := mocks.NewMockOracleKeeper(ctrl)
oracleMock.
EXPECT().
AllMedianPrices(gomock.Any()).
Return(mocks.ValidPrices()).
AnyTimes()

kb := keeper.NewKeeperBuilder(
app.AppCodec(),
Expand Down
Loading