-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
85c850a
commit 3e7d3f2
Showing
30 changed files
with
1,625 additions
and
177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package cli | ||
|
||
import ( | ||
"strconv" | ||
|
||
"github.com/cosmos/cosmos-sdk/client" | ||
"github.com/cosmos/cosmos-sdk/client/flags" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/elys-network/elys/x/amm/types" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var _ = strconv.Itoa(0) | ||
|
||
func CmdSwapEstimationByDenom() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "swap-estimation-by-denom [amount] [denom-in] [denom-out]", | ||
Short: "Query swap-estimation-by-denom", | ||
Example: "elysd q amm swap-estimation-by-denom 100uatom uatom uosmo", | ||
Args: cobra.ExactArgs(3), | ||
RunE: func(cmd *cobra.Command, args []string) (err error) { | ||
reqAmount, err := sdk.ParseCoinNormalized(args[0]) | ||
if err != nil { | ||
return err | ||
} | ||
reqDenomIn := args[1] | ||
reqDenomOut := args[2] | ||
|
||
clientCtx, err := client.GetClientQueryContext(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
queryClient := types.NewQueryClient(clientCtx) | ||
|
||
params := &types.QuerySwapEstimationByDenomRequest{ | ||
|
||
Amount: reqAmount, | ||
DenomIn: reqDenomIn, | ||
DenomOut: reqDenomOut, | ||
} | ||
|
||
res, err := queryClient.SwapEstimationByDenom(cmd.Context(), params) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return clientCtx.PrintProto(res) | ||
}, | ||
} | ||
|
||
flags.AddQueryFlagsToCmd(cmd) | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package wasm | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
errorsmod "cosmossdk.io/errors" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
ammtypes "github.com/elys-network/elys/x/amm/types" | ||
) | ||
|
||
func (oq *Querier) querySwapEstimationByDenom(ctx sdk.Context, query *ammtypes.QuerySwapEstimationByDenomRequest) ([]byte, error) { | ||
res, err := oq.keeper.SwapEstimationByDenom(sdk.WrapSDKContext(ctx), query) | ||
if err != nil { | ||
return nil, errorsmod.Wrap(err, "failed to get swap estimation by denom") | ||
} | ||
|
||
responseBytes, err := json.Marshal(res) | ||
if err != nil { | ||
return nil, errorsmod.Wrap(err, "failed to serialize swap estimation by denom response") | ||
} | ||
return responseBytes, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package keeper | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/elys-network/elys/x/amm/types" | ||
) | ||
|
||
// CalcInRouteSpotPrice calculates the spot price of the given token and in route | ||
func (k Keeper) CalcInRouteSpotPrice(ctx sdk.Context, tokenIn sdk.Coin, routes []*types.SwapAmountInRoute) (sdk.Dec, sdk.Coin, error) { | ||
if routes == nil || len(routes) == 0 { | ||
return sdk.ZeroDec(), sdk.Coin{}, types.ErrEmptyRoutes | ||
} | ||
|
||
// Start with the initial token input | ||
tokensIn := sdk.Coins{tokenIn} | ||
|
||
// The final token out denom | ||
var tokenOutDenom string | ||
|
||
for _, route := range routes { | ||
poolId := route.PoolId | ||
tokenOutDenom = route.TokenOutDenom | ||
|
||
pool, found := k.GetPool(ctx, poolId) | ||
if !found { | ||
return sdk.ZeroDec(), sdk.Coin{}, types.ErrPoolNotFound | ||
} | ||
|
||
// Estimate swap | ||
snapshot := k.GetPoolSnapshotOrSet(ctx, pool) | ||
swapResult, err := k.CalcOutAmtGivenIn(ctx, pool.PoolId, k.oracleKeeper, &snapshot, tokensIn, tokenOutDenom, sdk.ZeroDec()) | ||
|
||
if err != nil { | ||
return sdk.ZeroDec(), sdk.Coin{}, err | ||
} | ||
|
||
if swapResult.IsZero() { | ||
return sdk.ZeroDec(), sdk.Coin{}, types.ErrAmountTooLow | ||
} | ||
|
||
// Use the current swap result as the input for the next iteration | ||
tokensIn = sdk.Coins{swapResult} | ||
} | ||
|
||
// Calculate the spot price given the initial token in and the final token out | ||
spotPrice := sdk.NewDecFromInt(tokensIn[0].Amount).Quo(sdk.NewDecFromInt(tokenIn.Amount)) | ||
|
||
// Calculate the token out amount | ||
tokenOutAmt := sdk.NewDecFromInt(tokenIn.Amount).Mul(spotPrice) | ||
|
||
// Construct the token out coin | ||
tokenOut := sdk.NewCoin(tokenOutDenom, tokenOutAmt.TruncateInt()) | ||
|
||
return spotPrice, tokenOut, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"testing" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
keepertest "github.com/elys-network/elys/testutil/keeper" | ||
"github.com/elys-network/elys/x/amm/types" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCalcInRouteSpotPrice(t *testing.T) { | ||
k, ctx, accountedPoolKeeper, _ := keepertest.AmmKeeper(t) | ||
SetupMockPools(k, ctx) | ||
|
||
// Use token in for all tests | ||
tokenIn := sdk.NewCoin("denom1", sdk.NewInt(100)) | ||
|
||
// Test single route | ||
accountedPoolKeeper.On("GetAccountedBalance", ctx, uint64(1), "denom1").Return(sdk.NewInt(1000)) | ||
accountedPoolKeeper.On("GetAccountedBalance", ctx, uint64(1), "denom2").Return(sdk.NewInt(1000)) | ||
routes := []*types.SwapAmountInRoute{{PoolId: 1, TokenOutDenom: "denom2"}} | ||
spotPrice, _, err := k.CalcInRouteSpotPrice(ctx, tokenIn, routes) | ||
require.NoError(t, err) | ||
require.NotZero(t, spotPrice) | ||
accountedPoolKeeper.AssertExpectations(t) | ||
|
||
// Test multiple routes | ||
accountedPoolKeeper.On("GetAccountedBalance", ctx, uint64(2), "denom1").Return(sdk.NewInt(1000)) | ||
accountedPoolKeeper.On("GetAccountedBalance", ctx, uint64(2), "baseCurrency").Return(sdk.NewInt(1000)) | ||
accountedPoolKeeper.On("GetAccountedBalance", ctx, uint64(3), "baseCurrency").Return(sdk.NewInt(1000)) | ||
accountedPoolKeeper.On("GetAccountedBalance", ctx, uint64(3), "denom3").Return(sdk.NewInt(1000)) | ||
routes = []*types.SwapAmountInRoute{ | ||
{PoolId: 2, TokenOutDenom: "baseCurrency"}, | ||
{PoolId: 3, TokenOutDenom: "denom3"}, | ||
} | ||
spotPrice, _, err = k.CalcInRouteSpotPrice(ctx, tokenIn, routes) | ||
require.NoError(t, err) | ||
require.NotZero(t, spotPrice) | ||
accountedPoolKeeper.AssertExpectations(t) | ||
|
||
// Test no routes | ||
_, _, err = k.CalcInRouteSpotPrice(ctx, tokenIn, nil) | ||
require.Error(t, err) | ||
|
||
// Test invalid pool | ||
routes = []*types.SwapAmountInRoute{{PoolId: 9999, TokenOutDenom: "denom2"}} | ||
_, _, err = k.CalcInRouteSpotPrice(ctx, tokenIn, routes) | ||
require.Error(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.