@@ -15,43 +15,26 @@ import { QuotePoolParams } from "./quote-builder";
15
15
*/
16
16
17
17
function getRate ( inputTradeAmountU64 : u64 , params : QuotePoolParams ) : Decimal {
18
- const expectedOutputAmountU64 = getExpectedOutputAmount (
19
- inputTradeAmountU64 ,
20
- params
21
- ) ;
22
- const inputTradeAmount = DecimalUtil . fromU64 (
23
- inputTradeAmountU64 ,
24
- params . inputToken . decimals
25
- ) ;
18
+ const expectedOutputAmountU64 = getExpectedOutputAmount ( inputTradeAmountU64 , params ) ;
19
+ const inputTradeAmount = DecimalUtil . fromU64 ( inputTradeAmountU64 , params . inputToken . decimals ) ;
26
20
const outputTradeAmount = DecimalUtil . fromU64 (
27
21
expectedOutputAmountU64 ,
28
22
params . outputToken . decimals
29
23
) ;
30
24
return outputTradeAmount . div ( inputTradeAmount ) ;
31
25
}
32
26
33
- function getPriceImpact (
34
- inputTradeAmount : u64 ,
35
- params : QuotePoolParams
36
- ) : Decimal {
37
- const noSlippageOutputCountU64 = getExpectedOutputAmountWithNoSlippage (
38
- inputTradeAmount ,
39
- params
40
- ) ;
27
+ function getPriceImpact ( inputTradeAmount : u64 , params : QuotePoolParams ) : Decimal {
28
+ const noSlippageOutputCountU64 = getExpectedOutputAmountWithNoSlippage ( inputTradeAmount , params ) ;
41
29
const outputCountU64 = getExpectedOutputAmount ( inputTradeAmount , params ) ;
42
30
43
31
const noSlippageOutputCount = DecimalUtil . fromU64 (
44
32
noSlippageOutputCountU64 ,
45
33
params . outputToken . decimals
46
34
) ;
47
- const outputCount = DecimalUtil . fromU64 (
48
- outputCountU64 ,
49
- params . outputToken . decimals
50
- ) ;
35
+ const outputCount = DecimalUtil . fromU64 ( outputCountU64 , params . outputToken . decimals ) ;
51
36
52
- const impact = noSlippageOutputCount
53
- . sub ( outputCount )
54
- . div ( noSlippageOutputCount ) ;
37
+ const impact = noSlippageOutputCount . sub ( outputCount ) . div ( noSlippageOutputCount ) ;
55
38
return impact . mul ( 100 ) ;
56
39
}
57
40
@@ -68,37 +51,22 @@ function getFees(inputTradeAmount: u64, params: QuotePoolParams): u64 {
68
51
return new u64 ( tradingFee . add ( ownerFee ) . toString ( ) ) ;
69
52
}
70
53
71
- function getExpectedOutputAmount (
72
- inputTradeAmount : u64 ,
73
- params : QuotePoolParams
74
- ) : u64 {
75
- const inputTradeLessFees = inputTradeAmount . sub (
76
- getFees ( inputTradeAmount , params )
77
- ) ;
54
+ function getExpectedOutputAmount ( inputTradeAmount : u64 , params : QuotePoolParams ) : u64 {
55
+ const inputTradeLessFees = inputTradeAmount . sub ( getFees ( inputTradeAmount , params ) ) ;
78
56
return getOutputAmount ( inputTradeLessFees , params ) ;
79
57
}
80
58
81
59
function getExpectedOutputAmountWithNoSlippage (
82
60
inputTradeAmount : u64 ,
83
61
params : QuotePoolParams
84
62
) : u64 {
85
- const inputTradeLessFees = inputTradeAmount . sub (
86
- getFees ( inputTradeAmount , params )
87
- ) ;
88
- return inputTradeLessFees
89
- . mul ( params . outputTokenCount )
90
- . div ( params . inputTokenCount ) ;
63
+ const inputTradeLessFees = inputTradeAmount . sub ( getFees ( inputTradeAmount , params ) ) ;
64
+ return inputTradeLessFees . mul ( params . outputTokenCount ) . div ( params . inputTokenCount ) ;
91
65
}
92
66
93
- function getMinimumAmountOut (
94
- inputTradeAmount : u64 ,
95
- params : QuotePoolParams
96
- ) : u64 {
67
+ function getMinimumAmountOut ( inputTradeAmount : u64 , params : QuotePoolParams ) : u64 {
97
68
const slippageTolerance = params . slippageTolerance ;
98
- const expectedOutputAmountFees = getExpectedOutputAmount (
99
- inputTradeAmount ,
100
- params
101
- ) ;
69
+ const expectedOutputAmountFees = getExpectedOutputAmount ( inputTradeAmount , params ) ;
102
70
const result = expectedOutputAmountFees
103
71
. mul ( slippageTolerance . denominator . sub ( slippageTolerance . numerator ) )
104
72
. div ( slippageTolerance . denominator ) ;
@@ -109,10 +77,7 @@ function getMinimumAmountOut(
109
77
// Given k = currInputTokenCount * currOutputTokenCount and k = newInputTokenCount * newOutputTokenCount,
110
78
// solve for newOutputTokenCount
111
79
function getOutputAmount ( inputTradeAmount : u64 , params : QuotePoolParams ) : u64 {
112
- const [ poolInputAmount , poolOutputAmount ] = [
113
- params . inputTokenCount ,
114
- params . outputTokenCount ,
115
- ] ;
80
+ const [ poolInputAmount , poolOutputAmount ] = [ params . inputTokenCount , params . outputTokenCount ] ;
116
81
117
82
const invariant = poolInputAmount . mul ( poolOutputAmount ) ;
118
83
@@ -127,18 +92,12 @@ function getOutputAmount(inputTradeAmount: u64, params: QuotePoolParams): u64 {
127
92
}
128
93
129
94
export class ConstantProductPoolQuoteBuilder {
130
- async buildQuote (
131
- params : QuotePoolParams ,
132
- inputTradeAmount : u64
133
- ) : Promise < Quote > {
95
+ async buildQuote ( params : QuotePoolParams , inputTradeAmount : u64 ) : Promise < Quote > {
134
96
return {
135
97
getRate : ( ) => getRate ( inputTradeAmount , params ) ,
136
98
getPriceImpact : ( ) => getPriceImpact ( inputTradeAmount , params ) ,
137
99
getFees : ( ) =>
138
- U64Utils . toOrcaU64 (
139
- getFees ( inputTradeAmount , params ) ,
140
- params . inputToken . decimals
141
- ) ,
100
+ U64Utils . toOrcaU64 ( getFees ( inputTradeAmount , params ) , params . inputToken . decimals ) ,
142
101
getExpectedOutputAmount : ( ) =>
143
102
U64Utils . toOrcaU64 (
144
103
getExpectedOutputAmount ( inputTradeAmount , params ) ,
0 commit comments