Skip to content

Commit

Permalink
fix masterchef swap method
Browse files Browse the repository at this point in the history
  • Loading branch information
amityadav0 committed Dec 3, 2024
1 parent 1889864 commit 2969989
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
3 changes: 2 additions & 1 deletion x/amm/types/params_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package types_test

import (
"testing"

"cosmossdk.io/math"
"github.com/elys-network/elys/x/amm/types"
"github.com/stretchr/testify/require"
"testing"
)

func TestDefaultParams(t *testing.T) {
Expand Down
17 changes: 1 addition & 16 deletions x/masterchef/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,10 +332,7 @@ func (k Keeper) ConvertGasFeesToUsdc(ctx sdk.Context, baseCurrency string, addre
continue
}

// Executes the swap in the pool and stores the output. Updates pool assets but
// does not actually transfer any tokens to or from the pool.
snapshot := k.amm.GetAccountedPoolSnapshotOrSet(ctx, pool)
tokenOutCoin, _, _, _, err := k.amm.SwapOutAmtGivenIn(ctx, pool.PoolId, k.oracleKeeper, &snapshot, sdk.Coins{tokenIn}, baseCurrency, math.LegacyZeroDec(), math.LegacyOneDec())
tokenOutAmount, err := k.amm.InternalSwapExactAmountIn(ctx, address, address, pool, tokenIn, baseCurrency, math.ZeroInt(), math.LegacyZeroDec())
if err != nil {
// Continue as we can swap it when this amount is higher
if err == ammtypes.ErrTokenOutAmountZero {
Expand All @@ -352,18 +349,6 @@ func (k Keeper) ConvertGasFeesToUsdc(ctx sdk.Context, baseCurrency string, addre
return sdk.Coins{}, err
}

tokenOutAmount := tokenOutCoin.Amount
if !tokenOutAmount.IsPositive() {
continue
}

// Settles balances between the tx sender and the pool to match the swap that was executed earlier.
// Also emits a swap event and updates related liquidity metrics.
_, err = k.amm.UpdatePoolForSwap(ctx, pool, address, address, tokenIn, tokenOutCoin, math.LegacyZeroDec(), math.LegacyZeroDec(), math.LegacyZeroDec())
if err != nil {
return sdk.Coins{}, err
}

// Swapped USDC coin
swappedCoins := sdk.NewCoins(sdk.NewCoin(baseCurrency, tokenOutAmount))

Expand Down
10 changes: 10 additions & 0 deletions x/masterchef/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ type AmmKeeper interface {
CalcOutAmtGivenIn(ctx sdk.Context, poolId uint64, oracle ammtypes.OracleKeeper, snapshot *ammtypes.Pool, tokensIn sdk.Coins, tokenOutDenom string, swapFee math.LegacyDec) (sdk.Coin, math.LegacyDec, error)
GetEdenDenomPrice(ctx sdk.Context, baseCurrency string) math.LegacyDec
GetTokenPrice(ctx sdk.Context, tokenInDenom, baseCurrency string) math.LegacyDec
InternalSwapExactAmountIn(
ctx sdk.Context,
sender sdk.AccAddress,
recipient sdk.AccAddress,
pool ammtypes.Pool,
tokenIn sdk.Coin,
tokenOutDenom string,
tokenOutMinAmount math.Int,
swapFee math.LegacyDec,
) (tokenOutAmount math.Int, err error)
}

// OracleKeeper defines the expected interface needed to retrieve price info
Expand Down

0 comments on commit 2969989

Please sign in to comment.