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
0b39cbad
Unverified
Commit
0b39cbad
authored
Oct 16, 2018
by
Chi Kei Chan
Committed by
GitHub
Oct 16, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ERC20 to ERC20 calculateInput & calculateOutput (#39)
parent
fd67c060
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
64 additions
and
1 deletion
+64
-1
exchange-utils.js
src/helpers/exchange-utils.js
+64
-1
No files found.
src/helpers/exchange-utils.js
View file @
0b39cbad
...
...
@@ -2,6 +2,11 @@ import {BigNumber as BN} from "bignumber.js";
export
const
calculateExchangeRateFromInput
=
async
opts
=>
{
const
{
inputCurrency
,
outputCurrency
}
=
opts
;
if
(
!
inputCurrency
||
!
outputCurrency
)
{
return
;
}
if
(
inputCurrency
===
outputCurrency
)
{
console
.
error
(
`Input and Output currency cannot be the same`
);
return
;
...
...
@@ -14,10 +19,18 @@ export const calculateExchangeRateFromInput = async opts => {
if
(
outputCurrency
===
'
ETH
'
&&
inputCurrency
!==
'
ETH
'
)
{
return
ERC20_TO_ETH
.
calculateOutput
(
opts
);
}
return
ERC20_TO_ERC20
.
calculateOutput
(
opts
);
};
export
const
calculateExchangeRateFromOutput
=
async
opts
=>
{
const
{
inputCurrency
,
outputCurrency
}
=
opts
;
if
(
!
inputCurrency
||
!
outputCurrency
)
{
return
;
}
if
(
inputCurrency
===
outputCurrency
)
{
console
.
error
(
`Input and Output currency cannot be the same`
);
return
;
...
...
@@ -30,6 +43,8 @@ export const calculateExchangeRateFromOutput = async opts => {
if
(
outputCurrency
===
'
ETH
'
&&
inputCurrency
!==
'
ETH
'
)
{
return
ERC20_TO_ETH
.
calculateInput
(
opts
);
}
return
ERC20_TO_ERC20
.
calculateInput
(
opts
);
};
const
ETH_TO_ERC20
=
{
...
...
@@ -202,7 +217,6 @@ const ERC20_TO_ETH = {
contractStore
,
});
// const outputDecimals = await getDecimals({ address: inputCurrency, contractStore, drizzleCtx });
const
outputAmount
=
BN
(
output
).
multipliedBy
(
10
**
18
);
const
numerator
=
outputAmount
.
multipliedBy
(
BN
(
inputReserve
).
multipliedBy
(
1000
));
const
denominator
=
BN
(
outputReserve
).
minus
(
outputAmount
).
multipliedBy
(
997
);
...
...
@@ -217,6 +231,55 @@ const ERC20_TO_ETH = {
},
};
const
ERC20_TO_ERC20
=
{
calculateOutput
:
async
opts
=>
{
const
inputDecimals
=
await
getDecimals
({
address
:
opts
.
inputCurrency
,
contractStore
:
opts
.
contractStore
,
drizzleCtx
:
opts
.
drizzleCtx
});
const
inputAmountA
=
BN
(
opts
.
input
).
multipliedBy
(
BN
(
10
**
inputDecimals
));
const
exchangeRateA
=
await
ERC20_TO_ETH
.
calculateOutput
({
...
opts
,
outputCurrency
:
'
ETH
'
});
const
inputAmountB
=
inputAmountA
.
multipliedBy
(
exchangeRateA
);
const
exchangeRateB
=
await
ETH_TO_ERC20
.
calculateOutput
({
...
opts
,
input
:
inputAmountB
.
dividedBy
(
BN
(
10
**
18
)),
inputCurrency
:
'
ETH
'
,
});
if
(
!
exchangeRateA
||
!
exchangeRateB
)
{
return
;
}
return
exchangeRateA
.
multipliedBy
(
exchangeRateB
);
},
calculateInput
:
async
opts
=>
{
const
outputDecimals
=
await
getDecimals
({
address
:
opts
.
outputCurrency
,
contractStore
:
opts
.
contractStore
,
drizzleCtx
:
opts
.
drizzleCtx
});
const
outputAmountA
=
BN
(
opts
.
output
).
multipliedBy
(
BN
(
10
**
outputDecimals
))
const
exchangeRateA
=
await
ETH_TO_ERC20
.
calculateInput
({
...
opts
,
inputCurrency
:
'
ETH
'
});
if
(
!
exchangeRateA
)
{
return
;
}
const
inputAmountB
=
outputAmountA
.
dividedBy
(
exchangeRateA
).
dividedBy
(
10
**
18
);
const
exchangeRateB
=
await
ERC20_TO_ETH
.
calculateInput
({
...
opts
,
outputCurrency
:
'
ETH
'
,
output
:
inputAmountB
,
});
if
(
!
exchangeRateB
)
{
return
;
}
return
exchangeRateA
.
multipliedBy
(
exchangeRateB
);
},
};
function
getDecimals
({
address
,
drizzleCtx
,
contractStore
})
{
...
...
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