-
Notifications
You must be signed in to change notification settings - Fork 41
/
query_join_pool_estimation.go
72 lines (61 loc) · 2.37 KB
/
query_join_pool_estimation.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package keeper
import (
"context"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/elys-network/elys/x/amm/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func (k Keeper) JoinPoolEstimation(goCtx context.Context, req *types.QueryJoinPoolEstimationRequest) (*types.QueryJoinPoolEstimationResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := sdk.UnwrapSDKContext(goCtx)
tokensIn, sharesOut, slippage, weightBalanceBonus, err := k.joinPoolEstimation(ctx, req.PoolId, req.AmountsIn)
if err != nil {
return nil, err
}
shareDenom := types.GetPoolShareDenom(req.PoolId)
return &types.QueryJoinPoolEstimationResponse{
ShareAmountOut: sdk.NewCoin(shareDenom, sharesOut),
AmountsIn: tokensIn,
Slippage: slippage,
WeightBalanceRatio: weightBalanceBonus,
}, nil
}
func (k Keeper) joinPoolEstimation(
ctx sdk.Context,
poolId uint64,
tokenInMaxs sdk.Coins,
) (tokensIn sdk.Coins, sharesOut math.Int, slippage sdk.Dec, weightBalanceBonus sdk.Dec, err error) {
// all pools handled within this method are pointer references, `JoinPool` directly updates the pools
pool, poolExists := k.GetPool(ctx, poolId)
if !poolExists {
return nil, sdk.ZeroInt(), sdk.ZeroDec(), sdk.ZeroDec(), types.ErrInvalidPoolId
}
if !pool.PoolParams.UseOracle {
tokensIn := tokenInMaxs
if len(tokensIn) != 1 {
numShares, tokensIn, err := pool.CalcJoinPoolNoSwapShares(tokenInMaxs)
if err != nil {
return tokensIn, numShares, sdk.ZeroDec(), sdk.ZeroDec(), err
}
}
snapshot := k.GetPoolSnapshotOrSet(ctx, pool)
cacheCtx, _ := ctx.CacheContext()
sharesOut, slippage, weightBalanceBonus, err = pool.JoinPool(cacheCtx, &snapshot, k.oracleKeeper, k.accountedPoolKeeper, tokensIn)
if err != nil {
return nil, sdk.ZeroInt(), sdk.ZeroDec(), sdk.ZeroDec(), err
}
return tokensIn, sharesOut, slippage, weightBalanceBonus, err
}
// on oracle pool, full tokenInMaxs are used regardless shareOutAmount
snapshot := k.GetPoolSnapshotOrSet(ctx, pool)
cacheCtx, _ := ctx.CacheContext()
sharesOut, slippage, weightBalanceBonus, err = pool.JoinPool(cacheCtx, &snapshot, k.oracleKeeper, k.accountedPoolKeeper, tokenInMaxs)
if err != nil {
return nil, sdk.ZeroInt(), sdk.ZeroDec(), sdk.ZeroDec(), err
}
return tokenInMaxs, sharesOut, slippage, weightBalanceBonus, err
}