-
Notifications
You must be signed in to change notification settings - Fork 52
/
estimate_swap.go
30 lines (26 loc) · 1000 Bytes
/
estimate_swap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package keeper
import (
errorsmod "cosmossdk.io/errors"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
ammtypes "github.com/elys-network/elys/x/amm/types"
"github.com/elys-network/elys/x/perpetual/types"
)
// Swap estimation using amm CalcOutAmtGivenIn function
func (k Keeper) EstimateSwap(ctx sdk.Context, tokenInAmount sdk.Coin, tokenOutDenom string, ammPool ammtypes.Pool) (math.Int, error) {
perpetualEnabled := k.IsPoolEnabled(ctx, ammPool.PoolId)
if !perpetualEnabled {
return sdk.ZeroInt(), errorsmod.Wrap(types.ErrPerpetualDisabled, "Perpetual disabled pool")
}
// Estimate swap
snapshot := k.amm.GetPoolSnapshotOrSet(ctx, ammPool)
tokensIn := sdk.Coins{tokenInAmount}
swapResult, _, err := k.amm.CalcOutAmtGivenIn(ctx, ammPool.PoolId, k.oracleKeeper, &snapshot, tokensIn, tokenOutDenom, sdk.ZeroDec())
if err != nil {
return sdk.ZeroInt(), err
}
if swapResult.IsZero() {
return sdk.ZeroInt(), types.ErrAmountTooLow
}
return swapResult.Amount, nil
}