-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: simulate swap as part of quotes
- Loading branch information
Showing
8 changed files
with
250 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package domain | ||
|
||
import ( | ||
"context" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/osmosis-labs/osmosis/osmomath" | ||
) | ||
|
||
// QuoteSimulator simulates a quote and returns the gas adjusted amount and the fee coin. | ||
type QuoteSimulator interface { | ||
// SimulateQuote simulates a quote and returns the gas adjusted amount and the fee coin. | ||
// CONTRACT: | ||
// - Only direct (non-split) quotes are supported. | ||
// Retursn error if: | ||
// - Simulator address does not have enough funds to pay for the quote. | ||
SimulateQuote(ctx context.Context, quote Quote, slippageToleranceMultiplier osmomath.Dec, simulatorAddress string) (uint64, sdk.Coin, error) | ||
} | ||
|
||
type QuotePriceInfo struct { | ||
AdjustedGasUsed uint64 `json:"adjusted_gas_used"` | ||
FeeCoin sdk.Coin `json:"fee_coin"` | ||
} |
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,77 @@ | ||
package quotesimulator | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/osmosis-labs/osmosis/osmomath" | ||
"github.com/osmosis-labs/osmosis/v26/app/params" | ||
txfeestypes "github.com/osmosis-labs/osmosis/v26/x/txfees/types" | ||
"github.com/osmosis-labs/sqs/domain" | ||
"github.com/osmosis-labs/sqs/domain/cosmos/auth/types" | ||
"github.com/osmosis-labs/sqs/domain/cosmos/tx" | ||
|
||
poolmanagertypes "github.com/osmosis-labs/osmosis/v26/x/poolmanager/types" | ||
) | ||
|
||
type quoteSimulator struct { | ||
gasCalculator tx.MsgSimulator | ||
encodingConfig params.EncodingConfig | ||
txFeesClient txfeestypes.QueryClient | ||
accountQueryClient types.QueryClient | ||
chainID string | ||
} | ||
|
||
func NewQuoteSimulator(gasCalculator tx.MsgSimulator, bech32Address string, encodingConfig params.EncodingConfig, txFeesClient txfeestypes.QueryClient, accountQueryClient types.QueryClient, chainID string) *quoteSimulator { | ||
return "eSimulator{ | ||
gasCalculator: gasCalculator, | ||
encodingConfig: encodingConfig, | ||
txFeesClient: txFeesClient, | ||
accountQueryClient: accountQueryClient, | ||
chainID: chainID, | ||
} | ||
} | ||
|
||
func (q *quoteSimulator) SimulateQuote(ctx context.Context, quote domain.Quote, slippageToleranceMultiplier osmomath.Dec, simulatorAddress string) (uint64, sdk.Coin, error) { | ||
route := quote.GetRoute() | ||
|
||
if len(route) != 1 { | ||
return 0, sdk.Coin{}, fmt.Errorf("route length must be 1, got %d", len(route)) | ||
} | ||
|
||
poolsInRoute := route[0].GetPools() | ||
|
||
poolManagerRoute := make([]poolmanagertypes.SwapAmountInRoute, len(poolsInRoute)) | ||
for i, r := range poolsInRoute { | ||
poolManagerRoute[i] = poolmanagertypes.SwapAmountInRoute{ | ||
PoolId: r.GetId(), | ||
TokenOutDenom: r.GetTokenOutDenom(), | ||
} | ||
} | ||
|
||
tokenIn := quote.GetAmountIn() | ||
|
||
slippageBound := tokenIn.Amount.ToLegacyDec().Mul(slippageToleranceMultiplier).TruncateInt() | ||
|
||
swapMsg := &poolmanagertypes.MsgSwapExactAmountIn{ | ||
Sender: simulatorAddress, | ||
Routes: poolManagerRoute, | ||
TokenIn: tokenIn, | ||
TokenOutMinAmount: slippageBound, | ||
} | ||
|
||
baseAccount, err := q.accountQueryClient.GetAccount(ctx, simulatorAddress) | ||
if err != nil { | ||
return 0, sdk.Coin{}, err | ||
} | ||
|
||
gasAdjusted, feeCoin, err := q.gasCalculator.PriceMsgs(ctx, q.txFeesClient, q.encodingConfig.TxConfig, baseAccount, q.chainID, swapMsg) | ||
if err != nil { | ||
return 0, sdk.Coin{}, err | ||
} | ||
|
||
return gasAdjusted, feeCoin, nil | ||
} | ||
|
||
var _ domain.QuoteSimulator = "eSimulator{} |
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