1
1
import { unwrapResponse } from 'clarity-codegen' ;
2
2
import { readonlyCall } from '../utils/readonlyCallExecutor' ;
3
- import { Currency } from '../currency' ;
4
- import { PoolData } from '../types' ;
5
- import { AMMRouteSegment , resolveAmmRoute } from '../utils/ammRouteResolver' ;
3
+ import type { Currency } from '../currency' ;
4
+ import type { PoolData } from '../types' ;
5
+ import { type AMMRouteSegment , resolveAmmRoute } from '../utils/ammRouteResolver' ;
6
+ import { hasLength } from '../utils/arrayHelper' ;
6
7
7
8
export const getYAmountFromXAmount = async (
8
9
tokenX : Currency ,
@@ -16,47 +17,51 @@ export const getYAmountFromXAmount = async (
16
17
if ( ammRoute . length === 0 ) {
17
18
throw new Error ( 'No AMM pool found for the given route' ) ;
18
19
}
19
- if ( ammRoute . length === 1 ) {
20
+ if ( hasLength ( ammRoute , 1 ) ) {
21
+ const [ segment ] = ammRoute ;
20
22
return await readonlyCall ( 'amm-pool-v2-01' , 'get-helper' , {
21
23
'token-x' : getContractId ( tokenX ) ,
22
- 'token-y' : getContractId ( ammRoute [ 0 ] ! . neighbour ) ,
24
+ 'token-y' : getContractId ( segment . neighbour ) ,
23
25
dx : fromAmount ,
24
- factor : ammRoute [ 0 ] ! . pool . factor ,
26
+ factor : segment . pool . factor ,
25
27
} ) . then ( unwrapResponse ) ;
26
28
}
27
- if ( ammRoute . length === 2 ) {
29
+ if ( hasLength ( ammRoute , 2 ) ) {
30
+ const [ segment1 , segment2 ] = ammRoute ;
28
31
return await readonlyCall ( 'amm-pool-v2-01' , 'get-helper-a' , {
29
32
'token-x' : getContractId ( tokenX ) ,
30
- 'token-y' : getContractId ( ammRoute [ 0 ] ! . neighbour ) ,
31
- 'token-z' : getContractId ( ammRoute [ 1 ] ! . neighbour ) ,
32
- 'factor-x' : ammRoute [ 0 ] ! . pool . factor ,
33
- 'factor-y' : ammRoute [ 1 ] ! . pool . factor ,
33
+ 'token-y' : getContractId ( segment1 . neighbour ) ,
34
+ 'token-z' : getContractId ( segment2 . neighbour ) ,
35
+ 'factor-x' : segment1 . pool . factor ,
36
+ 'factor-y' : segment2 . pool . factor ,
34
37
dx : fromAmount ,
35
38
} ) . then ( unwrapResponse ) ;
36
39
}
37
- if ( ammRoute . length === 3 ) {
40
+ if ( hasLength ( ammRoute , 3 ) ) {
41
+ const [ segment1 , segment2 , segment3 ] = ammRoute ;
38
42
return await readonlyCall ( 'amm-pool-v2-01' , 'get-helper-b' , {
39
43
'token-x' : getContractId ( tokenX ) ,
40
- 'token-y' : getContractId ( ammRoute [ 0 ] ! . neighbour ) ,
41
- 'token-z' : getContractId ( ammRoute [ 1 ] ! . neighbour ) ,
42
- 'token-w' : getContractId ( ammRoute [ 2 ] ! . neighbour ) ,
43
- 'factor-x' : ammRoute [ 0 ] ! . pool . factor ,
44
- 'factor-y' : ammRoute [ 1 ] ! . pool . factor ,
45
- 'factor-z' : ammRoute [ 2 ] ! . pool . factor ,
44
+ 'token-y' : getContractId ( segment1 . neighbour ) ,
45
+ 'token-z' : getContractId ( segment2 . neighbour ) ,
46
+ 'token-w' : getContractId ( segment3 . neighbour ) ,
47
+ 'factor-x' : segment1 . pool . factor ,
48
+ 'factor-y' : segment2 . pool . factor ,
49
+ 'factor-z' : segment3 . pool . factor ,
46
50
dx : fromAmount ,
47
51
} ) . then ( unwrapResponse ) ;
48
52
}
49
- if ( ammRoute . length === 4 ) {
53
+ if ( hasLength ( ammRoute , 4 ) ) {
54
+ const [ segment1 , segment2 , segment3 , segment4 ] = ammRoute ;
50
55
return await readonlyCall ( 'amm-pool-v2-01' , 'get-helper-c' , {
51
56
'token-x' : getContractId ( tokenX ) ,
52
- 'token-y' : getContractId ( ammRoute [ 0 ] ! . neighbour ) ,
53
- 'token-z' : getContractId ( ammRoute [ 1 ] ! . neighbour ) ,
54
- 'token-w' : getContractId ( ammRoute [ 2 ] ! . neighbour ) ,
55
- 'token-v' : getContractId ( ammRoute [ 3 ] ! . neighbour ) ,
56
- 'factor-x' : ammRoute [ 0 ] ! . pool . factor ,
57
- 'factor-y' : ammRoute [ 1 ] ! . pool . factor ,
58
- 'factor-z' : ammRoute [ 2 ] ! . pool . factor ,
59
- 'factor-w' : ammRoute [ 3 ] ! . pool . factor ,
57
+ 'token-y' : getContractId ( segment1 . neighbour ) ,
58
+ 'token-z' : getContractId ( segment2 . neighbour ) ,
59
+ 'token-w' : getContractId ( segment3 . neighbour ) ,
60
+ 'token-v' : getContractId ( segment4 . neighbour ) ,
61
+ 'factor-x' : segment1 . pool . factor ,
62
+ 'factor-y' : segment2 . pool . factor ,
63
+ 'factor-z' : segment3 . pool . factor ,
64
+ 'factor-w' : segment4 . pool . factor ,
60
65
dx : fromAmount ,
61
66
} ) . then ( unwrapResponse ) ;
62
67
}
0 commit comments