@@ -27,6 +27,7 @@ import {
2727 CosmosTxData ,
2828 EVMTxData ,
2929 SwapProvider ,
30+ ObservableQueryRouteInnerV2 ,
3031} from "@keplr-wallet/stores-internal" ;
3132import {
3233 EthereumAccountStore ,
@@ -109,11 +110,7 @@ export class SwapAmountConfig extends AmountConfig {
109110 if ( this . fraction > 0 ) {
110111 let result = this . maxAmount ;
111112
112- const slippageTolerancePercent = this . _getSlippageTolerancePercent ( ) ;
113-
114- const queryRoute = this . getQuerySwapHelper ( result ) ?. getRoute (
115- slippageTolerancePercent
116- ) ;
113+ const queryRoute = this . getQueryRoute ( result ) ;
117114 if ( queryRoute ?. response != null ) {
118115 const bridgeFee = queryRoute . bridgeFees . reduce (
119116 ( acc : CoinPretty , fee : CoinPretty ) => {
@@ -154,14 +151,9 @@ export class SwapAmountConfig extends AmountConfig {
154151 }
155152
156153 get outAmount ( ) : CoinPretty {
157- const querySwapHelper = this . getQuerySwapHelper ( ) ;
158- if ( ! querySwapHelper ) {
159- return new CoinPretty ( this . outCurrency , "0" ) ;
160- }
161-
162- const slippageTolerancePercent = this . _getSlippageTolerancePercent ( ) ;
163-
164- return querySwapHelper . getRoute ( slippageTolerancePercent ) . outAmount ;
154+ return (
155+ this . getQueryRoute ( ) ?. outAmount ?? new CoinPretty ( this . outCurrency , "0" )
156+ ) ;
165157 }
166158
167159 get outChainId ( ) : string {
@@ -181,25 +173,11 @@ export class SwapAmountConfig extends AmountConfig {
181173 }
182174
183175 get swapPriceImpact ( ) : RatePretty | undefined {
184- const querySwapHelper = this . getQuerySwapHelper ( ) ;
185- if ( ! querySwapHelper ) {
186- return undefined ;
187- }
188-
189- const slippageTolerancePercent = this . _getSlippageTolerancePercent ( ) ;
190-
191- return querySwapHelper . getRoute ( slippageTolerancePercent ) . swapPriceImpact ;
176+ return this . getQueryRoute ( ) ?. swapPriceImpact ;
192177 }
193178
194179 get provider ( ) : SwapProvider | undefined {
195- const querySwapHelper = this . getQuerySwapHelper ( ) ;
196- if ( ! querySwapHelper ) {
197- return undefined ;
198- }
199-
200- const slippageTolerancePercent = this . _getSlippageTolerancePercent ( ) ;
201-
202- return querySwapHelper . getRoute ( slippageTolerancePercent ) . provider ;
180+ return this . getQueryRoute ( ) ?. provider ;
203181 }
204182
205183 @action
@@ -218,35 +196,25 @@ export class SwapAmountConfig extends AmountConfig {
218196 }
219197
220198 get otherFees ( ) : CoinPretty [ ] {
221- const querySwapHelper = this . getQuerySwapHelper ( ) ;
222- if ( ! querySwapHelper ) {
223- return [ new CoinPretty ( this . outCurrency , "0" ) ] ;
224- }
225-
226- const slippageTolerancePercent = this . _getSlippageTolerancePercent ( ) ;
227-
228- return querySwapHelper . getRoute ( slippageTolerancePercent ) . bridgeFees ;
199+ return (
200+ this . getQueryRoute ( ) ?. bridgeFees ?? [
201+ new CoinPretty ( this . outCurrency , "0" ) ,
202+ ]
203+ ) ;
229204 }
230205
231206 async fetch ( ) : Promise < void > {
232- const querySwapHelper = this . getQuerySwapHelper ( ) ;
233- if ( querySwapHelper ) {
234- const slippageTolerancePercent = this . _getSlippageTolerancePercent ( ) ;
235-
236- await querySwapHelper . getRoute ( slippageTolerancePercent ) . fetch ( ) ;
207+ const queryRoute = this . getQueryRoute ( ) ;
208+ if ( queryRoute ) {
209+ await queryRoute . fetch ( ) ;
237210 }
238211 }
239212
240213 get isFetchingInAmount ( ) : boolean {
241214 if ( this . fraction === 1 ) {
242- const slippageTolerancePercent = this . _getSlippageTolerancePercent ( ) ;
243-
244215 return (
245- this . getQuerySwapHelper ( this . maxAmount ) ?. getRoute (
246- slippageTolerancePercent
247- ) . isFetching ??
248- this . getQuerySwapHelper ( ) ?. getRoute ( slippageTolerancePercent )
249- . isFetching ??
216+ this . getQueryRoute ( this . maxAmount ) ?. isFetching ??
217+ this . getQueryRoute ( ) ?. isFetching ??
250218 false
251219 ) ;
252220 }
@@ -255,27 +223,19 @@ export class SwapAmountConfig extends AmountConfig {
255223 }
256224
257225 get isFetchingOutAmount ( ) : boolean {
258- const slippageTolerancePercent = this . _getSlippageTolerancePercent ( ) ;
259-
260- return (
261- this . getQuerySwapHelper ( ) ?. getRoute ( slippageTolerancePercent )
262- . isFetching ?? false
263- ) ;
226+ return this . getQueryRoute ( ) ?. isFetching ?? false ;
264227 }
265228
266229 get type ( ) : "swap" | "transfer" | "not-ready" {
267- const querySwapHelper = this . getQuerySwapHelper ( ) ;
268- if ( ! querySwapHelper ) {
230+ const queryRoute = this . getQueryRoute ( ) ;
231+ if ( ! queryRoute ) {
269232 return "not-ready" ;
270233 }
271234
272- const slippageTolerancePercent = this . _getSlippageTolerancePercent ( ) ;
273-
274- const res = querySwapHelper . getRoute ( slippageTolerancePercent ) . response ;
235+ const res = queryRoute . response ;
275236 if ( ! res ) {
276237 return "not-ready" ;
277238 }
278-
279239 const containsSwap = res . data . steps . some (
280240 ( step ) => step . type === RouteStepType . SWAP
281241 ) ;
@@ -933,6 +893,32 @@ export class SwapAmountConfig extends AmountConfig {
933893 toAddress
934894 ) ;
935895 }
896+
897+ getQueryRoute ( amount ?: CoinPretty ) : ObservableQueryRouteInnerV2 | undefined {
898+ if ( ! amount && this . amount . length === 0 ) {
899+ return ;
900+ }
901+
902+ const amountIn = amount ?? this . amount [ 0 ] ;
903+ const fromAddress = this . getAddressSync ( this . chainId ) ;
904+ const toAddress = this . getAddressSync ( this . outChainId ) ;
905+ if ( ! fromAddress || ! toAddress ) {
906+ return ;
907+ }
908+ const slippageTolerancePercent = this . _getSlippageTolerancePercent ( ) ;
909+
910+ return this . swapQueries . querySwapHelper
911+ . getSwapHelper (
912+ this . chainId ,
913+ amountIn . currency . coinMinimalDenom ,
914+ amountIn . toCoin ( ) . amount ,
915+ this . outChainId ,
916+ this . outCurrency . coinMinimalDenom ,
917+ fromAddress ,
918+ toAddress
919+ )
920+ . getRoute ( slippageTolerancePercent ) ;
921+ }
936922}
937923
938924export const useSwapAmountConfig = (
0 commit comments