Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
swap-v2-sdk
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
swap-v2-sdk
Commits
4e048df4
Unverified
Commit
4e048df4
authored
Jul 13, 2020
by
Moody Salem
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add methods for getting mid price to the pair
parent
fe859b32
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
62 additions
and
2 deletions
+62
-2
pair.ts
src/entities/pair.ts
+27
-0
pair.test.ts
test/pair.test.ts
+35
-2
No files found.
src/entities/pair.ts
View file @
4e048df4
import
{
Price
}
from
'
./fractions/price
'
import
{
TokenAmount
}
from
'
./fractions/tokenAmount
'
import
invariant
from
'
tiny-invariant
'
import
JSBI
from
'
jsbi
'
...
...
@@ -68,6 +69,32 @@ export class Pair {
return
token
.
equals
(
this
.
token0
)
||
token
.
equals
(
this
.
token1
)
}
/**
* Returns the current mid price of the pair in terms of token0, i.e. the ratio of reserve1 to reserve0
*/
public
get
token0Price
():
Price
{
return
new
Price
(
this
.
token0
,
this
.
token1
,
this
.
tokenAmounts
[
0
].
raw
,
this
.
tokenAmounts
[
1
].
raw
)
}
/**
* Returns the current mid price of the pair in terms of token1, i.e. the ratio of reserve0 to reserve1
*/
public
get
token1Price
():
Price
{
return
new
Price
(
this
.
token1
,
this
.
token0
,
this
.
tokenAmounts
[
1
].
raw
,
this
.
tokenAmounts
[
0
].
raw
)
}
/**
* Return the price of the given token in terms of the other token in the pair.
* @param token token to return price of
*/
public
priceOf
(
token
:
Token
):
Price
{
invariant
(
this
.
involvesToken
(
token
),
'
TOKEN
'
)
return
token
.
equals
(
this
.
token0
)
?
this
.
token0Price
:
this
.
token1Price
}
/**
* Returns the chain ID of the tokens in the pair.
*/
public
get
chainId
():
ChainId
{
return
this
.
token0
.
chainId
}
...
...
test/pair.test.ts
View file @
4e048df4
import
{
Token
,
Pair
,
TokenAmount
,
WETH
}
from
'
../src/entities
'
import
{
ChainId
}
from
'
../src/constants
'
import
{
ChainId
,
Token
,
Pair
,
TokenAmount
,
WETH
,
Price
}
from
'
../src
'
describe
(
'
Pair
'
,
()
=>
{
const
USDC
=
new
Token
(
ChainId
.
MAINNET
,
'
0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48
'
,
18
,
'
USDC
'
,
'
USD Coin
'
)
...
...
@@ -52,6 +51,40 @@ describe('Pair', () => {
})
})
describe
(
'
#token0Price
'
,
()
=>
{
it
(
'
returns price of token0 in terms of token1
'
,
()
=>
{
expect
(
new
Pair
(
new
TokenAmount
(
USDC
,
'
101
'
),
new
TokenAmount
(
DAI
,
'
100
'
)).
token0Price
).
toEqual
(
new
Price
(
DAI
,
USDC
,
'
100
'
,
'
101
'
)
)
expect
(
new
Pair
(
new
TokenAmount
(
DAI
,
'
100
'
),
new
TokenAmount
(
USDC
,
'
101
'
)).
token0Price
).
toEqual
(
new
Price
(
DAI
,
USDC
,
'
100
'
,
'
101
'
)
)
})
})
describe
(
'
#token1Price
'
,
()
=>
{
it
(
'
returns price of token1 in terms of token0
'
,
()
=>
{
expect
(
new
Pair
(
new
TokenAmount
(
USDC
,
'
101
'
),
new
TokenAmount
(
DAI
,
'
100
'
)).
token1Price
).
toEqual
(
new
Price
(
USDC
,
DAI
,
'
101
'
,
'
100
'
)
)
expect
(
new
Pair
(
new
TokenAmount
(
DAI
,
'
100
'
),
new
TokenAmount
(
USDC
,
'
101
'
)).
token1Price
).
toEqual
(
new
Price
(
USDC
,
DAI
,
'
101
'
,
'
100
'
)
)
})
})
describe
(
'
#priceOf
'
,
()
=>
{
const
pair
=
new
Pair
(
new
TokenAmount
(
USDC
,
'
101
'
),
new
TokenAmount
(
DAI
,
'
100
'
))
it
(
'
returns price of token in terms of other token
'
,
()
=>
{
expect
(
pair
.
priceOf
(
DAI
)).
toEqual
(
pair
.
token0Price
)
expect
(
pair
.
priceOf
(
USDC
)).
toEqual
(
pair
.
token1Price
)
})
it
(
'
throws if invalid token
'
,
()
=>
{
expect
(()
=>
pair
.
priceOf
(
WETH
[
ChainId
.
MAINNET
])).
toThrow
(
'
TOKEN
'
)
})
})
describe
(
'
#reserveOf
'
,
()
=>
{
it
(
'
returns reserves of the given token
'
,
()
=>
{
expect
(
new
Pair
(
new
TokenAmount
(
USDC
,
'
100
'
),
new
TokenAmount
(
DAI
,
'
101
'
)).
reserveOf
(
USDC
)).
toEqual
(
...
...
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