diff --git a/x/metoken/expected_keepers.go b/x/metoken/expected_keepers.go index 0fbe27a669..b8104d6e70 100644 --- a/x/metoken/expected_keepers.go +++ b/x/metoken/expected_keepers.go @@ -17,6 +17,9 @@ type BankKeeper interface { SendCoinsFromAccountToModule( ctx sdk.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins, ) error + SendCoinsFromModuleToModule( + ctx sdk.Context, senderModule, recipientModule string, amt sdk.Coins, + ) error } // LeverageKeeper interface for interacting with x/leverage diff --git a/x/metoken/genesis.go b/x/metoken/genesis.go index d08de69792..8801884821 100644 --- a/x/metoken/genesis.go +++ b/x/metoken/genesis.go @@ -105,13 +105,11 @@ func (ib IndexBalances) AssetBalance(denom string) (AssetBalance, int) { // SetAssetBalance overrides an asset balance if exists in the list, otherwise add it to the list. func (ib *IndexBalances) SetAssetBalance(balance AssetBalance) { _, i := ib.AssetBalance(balance.Denom) - if i < 0 { ib.AssetBalances = append(ib.AssetBalances, balance) - return + } else { + ib.AssetBalances[i] = balance } - - ib.AssetBalances[i] = balance } // NewZeroAssetBalance creates a new AssetBalance object with all balances in zero. diff --git a/x/metoken/keeper/fee.go b/x/metoken/keeper/fee.go index e043208caf..e3f7c54b88 100644 --- a/x/metoken/keeper/fee.go +++ b/x/metoken/keeper/fee.go @@ -3,6 +3,7 @@ package keeper import ( sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + "github.com/umee-network/umee/v6/x/metoken" ) diff --git a/x/metoken/keeper/swap.go b/x/metoken/keeper/swap.go index 74d9eee5d9..94bdc8fac7 100644 --- a/x/metoken/keeper/swap.go +++ b/x/metoken/keeper/swap.go @@ -7,6 +7,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + "github.com/umee-network/umee/v6/x/auction" "github.com/umee-network/umee/v6/x/metoken" "github.com/umee-network/umee/v6/x/metoken/errors" ) @@ -19,7 +20,7 @@ type swapResponse struct { leveraged sdk.Coin } -func newSwapResponse(meTokens sdk.Coin, fee sdk.Coin, reserved sdk.Coin, leveraged sdk.Coin) swapResponse { +func newSwapResponse(meTokens, fee, reserved, leveraged sdk.Coin) swapResponse { return swapResponse{ meTokens: meTokens, fee: fee, @@ -102,6 +103,14 @@ func (k Keeper) swap(userAddr sdk.AccAddress, meTokenDenom string, asset sdk.Coi return swapResponse{}, err } + feeToAuction, feeToRevenue := k.breakFee(swapCarry.fee) + feeCoins := sdk.Coins{sdk.NewCoin(asset.Denom, feeToAuction)} + err = k.bankKeeper.SendCoinsFromModuleToModule(*k.ctx, metoken.ModuleName, auction.ModuleName, feeCoins) + if err != nil { + return swapResponse{}, err + } + // TODO: emit event + balances.MetokenSupply.Amount = balances.MetokenSupply.Amount.Add(swapCarry.meTokens) balance, i := balances.AssetBalance(asset.Denom) if i < 0 { @@ -111,7 +120,7 @@ func (k Keeper) swap(userAddr sdk.AccAddress, meTokenDenom string, asset sdk.Coi balance.Reserved = balance.Reserved.Add(swapCarry.toReserves) balance.Leveraged = balance.Leveraged.Add(swapCarry.toLeverage) - balance.Fees = balance.Fees.Add(swapCarry.fee) + balance.Fees = balance.Fees.Add(feeToRevenue) balances.SetAssetBalance(balance) if err = k.setIndexBalances(balances); err != nil { @@ -121,7 +130,7 @@ func (k Keeper) swap(userAddr sdk.AccAddress, meTokenDenom string, asset sdk.Coi return newSwapResponse( meTokens[0], sdk.NewCoin(asset.Denom, swapCarry.fee), - sdk.NewCoin(asset.Denom, swapCarry.toReserves), + sdk.NewCoin(asset.Denom, feeToRevenue), sdk.NewCoin(asset.Denom, swapCarry.toLeverage), ), nil } @@ -176,6 +185,13 @@ func (k Keeper) availableToSupply(denom string) (bool, sdkmath.Int, error) { return true, token.MaxSupply.Sub(total.Amount), nil } +// breakFee calculates the protocol fee for the burn auction and the reminder +func (k Keeper) breakFee(fee sdkmath.Int) (toAuction sdkmath.Int, revenue sdkmath.Int) { + p := k.GetParams() + toAuction = p.RewardsAuctionFactor.Mul(fee) + return toAuction, fee.Sub(toAuction) +} + // calculateSwap returns the amount of meToken to send to the user, the fee to be charged to him, // the amount of assets to send to x/metoken reserves and to x/leverage pools. // The formulas used for the calculations are: @@ -185,8 +201,7 @@ func (k Keeper) availableToSupply(denom string) (bool, sdkmath.Int, error) { // amount_to_reserves = assets_to_swap * reserve_portion // amount_to_leverage = assets_to_swap - amount_to_reserves // -// It returns meTokens to be minted, fee to be charged, -// amount to transfer to x/metoken reserves and x/leverage liquidity pools +// It returns calculated amount of tokens that need to be transferred as a result of a transfer. func (k Keeper) calculateSwap(index metoken.Index, indexPrices metoken.IndexPrices, asset sdk.Coin) ( swapCarry, error, ) {