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 `Kepeer.ExchangeToken -> Token2uTokenRate`, `Kepeer.ExchangeUToken -> UToken2TokenRate` and `Kepeer.ExchangeTokens -> Tokens2uTokensRate`.

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

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)
UToken2TokenRate(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.UToken2TokenRate(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) {
// UToken2TokenRate implements the expected leverage keeper, with uToken exchange rates always equal to 1
func (m *mockLeverageKeeper) UToken2TokenRate(_ 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.UToken2TokenRate(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.UToken2TokenRate(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.UToken2TokenRate(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.UToken2TokenRate(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.UToken2TokenRate(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.Tokens2uTokensRate(ctx, sdk.NewCoins(totalCollateral))
robert-zaremba marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return sdk.ZeroInt(), err
}
Expand Down
19 changes: 9 additions & 10 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) {
// Token2uTokenRate returns a uToken amount user would receive when supplying the token.
// Returns error if the token is not a Token.
func (k Keeper) Token2uTokenRate(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) {
// UToken2TokenRate returns a token amount user would receive when supplying the uToken.
// Returns error if the uToken is not a uToken.
func (k Keeper) UToken2TokenRate(ctx sdk.Context, uToken sdk.Coin) (sdk.Coin, error) {
if err := uToken.Validate(); err != nil {
return sdk.Coin{}, err
}
Expand All @@ -42,16 +41,16 @@ 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
// Tokens2uTokensRate converts an sdk.Coins containing uTokens to their values in base
robert-zaremba marked this conversation as resolved.
Show resolved Hide resolved
// tokens.
func (k Keeper) ExchangeUTokens(ctx sdk.Context, uTokens sdk.Coins) (sdk.Coins, error) {
func (k Keeper) Tokens2uTokensRate(ctx sdk.Context, uTokens sdk.Coins) (sdk.Coins, error) {
robert-zaremba marked this conversation as resolved.
Show resolved Hide resolved
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.UToken2TokenRate(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.UToken2TokenRate(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.UToken2TokenRate(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.Token2uTokenRate(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.Token2uTokenRate(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.UToken2TokenRate(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.UToken2TokenRate(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.Token2uTokenRate(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.Tokens2uTokensRate(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.UToken2TokenRate(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.UToken2TokenRate(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.Tokens2uTokensRate(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.UToken2TokenRate(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)
Token2uTokenRate(ctx sdk.Context, token sdk.Coin) (sdk.Coin, error)
UToken2TokenRate(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.Token2uTokenRate(ctx, borrowerSupply)
require.NoError(t, err)

err = app.LeverageKeeper.Collateralize(ctx, borrower, uTokens)
Expand Down
4 changes: 2 additions & 2 deletions x/metoken/keeper/mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ func (l Leverage) GetTokenSettings(_ sdk.Context, denom string) (ltypes.Token, e
return ts, nil
}

func (l Leverage) ExchangeToken(_ sdk.Context, _ sdk.Coin) (sdk.Coin, error) {
func (l Leverage) Token2uTokenRate(_ sdk.Context, _ sdk.Coin) (sdk.Coin, error) {
panic("not implemented")
}

func (l Leverage) ExchangeUToken(_ sdk.Context, _ sdk.Coin) (sdk.Coin, error) {
func (l Leverage) UToken2TokenRate(_ sdk.Context, _ sdk.Coin) (sdk.Coin, error) {
panic("not implemented")
}

Expand Down
2 changes: 1 addition & 1 deletion x/metoken/keeper/redeem.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (k Keeper) redeem(userAddr sdk.AccAddress, meToken sdk.Coin, assetDenom str
// - tokensWithdrawn: the amount tokens withdrawn from x/leverage.
// - error
func (k Keeper) withdrawFromLeverage(tokensToWithdraw sdk.Coin) (sdk.Coin, error) {
uTokensFromLeverage, err := k.leverageKeeper.ExchangeToken(*k.ctx, tokensToWithdraw)
uTokensFromLeverage, err := k.leverageKeeper.Token2uTokenRate(*k.ctx, tokensToWithdraw)
if err != nil {
return sdk.Coin{}, errors.Wrap(err, true)
}
Expand Down
Loading
Loading