Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
I
interface
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
LuckySwap
interface
Commits
57786335
Unverified
Commit
57786335
authored
May 10, 2021
by
Moody Salem
Committed by
GitHub
May 10, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix calculateSlippageAmount (#1497)
parent
948e01a1
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
1 deletion
+29
-1
index.test.ts
src/utils/index.test.ts
+24
-0
index.ts
src/utils/index.ts
+5
-1
No files found.
src/utils/index.test.ts
View file @
57786335
...
@@ -50,6 +50,30 @@ describe('utils', () => {
...
@@ -50,6 +50,30 @@ describe('utils', () => {
calculateSlippageAmount
(
tokenAmount
,
new
Percent
(
10000
,
10
_000
)).
map
((
bound
)
=>
bound
.
toString
())
calculateSlippageAmount
(
tokenAmount
,
new
Percent
(
10000
,
10
_000
)).
map
((
bound
)
=>
bound
.
toString
())
).
toEqual
([
'
0
'
,
'
200
'
])
).
toEqual
([
'
0
'
,
'
200
'
])
})
})
it
(
'
works for 18 decimals
'
,
()
=>
{
const
tokenAmount
=
new
TokenAmount
(
new
Token
(
ChainId
.
MAINNET
,
AddressZero
,
18
),
'
100
'
)
expect
(()
=>
calculateSlippageAmount
(
tokenAmount
,
new
Percent
(
-
1
,
10
_000
))).
toThrow
(
'
Unexpected slippage
'
)
expect
(()
=>
calculateSlippageAmount
(
tokenAmount
,
new
Percent
(
10
_001
,
10
_000
))).
toThrow
(
'
Unexpected slippage
'
)
expect
(
calculateSlippageAmount
(
tokenAmount
,
new
Percent
(
0
,
10
_000
)).
map
((
bound
)
=>
bound
.
toString
())).
toEqual
([
'
100
'
,
'
100
'
,
])
expect
(
calculateSlippageAmount
(
tokenAmount
,
new
Percent
(
5
,
100
)).
map
((
bound
)
=>
bound
.
toString
())).
toEqual
([
'
95
'
,
'
105
'
,
])
expect
(
calculateSlippageAmount
(
tokenAmount
,
new
Percent
(
100
,
10
_000
)).
map
((
bound
)
=>
bound
.
toString
())).
toEqual
([
'
99
'
,
'
101
'
,
])
expect
(
calculateSlippageAmount
(
tokenAmount
,
new
Percent
(
200
,
10
_000
)).
map
((
bound
)
=>
bound
.
toString
())).
toEqual
([
'
98
'
,
'
102
'
,
])
expect
(
calculateSlippageAmount
(
tokenAmount
,
new
Percent
(
10000
,
10
_000
)).
map
((
bound
)
=>
bound
.
toString
())
).
toEqual
([
'
0
'
,
'
200
'
])
})
})
})
describe
(
'
#isAddress
'
,
()
=>
{
describe
(
'
#isAddress
'
,
()
=>
{
...
...
src/utils/index.ts
View file @
57786335
...
@@ -66,7 +66,11 @@ export function calculateGasMargin(value: BigNumber): BigNumber {
...
@@ -66,7 +66,11 @@ export function calculateGasMargin(value: BigNumber): BigNumber {
const
ONE
=
new
Fraction
(
1
,
1
)
const
ONE
=
new
Fraction
(
1
,
1
)
export
function
calculateSlippageAmount
(
value
:
CurrencyAmount
,
slippage
:
Percent
):
[
JSBI
,
JSBI
]
{
export
function
calculateSlippageAmount
(
value
:
CurrencyAmount
,
slippage
:
Percent
):
[
JSBI
,
JSBI
]
{
if
(
slippage
.
lessThan
(
0
)
||
slippage
.
greaterThan
(
ONE
))
throw
new
Error
(
'
Unexpected slippage
'
)
if
(
slippage
.
lessThan
(
0
)
||
slippage
.
greaterThan
(
ONE
))
throw
new
Error
(
'
Unexpected slippage
'
)
return
[
value
.
multiply
(
ONE
.
subtract
(
slippage
)).
quotient
,
value
.
multiply
(
ONE
.
add
(
slippage
)).
quotient
]
const
decimalScaled
=
JSBI
.
exponentiate
(
JSBI
.
BigInt
(
10
),
JSBI
.
BigInt
(
value
.
currency
.
decimals
))
return
[
value
.
multiply
(
ONE
.
subtract
(
slippage
)).
multiply
(
decimalScaled
).
quotient
,
value
.
multiply
(
ONE
.
add
(
slippage
)).
multiply
(
decimalScaled
).
quotient
,
]
}
}
// account is not optional
// account is not optional
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment