-
Notifications
You must be signed in to change notification settings - Fork 52
/
create_multihop_expected_swap_outs.go
40 lines (34 loc) · 1.35 KB
/
create_multihop_expected_swap_outs.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
31
32
33
34
35
36
37
38
39
40
package keeper
import (
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/elys-network/elys/x/amm/types"
)
// createMultihopExpectedSwapOuts defines the output denom and output amount for the last pool in
// the route of pools the caller is intending to hop through in a fixed-output multihop tx. It estimates the input
// amount for this last pool and then chains that input as the output of the previous pool in the route, repeating
// until the first pool is reached. It returns an array of inputs, each of which corresponds to a pool ID in the
// route of pools for the original multihop transaction.
// TODO: test this.
func (k Keeper) createMultihopExpectedSwapOuts(
ctx sdk.Context,
routes []types.SwapAmountOutRoute,
tokenOut sdk.Coin,
) ([]math.Int, error) {
insExpected := make([]math.Int, len(routes))
for i := len(routes) - 1; i >= 0; i-- {
route := routes[i]
pool, poolExists := k.GetPool(ctx, route.PoolId)
if !poolExists {
return nil, types.ErrInvalidPoolId
}
snapshot := k.GetPoolSnapshotOrSet(ctx, pool)
tokenIn, _, err := pool.CalcInAmtGivenOut(ctx, k.oracleKeeper, &snapshot, sdk.NewCoins(tokenOut), route.TokenInDenom, pool.GetPoolParams().SwapFee, k.accountedPoolKeeper)
if err != nil {
return nil, err
}
insExpected[i] = math.Int(tokenIn.Amount)
tokenOut = tokenIn
}
return insExpected, nil
}