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
b1a5a6c8
Commit
b1a5a6c8
authored
Oct 22, 2018
by
Kenny Tran
Committed by
Chi Kei Chan
Oct 22, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Create send/transfer utils (#71)
parent
47386298
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
291 additions
and
52 deletions
+291
-52
exchange-utils.js
src/helpers/exchange-utils.js
+291
-52
No files found.
src/helpers/exchange-utils.js
View file @
b1a5a6c8
...
@@ -95,6 +95,49 @@ export const swapOutput = async opts => {
...
@@ -95,6 +95,49 @@ export const swapOutput = async opts => {
return
ERC20_TO_ERC20
.
swapOutput
(
opts
);
return
ERC20_TO_ERC20
.
swapOutput
(
opts
);
};
};
export
const
sendInput
=
async
opts
=>
{
const
{
inputCurrency
,
outputCurrency
}
=
opts
;
if
(
!
inputCurrency
||
!
outputCurrency
)
{
return
;
}
if
(
inputCurrency
===
outputCurrency
)
{
console
.
error
(
`Input and Output currency cannot be the same`
);
return
;
}
if
(
inputCurrency
===
'
ETH
'
&&
outputCurrency
!==
'
ETH
'
)
{
return
ETH_TO_ERC20
.
sendInput
(
opts
);
}
if
(
outputCurrency
===
'
ETH
'
&&
inputCurrency
!==
'
ETH
'
)
{
return
ERC20_TO_ETH
.
sendInput
(
opts
);
}
return
ERC20_TO_ERC20
.
sendInput
(
opts
);
};
export
const
sendOutput
=
async
opts
=>
{
const
{
inputCurrency
,
outputCurrency
}
=
opts
;
if
(
!
inputCurrency
||
!
outputCurrency
)
{
return
;
}
if
(
inputCurrency
===
outputCurrency
)
{
console
.
error
(
`Input and Output currency cannot be the same`
);
return
;
}
if
(
inputCurrency
===
'
ETH
'
&&
outputCurrency
!==
'
ETH
'
)
{
return
ETH_TO_ERC20
.
sendOutput
(
opts
);
}
if
(
outputCurrency
===
'
ETH
'
&&
inputCurrency
!==
'
ETH
'
)
{
return
ERC20_TO_ETH
.
sendOutput
(
opts
);
}
return
ERC20_TO_ERC20
.
sendOutput
(
opts
);
};
const
ETH_TO_ERC20
=
{
const
ETH_TO_ERC20
=
{
calculateOutput
:
async
({
drizzleCtx
,
contractStore
,
input
,
inputCurrency
,
outputCurrency
,
exchangeAddresses
})
=>
{
calculateOutput
:
async
({
drizzleCtx
,
contractStore
,
input
,
inputCurrency
,
outputCurrency
,
exchangeAddresses
})
=>
{
...
@@ -187,13 +230,7 @@ const ETH_TO_ERC20 = {
...
@@ -187,13 +230,7 @@ const ETH_TO_ERC20 = {
return
exchangeRate
;
return
exchangeRate
;
},
},
swapInput
:
async
({
drizzleCtx
,
contractStore
,
input
,
output
,
account
,
inputCurrency
,
outputCurrency
,
exchangeAddresses
})
=>
{
swapInput
:
async
({
drizzleCtx
,
contractStore
,
input
,
output
,
account
,
inputCurrency
,
outputCurrency
,
exchangeAddresses
})
=>
{
if
(
inputCurrency
!==
'
ETH
'
)
{
if
(
!
validEthToErc20
(
inputCurrency
,
outputCurrency
))
{
console
.
error
(
'
Input Currency should be ETH
'
);
return
;
}
if
(
!
outputCurrency
||
outputCurrency
===
'
ETH
'
)
{
console
.
error
(
'
Output Currency should be ERC20
'
);
return
;
return
;
}
}
...
@@ -202,15 +239,9 @@ const ETH_TO_ERC20 = {
...
@@ -202,15 +239,9 @@ const ETH_TO_ERC20 = {
if
(
!
exchangeAddress
||
!
exchange
)
{
if
(
!
exchangeAddress
||
!
exchange
)
{
console
.
error
(
`Cannot find Exchange Address for
${
outputCurrency
}
`
);
console
.
error
(
`Cannot find Exchange Address for
${
outputCurrency
}
`
);
return
;
return
;
}
}
const
{
web3
}
=
drizzleCtx
;
const
deadline
=
await
getDeadline
(
drizzleCtx
,
300
);
const
blockNumber
=
await
promisify
(
web3
,
'
getBlockNumber
'
);
const
block
=
await
promisify
(
web3
,
'
getBlock
'
,
blockNumber
);
const
deadline
=
block
.
timestamp
+
300
;
const
ALLOWED_SLIPPAGE
=
BN
(
0.025
);
const
ALLOWED_SLIPPAGE
=
BN
(
0.025
);
const
outputDecimals
=
await
getDecimals
({
address
:
outputCurrency
,
contractStore
,
drizzleCtx
});
const
outputDecimals
=
await
getDecimals
({
address
:
outputCurrency
,
contractStore
,
drizzleCtx
});
const
minOutput
=
BN
(
output
).
multipliedBy
(
10
**
outputDecimals
).
multipliedBy
(
BN
(
1
).
minus
(
ALLOWED_SLIPPAGE
));
const
minOutput
=
BN
(
output
).
multipliedBy
(
10
**
outputDecimals
).
multipliedBy
(
BN
(
1
).
minus
(
ALLOWED_SLIPPAGE
));
...
@@ -219,14 +250,31 @@ const ETH_TO_ERC20 = {
...
@@ -219,14 +250,31 @@ const ETH_TO_ERC20 = {
value
:
BN
(
input
).
multipliedBy
(
10
**
18
).
toFixed
(
0
),
value
:
BN
(
input
).
multipliedBy
(
10
**
18
).
toFixed
(
0
),
});
});
},
},
swapOutput
:
async
({
drizzleCtx
,
contractStore
,
input
,
output
,
account
,
inputCurrency
,
outputCurrency
,
exchangeAddresses
})
=>
{
swapOutput
:
async
({
drizzleCtx
,
contractStore
,
input
,
output
,
account
,
inputCurrency
,
outputCurrency
,
exchangeAddresses
})
=>
{
if
(
inputCurrency
!==
'
ETH
'
)
{
if
(
!
validEthToErc20
(
inputCurrency
,
outputCurrency
))
{
console
.
error
(
'
Input Currency should be ETH
'
);
return
;
return
;
}
}
if
(
!
outputCurrency
||
outputCurrency
===
'
ETH
'
)
{
const
exchangeAddress
=
exchangeAddresses
.
fromToken
[
outputCurrency
];
console
.
error
(
'
Output Currency should be ERC20
'
);
const
exchange
=
drizzleCtx
.
contracts
[
exchangeAddress
];
if
(
!
exchangeAddress
||
!
exchange
)
{
console
.
error
(
`Cannot find Exchange Address for
${
outputCurrency
}
`
);
return
;
}
const
deadline
=
await
getDeadline
(
drizzleCtx
,
300
);
const
ALLOWED_SLIPPAGE
=
BN
(
0.025
);
const
outputDecimals
=
await
getDecimals
({
address
:
outputCurrency
,
contractStore
,
drizzleCtx
});
const
outputAmount
=
BN
(
output
).
multipliedBy
(
BN
(
10
**
outputDecimals
));
const
maxInput
=
BN
(
input
).
multipliedBy
(
10
**
18
).
multipliedBy
(
BN
(
1
).
plus
(
ALLOWED_SLIPPAGE
));
return
exchange
.
methods
.
ethToTokenSwapOutput
.
cacheSend
(
outputAmount
.
toFixed
(
0
),
deadline
,
{
from
:
account
,
value
:
maxInput
.
toFixed
(
0
),
});
},
sendInput
:
async
({
drizzleCtx
,
contractStore
,
input
,
output
,
account
,
inputCurrency
,
outputCurrency
,
exchangeAddresses
,
recipient
})
=>
{
if
(
!
validEthToErc20
(
inputCurrency
,
outputCurrency
))
{
return
;
return
;
}
}
...
@@ -235,24 +283,50 @@ const ETH_TO_ERC20 = {
...
@@ -235,24 +283,50 @@ const ETH_TO_ERC20 = {
if
(
!
exchangeAddress
||
!
exchange
)
{
if
(
!
exchangeAddress
||
!
exchange
)
{
console
.
error
(
`Cannot find Exchange Address for
${
outputCurrency
}
`
);
console
.
error
(
`Cannot find Exchange Address for
${
outputCurrency
}
`
);
return
;
return
;
}
}
const
{
web3
}
=
drizzleCtx
;
const
deadline
=
await
getDeadline
(
drizzleCtx
,
300
);
const
blockNumber
=
await
promisify
(
web3
,
'
getBlockNumber
'
);
const
ALLOWED_SLIPPAGE
=
BN
(
0.025
);
const
block
=
await
promisify
(
web3
,
'
getBlock
'
,
blockNumber
);
const
outputDecimals
=
await
getDecimals
({
address
:
outputCurrency
,
contractStore
,
drizzleCtx
});
const
minOutput
=
BN
(
output
).
multipliedBy
(
10
**
outputDecimals
).
multipliedBy
(
BN
(
1
).
minus
(
ALLOWED_SLIPPAGE
));
return
exchange
.
methods
.
ethToTokenTransferInput
.
cacheSend
(
minOutput
.
toFixed
(
0
),
deadline
,
recipient
,
{
from
:
account
,
value
:
BN
(
input
).
multipliedBy
(
10
**
18
).
toFixed
(
0
),
}
);
},
sendOutput
:
async
({
drizzleCtx
,
contractStore
,
input
,
output
,
account
,
inputCurrency
,
outputCurrency
,
exchangeAddresses
,
recipient
})
=>
{
if
(
!
validEthToErc20
(
inputCurrency
,
outputCurrency
))
{
return
;
}
const
exchangeAddress
=
exchangeAddresses
.
fromToken
[
outputCurrency
];
const
exchange
=
drizzleCtx
.
contracts
[
exchangeAddress
];
if
(
!
exchangeAddress
||
!
exchange
)
{
console
.
error
(
`Cannot find Exchange Address for
${
outputCurrency
}
`
);
return
;
}
const
deadline
=
block
.
timestamp
+
300
;
const
deadline
=
await
getDeadline
(
drizzleCtx
,
300
)
;
const
ALLOWED_SLIPPAGE
=
BN
(
0.025
);
const
ALLOWED_SLIPPAGE
=
BN
(
0.025
);
const
outputDecimals
=
await
getDecimals
({
address
:
outputCurrency
,
contractStore
,
drizzleCtx
});
const
outputDecimals
=
await
getDecimals
({
address
:
outputCurrency
,
contractStore
,
drizzleCtx
});
const
outputAmount
=
BN
(
output
).
multipliedBy
(
BN
(
10
**
outputDecimals
));
const
outputAmount
=
BN
(
output
).
multipliedBy
(
BN
(
10
**
outputDecimals
));
const
maxInput
=
BN
(
input
).
multipliedBy
(
10
**
18
).
multipliedBy
(
BN
(
1
).
plus
(
ALLOWED_SLIPPAGE
));
const
maxInput
=
BN
(
input
).
multipliedBy
(
10
**
18
).
multipliedBy
(
BN
(
1
).
plus
(
ALLOWED_SLIPPAGE
));
return
exchange
.
methods
.
ethToTokenSwapOutput
.
cacheSend
(
outputAmount
.
toFixed
(
0
),
deadline
,
{
return
exchange
.
methods
.
ethToTokenTransferOutput
.
cacheSend
(
outputAmount
.
toFixed
(
0
),
deadline
,
recipient
,
{
from
:
account
,
from
:
account
,
value
:
maxInput
.
toFixed
(
0
),
value
:
maxInput
.
toFixed
(
0
),
});
}
},
);
}
};
};
const
ERC20_TO_ETH
=
{
const
ERC20_TO_ETH
=
{
...
@@ -345,7 +419,7 @@ const ERC20_TO_ETH = {
...
@@ -345,7 +419,7 @@ const ERC20_TO_ETH = {
return
exchangeRate
;
return
exchangeRate
;
},
},
swapInput
:
async
({
drizzleCtx
,
contractStore
,
input
,
output
,
account
,
inputCurrency
,
outputCurrency
,
exchangeAddresses
})
=>
{
swapInput
:
async
({
drizzleCtx
,
contractStore
,
input
,
output
,
account
,
inputCurrency
,
outputCurrency
,
exchangeAddresses
})
=>
{
if
(
outputCurrency
!==
'
ETH
'
)
{
if
(
outputCurrency
!==
'
ETH
'
)
{
console
.
error
(
'
Output Currency should be ETH
'
);
console
.
error
(
'
Output Currency should be ETH
'
);
return
;
return
;
...
@@ -363,12 +437,7 @@ const ERC20_TO_ETH = {
...
@@ -363,12 +437,7 @@ const ERC20_TO_ETH = {
return
;
return
;
}
}
const
{
web3
}
=
drizzleCtx
;
const
deadline
=
await
getDeadline
(
drizzleCtx
,
300
);
const
blockNumber
=
await
promisify
(
web3
,
'
getBlockNumber
'
);
const
block
=
await
promisify
(
web3
,
'
getBlock
'
,
blockNumber
);
const
deadline
=
block
.
timestamp
+
300
;
const
ALLOWED_SLIPPAGE
=
BN
(
0.025
);
const
ALLOWED_SLIPPAGE
=
BN
(
0.025
);
const
inputDecimals
=
await
getDecimals
({
address
:
inputCurrency
,
contractStore
,
drizzleCtx
});
const
inputDecimals
=
await
getDecimals
({
address
:
inputCurrency
,
contractStore
,
drizzleCtx
});
const
minOutput
=
BN
(
output
).
multipliedBy
(
10
**
18
).
multipliedBy
(
BN
(
1
).
minus
(
ALLOWED_SLIPPAGE
));
const
minOutput
=
BN
(
output
).
multipliedBy
(
10
**
18
).
multipliedBy
(
BN
(
1
).
minus
(
ALLOWED_SLIPPAGE
));
...
@@ -381,7 +450,7 @@ const ERC20_TO_ETH = {
...
@@ -381,7 +450,7 @@ const ERC20_TO_ETH = {
{
from
:
account
,
value
:
'
0x0
'
},
{
from
:
account
,
value
:
'
0x0
'
},
);
);
},
},
swapOutput
:
async
({
drizzleCtx
,
contractStore
,
input
,
output
,
account
,
inputCurrency
,
outputCurrency
,
exchangeAddresses
})
=>
{
swapOutput
:
async
({
drizzleCtx
,
contractStore
,
input
,
output
,
account
,
inputCurrency
,
outputCurrency
,
exchangeAddresses
})
=>
{
if
(
outputCurrency
!==
'
ETH
'
)
{
if
(
outputCurrency
!==
'
ETH
'
)
{
console
.
error
(
'
Output Currency should be ETH
'
);
console
.
error
(
'
Output Currency should be ETH
'
);
return
;
return
;
...
@@ -399,12 +468,58 @@ const ERC20_TO_ETH = {
...
@@ -399,12 +468,58 @@ const ERC20_TO_ETH = {
return
;
return
;
}
}
const
{
web3
}
=
drizzleCtx
;
const
deadline
=
await
getDeadline
(
drizzleCtx
,
300
);
const
blockNumber
=
await
promisify
(
web3
,
'
getBlockNumber
'
);
const
ALLOWED_SLIPPAGE
=
BN
(
0.025
);
const
block
=
await
promisify
(
web3
,
'
getBlock
'
,
blockNumber
);
const
inputDecimals
=
await
getDecimals
({
address
:
inputCurrency
,
contractStore
,
drizzleCtx
});
const
maxInput
=
BN
(
input
).
multipliedBy
(
10
**
inputDecimals
).
multipliedBy
(
BN
(
1
).
plus
(
ALLOWED_SLIPPAGE
));
const
outputAmount
=
BN
(
output
).
multipliedBy
(
10
**
18
);
return
exchange
.
methods
.
tokenToEthSwapOutput
.
cacheSend
(
outputAmount
.
toFixed
(
0
),
maxInput
.
toFixed
(
0
),
deadline
,
{
from
:
account
},
);
},
sendInput
:
async
({
drizzleCtx
,
contractStore
,
input
,
output
,
account
,
inputCurrency
,
outputCurrency
,
exchangeAddresses
,
recipient
})
=>
{
if
(
!
validErc20ToEth
(
inputCurrency
,
outputCurrency
))
{
return
;
}
const
exchangeAddress
=
exchangeAddresses
.
fromToken
[
inputCurrency
];
const
exchange
=
drizzleCtx
.
contracts
[
exchangeAddress
];
if
(
!
exchangeAddress
||
!
exchange
)
{
console
.
error
(
`Cannot find Exchange Address for
${
inputCurrency
}
`
);
return
;
}
const
deadline
=
block
.
timestamp
+
300
;
const
deadline
=
await
getDeadline
(
drizzleCtx
,
300
);
const
ALLOWED_SLIPPAGE
=
BN
(
0.025
);
const
inputDecimals
=
await
getDecimals
({
address
:
inputCurrency
,
contractStore
,
drizzleCtx
});
const
minOutput
=
BN
(
output
).
multipliedBy
(
10
**
18
).
multipliedBy
(
BN
(
1
).
minus
(
ALLOWED_SLIPPAGE
));
const
inputAmount
=
BN
(
input
).
multipliedBy
(
10
**
inputDecimals
);
return
exchange
.
methods
.
tokenToEthTransferInput
.
cacheSend
(
inputAmount
.
toFixed
(
0
),
minOutput
.
toFixed
(
0
),
deadline
,
recipient
,
{
from
:
account
,
value
:
'
0x0
'
},
);
},
sendOutput
:
async
({
drizzleCtx
,
contractStore
,
input
,
output
,
account
,
inputCurrency
,
outputCurrency
,
exchangeAddresses
,
recipient
})
=>
{
if
(
!
validErc20ToEth
(
inputCurrency
,
outputCurrency
))
{
return
;
}
const
exchangeAddress
=
exchangeAddresses
.
fromToken
[
inputCurrency
];
const
exchange
=
drizzleCtx
.
contracts
[
exchangeAddress
];
if
(
!
exchangeAddress
||
!
exchange
)
{
console
.
error
(
`Cannot find Exchange Address for
${
inputCurrency
}
`
);
return
;
}
const
deadline
=
await
getDeadline
(
drizzleCtx
,
300
);
const
ALLOWED_SLIPPAGE
=
BN
(
0.025
);
const
ALLOWED_SLIPPAGE
=
BN
(
0.025
);
const
inputDecimals
=
await
getDecimals
({
address
:
inputCurrency
,
contractStore
,
drizzleCtx
});
const
inputDecimals
=
await
getDecimals
({
address
:
inputCurrency
,
contractStore
,
drizzleCtx
});
const
maxInput
=
BN
(
input
).
multipliedBy
(
10
**
inputDecimals
).
multipliedBy
(
BN
(
1
).
plus
(
ALLOWED_SLIPPAGE
));
const
maxInput
=
BN
(
input
).
multipliedBy
(
10
**
inputDecimals
).
multipliedBy
(
BN
(
1
).
plus
(
ALLOWED_SLIPPAGE
));
...
@@ -414,9 +529,10 @@ const ERC20_TO_ETH = {
...
@@ -414,9 +529,10 @@ const ERC20_TO_ETH = {
outputAmount
.
toFixed
(
0
),
outputAmount
.
toFixed
(
0
),
maxInput
.
toFixed
(
0
),
maxInput
.
toFixed
(
0
),
deadline
,
deadline
,
recipient
,
{
from
:
account
},
{
from
:
account
},
);
);
}
,
}
};
};
const
ERC20_TO_ERC20
=
{
const
ERC20_TO_ERC20
=
{
...
@@ -484,12 +600,7 @@ const ERC20_TO_ERC20 = {
...
@@ -484,12 +600,7 @@ const ERC20_TO_ERC20 = {
return
;
return
;
}
}
const
{
web3
}
=
drizzleCtx
;
const
deadline
=
await
getDeadline
(
drizzleCtx
,
300
);
const
blockNumber
=
await
promisify
(
web3
,
'
getBlockNumber
'
);
const
block
=
await
promisify
(
web3
,
'
getBlock
'
,
blockNumber
);
const
deadline
=
block
.
timestamp
+
300
;
const
ALLOWED_SLIPPAGE
=
BN
(
0.04
);
const
ALLOWED_SLIPPAGE
=
BN
(
0.04
);
const
inputDecimals
=
await
getDecimals
({
address
:
inputCurrency
,
contractStore
,
drizzleCtx
});
const
inputDecimals
=
await
getDecimals
({
address
:
inputCurrency
,
contractStore
,
drizzleCtx
});
const
outputDecimals
=
await
getDecimals
({
address
:
outputCurrency
,
contractStore
,
drizzleCtx
});
const
outputDecimals
=
await
getDecimals
({
address
:
outputCurrency
,
contractStore
,
drizzleCtx
});
...
@@ -543,12 +654,89 @@ const ERC20_TO_ERC20 = {
...
@@ -543,12 +654,89 @@ const ERC20_TO_ERC20 = {
return
;
return
;
}
}
const
{
web3
}
=
drizzleCtx
;
const
deadline
=
await
getDeadline
(
drizzleCtx
,
300
);
const
blockNumber
=
await
promisify
(
web3
,
'
getBlockNumber
'
);
const
ALLOWED_SLIPPAGE
=
BN
(
0.04
);
const
block
=
await
promisify
(
web3
,
'
getBlock
'
,
blockNumber
);
const
inputDecimals
=
await
getDecimals
({
address
:
inputCurrency
,
contractStore
,
drizzleCtx
});
const
outputDecimals
=
await
getDecimals
({
address
:
outputCurrency
,
contractStore
,
drizzleCtx
});
const
inputAmount
=
BN
(
input
).
multipliedBy
(
BN
(
10
**
inputDecimals
));
const
outputAmount
=
BN
(
output
).
multipliedBy
(
BN
(
10
**
outputDecimals
));
const
inputAmountB
=
BN
(
output
).
dividedBy
(
exchangeRateA
).
multipliedBy
(
BN
(
10
**
18
));
const
tokenAddress
=
outputCurrency
;
const
tokensBought
=
outputAmount
.
toFixed
(
0
);
const
maxTokensSold
=
inputAmount
.
multipliedBy
(
BN
(
1
).
plus
(
ALLOWED_SLIPPAGE
)).
toFixed
(
0
);
const
maxEthSold
=
inputAmountB
.
multipliedBy
(
1.2
).
toFixed
(
0
);
const
deadline
=
block
.
timestamp
+
300
;
return
exchange
.
methods
.
tokenToTokenSwapOutput
.
cacheSend
(
tokensBought
,
maxTokensSold
,
maxEthSold
,
deadline
,
tokenAddress
,
{
from
:
account
},
);
},
sendInput
:
async
({
drizzleCtx
,
contractStore
,
input
,
output
,
account
,
inputCurrency
,
outputCurrency
,
exchangeAddresses
,
recipient
})
=>
{
if
(
!
validErc20ToErc20
(
inputCurrency
,
outputCurrency
))
{
return
;
}
const
exchangeAddress
=
exchangeAddresses
.
fromToken
[
inputCurrency
];
const
exchange
=
drizzleCtx
.
contracts
[
exchangeAddress
];
if
(
!
exchangeAddress
||
!
exchange
)
{
console
.
error
(
`Cannot find Exchange Address for
${
inputCurrency
}
`
);
return
;
}
const
deadline
=
await
getDeadline
(
drizzleCtx
,
300
);
const
ALLOWED_SLIPPAGE
=
BN
(
0.04
);
const
inputDecimals
=
await
getDecimals
({
address
:
inputCurrency
,
contractStore
,
drizzleCtx
});
const
outputDecimals
=
await
getDecimals
({
address
:
outputCurrency
,
contractStore
,
drizzleCtx
});
const
inputAmount
=
BN
(
input
).
multipliedBy
(
BN
(
10
**
inputDecimals
));
const
outputAmount
=
BN
(
input
).
multipliedBy
(
BN
(
10
**
outputDecimals
));
const
tokenAddress
=
outputCurrency
;
const
tokensSold
=
inputAmount
.
toFixed
(
0
);
const
minTokensBought
=
outputAmount
.
multipliedBy
(
BN
(
1
).
plus
(
ALLOWED_SLIPPAGE
)).
toFixed
(
0
);
const
minEthBought
=
1
;
return
exchange
.
methods
.
tokenToTokenTransferInput
.
cacheSend
(
tokensSold
,
minTokensBought
,
minEthBought
,
deadline
,
recipient
,
tokenAddress
,
{
from
:
account
},
);
},
sendOutput
:
async
opts
=>
{
const
{
drizzleCtx
,
contractStore
,
input
,
output
,
account
,
inputCurrency
,
outputCurrency
,
exchangeAddresses
,
recipient
,
}
=
opts
;
const
exchangeRateA
=
await
ETH_TO_ERC20
.
calculateInput
({
...
opts
,
inputCurrency
:
'
ETH
'
});
if
(
!
exchangeRateA
)
{
return
;
}
if
(
!
validErc20ToErc20
(
inputCurrency
,
outputCurrency
))
{
return
;
}
const
exchangeAddress
=
exchangeAddresses
.
fromToken
[
inputCurrency
];
const
exchange
=
drizzleCtx
.
contracts
[
exchangeAddress
];
if
(
!
exchangeAddress
||
!
exchange
)
{
console
.
error
(
`Cannot find Exchange Address for
${
inputCurrency
}
`
);
return
;
}
const
deadline
=
await
getDeadline
(
drizzleCtx
,
300
);
const
ALLOWED_SLIPPAGE
=
BN
(
0.04
);
const
ALLOWED_SLIPPAGE
=
BN
(
0.04
);
const
inputDecimals
=
await
getDecimals
({
address
:
inputCurrency
,
contractStore
,
drizzleCtx
});
const
inputDecimals
=
await
getDecimals
({
address
:
inputCurrency
,
contractStore
,
drizzleCtx
});
const
outputDecimals
=
await
getDecimals
({
address
:
outputCurrency
,
contractStore
,
drizzleCtx
});
const
outputDecimals
=
await
getDecimals
({
address
:
outputCurrency
,
contractStore
,
drizzleCtx
});
...
@@ -561,13 +749,64 @@ const ERC20_TO_ERC20 = {
...
@@ -561,13 +749,64 @@ const ERC20_TO_ERC20 = {
const
maxTokensSold
=
inputAmount
.
multipliedBy
(
BN
(
1
).
plus
(
ALLOWED_SLIPPAGE
)).
toFixed
(
0
);
const
maxTokensSold
=
inputAmount
.
multipliedBy
(
BN
(
1
).
plus
(
ALLOWED_SLIPPAGE
)).
toFixed
(
0
);
const
maxEthSold
=
inputAmountB
.
multipliedBy
(
1.2
).
toFixed
(
0
);
const
maxEthSold
=
inputAmountB
.
multipliedBy
(
1.2
).
toFixed
(
0
);
return
exchange
.
methods
.
tokenToToken
Swap
Output
.
cacheSend
(
return
exchange
.
methods
.
tokenToToken
Transfer
Output
.
cacheSend
(
tokensBought
,
tokensBought
,
maxTokensSold
,
maxTokensSold
,
maxEthSold
,
maxEthSold
,
deadline
,
deadline
,
recipient
,
tokenAddress
,
tokenAddress
,
{
from
:
account
},
{
from
:
account
},
);
);
},
},
};
};
async
function
getDeadline
(
drizzleCtx
,
additional
)
{
const
{
web3
}
=
drizzleCtx
;
const
blockNumber
=
await
promisify
(
web3
,
'
getBlockNumber
'
);
const
block
=
await
promisify
(
web3
,
'
getBlock
'
,
blockNumber
);
return
block
.
timestamp
+
additional
;
}
function
validEthToErc20
(
inputCurrency
,
outputCurrency
)
{
if
(
inputCurrency
!==
'
ETH
'
)
{
console
.
error
(
'
Input Currency should be ETH
'
);
return
false
;
}
if
(
!
outputCurrency
||
outputCurrency
===
'
ETH
'
)
{
console
.
error
(
'
Output Currency should be ERC20
'
);
return
false
;
}
return
true
;
}
function
validErc20ToEth
(
inputCurrency
,
outputCurrency
)
{
if
(
outputCurrency
!==
'
ETH
'
)
{
console
.
error
(
'
Output Currency should be ETH
'
);
return
false
;
}
if
(
!
inputCurrency
||
inputCurrency
===
'
ETH
'
)
{
console
.
error
(
'
Input Currency should be ERC20
'
);
return
false
;
}
return
true
;
}
function
validErc20ToErc20
(
inputCurrency
,
outputCurrency
)
{
if
(
!
outputCurrency
||
outputCurrency
===
'
ETH
'
)
{
console
.
error
(
'
Output Currency should be ERC20
'
);
return
false
;
}
if
(
!
inputCurrency
||
inputCurrency
===
'
ETH
'
)
{
console
.
error
(
'
Input Currency should be ERC20
'
);
return
false
;
}
return
true
;
}
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